Skip to content

Commit e51a839

Browse files
authored
Rollup merge of #75352 - estebank:incorrect-tuple-struct-pat, r=oli-obk
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`".
2 parents a75bdfa + 9149ec7 commit e51a839

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

src/librustc_typeck/check/pat.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1229,8 +1229,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12291229
Applicability::MaybeIncorrect,
12301230
);
12311231

1232-
// we don't want to throw `E0027` in case we have thrown `E0026` for them
1233-
unmentioned_fields.retain(|&x| x.name != suggested_name);
1232+
// When we have a tuple struct used with struct we don't want to suggest using
1233+
// the (valid) struct syntax with numeric field names. Instead we want to
1234+
// suggest the expected syntax. We infer that this is the case by parsing the
1235+
// `Ident` into an unsized integer. The suggestion will be emitted elsewhere in
1236+
// `smart_resolve_context_dependent_help`.
1237+
if suggested_name.to_ident_string().parse::<usize>().is_err() {
1238+
// We don't want to throw `E0027` in case we have thrown `E0026` for them.
1239+
unmentioned_fields.retain(|&x| x.name != suggested_name);
1240+
}
12341241
}
12351242
}
12361243
}

src/test/ui/issues/issue-17800.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ enum MyOption<T> {
66
fn main() {
77
match MyOption::MySome(42) {
88
MyOption::MySome { x: 42 } => (),
9-
//~^ ERROR variant `MyOption::MySome` does not have a field named `x`
9+
//~^ ERROR tuple variant `MyOption::MySome` written as struct variant
1010
_ => (),
1111
}
1212
}

src/test/ui/issues/issue-17800.stderr

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
error[E0026]: variant `MyOption::MySome` does not have a field named `x`
2-
--> $DIR/issue-17800.rs:8:28
1+
error[E0769]: tuple variant `MyOption::MySome` written as struct variant
2+
--> $DIR/issue-17800.rs:8:9
33
|
44
LL | MyOption::MySome { x: 42 } => (),
5-
| ^
6-
| |
7-
| variant `MyOption::MySome` does not have this field
8-
| help: a field with a similar name exists: `0`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the tuple variant pattern syntax instead: `MyOption::MySome(42)`
96

107
error: aborting due to previous error
118

12-
For more information about this error, try `rustc --explain E0026`.
9+
For more information about this error, try `rustc --explain E0769`.

0 commit comments

Comments
 (0)