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

E0507 suggestion conflicts with Rust 2024 match ergonomics suggestion #133370

Closed
dianne opened this issue Nov 23, 2024 · 1 comment
Closed

E0507 suggestion conflicts with Rust 2024 match ergonomics suggestion #133370

dianne opened this issue Nov 23, 2024 · 1 comment
Assignees
Labels
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.

Comments

@dianne
Copy link
Contributor

dianne commented Nov 23, 2024

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

@dianne dianne 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 Nov 23, 2024
@dianne dianne changed the title E0507 (cannot move out of a shared reference) suggests patterns invalid in Rust 2024 E0507 suggestion conflicts with Rust 2024 match ergonomics suggestion Nov 23, 2024
@dianne
Copy link
Contributor Author

dianne commented Nov 23, 2024

I just realized the suggestions can actually be applied together and it works fine (at least in this case). &(_, &ref s) is just a strange pattern to suggest. This probably doesn't need a separate issue. Sorry for the noise.

@dianne dianne closed this as not planned Won't fix, can't repro, duplicate, stale Nov 23, 2024
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 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant