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

Poor error message with a function that takes a closure that takes a reference #95277

Closed
bstrie opened this issue Mar 24, 2022 · 2 comments
Closed
Labels
A-closures Area: closures (`|args| { .. }`) A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: lifetime related D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@bstrie
Copy link
Contributor

bstrie commented Mar 24, 2022

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=d21e5af3a39c5469cff10767987be29a

fn main() {
    fn qux(_: impl Fn(&())) {}
    let bar = |_| {};
    qux(bar)
}

The current output is:

error: implementation of `FnOnce` is not general enough
 --> src/main.rs:4:5
  |
4 |     qux(bar)
  |     ^^^ implementation of `FnOnce` is not general enough
  |
  = note: closure with signature `fn(&'2 ())` must implement `FnOnce<(&'1 (),)>`, for any lifetime `'1`...
  = note: ...but it actually implements `FnOnce<(&'2 (),)>`, for some specific lifetime `'2`

error: could not compile `playground` due to previous error

This is a poor error message for the following reasons:

  1. The arrows point to the function, but the error message references a closure
  2. The function references Fn, but the error message only mentions fn and FnOnce, not Fn
  3. There is no suggestion as to how to fix this, nor even an extended error message.

Truth be told I'm not even sure if this should be an error. It's quite confusing; neither of the following produce the same error:

    fn qux(_: impl Fn(&())) {}
    let bar = |_: &()| {}; // works
    qux(bar)
    fn qux(_: impl Fn(&())) {}
    qux(|_| {}) // works

So maybe this is an error in type inference?

Worth nothing that I suspect the following could be considered the "correct" fix in this case:

    fn qux<'a>(_: impl Fn(&'a ())) {}
    let bar = |_| {};
    qux(bar)

So, if nothing else, perhaps it should suggest explicitly specifying the lifetime on fn qux.

@bstrie bstrie added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-closures Area: closures (`|args| { .. }`) labels Mar 24, 2022
@estebank estebank added A-lifetimes Area: lifetime related D-confusing Diagnostics: Confusing error or lint that should be reworked. labels Mar 24, 2022
@estebank
Copy link
Contributor

Current output:

error[E0308]: mismatched types
 --> src/main.rs:4:5
  |
4 |     qux(bar)
  |     ^^^^^^^^ one type is more general than the other
  |
  = note: expected trait `for<'a> Fn<(&'a (),)>`
             found trait `Fn<(&(),)>`
note: this closure does not fulfill the lifetime requirements
 --> src/main.rs:3:15
  |
3 |     let bar = |_| {};
  |               ^^^
note: the lifetime requirement is introduced here
 --> src/main.rs:2:20
  |
2 |     fn qux(_: impl Fn(&())) {}
  |                    ^^^^^^^
help: consider specifying the type of the closure parameters
  |
3 |     let bar = |_: &_| {};
  |               ~~~~~~~

error: implementation of `FnOnce` is not general enough
 --> src/main.rs:4:5
  |
4 |     qux(bar)
  |     ^^^^^^^^ implementation of `FnOnce` is not general enough
  |
  = note: closure with signature `fn(&'2 ())` must implement `FnOnce<(&'1 (),)>`, for any lifetime `'1`...
  = note: ...but it actually implements `FnOnce<(&'2 (),)>`, for some specific lifetime `'2`

@bstrie
Copy link
Contributor Author

bstrie commented Dec 18, 2023

The help text alone is a big improvement, although I suspect to a new user the underlying problem will still be quite baffling even with the error message... although that might not really be something that it's the responsibility of error messages to teach. I believe the underlying problem is the lack of #97362 , yes? Regardless, it would be nice if this could "just work", although that's certainly outside the scope of this diagnostic issue.

This part does seem to be the crux, but I expect some head-scratching when parsing it:

  = note: closure with signature `fn(&'2 ())` must implement `FnOnce<(&'1 (),)>`, for any lifetime `'1`...
  = note: ...but it actually implements `FnOnce<(&'2 (),)>`, for some specific lifetime `'2`

Which is to say, the explicitly-numbered lifetimes are IMO what the reader will focus on, but the real error is the "for any lifetime" vs. "for some specific lifetime" part, which as mentioned above we don't even have syntax for.

So I think I'm willing to close this for now, and if we do eventually get #97362 then improving error messages to take advantage of it sounds like a separate task. @estebank feel free to reopen if you disagree.

@bstrie bstrie closed this as completed Dec 18, 2023
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 D-confusing Diagnostics: Confusing error or lint that should be reworked. 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

2 participants