Skip to content
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

Confusing error message with Fn(&T) and unsatisfied trait bounds. #18794

Open
orodley opened this Issue Nov 9, 2014 · 6 comments

Comments

Projects
None yet
6 participants
@orodley
Copy link

orodley commented Nov 9, 2014

The following (invalid) code triggers an ICE:

trait Foo { }

fn foo<T: Foo>(x: T) { }

fn main() {
    // '&mut int' also triggers an ICE, as does replacing 'int' with any other type.
    // Just 'int' produces a very similar compile error, but with no ICE.
    let x: |&int| = foo;
}

Compiler output:

$ RUST_BACKTRACE=1 rustc ice.rs 
ice.rs:8:21: 8:24 error: the trait `Foo` is not implemented for the type `&int`
ice.rs:8     let x: |&int| = foo;
                             ^~~
ice.rs:8:21: 8:24 note: the trait `Foo` must be implemented because it is required by `foo`
ice.rs:8     let x: |&int| = foo;
                             ^~~
error: internal compiler error: unexpected panic
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: http://doc.rust-lang.org/complement-bugreport.html
note: run with `RUST_BACKTRACE=1` for a backtrace
task 'rustc' panicked at 'assertion failed: !ty::type_needs_infer(ty)', /home/rustbuild/src/rust-buildbot/slave/nightly-linux/build/src/librustc/middle/typeck/mod.rs:287

stack backtrace:
   1: 0xb6240e70 - rt::backtrace::imp::write::h7a758165f6ba41d0scq
   2: 0xb6243cb0 - failure::on_fail::h9832b9ccb4fdbc0e3xq
   3: 0xb65bd590 - unwind::begin_unwind_inner::h34603f1d03c7301c7Rd
   4: 0xb674db90 - unwind::begin_unwind::h16442260839102883864
   5: 0xb6c3b800 - middle::typeck::write_ty_to_tcx::h8af730520c710be1amp
   6: 0xb6c37e10 - middle::typeck::check::writeback::WritebackCx<'cx, 'tcx>::visit_node_id::hc06bb3e173d3905ecZO
   7: 0xb6c363f0 - middle::typeck::check::writeback::WritebackCx<'cx, 'tcx>.Visitor<'v>::visit_expr::h17ddfb3651503235cOO
   8: 0xb6c39800 - middle::typeck::check::writeback::WritebackCx<'cx, 'tcx>.Visitor<'v>::visit_local::h4608353f0c9b5790JSO
   9: 0xb6c3aca0 - visit::walk_block::h11861875672251434685
  10: 0xb6c37b90 - middle::typeck::check::writeback::resolve_type_vars_in_fn::h6349b48253cd1f93iJO
  11: 0xb6c91630 - middle::typeck::check::check_bare_fn::he6d7c7b3bc920a95UhW
  12: 0xb6c8d650 - middle::typeck::check::check_item::h60fc147f59e915423BW
  13: 0xb6c913e0 - middle::typeck::check::check_item_types::he4f43b9788b1001f4gW
  14: 0xb6f9d480 - middle::typeck::check_crate::closure.140551
  15: 0xb676adc0 - util::common::time::h5171196413795613556
  16: 0xb6f9c590 - middle::typeck::check_crate::hfe6d18bc6c2f50a0JEp
  17: 0xb7008cf0 - driver::driver::phase_3_run_analysis_passes::h9d97fef3fdd62215JoC
  18: 0xb7003970 - driver::driver::compile_input::h3c2343e4d8471330u5B
  19: 0xb7089540 - driver::run_compiler::haa3ef0d50c3e4454LVF
  20: 0xb7089420 - driver::run::closure.146534
  21: 0xb67835b0 - task::TaskBuilder<S>::try_future::closure.104847
  22: 0xb67833a0 - task::TaskBuilder<S>::spawn_internal::closure.104818
  23: 0xb7707d60 - task::NativeSpawner.Spawner::spawn::closure.8448
  24: 0xb65bd010 - unwind::try::try_fn::h8e5dedf36bdf5fe00Id
  25: 0xb66197d0 - rust_try_inner
  26: 0xb6619790 - rust_try
  27: 0xb65bae40 - unwind::try::h4eb595f617e1a97fPGd
  28: 0xb65bacb0 - task::Task::run::h7184ce8d54e7ef9aFMc
  29: 0xb7707ac0 - task::NativeSpawner.Spawner::spawn::closure.8386
  30: 0xb65bc540 - thread::thread_start::hbe8cf1b92d073927U7c
  31: 0xb611ce30 - start_thread
  32: 0xb64b4de2 - clone
  33:        0x0 - <unknown>

