Skip to content

Error E0507 suggestion is invalid with let else statements #104838

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
sgued opened this issue Nov 24, 2022 · 4 comments · Fixed by #105476
Closed

Error E0507 suggestion is invalid with let else statements #104838

sgued opened this issue Nov 24, 2022 · 4 comments · Fixed by #105476
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

@sgued
Copy link
Contributor

sgued commented Nov 24, 2022

Given the following code (playground):

struct Testing {
    a: Option<String>
}

fn testing(a: &Testing) {
    let Some(_s) = a.a else {
        return;
    };
}

The current output is:

error[[E0507]](https://doc.rust-lang.org/nightly/error-index.html#E0507): cannot move out of `a.a` as enum variant `Some` which is behind a shared reference
 --> src/lib.rs:6:14
  |
6 |     let Some(_s) = a.a else {
  |              ^^
  |              |
  |              move occurs because `a.a.0` has type `String`, which does not implement the `Copy` trait
  |              help: consider borrowing here: `&_s`

For more information about this error, try `rustc --explain E0507`.
error: could not compile `playground` due to previous error

Ideally the output should look like:

error[[E0507]](https://doc.rust-lang.org/nightly/error-index.html#E0507): cannot move out of `a.a` as enum variant `Some` which is behind a shared reference
 --> src/lib.rs:6:14
  |
6 |     let Some(_s) = a.a else {
  |              ^^    ^^^
  |              |     |
  |              |     help: consider borrowing here: `&a.a`       
  |              |
  |              move occurs because `a.a.0` has type `String`, which does not implement the `Copy` trait


For more information about this error, try `rustc --explain E0507`.
error: could not compile `playground` due to previous error

An alternative would be to recommend using ref, but this would not be consistent with the match suggestion:

fn testing2(a: &Testing) {
    match a.a {
        Some(_s) => return,
        None => {}
    }
}
error[[E0507]](https://doc.rust-lang.org/nightly/error-index.html#E0507): cannot move out of `a.a` as enum variant `Some` which is behind a shared reference
  --> src/lib.rs:12:11
   |
12 |     match a.a {
   |           ^^^ help: consider borrowing here: `&a.a`
13 |         Some(_s) => return,
   |              --
   |              |
   |              data moved here
   |              move occurs because `_s` has type `String`, which does not implement the `Copy` trait

Tested with Nightly version: 1.67.0-nightly (2022-11-23 70f8737)

@sgued sgued 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 24, 2022
@chenyukang
Copy link
Member

Maybe related to #103908, but not in name resolve phase.

@sgued
Copy link
Contributor Author

sgued commented Nov 26, 2022

I think it's an issue of the statement and the match span being inverted when they shouldn't here. It seems that for let … else errors, match_place is None when it shouldn't.

@sgued
Copy link
Contributor Author

sgued commented Nov 26, 2022

The missing match_place should probably be added here

@Jerrody
Copy link
Contributor

Jerrody commented Dec 5, 2022

The missing match_place should probably be added here

@estebank
Hey, can you fix your PR, please? Maybe this is a potential fix, thanks.
#103908

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 13, 2022
…er-errors

Change pattern borrowing suggestions to be verbose and remove invalid suggestion

Synthesize a more accurate span and use verbose suggestion output to
make the message clearer.

Do not suggest borrowing binding in pattern in let else. Fix rust-lang#104838.
@bors bors closed this as completed in 4f7c257 Dec 14, 2022
# for free to join this conversation on GitHub. Already have an account? # 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

Successfully merging a pull request may close this issue.

3 participants