Skip to content

Commit

Permalink
Tweak conditions for E0026 and E0769
Browse files Browse the repository at this point in the history
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`".
  • Loading branch information
estebank committed Aug 10, 2020
1 parent 18f3be7 commit 9149ec7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
11 changes: 9 additions & 2 deletions src/librustc_typeck/check/pat.rs
Expand Up @@ -1229,8 +1229,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Applicability::MaybeIncorrect,
);

// we don't want to throw `E0027` in case we have thrown `E0026` for them
unmentioned_fields.retain(|&x| x.name != suggested_name);
// 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. 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);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-17800.rs
Expand Up @@ -6,7 +6,7 @@ enum MyOption<T> {
fn main() {
match MyOption::MySome(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
_ => (),
}
}
11 changes: 4 additions & 7 deletions src/test/ui/issues/issue-17800.stderr
@@ -1,12 +1,9 @@
error[E0026]: variant `MyOption::MySome` does not have a field named `x`
--> $DIR/issue-17800.rs:8:28
error[E0769]: tuple variant `MyOption::MySome` written as struct variant
--> $DIR/issue-17800.rs:8:9
|
LL | MyOption::MySome { x: 42 } => (),
| ^
| |
| variant `MyOption::MySome` does not have this field
| help: a field with a similar name exists: `0`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the tuple variant pattern syntax instead: `MyOption::MySome(42)`

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`.

0 comments on commit 9149ec7

Please sign in to comment.