This occurs under:

$ uname -a
Linux <hostname> 3.16-3-686-pae #1 SMP Debian 3.16.5-1 (2014-10-10) i686 GNU/Linux
$ rustc --version=verbose
rustc 0.13.0-nightly (45cbdec41 2014-11-07 00:02:18 +0000)
binary: rustc
commit-hash: 45cbdec4174778bf915f17561ef971c068a7fcbc
commit-date: 2014-11-07 00:02:18 +0000
host: i686-unknown-linux-gnu
release: 0.13.0-nightly

@huonw huonw added the I-ICE label Nov 12, 2014

@steveklabnik

This comment has been minimized.

Copy link
Member

steveklabnik commented Jan 16, 2015

This syntax is now gone, so I'm not sure this issue is relevant. Does an updated version still ICE for you?

@orodley

This comment has been minimized.

Copy link
Author

orodley commented Jan 16, 2015

An updated version on rustc 1.0.0-nightly (8903c21d6 2015-01-15 22:42:58 +0000) doesn't ICE, but I do get a very unhelpful error message in the &i32 case compared to the i32 case.

Compiling:

trait Foo { }

fn foo<T: Foo>(x: T) { }

fn main() {
    let x: &Fn(i32) = &foo;
}

Produces:

$ rustc ice.rs
ice.rs:6:24: 6:27 error: the trait `Foo` is not implemented for the type `i32`
ice.rs:6     let x: &Fn(i32) = &foo;
                                ^~~
error: aborting due to previous error

Which is exactly what I would expect.

Compiling:

trait Foo { }

fn foo<T: Foo>(x: T) { }

fn main() {
    let x: &Fn(&i32) = &foo;
}

Produces:

$ rustc ice.rs
ice.rs:6:24: 6:28 error: type mismatch: the type `fn(_) {foo}` implements the trait `core::ops::Fn(_)`, but the trait `for<'r> core::ops::Fn(&'r i32)` is required (expected concrete lifetime, found bound lifetime parameter )
ice.rs:6     let x: &Fn(&i32) = &foo;
                                ^~~~
ice.rs:6:24: 6:28 note: required for the cast to the object type `for<'r> core::ops::Fn(&'r i32)`
ice.rs:6     let x: &Fn(&i32) = &foo;
                                ^~~~
error: aborting due to previous error

Which is basically incomprehensible to me. I feel like these two cases should probably be producing the same error message, as the problem in both cases is the same - the trait isn't implemented for the specified type.

@orodley

This comment has been minimized.

Copy link
Author

orodley commented Apr 10, 2015

An update on this:

I tested it again under the latest nightly (rustc 1.0.0-beta (9854143cb 2015-04-02) (built 2015-04-02)), and the error message for the &i32 case is even more confusing:

Compiling:

trait Foo {
    fn foo(s: Self);
}

fn foo<T: Foo>(x: T) { }

fn main() {
    let x: &Fn(&i32) = &foo;
}

produces:

$ rustc test.rs
test.rs:8:24: 8:28 error: type mismatch: the type `fn(_) {foo}` implements the trait `core::ops::Fn<(_,)>`, but the trait `for<'r> core::ops::Fn<(&'r i32,)>` is required (expected concrete lifetime, found bound lifetime parameter ) [E0281]
test.rs:8     let x: &Fn(&i32) = &foo;
                                 ^~~~
