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

Suggest replacing an inexisting field for an unmentioned field #87960

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion compiler/rustc_typeck/src/check/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1447,7 +1447,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
plural
),
);
if plural == "" {

if unmentioned_fields.len() == 1 {
let input =
unmentioned_fields.iter().map(|(_, field)| field.name).collect::<Vec<_>>();
let suggested_name = find_best_match_for_name(&input, ident.name, None);
Expand All @@ -1468,6 +1469,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// We don't want to throw `E0027` in case we have thrown `E0026` for them.
unmentioned_fields.retain(|&(_, x)| x.name != suggested_name);
}
} else if inexistent_fields.len() == 1 {
let unmentioned_field = unmentioned_fields[0].1.name;
err.span_suggestion_short(
ident.span,
&format!(
"`{}` has a field named `{}`",
tcx.def_path_str(variant.def_id),
unmentioned_field
),
unmentioned_field.to_string(),
Applicability::MaybeIncorrect,
);
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/test/ui/issues/issue-51102.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ error[E0026]: struct `SimpleStruct` does not have a field named `state`
--> $DIR/issue-51102.rs:13:17
|
LL | state: 0,
| ^^^^^ struct `SimpleStruct` does not have this field
| ^^^^^
| |
| struct `SimpleStruct` does not have this field
| help: `SimpleStruct` has a field named `no_state_here`

error[E0025]: field `no_state_here` bound multiple times in the pattern
--> $DIR/issue-51102.rs:24:17
Expand Down
5 changes: 4 additions & 1 deletion src/test/ui/numeric/numeric-fields.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ error[E0026]: struct `S` does not have a field named `0x1`
--> $DIR/numeric-fields.rs:7:17
|
LL | S{0: a, 0x1: b, ..} => {}
| ^^^ struct `S` does not have this field
| ^^^
| |
| struct `S` does not have this field
| help: `S` has a field named `1`

error: aborting due to 2 previous errors

Expand Down