Skip to content

Commit a591ede

Browse files
committed
Only special-case empty matches when exhaustive_patterns is off
When the feature is on, the special casing is not needed. That way when we stabilize the feature this `if` can just be removed.
1 parent bfb556f commit a591ede

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

src/librustc_mir/hair/pattern/check_match.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
168168

169169
// Fifth, check if the match is exhaustive.
170170
let scrut_ty = self.tables.node_type(scrut.hir_id);
171+
// Note: An empty match isn't the same as an empty matrix for diagnostics purposes,
172+
// since an empty matrix can occur when there are arms, if those arms all have guards.
171173
let is_empty_match = inlined_arms.is_empty();
172174
check_exhaustive(cx, scrut_ty, scrut.span, &matrix, scrut.hir_id, is_empty_match);
173175
})
@@ -435,27 +437,22 @@ fn check_exhaustive<'p, 'tcx>(
435437
hir_id: HirId,
436438
is_empty_match: bool,
437439
) {
438-
// If the match has no arms, check whether the scrutinee is uninhabited.
439-
// Note: An empty match isn't the same as an empty matrix for diagnostics purposes, since an
440-
// empty matrix can occur when there are arms, if those arms all have guards.
441-
let scrutinee_is_visibly_uninhabited = if cx.tcx.features().exhaustive_patterns {
442-
let module = cx.tcx.hir().get_module_parent(hir_id);
443-
cx.tcx.is_ty_uninhabited_from(module, scrut_ty)
444-
} else {
445-
match scrut_ty.kind {
440+
// In the absence of the `exhaustive_patterns` feature, empty matches are not detected by
441+
// `is_useful` to exhaustively match uninhabited types, so we manually check here.
442+
if is_empty_match && !cx.tcx.features().exhaustive_patterns {
443+
let scrutinee_is_visibly_uninhabited = match scrut_ty.kind {
446444
ty::Never => true,
447-
ty::Adt(def, _) if def.is_enum() => {
448-
def.variants.is_empty() && !cx.is_foreign_non_exhaustive_enum(scrut_ty)
445+
ty::Adt(def, _) => {
446+
def.is_enum()
447+
&& def.variants.is_empty()
448+
&& !cx.is_foreign_non_exhaustive_enum(scrut_ty)
449449
}
450450
_ => false,
451+
};
452+
if scrutinee_is_visibly_uninhabited {
453+
// If the type *is* uninhabited, an empty match is vacuously exhaustive.
454+
return;
451455
}
452-
};
453-
if is_empty_match && scrutinee_is_visibly_uninhabited {
454-
// If the type *is* uninhabited, it's vacuously exhaustive.
455-
// This early return is only needed here because in the absence of the
456-
// `exhaustive_patterns` feature, empty matches are not detected by `is_useful`
457-
// to exhaustively match uninhabited types.
458-
return;
459456
}
460457

461458
let witnesses = match check_not_useful(cx, scrut_ty, matrix, hir_id) {

0 commit comments

Comments
 (0)