Skip to content

Error message help suggests type ascription on pattern binding #55174

Closed
@sunjay

Description

@sunjay

Here's the minimal reproduction:

fn main() {
    let x = vec![1i32];
    match &x[..] {
        [&v] => {},
        _ => {},
    }
}

This produces the following error:

error[E0308]: mismatched types
 --> src/main.rs:4:10
  |
4 |         [&v] => {},
  |          ^^ expected i32, found reference
  |
  = note: expected type `i32`
             found type `&_`
  = help: did you mean `v: &i32`?

The error rightly points out that we are matching a reference when we actually have the full value itself.

The problem is the help message at the bottom:

help: did you mean `v: &i32`?

You can't annotate the type of a variable bound in a pattern, so trying this ends up with a syntax error:

error: expected one of `,`, `..`, or `@`, found `:`
 --> src/main.rs:4:12
  |
4 |         [&v: &i32] => {},
  |            ^ expected one of `,`, `..`, or `@` here

error: aborting due to previous error

That help message should probably not show up in patterns at all.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions