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

Nonsensical suggestion on closure param type error #114180

Closed
mark-i-m opened this issue Jul 28, 2023 · 0 comments · Fixed by #114256
Closed

Nonsensical suggestion on closure param type error #114180

mark-i-m opened this issue Jul 28, 2023 · 0 comments · Fixed by #114256
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@mark-i-m
Copy link
Member

Code

// Minimal reproduction -- works on rust playground as of 7/28/23
fn main() {
    let mut v = vec![(1,)];
    let compare = |(a,), (e,)| todo!();
    v.sort_by(compare);
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0631]: type mismatch in closure arguments
 --> src/main.rs:6:15
  |
5 |     let compare = |(a,), (e,)| todo!();
  |                   ------------ found signature defined here
6 |     v.sort_by(compare);
  |       ------- ^^^^^^^ expected due to this
  |       |
  |       required by a bound introduced by this call
  |
  = note: expected closure signature `for<'a, 'b> fn(&'a ({integer},), &'b ({integer},)) -> _`
             found closure signature `fn((_,), (_,)) -> _`
note: required by a bound in `slice::<impl [T]>::sort_by`
 --> /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/alloc/src/slice.rs:263:5
help: consider borrowing the argument
  |
5 |     let compare = |(a,&), (e,&)| todo!();
  |                       +      +

For more information about this error, try `rustc --explain E0631`.
error: could not compile `playground` (bin "playground") due to previous error

Desired output

I don't know. There is an HRTB involved here, and I'm not sure there is any simple suggestion that fixes this code.

But certainly the current suggestion should not be there.

Rationale and extra context

Currently, the compiler outputs the following suggestion after the type error:

help: consider borrowing the argument
  |
5 |     let compare = |(a,&), (e,&)| todo!();
  |                       +      +

This is syntactically invalid code.

Other cases

No response

Anything else?

I'm using 1.70 stable and getting this. I also get it with 1.71 stable on the rust playground.

@mark-i-m mark-i-m 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. labels Jul 28, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 28, 2023
@saethlin saethlin added C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jul 29, 2023
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jul 30, 2023
…pkin

Fix invalid suggestion for mismatched types in closure arguments

This PR fixes the invalid suggestion for mismatched types in closure arguments.

The invalid suggestion came from a wrongly created span in the parser for closure arguments that don't have a type specified. Specifically, the span in this case was the last token span, but in the case of tuples, the span represented the last parenthesis instead of the whole tuple, which is fixed by taking the more accurate span of the pattern.

There is one unfortunate downside of this fix, it worsens even more the diagnostic for mismatched types in closure args without an explicit type. This happens because there is no correct span for implied inferred type. I tried also fixing this but it's a rabbit hole.

Fixes rust-lang#114180
@bors bors closed this as completed in 97bacad Jul 30, 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-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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