test.rs:8:24: 8:28 note: required for the cast to the object type `for<'r> core::ops::Fn(&'r i32)`
test.rs:8     let x: &Fn(&i32) = &foo;
                                 ^~~~
test.rs:8:24: 8:28 error: type mismatch resolving `for<'r> <fn(_) {foo} as core::ops::FnOnce<(&'r i32,)>>::Output == ()`:
 expected bound lifetime parameter ,
    found concrete lifetime [E0271]
test.rs:8     let x: &Fn(&i32) = &foo;
                                 ^~~~
test.rs:8:24: 8:28 note: required for the cast to the object type `for<'r> core::ops::Fn(&'r i32)`
test.rs:8     let x: &Fn(&i32) = &foo;
                                 ^~~~
error: aborting due to 2 previous errors

@orodley orodley changed the title ICE: Trait not implemented for &int Confusing error message with Fn(&T) and unsatisfied trait bounds. Apr 10, 2015

@arielb1 arielb1 added A-diagnostics P-low and removed I-ICE labels Jul 8, 2015

@steveklabnik

This comment has been minimized.

Copy link
Member

steveklabnik commented Jan 3, 2017

Triage: no change

@estebank

This comment has been minimized.

Copy link
Contributor

estebank commented Sep 22, 2017

Current output, no change:

error[E0281]: type mismatch: `fn(_) {foo::<_>}` implements the trait `std::ops::Fn<(_,)>`, but the trait `for<'r> std::ops::Fn<(&'r i32,)>` is required
 --> src/main.rs:6:24
  |
6 |     let x: &Fn(&i32) = &foo;
  |                        ^^^^ expected concrete lifetime, found bound lifetime parameter
  |
  = note: required for the cast to the object type `for<'r> std::ops::Fn(&'r i32)`

error[E0271]: type mismatch resolving `for<'r> <fn(_) {foo::<_>} as std::ops::FnOnce<(&'r i32,)>>::Output == ()`
 --> src/main.rs:6:24
  |
6 |     let x: &Fn(&i32) = &foo;
  |                        ^^^^ expected bound lifetime parameter, found concrete lifetime
  |
  = note: required for the cast to the object type `for<'r> std::ops::Fn(&'r i32)`
@estebank

This comment has been minimized.

Copy link
Contributor

estebank commented Dec 6, 2017

Current output:

error[E0631]: type mismatch in function arguments
 --> src/main.rs:8:24
  |
8 |     let x: &Fn(&i32) = &foo;
  |                        ^^^^
  |                        |
  |                        expected signature of `for<'r> fn(&'r i32) -> _`
  |                        found signature of `fn(_) -> _`
  |
  = note: required for the cast to the object type `for<'r> std::ops::Fn(&'r i32)`

error[E0271]: type mismatch resolving `for<'r> <fn(_) {foo::<_>} as std::ops::FnOnce<(&'r i32,)>>::Output == ()`
 --> src/main.rs:8:24
  |
8 |     let x: &Fn(&i32) = &foo;
  |                        ^^^^ expected bound lifetime parameter, found concrete lifetime
  |
  = note: required for the cast to the object type `for<'r> std::ops::Fn(&'r i32)

Updated output:

error[E0631]: type mismatch in function arguments
 --> src/main.rs:8:24
  |
5 | fn foo<T: Foo>(x: T) { }
  | -------------------- found signature of `fn(_) -> _`
...
8 |     let x: &Fn(&i32) = &foo;
  |                        ^^^^ expected signature of `for<'r> fn(&'r i32) -> _`
  |
  = note: required for the cast to the object type `for<'r> std::ops::Fn(&'r i32)`

error[E0271]: type mismatch resolving `for<'r> <fn(_) {foo::<_>} as std::ops::FnOnce<(&'r i32,)>>::Output == ()`
 --> src/main.rs:8:24
  |
8 |     let x: &Fn(&i32) = &foo;
  |                        ^^^^ expected bound lifetime parameter, found concrete lifetime
  |
  = note: required for the cast to the object type `for<'r> std::ops::Fn(&'r i32)`

error: aborting due to 2 previous errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.