Skip to content

Commit 8a49d83

Browse files
committed
Explain why we require _ for empty patterns
1 parent 710add5 commit 8a49d83

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

Diff for: compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,10 @@ fn report_non_exhaustive_match<'p, 'tcx>(
10191019
err.note("`&str` cannot be matched exhaustively, so a wildcard `_` is necessary");
10201020
} else if cx.is_foreign_non_exhaustive_enum(ty) {
10211021
err.note(format!("`{ty}` is marked as non-exhaustive, so a wildcard `_` is necessary to match exhaustively"));
1022+
} else if cx.is_uninhabited(ty.inner()) && cx.tcx.features().min_exhaustive_patterns {
1023+
// The type is uninhabited yet there is a witness: we must be in the `MaybeInvalid`
1024+
// case.
1025+
err.note(format!("`{ty}` is uninhabited but is not being matched by value, so a wildcard `_` is required"));
10221026
}
10231027
}
10241028
}

Diff for: tests/ui/pattern/usefulness/empty-types.min_exh_pats.stderr

+9
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ note: `Option<Void>` defined here
204204
|
205205
= note: not covered
206206
= note: the matched value is of type `Option<Void>`
207+
= note: `Void` is uninhabited but is not being matched by value, so a wildcard `_` is required
207208
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
208209
|
209210
LL ~ None => {},
@@ -349,6 +350,7 @@ LL | match slice_never {
349350
| ^^^^^^^^^^^ pattern `&[_, ..]` not covered
350351
|
351352
= note: the matched value is of type `&[!]`
353+
= note: `!` is uninhabited but is not being matched by value, so a wildcard `_` is required
352354
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
353355
|
354356
LL ~ [] => {},
@@ -484,6 +486,7 @@ note: `Option<!>` defined here
484486
|
485487
= note: not covered
486488
= note: the matched value is of type `&Option<!>`
489+
= note: `!` is uninhabited but is not being matched by value, so a wildcard `_` is required
487490
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
488491
|
489492
LL ~ &None => {},
@@ -502,6 +505,7 @@ note: `Option<!>` defined here
502505
|
503506
= note: not covered
504507
= note: the matched value is of type `Option<!>`
508+
= note: `!` is uninhabited but is not being matched by value, so a wildcard `_` is required
505509
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
506510
|
507511
LL ~ None => {},
@@ -520,6 +524,7 @@ note: `Result<!, !>` defined here
520524
|
521525
= note: not covered
522526
= note: the matched value is of type `Result<!, !>`
527+
= note: `!` is uninhabited but is not being matched by value, so a wildcard `_` is required
523528
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
524529
|
525530
LL ~ Ok(_) => {},
@@ -538,6 +543,7 @@ note: `Result<!, !>` defined here
538543
|
539544
= note: not covered
540545
= note: the matched value is of type `Result<!, !>`
546+
= note: `!` is uninhabited but is not being matched by value, so a wildcard `_` is required
541547
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
542548
|
543549
LL ~ Ok(_a) => {},
@@ -589,6 +595,7 @@ LL | match ref_never {
589595
| ^^^^^^^^^ pattern `&_` not covered
590596
|
591597
= note: the matched value is of type `&!`
598+
= note: `!` is uninhabited but is not being matched by value, so a wildcard `_` is required
592599
= note: references are always considered inhabited
593600
= note: match arms with guards don't count towards exhaustivity
594601
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
@@ -609,6 +616,7 @@ note: `Result<!, !>` defined here
609616
|
610617
= note: not covered
611618
= note: the matched value is of type `Result<!, !>`
619+
= note: `!` is uninhabited but is not being matched by value, so a wildcard `_` is required
612620
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
613621
|
614622
LL ~ Err(_) => {},
@@ -627,6 +635,7 @@ note: `Option<Result<!, !>>` defined here
627635
|
628636
= note: not covered
629637
= note: the matched value is of type `Option<Result<!, !>>`
638+
= note: `Result<!, !>` is uninhabited but is not being matched by value, so a wildcard `_` is required
630639
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
631640
|
632641
LL ~ None => {},

Diff for: tests/ui/pattern/usefulness/slice_of_empty.min_exhaustive_patterns.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | match nevers {
55
| ^^^^^^ pattern `&[_, ..]` not covered
66
|
77
= note: the matched value is of type `&[!]`
8+
= note: `!` is uninhabited but is not being matched by value, so a wildcard `_` is required
89
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
910
|
1011
LL ~ &[] => (),

0 commit comments

Comments
 (0)