Skip to content

E0507 suggestion conflicts with Rust 2024 match ergonomics suggestion #133370

@dianne

Description

@dianne

Code

fn main() {
    let a = vec![String::from("a")];
    let opt = a.iter().enumerate().find(|(_, &s)| {
        //~^ ERROR cannot move out
        *s == String::from("d")
    }).map(|(i, _)| i);
    println!("{:?}", opt);
}

Current output

error: patterns are not allowed to reset the default binding mode in edition 2024
 --> src/main.rs:3:42
  |
3 |     let opt = a.iter().enumerate().find(|(_, &s)| {
  |                                          -^^^^^^
  |                                          |
  |                                          help: desugar the match ergonomics: `&`

error[E0507]: cannot move out of a shared reference
 --> src/main.rs:3:42
  |
3 |     let opt = a.iter().enumerate().find(|(_, &s)| {
  |                                          ^^^^^-^
  |                                               |
  |                                               data moved here
  |                                               move occurs because `s` has type `String`, which does not implement the `Copy` trait
  |
help: consider borrowing the pattern binding
  |
3 |     let opt = a.iter().enumerate().find(|(_, &ref s)| {
  |                                               +++

Desired output

error: patterns are not allowed to reset the default binding mode in edition 2024
 --> src/main.rs:3:42
  |
3 |     let opt = a.iter().enumerate().find(|(_, &s)| {
  |                                          -^^^^^^
  |                                          |
  |                                          help: desugar the match ergonomics: `&`

error[E0507]: cannot move out of a shared reference
 --> src/main.rs:3:42
  |
3 |     let opt = a.iter().enumerate().find(|(_, &s)| {
  |                                          ^^^^^-^
  |                                               |
  |                                               data moved here
  |                                               move occurs because `s` has type `String`, which does not implement the `Copy` trait
  |
help: consider moving the borrow
  |
3 -     let opt = a.iter().enumerate().find(|(_, &s)| {
3 +     let opt = a.iter().enumerate().find(|&(_, s)| {
  |

error: aborting due to 1 previous error

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

Rationale and extra context

The two suggestions conflict, both claim to be MachineApplicable, and both lead to errors. Applying the current suggestion for E0507 in Rust 2024 produces the error

error: patterns are not allowed to reset the default binding mode in edition 2024
 --> src/main.rs:3:42
  |
3 |     let opt = a.iter().enumerate().find(|(_, &ref s)| {
  |                                          -^^^^^^^^^^
  |                                          |
  |                                          help: desugar the match ergonomics: `&`

whereas applying the match ergonomics suggestion instead would lead to E0507 suggesting to remove the leading & (#132806), taking us back to the start. I'm not sure how best to report this, since it's still a bit confusing with the separate errors giving different advice for the same code. I'll have to ask/see if there's any best practices there.

I found this when looking at how my upcoming fix for #132806 would change existing test output. That fix happens to make the E0507 suggestion work on Rust 2024, but I figure they should be tracked separately. @rustbot claim

Other cases

Rust Version

rustc 1.85.0-nightly (a47555110 2024-11-22)
binary: rustc
commit-hash: a47555110cf09b3ed59811d9b02235443e76a595
commit-date: 2024-11-22
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.4

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions