Skip to content

Nonsensical suggestion on closure param type error #114180

Closed
@mark-i-m

Description

@mark-i-m

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions