Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit ff90c63

Browse files
committed
Cleanup the reporting of unreachable patterns
1 parent 003bbcb commit ff90c63

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -190,20 +190,16 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
190190
let scrut_ty = self.typeck_results.expr_ty_adjusted(scrut);
191191
let report = compute_match_usefulness(&cx, &arms, scrut.hir_id, scrut_ty);
192192

193-
report_arm_reachability(&cx, &report, |arm_span, arm_hir_id, catchall| {
194-
match source {
195-
hir::MatchSource::ForLoopDesugar | hir::MatchSource::Normal => {
196-
unreachable_pattern(cx.tcx, arm_span, arm_hir_id, catchall);
197-
}
198-
// Unreachable patterns in try and await expressions occur when one of
199-
// the arms are an uninhabited type. Which is OK.
200-
hir::MatchSource::AwaitDesugar | hir::MatchSource::TryDesugar => {}
193+
match source {
194+
hir::MatchSource::ForLoopDesugar | hir::MatchSource::Normal => {
195+
report_arm_reachability(&cx, &report)
201196
}
202-
});
197+
// Unreachable patterns in try and await expressions occur when one of
198+
// the arms are an uninhabited type. Which is OK.
199+
hir::MatchSource::AwaitDesugar | hir::MatchSource::TryDesugar => {}
200+
}
203201

204202
// Check if the match is exhaustive.
205-
// Note: An empty match isn't the same as an empty matrix for diagnostics purposes,
206-
// since an empty matrix can occur when there are arms, if those arms all have guards.
207203
let is_empty_match = arms.is_empty();
208204
let witnesses = report.non_exhaustiveness_witnesses;
209205
if !witnesses.is_empty() {
@@ -434,9 +430,10 @@ fn check_let_reachability<'p, 'tcx>(
434430
let arms = [MatchArm { pat, hir_id: pat_id, has_guard: false }];
435431
let report = compute_match_usefulness(&cx, &arms, pat_id, pat.ty);
436432

437-
report_arm_reachability(&cx, &report, |arm_span, arm_hir_id, _| {
438-
unreachable_pattern(cx.tcx, arm_span, arm_hir_id, None)
439-
});
433+
// Report if the pattern is unreachable, which can only occur when the type is uninhabited.
434+
// This also reports unreachable sub-patterns though, so we can't just replace it with an
435+
// `is_uninhabited` check.
436+
report_arm_reachability(&cx, &report);
440437

441438
if report.non_exhaustiveness_witnesses.is_empty() {
442439
// The match is exhaustive, i.e. the `if let` pattern is irrefutable.
@@ -445,18 +442,15 @@ fn check_let_reachability<'p, 'tcx>(
445442
}
446443

447444
/// Report unreachable arms, if any.
448-
fn report_arm_reachability<'p, 'tcx, F>(
445+
fn report_arm_reachability<'p, 'tcx>(
449446
cx: &MatchCheckCtxt<'p, 'tcx>,
450447
report: &UsefulnessReport<'p, 'tcx>,
451-
unreachable: F,
452-
) where
453-
F: Fn(Span, HirId, Option<Span>),
454-
{
448+
) {
455449
use Reachability::*;
456450
let mut catchall = None;
457451
for (arm, is_useful) in report.arm_usefulness.iter() {
458452
match is_useful {
459-
Unreachable => unreachable(arm.pat.span, arm.hir_id, catchall),
453+
Unreachable => unreachable_pattern(cx.tcx, arm.pat.span, arm.hir_id, catchall),
460454
Reachable(unreachables) if unreachables.is_empty() => {}
461455
// The arm is reachable, but contains unreachable subpatterns (from or-patterns).
462456
Reachable(unreachables) => {

0 commit comments

Comments
 (0)