Skip to content

match exhaustiveness diagnostics: show a trailing comma on singleton tuple consructors in witness patterns (and clean up a little) #145234

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 5 additions & 17 deletions compiler/rustc_pattern_analysis/src/rustc/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,11 @@ pub(crate) fn write_struct_like<'tcx>(
let num_fields = variant_and_name.as_ref().map_or(subpatterns.len(), |(v, _)| v.fields.len());
if num_fields != 0 || variant_and_name.is_none() {
write!(f, "(")?;
for i in 0..num_fields {
write!(f, "{}", start_or_comma())?;

// Common case: the field is where we expect it.
if let Some(p) = subpatterns.get(i) {
if p.field.index() == i {
write!(f, "{}", p.pattern)?;
continue;
}
}

// Otherwise, we have to go looking for it.
if let Some(p) = subpatterns.iter().find(|p| p.field.index() == i) {
write!(f, "{}", p.pattern)?;
} else {
write!(f, "_")?;
}
for FieldPat { pattern, .. } in subpatterns {
write!(f, "{}{pattern}", start_or_comma())?;
}
if matches!(ty.kind(), ty::Tuple(..)) && num_fields == 1 {
write!(f, ",")?;
}
write!(f, ")")?;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/or-patterns/exhaustiveness-non-exhaustive.rs
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't added a test specifically for singleton tuple witnesses since this test included them. I could add a separate test if it would be preferable, though, to make sure it's still tested even if this test changes.

Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ fn main() {
(0 | 1, 2 | 3) => {}
}
match ((0u8,),) {
//~^ ERROR non-exhaustive patterns: `((4_u8..=u8::MAX))`
//~^ ERROR non-exhaustive patterns: `((4_u8..=u8::MAX,),)`
((0 | 1,) | (2 | 3,),) => {}
}
match (Some(0u8),) {
//~^ ERROR non-exhaustive patterns: `(Some(2_u8..=u8::MAX))`
//~^ ERROR non-exhaustive patterns: `(Some(2_u8..=u8::MAX),)`
(None | Some(0 | 1),) => {}
}
}
12 changes: 6 additions & 6 deletions tests/ui/or-patterns/exhaustiveness-non-exhaustive.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@ LL ~ (0 | 1, 2 | 3) => {},
LL + (2_u8..=u8::MAX, _) => todo!()
|

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

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

error: aborting due to 3 previous errors
Expand Down
Loading