Skip to content

customize error message in the case where user supplies erroneous type annotation on closure argument #45727

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

Closed
nikomatsakis opened this issue Nov 2, 2017 · 3 comments · Fixed by #106891
Labels
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. WG-diagnostics Working group: Diagnostics

Comments

@nikomatsakis
Copy link
Contributor

nikomatsakis commented Nov 2, 2017

As discussed in https://github.com/rust-lang/rust/pull/45072, we have a newer approach to handling type annotations on closure arguments that should allow us to give clearer errors in many cases. Currently, if the user annotations would yield a unification error, we wind up falling back to some more general code. So for example in the ui/anonymous-higher-ranked-lifetime.rs test, for code like:

fn g2<F>(_: F) where F: Fn(&(), fn(&())) {}

fn main() {
    g2(|_: (), _: ()| {});
}

we generate

[00:39:53] error[E0631]: type mismatch in closure arguments
[00:39:53]   --> $DIR/anonymous-higher-ranked-lifetime.rs:18:5
[00:39:53]    |
[00:39:53] 18 |     g2(|_: (), _: ()| {});
[00:39:53]    |     ^^ ----------------- found signature of `fn((), ()) -> _`
[00:39:53]    |     |
[00:39:53]    |     expected signature of `for<'r> fn(&'r (), for<'s> fn(&'s ())) -> _`
[00:39:53]    |
[00:39:53]    = note: required by `g2`

but we could now generate something specific to one of the parameter types, rather like:

[00:39:53] error[E0631]: type mismatch in closure argument
[00:39:53]   --> $DIR/anonymous-higher-ranked-lifetime.rs:18:5
[00:39:53]    |
[00:39:53] 18 |     g2(|_: (), _: ()| {});
[00:39:53]    |            ^^ argument needs type `&()`
[00:39:53]    |     
[00:39:53]    = note: expected type `&()`
[00:39:53]               found type `()`

To do this, we would want to search around for the FIXME(#45727) -- this identifies the newer code, which is currently running in a transaction and falling back to the older strategy in the event of error.

@nikomatsakis nikomatsakis 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. WG-diagnostics Working group: Diagnostics E-needs-mentor labels Nov 2, 2017
@XAMPPRocky XAMPPRocky added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Feb 12, 2018
@estebank
Copy link
Contributor

estebank commented May 29, 2018

Current output:

error[E0631]: type mismatch in closure arguments
 --> src/main.rs:4:5
  |
4 |     g2(|_: (), _: ()| {});
  |     ^^ -------------- found signature of `fn((), ()) -> _`
  |     |
  |     expected signature of `for<'r> fn(&'r (), for<'s> fn(&'s ())) -> _`
  |
note: required by `g2`
 --> src/main.rs:1:1
  |
1 | fn g2<F>(_: F) where F: Fn(&(), fn(&())) {}
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For the given test case, the output would be closer to (as there are multiple arguments with incorrect type):

error[E0631]: type mismatch in closure arguments
 --> src/main.rs:4:5
  |
4 |     g2(|_: (), _: ()| {});
  |         ^^^^^  ^^^^^ expected argument of type `&()`
  |         |
  |         expected argument of type `&()`
  |
note: required by `g2`
 --> src/main.rs:1:1
  |
1 | fn g2<F>(_: F) where F: Fn(&(), fn(&())) {}
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Update:

error[E0631]: type mismatch in closure arguments
 --> src/main.rs:4:5
  |
1 | fn g2<F>(_: F) where F: Fn(&(), fn(&())) {}
  |    --                   ---------------- required by this bound in `g2`
...
4 |     g2(|_: (), _: ()| {});
  |     ^^ -------------- found signature of `fn((), ()) -> _`
  |     |
  |     expected signature of `for<'r> fn(&'r (), for<'s> fn(&'s ())) -> _`

@quat1024
Copy link

quat1024 commented Dec 16, 2022

This sort of thing gave me a head-scratcher in my code (before i added the & on line 88), here's a reduction:

fn main() {
    let _ = (-10..=10).find(|x: i32| x.signum() == 0);
}

erroring as:

error[E0631]: type mismatch in closure arguments
 --> src/lib.rs:2:24
  |
2 |     let _ = (-10..=10).find(|x: i32| x.signum() == 0);
  |                        ^^^^ -------- found signature defined here
  |                        |
  |                        expected due to this
  |
  = note: expected closure signature `for<'a> fn(&'a {integer}) -> _`
             found closure signature `fn(i32) -> _`
note: required by a bound in `find`

For more information about this error, try `rustc --explain E0631`.

but I would much prefer a direct suggestion to replace the closure parameter with |x: &i32|

@estebank
Copy link
Contributor

Current output for the original report:

error[E0631]: type mismatch in closure arguments
 --> src/main.rs:4:5
  |
4 |     g2(|_: (), _: ()| {});
  |     ^^ --------------
  |     |  |   |
  |     |  |   help: consider borrowing the argument: `&()`
  |     |  found signature defined here
  |     expected due to this
  |
  = note: expected closure signature `for<'a> fn(&'a (), for<'a> fn(&'a ())) -> _`
             found closure signature `fn((), ()) -> _`
note: required by a bound in `g2`
 --> src/main.rs:1:25
  |
1 | fn g2<F>(_: F) where F: Fn(&(), fn(&())) {}
  |                         ^^^^^^^^^^^^^^^^ required by this bound in `g2`

And for the recent comment:

error[E0631]: type mismatch in closure arguments
 --> src/main.rs:2:24
  |
2 |     let _ = (-10..=10).find(|x: i32| x.signum() == 0);
  |                        ^^^^ -------- found signature defined here
  |                        |
  |                        expected due to this
  |
  = note: expected closure signature `for<'a> fn(&'a {integer}) -> _`
             found closure signature `fn(i32) -> _`
note: required by a bound in `find`
 --> /rustc/0b90256ada21c6a81b4c18f2c7a23151ab5fc232/library/core/src/iter/traits/iterator.rs:2712:5

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 20, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 20, 2023
@bors bors closed this as completed in 33e11a3 Jan 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants