Skip to content

Commit

Permalink
The need for Single to cover Unlistable was a hack
Browse files Browse the repository at this point in the history
It is now unneeded, since we handle `&str` patterns in a consistent way.
  • Loading branch information
Nadrieril committed Nov 1, 2020
1 parent 4cd3019 commit 1bdcd02
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions compiler/rustc_mir_build/src/thir/pattern/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,11 +799,9 @@ enum Constructor<'tcx> {
/// boxes for the purposes of exhaustiveness: we must not inspect them, and they
/// don't count towards making a match exhaustive.
Opaque,
/// Fake extra constructor for enums that aren't allowed to be matched exhaustively.
/// Fake extra constructor for enums that aren't allowed to be matched exhaustively. Also used
/// for those types for which we cannot list constructors explicitly, like `f64` and `str`.
NonExhaustive,
/// Fake constructor for those types for which we can't list constructors explicitly, like
/// `f64` and `str`.
Unlistable,
/// Wildcard pattern.
Wildcard,
}
Expand Down Expand Up @@ -897,6 +895,7 @@ impl<'tcx> Constructor<'tcx> {
/// For the simple cases, this is simply checking for equality. For the "grouped" constructors,
/// this checks for inclusion.
fn is_covered_by<'p>(&self, pcx: PatCtxt<'_, 'p, 'tcx>, other: &Self) -> bool {
// This must be kept in sync with `is_covered_by_any`.
match (self, other) {
// Wildcards cover anything
(_, Wildcard) => true,
Expand Down Expand Up @@ -939,11 +938,6 @@ impl<'tcx> Constructor<'tcx> {
(Opaque, _) | (_, Opaque) => false,
// Only a wildcard pattern can match the special extra constructor.
(NonExhaustive, _) => false,
// If we encounter a `Single` here, this means there was only one constructor for this
// type after all.
(Unlistable, Single) => true,
// Otherwise, only a wildcard pattern can match the special extra constructor.
(Unlistable, _) => false,

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

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

// This must be kept in sync with `is_covered_by`.
match self {
// `used_ctors` cannot contain anything else than `Single`s.
// If `self` is `Single`, `used_ctors` cannot contain anything else than `Single`s.
Single => !used_ctors.is_empty(),
Variant(_) => used_ctors.iter().any(|c| c == self),
IntRange(range) => used_ctors
Expand All @@ -979,8 +975,6 @@ impl<'tcx> Constructor<'tcx> {
.any(|other| slice.is_covered_by(other)),
// This constructor is never covered by anything else
NonExhaustive => false,
// This constructor is only covered by `Single`s
Unlistable => used_ctors.iter().any(|c| *c == Single),
Str(..) | FloatRange(..) | Opaque | Wildcard => {
bug!("found unexpected ctor in all_ctors: {:?}", self)
}
Expand Down Expand Up @@ -1064,7 +1058,7 @@ impl<'tcx> Constructor<'tcx> {
&Str(value) => PatKind::Constant { value },
&FloatRange(lo, hi, end) => PatKind::Range(PatRange { lo, hi, end }),
IntRange(range) => return range.to_pat(pcx.cx.tcx),
NonExhaustive | Unlistable => PatKind::Wild,
NonExhaustive => PatKind::Wild,
Opaque => bug!("we should not try to apply an opaque constructor"),
Wildcard => bug!(
"trying to apply a wildcard constructor; this should have been done in `apply_constructors`"
Expand Down Expand Up @@ -1213,8 +1207,9 @@ impl<'p, 'tcx> Fields<'p, 'tcx> {
}
_ => bug!("bad slice pattern {:?} {:?}", constructor, ty),
},
Str(..) | FloatRange(..) | IntRange(..) | NonExhaustive | Opaque | Unlistable
| Wildcard => Fields::empty(),
Str(..) | FloatRange(..) | IntRange(..) | NonExhaustive | Opaque | Wildcard => {
Fields::empty()
}
};
debug!("Fields::wildcards({:?}, {:?}) = {:#?}", constructor, ty, ret);
ret
Expand Down Expand Up @@ -1624,8 +1619,8 @@ fn all_constructors<'p, 'tcx>(pcx: PatCtxt<'_, 'p, 'tcx>) -> Vec<Constructor<'tc
}
_ if cx.is_uninhabited(pcx.ty) => vec![],
ty::Adt(..) | ty::Tuple(..) | ty::Ref(..) => vec![Single],
// This type is one for which we don't know how to list constructors, like `str` or `f64`.
_ => vec![Unlistable],
// This type is one for which we cannot list constructors, like `str` or `f64`.
_ => vec![NonExhaustive],
}
}

Expand Down

0 comments on commit 1bdcd02

Please sign in to comment.