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

Commit fd2d6ce

Browse files
committed
Shortcut on single wildcard
Obtained by repeatedly inlining and simplifying, beyond the point of reason.
1 parent 3504a96 commit fd2d6ce

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,17 @@ impl ConstructorSet {
867867
}
868868
}
869869

870+
/// Whether `self.split().missing` is empty regardless of the column.
871+
pub(super) fn is_empty(&self, cx: &MatchCheckCtxt<'_, '_>, is_top_level: bool) -> bool {
872+
match self {
873+
ConstructorSet::Variants {
874+
visible_variants, hidden_variants, non_exhaustive, ..
875+
} => visible_variants.is_empty() && hidden_variants.is_empty() && !*non_exhaustive,
876+
ConstructorSet::Uninhabited => cx.tcx.features().exhaustive_patterns || is_top_level,
877+
_ => false,
878+
}
879+
}
880+
870881
/// This is the core logical operation of exhaustiveness checking. This analyzes a column a
871882
/// constructors to 1/ determine which constructors of the type (if any) are missing; 2/ split
872883
/// constructors to handle non-trivial intersections e.g. on ranges or slices.

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,28 @@ pub(crate) fn compute_match_usefulness<'p, 'tcx>(
11481148
scrut_ty: Ty<'tcx>,
11491149
scrut_span: Span,
11501150
) -> UsefulnessReport<'p, 'tcx> {
1151+
// Shortcut on single wildcard.
1152+
if let [arm] = arms {
1153+
if arm.pat.ctor().is_wildcard() && !arm.has_guard {
1154+
let mut ty = scrut_ty;
1155+
// Opaque types can't get destructured/split, but the patterns can
1156+
// actually hint at hidden types, so we use the patterns' types instead.
1157+
if let ty::Alias(ty::Opaque, ..) = ty.kind() {
1158+
ty = arm.pat.ty();
1159+
}
1160+
let set = ConstructorSet::for_ty(cx, ty);
1161+
let reachability = if set.is_empty(cx, true) {
1162+
Reachability::Unreachable
1163+
} else {
1164+
Reachability::Reachable(vec![])
1165+
};
1166+
return UsefulnessReport {
1167+
arm_usefulness: vec![(*arm, reachability)],
1168+
non_exhaustiveness_witnesses: vec![],
1169+
};
1170+
}
1171+
}
1172+
11511173
let mut matrix = Matrix::empty();
11521174
for (row_id, arm) in arms.iter().enumerate() {
11531175
let v = PatStack::from_pattern(arm.pat, row_id, arm.has_guard);

0 commit comments

Comments
 (0)