Skip to content

Commit a398994

Browse files
committed
Account for existing _ field pattern when suggesting ..
Follow up to #80017.
1 parent 7907345 commit a398994

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

compiler/rustc_typeck/src/check/pat.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -1041,12 +1041,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10411041
vec![(left, "(".to_string()), (right.shrink_to_hi(), ")".to_string())],
10421042
Applicability::MachineApplicable,
10431043
);
1044-
} else if fields.len() > subpats.len() {
1045-
let after_fields_span = if pat_span == DUMMY_SP {
1046-
pat_span
1047-
} else {
1048-
pat_span.with_hi(pat_span.hi() - BytePos(1)).shrink_to_hi()
1049-
};
1044+
} else if fields.len() > subpats.len() && pat_span != DUMMY_SP {
1045+
let after_fields_span = pat_span.with_hi(pat_span.hi() - BytePos(1)).shrink_to_hi();
10501046
let all_fields_span = match subpats {
10511047
[] => after_fields_span,
10521048
[field] => field.span,
@@ -1055,7 +1051,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10551051

10561052
// Check if all the fields in the pattern are wildcards.
10571053
let all_wildcards = subpats.iter().all(|pat| matches!(pat.kind, PatKind::Wild));
1054+
let first_tail_wildcard =
1055+
subpats.iter().enumerate().fold(None, |acc, (pos, pat)| match (acc, &pat.kind) {
1056+
(None, PatKind::Wild) => Some(pos),
1057+
(Some(_), PatKind::Wild) => acc,
1058+
_ => None,
1059+
});
1060+
let tail_span = match first_tail_wildcard {
1061+
None => after_fields_span,
1062+
Some(0) => subpats[0].span.to(after_fields_span),
1063+
Some(pos) => subpats[pos - 1].span.shrink_to_hi().to(after_fields_span),
1064+
};
10581065

1066+
// FIXME: heuristic-based suggestion to check current types for where to add `_`.
10591067
let mut wildcard_sugg = vec!["_"; fields.len() - subpats.len()].join(", ");
10601068
if !subpats.is_empty() {
10611069
wildcard_sugg = String::from(", ") + &wildcard_sugg;
@@ -1080,7 +1088,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10801088
);
10811089
} else {
10821090
err.span_suggestion_verbose(
1083-
after_fields_span,
1091+
tail_span,
10841092
"use `..` to ignore the rest of the fields",
10851093
String::from(", .."),
10861094
Applicability::MaybeIncorrect,

src/test/ui/pattern/pat-tuple-underfield.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ LL | Point4( a , _ , _, _) => {}
122122
| ^^^^^^
123123
help: use `..` to ignore the rest of the fields
124124
|
125-
LL | Point4( a , _ , ..) => {}
126-
| ^^^^
125+
LL | Point4( a, ..) => {}
126+
| ^^^^
127127

128128
error: aborting due to 8 previous errors
129129

0 commit comments

Comments
 (0)