Skip to content

Commit 1bdcd02

Browse files
committed
The need for Single to cover Unlistable was a hack
It is now unneeded, since we handle `&str` patterns in a consistent way.
1 parent 4cd3019 commit 1bdcd02

File tree

1 file changed

+13
-18
lines changed
  • compiler/rustc_mir_build/src/thir/pattern

1 file changed

+13
-18
lines changed

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

+13-18
Original file line numberDiff line numberDiff line change
@@ -799,11 +799,9 @@ enum Constructor<'tcx> {
799799
/// boxes for the purposes of exhaustiveness: we must not inspect them, and they
800800
/// don't count towards making a match exhaustive.
801801
Opaque,
802-
/// Fake extra constructor for enums that aren't allowed to be matched exhaustively.
802+
/// Fake extra constructor for enums that aren't allowed to be matched exhaustively. Also used
803+
/// for those types for which we cannot list constructors explicitly, like `f64` and `str`.
803804
NonExhaustive,
804-
/// Fake constructor for those types for which we can't list constructors explicitly, like
805-
/// `f64` and `str`.
806-
Unlistable,
807805
/// Wildcard pattern.
808806
Wildcard,
809807
}
@@ -897,6 +895,7 @@ impl<'tcx> Constructor<'tcx> {
897895
/// For the simple cases, this is simply checking for equality. For the "grouped" constructors,
898896
/// this checks for inclusion.
899897
fn is_covered_by<'p>(&self, pcx: PatCtxt<'_, 'p, 'tcx>, other: &Self) -> bool {
898+
// This must be kept in sync with `is_covered_by_any`.
900899
match (self, other) {
901900
// Wildcards cover anything
902901
(_, Wildcard) => true,
@@ -939,11 +938,6 @@ impl<'tcx> Constructor<'tcx> {
939938
(Opaque, _) | (_, Opaque) => false,
940939
// Only a wildcard pattern can match the special extra constructor.
941940
(NonExhaustive, _) => false,
942-
// If we encounter a `Single` here, this means there was only one constructor for this
943-
// type after all.
944-
(Unlistable, Single) => true,
945-
// Otherwise, only a wildcard pattern can match the special extra constructor.
946-
(Unlistable, _) => false,
947941

948942
_ => span_bug!(
949943
pcx.span,
@@ -955,7 +949,8 @@ impl<'tcx> Constructor<'tcx> {
955949
}
956950

957951
/// Faster version of `is_covered_by` when applied to many constructors. `used_ctors` is
958-
/// assumed to be built from `matrix.head_ctors()`, and `self` is assumed to have been split.
952+
/// assumed to be built from `matrix.head_ctors()` with wildcards filtered out, and `self` is
953+
/// assumed to have been split from a wildcard.
959954
fn is_covered_by_any<'p>(
960955
&self,
961956
pcx: PatCtxt<'_, 'p, 'tcx>,
@@ -965,8 +960,9 @@ impl<'tcx> Constructor<'tcx> {
965960
return false;
966961
}
967962

963+
// This must be kept in sync with `is_covered_by`.
968964
match self {
969-
// `used_ctors` cannot contain anything else than `Single`s.
965+
// If `self` is `Single`, `used_ctors` cannot contain anything else than `Single`s.
970966
Single => !used_ctors.is_empty(),
971967
Variant(_) => used_ctors.iter().any(|c| c == self),
972968
IntRange(range) => used_ctors
@@ -979,8 +975,6 @@ impl<'tcx> Constructor<'tcx> {
979975
.any(|other| slice.is_covered_by(other)),
980976
// This constructor is never covered by anything else
981977
NonExhaustive => false,
982-
// This constructor is only covered by `Single`s
983-
Unlistable => used_ctors.iter().any(|c| *c == Single),
984978
Str(..) | FloatRange(..) | Opaque | Wildcard => {
985979
bug!("found unexpected ctor in all_ctors: {:?}", self)
986980
}
@@ -1064,7 +1058,7 @@ impl<'tcx> Constructor<'tcx> {
10641058
&Str(value) => PatKind::Constant { value },
10651059
&FloatRange(lo, hi, end) => PatKind::Range(PatRange { lo, hi, end }),
10661060
IntRange(range) => return range.to_pat(pcx.cx.tcx),
1067-
NonExhaustive | Unlistable => PatKind::Wild,
1061+
NonExhaustive => PatKind::Wild,
10681062
Opaque => bug!("we should not try to apply an opaque constructor"),
10691063
Wildcard => bug!(
10701064
"trying to apply a wildcard constructor; this should have been done in `apply_constructors`"
@@ -1213,8 +1207,9 @@ impl<'p, 'tcx> Fields<'p, 'tcx> {
12131207
}
12141208
_ => bug!("bad slice pattern {:?} {:?}", constructor, ty),
12151209
},
1216-
Str(..) | FloatRange(..) | IntRange(..) | NonExhaustive | Opaque | Unlistable
1217-
| Wildcard => Fields::empty(),
1210+
Str(..) | FloatRange(..) | IntRange(..) | NonExhaustive | Opaque | Wildcard => {
1211+
Fields::empty()
1212+
}
12181213
};
12191214
debug!("Fields::wildcards({:?}, {:?}) = {:#?}", constructor, ty, ret);
12201215
ret
@@ -1624,8 +1619,8 @@ fn all_constructors<'p, 'tcx>(pcx: PatCtxt<'_, 'p, 'tcx>) -> Vec<Constructor<'tc
16241619
}
16251620
_ if cx.is_uninhabited(pcx.ty) => vec![],
16261621
ty::Adt(..) | ty::Tuple(..) | ty::Ref(..) => vec![Single],
1627-
// This type is one for which we don't know how to list constructors, like `str` or `f64`.
1628-
_ => vec![Unlistable],
1622+
// This type is one for which we cannot list constructors, like `str` or `f64`.
1623+
_ => vec![NonExhaustive],
16291624
}
16301625
}
16311626

0 commit comments

Comments
 (0)