Skip to content

Commit 2f210be

Browse files
authored
Rollup merge of rust-lang#87960 - hkmatsumoto:suggest-inexisting-field-for-unmentioned-field, r=estebank
Suggest replacing an inexisting field for an unmentioned field Fix rust-lang#87938 This PR adds a suggestion to replace an inexisting field for an unmentioned field. Given the following code: ```rust enum Foo { Bar { alpha: u8, bravo: u8, charlie: u8 }, } fn foo(foo: Foo) { match foo { Foo::Bar { alpha, beta, // `bravo` miswritten as `beta` here. charlie, } => todo!(), } } ``` the compiler now emits the error messages below. ```text error[E0026]: variant `Foo::Bar` does not have a field named `beta` --> src/lib.rs:9:13 | 9 | beta, // `bravo` miswritten as `beta` here. | ^^^^ | | | variant `Foo::Bar` does not have this field | help: `Foo::Bar` has a field named `bravo`: `bravo` ``` Note that this suggestion is available iff the number of inexisting fields and unmentioned fields are both 1.
2 parents 10967a1 + 37196e3 commit 2f210be

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

compiler/rustc_typeck/src/check/pat.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1452,7 +1452,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14521452
plural
14531453
),
14541454
);
1455-
if plural == "" {
1455+
1456+
if unmentioned_fields.len() == 1 {
14561457
let input =
14571458
unmentioned_fields.iter().map(|(_, field)| field.name).collect::<Vec<_>>();
14581459
let suggested_name = find_best_match_for_name(&input, ident.name, None);
@@ -1473,6 +1474,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14731474
// We don't want to throw `E0027` in case we have thrown `E0026` for them.
14741475
unmentioned_fields.retain(|&(_, x)| x.name != suggested_name);
14751476
}
1477+
} else if inexistent_fields.len() == 1 {
1478+
let unmentioned_field = unmentioned_fields[0].1.name;
1479+
err.span_suggestion_short(
1480+
ident.span,
1481+
&format!(
1482+
"`{}` has a field named `{}`",
1483+
tcx.def_path_str(variant.def_id),
1484+
unmentioned_field
1485+
),
1486+
unmentioned_field.to_string(),
1487+
Applicability::MaybeIncorrect,
1488+
);
14761489
}
14771490
}
14781491
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0026]: struct `SimpleStruct` does not have a field named `state`
22
--> $DIR/issue-51102.rs:13:17
33
|
44
LL | state: 0,
5-
| ^^^^^ struct `SimpleStruct` does not have this field
5+
| ^^^^^
6+
| |
7+
| struct `SimpleStruct` does not have this field
8+
| help: `SimpleStruct` has a field named `no_state_here`
69

710
error[E0025]: field `no_state_here` bound multiple times in the pattern
811
--> $DIR/issue-51102.rs:24:17

src/test/ui/numeric/numeric-fields.stderr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ error[E0026]: struct `S` does not have a field named `0x1`
1616
--> $DIR/numeric-fields.rs:7:17
1717
|
1818
LL | S{0: a, 0x1: b, ..} => {}
19-
| ^^^ struct `S` does not have this field
19+
| ^^^
20+
| |
21+
| struct `S` does not have this field
22+
| help: `S` has a field named `1`
2023

2124
error: aborting due to 2 previous errors
2225

0 commit comments

Comments
 (0)