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 diagnostic when using closures #81511

Open
StyMaar opened this issue Jan 29, 2021 · 4 comments
Open

Confusing diagnostic when using closures #81511

StyMaar opened this issue Jan 29, 2021 · 4 comments
Labels
A-closures Area: closures (`|args| { .. }`) A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: lifetime related C-bug Category: This is a bug. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@StyMaar
Copy link

StyMaar commented Jan 29, 2021

When trying to compile this code (Rust stable 1.49.0)

fn my_function(callback: impl FnMut(&str) + Send + 'static){}

fn main(){
    let closure_consumer = |data| {};
    my_function(closure_consumer);
}

I expected it to compile fine,

Instead, we get this funny nonsensical error :

  |
  |     my_function(closure_consumer);
  |     ^^^^^^^^^^^ one type is more general than the other
  |
  = note: expected type `FnOnce<(&str,)>`
             found type `FnOnce<(&str,)>`

The code compiles fine if we add a type annotation to the closure (notice the |data: &str|)

fn my_function(callback: impl FnMut(&str) + Send + 'static){}

fn main(){
    let closure_consumer = |data: &str| {};
    my_function(closure_consumer);
}

This may be a type inference bug (because closure are supposed to work even without type annotations) but it's also (and most importantly IMHO) a diagnostic bug, because the error gives me zero information about what is going on.

When compiling with the latest nightly (1.51.0-nightly (2021-01-28 c0b64d9)), we have a more detailed error, but it doesn't make a better job at explaining what we are supposed to do to make this code run :

  |
6 |     my_function(closure_consumer);
  |     ^^^^^^^^^^^ lifetime mismatch
  |
  = note: expected type `FnOnce<(&str,)>`
             found type `FnOnce<(&str,)>`
note: this closure does not fulfill the lifetime requirements
 --> src/main.rs:5:28
  |
5 |     let closure_consumer = |data| {};
  |                            ^^^^^^^^^
note: the lifetime requirement is introduced here
 --> src/main.rs:2:31
  |
2 | fn my_function(callback: impl FnMut(&str) + Send + 'static){}
@StyMaar StyMaar added the C-bug Category: This is a bug. label Jan 29, 2021
@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-closures Area: closures (`|args| { .. }`) A-lifetimes Area: lifetime related D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 29, 2021
@mbrubeck
Copy link
Contributor

mbrubeck commented Feb 7, 2021

See also #75791.

@subtle-byte
Copy link

subtle-byte commented Feb 14, 2021

Adding the second argument to the closure

fn my_function(_callback: impl FnMut(&mut i32, &mut i32)) {}

fn main() {
    let closure_consumer = |_a: &mut i32, _b| {};
    my_function(closure_consumer);
}

(playground)

makes the error message even more strange:

error[E0308]: mismatched types
 --> src/main.rs:5:5
  |
5 |     my_function(closure_consumer);
  |     ^^^^^^^^^^^ one type is more general than the other
  |
  = note: expected type `FnOnce<(&mut i32, &mut i32)>`
             found type `for<'r> FnOnce<(&'r mut i32, &mut i32)>`

It seems like the problem is with the first argument, but indeed adding the explicit type to the second argument (i.e. _b: &mut i32) removes the error.

@whatisaphone
Copy link
Contributor

You can work around it by partially specifying the type:

fn foo(f: impl Fn(&i32)) {}

fn main() {
    let broken = |_| {};
    foo(broken);

    let works = |_: &_| {};
    foo(works);
}

@estebank
Copy link
Contributor

estebank commented Jan 8, 2023

Current output:

error[E0308]: mismatched types
 --> src/main.rs:6:5
  |
6 |     my_function(closure_consumer);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
  |
  = note: expected trait `for<'a> FnMut<(&'a str,)>`
             found trait `FnMut<(&str,)>`
note: this closure does not fulfill the lifetime requirements
 --> src/main.rs:5:28
  |
5 |     let closure_consumer = |data| {};
  |                            ^^^^^^
note: the lifetime requirement is introduced here
 --> src/main.rs:2:31
  |
2 | fn my_function(callback: impl FnMut(&str) + Send + 'static){}
  |                               ^^^^^^^^^^^

error: implementation of `FnOnce` is not general enough
 --> src/main.rs:6:5
  |
6 |     my_function(closure_consumer);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `FnOnce` is not general enough
  |
  = note: closure with signature `fn(&'2 str)` must implement `FnOnce<(&'1 str,)>`, for any lifetime `'1`...
  = note: ...but it actually implements `FnOnce<(&'2 str,)>`, for some specific lifetime `'2`
error[E0308]: mismatched types
 --> src/main.rs:5:5
  |
5 |     my_function(closure_consumer);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
  |
  = note: expected trait `for<'a, 'b> FnMut<(&'a mut i32, &'b mut i32)>`
             found trait `for<'a> FnMut<(&'a mut i32, &mut i32)>`
note: this closure does not fulfill the lifetime requirements
 --> src/main.rs:4:28
  |
4 |     let closure_consumer = |_a: &mut i32, _b| {};
  |                            ^^^^^^^^^^^^^^^^^^
note: the lifetime requirement is introduced here
 --> src/main.rs:1:32
  |
1 | fn my_function(_callback: impl FnMut(&mut i32, &mut i32)) {}
  |                                ^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0308]: mismatched types
 --> src/main.rs:5:5
  |
5 |     my_function(closure_consumer);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
  |
  = note: expected trait `for<'a, 'b> FnOnce<(&'a mut i32, &'b mut i32)>`
             found trait `for<'a> FnOnce<(&'a mut i32, &mut i32)>`
note: this closure does not fulfill the lifetime requirements
 --> src/main.rs:4:28
  |
4 |     let closure_consumer = |_a: &mut i32, _b| {};
  |                            ^^^^^^^^^^^^^^^^^^
note: the lifetime requirement is introduced here
 --> src/main.rs:1:32
  |
1 | fn my_function(_callback: impl FnMut(&mut i32, &mut i32)) {}
  |                                ^^^^^^^^^^^^^^^^^^^^^^^^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: closures (`|args| { .. }`) A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: lifetime related C-bug Category: This is a bug. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants