Skip to content

Commit c372272

Browse files
authored
Unrolled build for #145234
Rollup merge of #145234 - dianne:1-tuple-witnesses, r=jackh726 match exhaustiveness diagnostics: show a trailing comma on singleton tuple consructors in witness patterns (and clean up a little) Constructor patterns of type `(T,)` are written `(pat,)`, not `(pat)`. However, exhaustiveness/usefulness diagnostics would print them as `(pat)` when e.g. providing a witness of non-exhaustiveness and suggesting adding arms to make matches exhaustive; this would result in an error when applied. rust-analyzer already prints the trailing comma, so it doesn't need changing. This also includes some cleanup in the second commit, with justification in the commit message.
2 parents 69b76df + 8f649a7 commit c372272

File tree

3 files changed

+13
-25
lines changed

3 files changed

+13
-25
lines changed

compiler/rustc_pattern_analysis/src/rustc/print.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,23 +101,11 @@ pub(crate) fn write_struct_like<'tcx>(
101101
let num_fields = variant_and_name.as_ref().map_or(subpatterns.len(), |(v, _)| v.fields.len());
102102
if num_fields != 0 || variant_and_name.is_none() {
103103
write!(f, "(")?;
104-
for i in 0..num_fields {
105-
write!(f, "{}", start_or_comma())?;
106-
107-
// Common case: the field is where we expect it.
108-
if let Some(p) = subpatterns.get(i) {
109-
if p.field.index() == i {
110-
write!(f, "{}", p.pattern)?;
111-
continue;
112-
}
113-
}
114-
115-
// Otherwise, we have to go looking for it.
116-
if let Some(p) = subpatterns.iter().find(|p| p.field.index() == i) {
117-
write!(f, "{}", p.pattern)?;
118-
} else {
119-
write!(f, "_")?;
120-
}
104+
for FieldPat { pattern, .. } in subpatterns {
105+
write!(f, "{}{pattern}", start_or_comma())?;
106+
}
107+
if matches!(ty.kind(), ty::Tuple(..)) && num_fields == 1 {
108+
write!(f, ",")?;
121109
}
122110
write!(f, ")")?;
123111
}

tests/ui/or-patterns/exhaustiveness-non-exhaustive.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ fn main() {
77
(0 | 1, 2 | 3) => {}
88
}
99
match ((0u8,),) {
10-
//~^ ERROR non-exhaustive patterns: `((4_u8..=u8::MAX))`
10+
//~^ ERROR non-exhaustive patterns: `((4_u8..=u8::MAX,),)`
1111
((0 | 1,) | (2 | 3,),) => {}
1212
}
1313
match (Some(0u8),) {
14-
//~^ ERROR non-exhaustive patterns: `(Some(2_u8..=u8::MAX))`
14+
//~^ ERROR non-exhaustive patterns: `(Some(2_u8..=u8::MAX),)`
1515
(None | Some(0 | 1),) => {}
1616
}
1717
}

tests/ui/or-patterns/exhaustiveness-non-exhaustive.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,30 @@ LL ~ (0 | 1, 2 | 3) => {},
1111
LL + (2_u8..=u8::MAX, _) => todo!()
1212
|
1313

14-
error[E0004]: non-exhaustive patterns: `((4_u8..=u8::MAX))` not covered
14+
error[E0004]: non-exhaustive patterns: `((4_u8..=u8::MAX,),)` not covered
1515
--> $DIR/exhaustiveness-non-exhaustive.rs:9:11
1616
|
1717
LL | match ((0u8,),) {
18-
| ^^^^^^^^^ pattern `((4_u8..=u8::MAX))` not covered
18+
| ^^^^^^^^^ pattern `((4_u8..=u8::MAX,),)` not covered
1919
|
2020
= note: the matched value is of type `((u8,),)`
2121
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
2222
|
2323
LL ~ ((0 | 1,) | (2 | 3,),) => {},
24-
LL + ((4_u8..=u8::MAX)) => todo!()
24+
LL + ((4_u8..=u8::MAX,),) => todo!()
2525
|
2626

27-
error[E0004]: non-exhaustive patterns: `(Some(2_u8..=u8::MAX))` not covered
27+
error[E0004]: non-exhaustive patterns: `(Some(2_u8..=u8::MAX),)` not covered
2828
--> $DIR/exhaustiveness-non-exhaustive.rs:13:11
2929
|
3030
LL | match (Some(0u8),) {
31-
| ^^^^^^^^^^^^ pattern `(Some(2_u8..=u8::MAX))` not covered
31+
| ^^^^^^^^^^^^ pattern `(Some(2_u8..=u8::MAX),)` not covered
3232
|
3333
= note: the matched value is of type `(Option<u8>,)`
3434
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
3535
|
3636
LL ~ (None | Some(0 | 1),) => {},
37-
LL + (Some(2_u8..=u8::MAX)) => todo!()
37+
LL + (Some(2_u8..=u8::MAX),) => todo!()
3838
|
3939

4040
error: aborting due to 3 previous errors

0 commit comments

Comments
 (0)