Skip to content

Incorrect suggestion for extra & in pattern #106182

Closed

Description

@RalfJung

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) => {

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Dec 27, 2022
  2. obeis commented on Dec 27, 2022

    @obeis
    Contributor

    @rustbot claim

  3. added
    D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.
    on Dec 30, 2022
  4. added 2 commits that reference this issue on Jan 9, 2023
    00cfb19
    b91bb01
  5. added a commit that references this issue on Jan 9, 2023
    37c1d6d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-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