-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Labels
A-closures
Area: Closures (`|…| { … }`)
A-diagnostics
Area: Messages for errors, warnings, and lints
A-lifetimes
Area: Lifetimes / regions
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
estebank
added
A-diagnostics
Area: Messages for errors, warnings, and lints
A-closures
Area: Closures (`|…| { … }`)
A-lifetimes
Area: Lifetimes / regions
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
See also #75791. |
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);
} 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. |
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);
} |
Current output:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
A-closures
Area: Closures (`|…| { … }`)
A-diagnostics
Area: Messages for errors, warnings, and lints
A-lifetimes
Area: Lifetimes / regions
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.
When trying to compile this code (Rust stable 1.49.0)
I expected it to compile fine,
Instead, we get this funny nonsensical error :
The code compiles fine if we add a type annotation to the closure (notice the
|data: &str|
)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 :
The text was updated successfully, but these errors were encountered: