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

Account for existing _ field pattern when suggesting .. #81422

Merged
merged 1 commit into from
Jan 31, 2021
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
22 changes: 15 additions & 7 deletions compiler/rustc_typeck/src/check/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,12 +1041,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
vec![(left, "(".to_string()), (right.shrink_to_hi(), ")".to_string())],
Applicability::MachineApplicable,
);
} else if fields.len() > subpats.len() {
let after_fields_span = if pat_span == DUMMY_SP {
pat_span
} else {
pat_span.with_hi(pat_span.hi() - BytePos(1)).shrink_to_hi()
};
} else if fields.len() > subpats.len() && pat_span != DUMMY_SP {
let after_fields_span = pat_span.with_hi(pat_span.hi() - BytePos(1)).shrink_to_hi();
let all_fields_span = match subpats {
[] => after_fields_span,
[field] => field.span,
Expand All @@ -1055,7 +1051,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

// Check if all the fields in the pattern are wildcards.
let all_wildcards = subpats.iter().all(|pat| matches!(pat.kind, PatKind::Wild));
let first_tail_wildcard =
subpats.iter().enumerate().fold(None, |acc, (pos, pat)| match (acc, &pat.kind) {
(None, PatKind::Wild) => Some(pos),
(Some(_), PatKind::Wild) => acc,
_ => None,
});
let tail_span = match first_tail_wildcard {
None => after_fields_span,
Some(0) => subpats[0].span.to(after_fields_span),
Some(pos) => subpats[pos - 1].span.shrink_to_hi().to(after_fields_span),
};

// FIXME: heuristic-based suggestion to check current types for where to add `_`.
let mut wildcard_sugg = vec!["_"; fields.len() - subpats.len()].join(", ");
if !subpats.is_empty() {
wildcard_sugg = String::from(", ") + &wildcard_sugg;
Expand All @@ -1080,7 +1088,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
);
} else {
err.span_suggestion_verbose(
after_fields_span,
tail_span,
"use `..` to ignore the rest of the fields",
String::from(", .."),
Applicability::MaybeIncorrect,
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/pattern/pat-tuple-underfield.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ LL | Point4( a , _ , _, _) => {}
| ^^^^^^
help: use `..` to ignore the rest of the fields
|
LL | Point4( a , _ , ..) => {}
| ^^^^
LL | Point4( a, ..) => {}
| ^^^^

error: aborting due to 8 previous errors

Expand Down