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

Incorrect suggestion for extra & in pattern #106182

Closed
RalfJung opened this issue Dec 27, 2022 · 1 comment · Fixed by #106291
Closed

Incorrect suggestion for extra & in pattern #106182

RalfJung opened this issue Dec 27, 2022 · 1 comment · Fixed by #106291
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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

@RalfJung
Copy link
Member

RalfJung commented Dec 27, 2022

Given the following code:

struct S(u32, Vec<i32>);

fn foo(x: &S) {
    match x {
        S(& (mut y), v) => {
        }
    }
}

The current output is:

error[[E0308]](https://doc.rust-lang.org/nightly/error-index.html#E0308): mismatched types
 --> src/lib.rs:5:11
  |
4 |     match x {
  |           - this expression has type `&S`
5 |         S(& (mut y), v) => {
  |           ^^^^^^^^^ expected `u32`, found reference
  |
  = note:   expected type `u32`
          found reference `&_`
help: consider removing `&` from the pattern
  |
5 -         S(& (mut y), v) => {
5 +         S(mut y), v) => {
  |

Note that the suggestion is wrong, it removes the ( but left in the matching ). Applying that suggestion leads to invalid code:

struct S(u32, Vec<i32>);

fn foo(x: &S) {
    match x {
        S(mut y), v) => {
        }
    }
}

It should instead suggest

5 -         S(& (mut y), v) => {
5 +         S(mut y, v) => {
@RalfJung RalfJung 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 Dec 27, 2022
@obeis
Copy link
Contributor

obeis commented Dec 27, 2022

@rustbot claim

@estebank estebank added the D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. label Dec 30, 2022
obeis added a commit to obeis/rust that referenced this issue Dec 30, 2022
obeis added a commit to obeis/rust that referenced this issue Dec 30, 2022
JohnTitor pushed a commit to JohnTitor/rust that referenced this issue Jan 9, 2023
Fix incorrect suggestion for extra `&` in pattern

Closes rust-lang#106182
JohnTitor pushed a commit to JohnTitor/rust that referenced this issue Jan 9, 2023
Fix incorrect suggestion for extra `&` in pattern

Closes rust-lang#106182
@bors bors closed this as completed in 37c1d6d Jan 9, 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 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