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

Error message help suggests type ascription on pattern binding #55174

Closed
sunjay opened this issue Oct 18, 2018 · 0 comments · Fixed by #60393
Closed

Error message help suggests type ascription on pattern binding #55174

sunjay opened this issue Oct 18, 2018 · 0 comments · Fixed by #60393
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug.

Comments

@sunjay
Copy link
Member

sunjay commented Oct 18, 2018

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.

@Havvy Havvy added A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. labels Oct 18, 2018
Centril added a commit to Centril/rust that referenced this issue May 2, 2019
 Do not suggest incorrect syntax on pattern type error due to borrow

Fix rust-lang#55174.
Centril added a commit to Centril/rust that referenced this issue May 3, 2019
 Do not suggest incorrect syntax on pattern type error due to borrow

Fix rust-lang#55174.
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 C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants