Tweak conditions for E0026 and E0769

When we have a tuple struct used with struct we don't want to suggest using
the (valid) struct syntax with numeric field names. Instead we want to
suggest the expected syntax.

Given

```rust
fn main() {
    match MyOption::MySome(42) {
        MyOption::MySome { x: 42 } => (),
        _ => (),
    }
}
```

We now emit E0769 "tuple variant `MyOption::MySome` written as struct variant"
instead of E0026 "variant `MyOption::MySome` does not have a field named `x`".
This commit is contained in:
Esteban Küber 2020-08-09 16:56:47 -07:00
parent 18f3be7704
commit 9149ec74db
3 changed files with 14 additions and 10 deletions

View file

@ -1229,8 +1229,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
); );
// we don't want to throw `E0027` in case we have thrown `E0026` for them // When we have a tuple struct used with struct we don't want to suggest using
unmentioned_fields.retain(|&x| x.name != suggested_name); // the (valid) struct syntax with numeric field names. Instead we want to
// suggest the expected syntax. We infer that this is the case by parsing the
// `Ident` into an unsized integer. The suggestion will be emitted elsewhere in
// `smart_resolve_context_dependent_help`.
if suggested_name.to_ident_string().parse::<usize>().is_err() {
// We don't want to throw `E0027` in case we have thrown `E0026` for them.
unmentioned_fields.retain(|&x| x.name != suggested_name);
}
} }
} }
} }

View file

@ -6,7 +6,7 @@ enum MyOption<T> {
fn main() { fn main() {
match MyOption::MySome(42) { match MyOption::MySome(42) {
MyOption::MySome { x: 42 } => (), MyOption::MySome { x: 42 } => (),
//~^ ERROR variant `MyOption::MySome` does not have a field named `x` //~^ ERROR tuple variant `MyOption::MySome` written as struct variant
_ => (), _ => (),
} }
} }

View file

@ -1,12 +1,9 @@
error[E0026]: variant `MyOption::MySome` does not have a field named `x` error[E0769]: tuple variant `MyOption::MySome` written as struct variant
--> $DIR/issue-17800.rs:8:28 --> $DIR/issue-17800.rs:8:9
| |
LL | MyOption::MySome { x: 42 } => (), LL | MyOption::MySome { x: 42 } => (),
| ^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the tuple variant pattern syntax instead: `MyOption::MySome(42)`
| |
| variant `MyOption::MySome` does not have this field
| help: a field with a similar name exists: `0`
error: aborting due to previous error error: aborting due to previous error
For more information about this error, try `rustc --explain E0026`. For more information about this error, try `rustc --explain E0769`.