Skip to content

Bad error message when using try operator in a closure #46680

New issue

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

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

Already on GitHub? # to your account

Closed
eira-fransham opened this issue Dec 12, 2017 · 1 comment · Fixed by #63507
Closed

Bad error message when using try operator in a closure #46680

eira-fransham opened this issue Dec 12, 2017 · 1 comment · Fixed by #63507
Labels
A-closures Area: Closures (`|…| { … }`) A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@eira-fransham
Copy link

Minimal reproduction:

|| {
    Err(())?;
    Ok(())
}

Leads to an error message like:

error[E0282]: type annotations needed
 --> src/main.rs:3:9
  |
3 |         Err(())?;
  |         --------
  |         |
  |         cannot infer type for `_`
  |         in this macro invocation

error: aborting due to previous error

Because ? uses Into and it's ambiguous which type you're converting this error into. Even as a relatively-veteran Rust programmer it still took me some time and playing around before I worked out what was causing this error, I can't imagine how it must be for a new recruit. I think the ideal solution would be to have it default to Into<Self> as long as the error types of all of the expressions ? is used on match, but that would require an RFC and at the bare minimum a more explicit error message could be written for this case.

@pietroalbini pietroalbini added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints A-closures Area: Closures (`|…| { … }`) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 30, 2018
@estebank
Copy link
Contributor

estebank commented Aug 12, 2019

Current output:

error[E0282]: type annotations needed for `[closure@src/main.rs:3:13: 6:6]`
 --> src/main.rs:4:9
  |
3 |     let x = || {
  |         - consider giving `x` the explicit type `[closure@src/main.rs:3:13: 6:6]`, with the type parameters specified
4 |         Err(())?;
  |         ^^^^^^^^ cannot infer type

Centril added a commit to Centril/rust that referenced this issue Aug 14, 2019
…ntril

When needing type annotations in local bindings, account for impl Trait and closures

Fix rust-lang#46680, fix rust-lang#63504, fix rust-lang#63506, fix rust-lang#40014, cc rust-lang#63502.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-closures Area: Closures (`|…| { … }`) A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants