Skip to content

Commit

Permalink
Explain why we require _ for empty patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Jul 21, 2024
1 parent 710add5 commit 8a49d83
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,10 @@ fn report_non_exhaustive_match<'p, 'tcx>(
err.note("`&str` cannot be matched exhaustively, so a wildcard `_` is necessary");
} else if cx.is_foreign_non_exhaustive_enum(ty) {
err.note(format!("`{ty}` is marked as non-exhaustive, so a wildcard `_` is necessary to match exhaustively"));
} else if cx.is_uninhabited(ty.inner()) && cx.tcx.features().min_exhaustive_patterns {
// The type is uninhabited yet there is a witness: we must be in the `MaybeInvalid`
// case.
err.note(format!("`{ty}` is uninhabited but is not being matched by value, so a wildcard `_` is required"));
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/pattern/usefulness/empty-types.min_exh_pats.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ note: `Option<Void>` defined here
|
= note: not covered
= note: the matched value is of type `Option<Void>`
= note: `Void` is uninhabited but is not being matched by value, so a wildcard `_` is required
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 => {},
Expand Down Expand Up @@ -349,6 +350,7 @@ LL | match slice_never {
| ^^^^^^^^^^^ pattern `&[_, ..]` not covered
|
= note: the matched value is of type `&[!]`
= note: `!` is uninhabited but is not being matched by value, so a wildcard `_` is required
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 ~ [] => {},
Expand Down Expand Up @@ -484,6 +486,7 @@ note: `Option<!>` defined here
|
= note: not covered
= note: the matched value is of type `&Option<!>`
= note: `!` is uninhabited but is not being matched by value, so a wildcard `_` is required
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 => {},
Expand All @@ -502,6 +505,7 @@ note: `Option<!>` defined here
|
= note: not covered
= note: the matched value is of type `Option<!>`
= note: `!` is uninhabited but is not being matched by value, so a wildcard `_` is required
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 => {},
Expand All @@ -520,6 +524,7 @@ note: `Result<!, !>` defined here
|
= note: not covered
= note: the matched value is of type `Result<!, !>`
= note: `!` is uninhabited but is not being matched by value, so a wildcard `_` is required
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 ~ Ok(_) => {},
Expand All @@ -538,6 +543,7 @@ note: `Result<!, !>` defined here
|
= note: not covered
= note: the matched value is of type `Result<!, !>`
= note: `!` is uninhabited but is not being matched by value, so a wildcard `_` is required
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 ~ Ok(_a) => {},
Expand Down Expand Up @@ -589,6 +595,7 @@ LL | match ref_never {
| ^^^^^^^^^ pattern `&_` not covered
|
= note: the matched value is of type `&!`
= note: `!` is uninhabited but is not being matched by value, so a wildcard `_` is required
= note: references are always considered inhabited
= note: match arms with guards don't count towards exhaustivity
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
Expand All @@ -609,6 +616,7 @@ note: `Result<!, !>` defined here
|
= note: not covered
= note: the matched value is of type `Result<!, !>`
= note: `!` is uninhabited but is not being matched by value, so a wildcard `_` is required
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 ~ Err(_) => {},
Expand All @@ -627,6 +635,7 @@ note: `Option<Result<!, !>>` defined here
|
= note: not covered
= note: the matched value is of type `Option<Result<!, !>>`
= note: `Result<!, !>` is uninhabited but is not being matched by value, so a wildcard `_` is required
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 => {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | match nevers {
| ^^^^^^ pattern `&[_, ..]` not covered
|
= note: the matched value is of type `&[!]`
= note: `!` is uninhabited but is not being matched by value, so a wildcard `_` is required
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 ~ &[] => (),
Expand Down

0 comments on commit 8a49d83

Please sign in to comment.