New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to dispatch fn traits depending on number of parameters #55986

Open
wants to merge 7 commits into
base: master
from

Conversation

Projects
None yet
6 participants
@cjgillot

cjgillot commented Nov 15, 2018

Hello,

By following @eddyb's advise on issue #45510, I managed to have the snippets of code in #45510 and #18952 passing without breaking older diagnostics.

However, this breaks a codegen tests: codegen/lto-removes-invokes.rs with a LLVM assertion

stderr:
------------------------------------------
rustc: /home/k1000/src/rust/rust/src/llvm/lib/IR/Value.cpp:256: void llvm::Value::setNameImpl(const llvm::Twine&): Assertion `!getType()->isVoidTy() && "Cannot assign a name to void values!"' fa
iled.

------------------------------------------

thread '[codegen] codegen/lto-removes-invokes.rs' panicked at 'explicit panic', tools/compiletest/src/runtest.rs:3282:9
stack backtrace:
   0:     0x55de7755457f - std::sys::unix::backtrace::tracing::imp::unwind_backtrace::h3c55afa80a996a32
                               at libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1:     0x55de77561b87 - std::sys_common::backtrace::print::hcef907ea7a07cd40
                               at libstd/sys_common/backtrace.rs:71
                               at libstd/sys_common/backtrace.rs:59
   2:     0x55de7755a2ef - std::panicking::default_hook::{{closure}}::hf541876c65f36ea7
                               at libstd/panicking.rs:211
   3:     0x55de77559fee - std::panicking::default_hook::h4ecc06b30ae4459d
                               at libstd/panicking.rs:221
   4:     0x55de7755a9ce - std::panicking::rust_panic_with_hook::h0cc6d9cc9f34a947
                               at libstd/panicking.rs:476
   5:     0x55de77405ee7 - std::panicking::begin_panic::he81d62562fdfe95f
   6:     0x55de774371c3 - compiletest::runtest::ProcRes::fatal::h18b98df583e11ce3
   7:     0x55de77432702 - compiletest::runtest::TestCx::fatal_proc_rec::hc06c570887f0112f
   8:     0x55de774202ca - compiletest::runtest::TestCx::run_revision::hbf1a4daef32a28c5
   9:     0x55de7741774f - compiletest::runtest::run::hda86a76e57ae0a35
  10:     0x55de7740d60a - <F as alloc::boxed::FnBox<A>>::call_box::h091bcbeb98acbe4b
  11:     0x55de77478a22 - <F as alloc::boxed::FnBox<A>>::call_box::hecf56d722ce7bb71
                               at libtest/lib.rs:1461
                               at liballoc/boxed.rs:672
  12:     0x55de7756cdb9 - __rust_maybe_catch_panic
                               at libpanic_unwind/lib.rs:102
  13:     0x55de77496472 - std::sys_common::backtrace::__rust_begin_short_backtrace::h1bb71a7386fd3269
                               at libstd/panicking.rs:289
                               at libstd/panic.rs:392
                               at libtest/lib.rs:1423
                               at libstd/sys_common/backtrace.rs:136
  14:     0x55de77496f24 - std::panicking::try::do_call::h9b94719c3074bb5c
                               at libstd/thread/mod.rs:409
                               at libstd/panic.rs:313
                               at libstd/panicking.rs:310
  15:     0x55de7756cdb9 - __rust_maybe_catch_panic
                               at libpanic_unwind/lib.rs:102
  16:     0x55de774856dc - <F as alloc::boxed::FnBox<A>>::call_box::hb3bcd66d16fd3e84
                               at libstd/panicking.rs:289
                               at libstd/panic.rs:392
                               at libstd/thread/mod.rs:408
                               at liballoc/boxed.rs:672
  17:     0x55de77556d4d - std::sys_common::thread::start_thread::had0c80d121669d29
                               at liballoc/boxed.rs:682
                               at libstd/sys_common/thread.rs:24
  18:     0x55de775487e5 - std::sys::unix::thread::Thread::new::thread_start::h4a5b66a70d76f47f
                               at libstd/sys/unix/thread.rs:90
  19:     0x7fa6ed7d8a9c - start_thread
  20:     0x7fa6ed6eeb22 - clone
  21:                0x0 - <unknown>

I cannot debug that by myself (and I have absolutely no idea why it would happen).
If any kind reviewer has any advice, you are very welcome.

@rust-highfive

This comment has been minimized.

Collaborator

rust-highfive commented Nov 15, 2018

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @cramertj (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@eddyb

This comment has been minimized.

Member

eddyb commented Nov 15, 2018

@bors

This comment has been minimized.

Contributor

bors commented Nov 19, 2018

☔️ The latest upstream changes (presumably #56051) made this pull request unmergeable. Please resolve the merge conflicts.

@nikomatsakis

Looks good! Can you add the comment and explain that one case?

Show resolved Hide resolved src/librustc_typeck/check/callee.rs

@cjgillot cjgillot force-pushed the cjgillot:issue-45510 branch from ab168ba to 2ab7a74 Nov 21, 2018

@cjgillot cjgillot changed the title from WIP: Allow to dispatch fn traits depending on number of parameters to Allow to dispatch fn traits depending on number of parameters Nov 25, 2018

@nikomatsakis

nikomatsakis requested changes Nov 26, 2018 edited

can we tweak the tests a bit to show that we are actually calling the fn as expected etc?

Show resolved Hide resolved src/test/ui/issue-18952.rs Outdated
Show resolved Hide resolved src/test/ui/issue-45510.rs Outdated
@rust-highfive

This comment has been minimized.

Collaborator

rust-highfive commented Nov 28, 2018

The job x86_64-gnu-llvm-5.0 of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
travis_time:end:1598c091:start=1543407844229716095,finish=1543407901626499970,duration=57396783875
$ git checkout -qf FETCH_HEAD
travis_fold:end:git.checkout

Encrypted environment variables have been removed for security reasons.
See https://docs.travis-ci.com/user/pull-requests/#Pull-Requests-and-Security-Restrictions
$ export SCCACHE_BUCKET=rust-lang-ci-sccache2
$ export SCCACHE_REGION=us-west-1
Setting environment variables from .travis.yml
$ export IMAGE=x86_64-gnu-llvm-5.0
---
[00:48:25] .................................................................................................... 100/5065
[00:48:28] .................................................................................................... 200/5065
[00:48:30] .............................ii............................................ii...................ii.. 300/5065
[00:48:33] ..............................................................................................iii... 400/5065
[00:48:36] .....iiiiiiii.iii............................iii...........................................i........ 500/5065
[00:48:42] .................................................................................................... 700/5065
[00:48:48] ................................................................................................i... 800/5065
[00:48:52] ........i........................................................................................... 900/5065
[00:48:55] ...............iiiii..................ii.iiii....................................................... 1000/5065
---
[00:49:33] .................................................................................................... 2300/5065
[00:49:37] .................................................................................................... 2400/5065
[00:49:41] .................................................................................................... 2500/5065
[00:49:45] .................................................................................................... 2600/5065
[00:49:48] ........iiiiiiiii................................................................................... 2700/5065
[00:49:54] .................................................................................................... 2900/5065
[00:49:58] .................................................................................................... 3000/5065
[00:50:01] .......................................................................i............................ 3100/5065
[00:50:04] .................................................................................................... 3200/5065
---
[00:51:57] .................................................................................................... 600/2889
[00:52:11] .................................................................................................... 700/2889
[00:52:22] .................................................................................................... 800/2889
[00:52:31] .................................................................................................... 900/2889
[00:52:46] ..............................................................................F..................... 1000/2889
[00:53:06] .................................................................................................... 1200/2889
[00:53:14] .................................................................................................... 1300/2889
[00:53:26] .................................................................................i.................. 1400/2889
[00:53:37] .................................................................................................... 1500/2889
---
[00:55:55] .................................................................................................... 2500/2889
[00:56:25] .................................................................................................... 2600/2889
[00:56:35] .................................................................................................... 2700/2889
[00:56:43] .................................................................................................... 2800/2889
/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/run-pass/issue-45510/auxiliary"
[00:56:55] ------------------------------------------
[00:56:55] 
[00:56:55] ------------------------------------------
[00:56:55] stderr:
[00:56:55] stderr:
[00:56:55] ------------------------------------------
[00:56:55] {"message":"can't compare `Ishmael` with `Ishmael`","code":{"code":"E0277","explanation":"\nYou tried to use a type which doesn't implement some trait in a place which\nexpected that trait. Erroneous code example:\n\n```compile_fail,E0277\n// here we declare the Foo trait with a bar method\ntrait Foo {\n    fn bar(&self);\n}\n\n// we now declare a function which takes an object implementing the Foo trait\nfn some_func<T: Foo>(foo: T) {\n    foo.bar();\n}\n\nfn main() {\n    // we now call the method with the i32 type, which doesn't implement\n    // the Foo trait\n    some_func(5i32); // error: the trait bound `i32 : Foo` is not satisfied\n}\n```\n\nIn order to fix this error, verify that the type you're using does implement\nthe trait. Example:\n\n```\ntrait Foo {\n    fn bar(&self);\n}\n\nfn some_func<T: Foo>(foo: T) {\n    foo.bar(); // we can now use this method since i32 implements the\n               // Foo trait\n}\n\n// we implement the trait on the i32 type\nimpl Foo for i32 {\n    fn bar(&self) {}\n}\n\nfn main() {\n    some_func(5i32); // ok!\n}\n```\n\nOr in a generic context, an erroneous code example would look like:\n\n```compile_fail,E0277\nfn some_func<T>(foo: T) {\n    println!(\"{:?}\", foo); // error: the trait `core::fmt::Debug` is not\n                           //        implemented for the type `T`\n}\n\nfn main() {\n    // We now call the method with the i32 type,\n    // which *does* implement the Debug trait.\n    some_func(5i32);\n}\n```\n\nNote that the error here is in the definition of the generic function: Although\nwe only call it with a parameter that does implement `Debug`, the compiler\nstill rejects the function: It must work with all possible input types. In\norder to make this example compile, we need to restrict the generic type we're\naccepting:\n\n```\nuse std::fmt;\n\n// Restrict the input type to types that implement Debug.\nfn some_func<T: fmt::Debug>(foo: T) {\n    println!(\"{:?}\", foo);\n}\n\nfn main() {\n    // Calling the method is still fine, as i32 implements Debug.\n    some_func(5i32);\n\n    // This would fail to compile now:\n    // struct WithoutDebug;\n    // some_func(WithoutDebug);\n}\n```\n\nRust only looks at the signature of the called function, as such it must\nalready specify all requirements that will be used for every type parameter.\n"},"level":"error","spans":[{"file_name":"/checkout/src/test/run-pass/issue-45510.rs","byte_start":127,"byte_end":129,"line_start":7,"line_end":7,"column_start":17,"column_end":19,"is_primary":true,"text":[{"text":"#[derive(Debug, Eq)]","highlight_start":17,"highlight_end":19}],"label":"no implementation for `Ishmael == Ishmael`","suggested_replacement":null,"suggestion_applicability":null,"expansion":{"span":{"file_name":"/checkout/src/test/run-pass/issue-45510.rs","byte_start":127,"byte_end":129,"line_start":7,"line_end":7,"column_start":17,"column_end":19,"is_primary":falsee implement the trait on the i32 type\nimpl Foo for i32 {\n    fn bar(&self) {}\n}\n\nfn main() {\n    some_func(5i32); // ok!\n}\n```\n\nOr in a generic context, an erroneous code example would look like:\n\n```compile_fail,E0277\nfn some_func<T>(foo: T) {\n    println!(\"{:?}\", foo); // error: the trait `core::fmt::Debug` is not\n                           //        implemented for the type `T`\n}\n\nfn main() {\n    // We now call the method with the i32 type,\n    // which *does* implement the Debug trait.\n    some_func(5i32);\n}\n```\n\nNote that the error here is in the definition of the generic function: Although\nwe only call it with a parameter that does implement `Debug`, the compiler\nstill rejects the function: It must work with all possible input types. In\norder to make this example compile, we need to restrict the generic type we're\naccepting:\n\n```\nuse std::fmt;\n\n// Restrict the input type to types that implement Debug.\nfn some_func<T: fmt::Debug>(foo: T) {\n    println!(\"{:?}\", foo);\n}\n\nfn main() {\n    // Calling the method is still fine, as i32 implements Debug.\n    some_func(5i32);\n\n    // This would fail to compile now:\n    // struct WithoutDebug;\n    // some_func(WithoutDebug);\n}\n```\n\nRust only looks at the signature of the called function, as such it must\nalready specify all requirements that will be used for every type parameter.\n"},"level":"error","spans":[{"file_name":"/checkout/src/test/run-pass/issue-45510.rs","byte_start":164,"byte_end":166,"line_start":9,"line_end":9,"column_start":17,"column_end":19,"is_primary":true,"text":[{"text":"#[derive(Debug, Eq)]","highlight_start":17,"highlight_end":19}],"label":"no implementation for `Maybe == Maybe`","suggested_replacement":null,"suggestion_applicability":null,"expansion":{"span":{"file_name":"/checkout/src/test/run-pass/issue-45510.rs","byte_start":164,"byte_end":166,"line_start":9,"line_end":9,"column_start":17,"column_end":19,"is_primary":false,"text":[{"text":"#[derive(Debug, Eq)]","highlight_start":17,"highlight_end":19}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null},"macro_decl_name":"#[derive(Eq)]","def_site_span":null}}],"children":[{"message":"the trait `std::cmp::PartialEq` is not implemented for `Maybe`","code":null,"level":"help","spans":[],"children":[],"rendered":null}],"rendered":"error[E0277]: can't compare `Maybe` with `Maybe`\n  --> /checkout/src/test/run-pass/issue-45510.rs:9:17\n   |\nLL | #[derive(Debug, Eq)]\n   |                 ^^ no implementation for `Maybe == Maybe`\n   |\n   = help: the trait `std::cmp::PartialEq` is not implemented for `Maybe`\n\n"}
[00:56:55] {"message":"aborting due to 2 previous errors","code":null,"level":"error","spans":[],"children":[],"rendered":"error: aborting due to 2 previous errors\n\n"}
[00:56:55] {"message":"For more information about this error, try `rustc --explain E0277`.","code":null,"level":"","spans":[],"children":[],"rendered":"For more information about this error, try `rustc --explain E0277`.\n"}
[00:56:55] ------------------------------------------
[00:56:55] 
[00:56:55] thread '[run-pass] run-pass/issue-45510.rs' panicked at 'explicit panic', src/tools/compiletest/src/runtest.rs:3282:9
[00:56:55] note: Run with `RUST_BACKTRACE=1` for a backtrace.
---
[00:56:55] 
[00:56:55] thread 'main' panicked at 'Some tests failed', src/tools/compiletest/src/main.rs:503:22
[00:56:55] 
[00:56:55] 
[00:56:55] command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-tools-bin/compiletest" "--compile-lib-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/lib" "--run-lib-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/x86_64-unknown-linux-gnu/lib" "--rustc-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "--src-base" "/checkout/src/test/run-pass" "--build-base" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/run-pass" "--stage-id" "stage2-x86_64-unknown-linux-gnu" "--mode" "run-pass" "--target" "x86_64-unknown-linux-gnu" "--host" "x86_64-unknown-linux-gnu" "--llvm-filecheck" "/usr/lib/llvm-5.0/bin/FileCheck" "--host-rustcflags" "-Crpath -O -Zunstable-options " "--target-rustcflags" "-Crpath -O -Zunstable-options  -Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--docck-python" "/usr/bin/python2.7" "--lldb-python" "/usr/bin/python2.7" "--gdb" "/usr/bin/gdb" "--quiet" "--llvm-version" "5.0.0\n" "--system-llvm" "--cc" "" "--cxx" "" "--cflags" "" "--llvm-components" "" "--llvm-cxxflags" "" "--adb-path" "adb" "--adb-test-dir" "/data/tmp/work" "--android-cross-path" "" "--color" "always"
[00:56:55] 
[00:56:55] 
[00:56:55] failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test
[00:56:55] Build completed unsuccessfully in 0:12:20
[00:56:55] Build completed unsuccessfully in 0:12:20
[00:56:55] Makefile:58: recipe for target 'check' failed
[00:56:55] make: *** [check] Error 1

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

@cjgillot cjgillot force-pushed the cjgillot:issue-45510 branch from 49ed503 to bea0ae6 Dec 1, 2018

@bors

This comment has been minimized.

Contributor

bors commented Dec 7, 2018

☔️ The latest upstream changes (presumably #56502) made this pull request unmergeable. Please resolve the merge conflicts.

cjgillot added some commits Nov 4, 2018

test: Add test for issues 45510 and 18952.
Those tests are directly taken from the comments on the bug reports.
rustc_typeck: Implement resolution advised in issue 45510.
When resolving Fn traits, the compiler failed to take into account
overloaded implementations.  To resolve this, we inform the trait dispatch
code that the arguments will becoming as a tuple of correct arity.
Change return types and check return values in tests.
This allows to verify that we handle
different types as return types,
and that we call the expected functions.

@cjgillot cjgillot force-pushed the cjgillot:issue-45510 branch from bea0ae6 to ee1c962 Dec 8, 2018

@bors

This comment has been minimized.

Contributor

bors commented Dec 16, 2018

☔️ The latest upstream changes (presumably #56875) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment