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

Erroneous suggestion to add .field when matching arrays #91058

Closed
compiler-errors opened this issue Nov 19, 2021 · 1 comment · Fixed by #91098
Closed

Erroneous suggestion to add .field when matching arrays #91058

compiler-errors opened this issue Nov 19, 2021 · 1 comment · Fixed by #91098
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

@compiler-errors
Copy link
Member

compiler-errors commented Nov 19, 2021

Given this code:

struct S(());

fn main() {
    let array = [S(())];
    
    match array {
        [()] => {}
        _ => {}
    }
}

The current output is:

error[E0308]: mismatched types
 --> src/main.rs:7:10
  |
6 |     match array {
  |           ----- this expression has type `[S; 1]`
7 |         [()] => {}
  |          ^^ expected struct `S`, found `()`
  |
help: you might have meant to use field `0` whose type is `()`
  |
6 |     match array.0 {
  |           ~~~~~~~

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

We should suppress the help: suggestion in this case, since it's not possible to apply that change.

@compiler-errors compiler-errors 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 19, 2021
@compiler-errors
Copy link
Member Author

The bug probably exists in suggest_accessing_field_where_appropriate in rustc_infer::infer::error_reporting. I've been touching stuff around that file, so I can claim this issue and put up a fix!

cc: @estebank since I mentioned this error in #91022

@rustbot claim

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
2 participants