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

Commit edb27a3

Browse files
committed
Make all the empty pattern decisions in usefulness
1 parent bf913ad commit edb27a3

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

compiler/rustc_pattern_analysis/src/constructor.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -858,12 +858,14 @@ impl<Cx: TypeCx> ConstructorSet<Cx> {
858858
/// any) are missing; 2/ split constructors to handle non-trivial intersections e.g. on ranges
859859
/// or slices. This can get subtle; see [`SplitConstructorSet`] for details of this operation
860860
/// and its invariants.
861-
#[instrument(level = "debug", skip(self, pcx, ctors), ret)]
861+
#[instrument(level = "debug", skip(self, ctors), ret)]
862862
pub(crate) fn split<'a>(
863863
&self,
864-
pcx: &PlaceCtxt<'a, Cx>,
865864
ctors: impl Iterator<Item = &'a Constructor<Cx>> + Clone,
866-
) -> SplitConstructorSet<Cx> {
865+
) -> SplitConstructorSet<Cx>
866+
where
867+
Cx: 'a,
868+
{
867869
let mut present: SmallVec<[_; 1]> = SmallVec::new();
868870
// Empty constructors found missing.
869871
let mut missing_empty = Vec::new();
@@ -1003,17 +1005,6 @@ impl<Cx: TypeCx> ConstructorSet<Cx> {
10031005
}
10041006
}
10051007

1006-
// We have now grouped all the constructors into 3 buckets: present, missing, missing_empty.
1007-
// In the absence of the `exhaustive_patterns` feature however, we don't count nested empty
1008-
// types as empty. Only non-nested `!` or `enum Foo {}` are considered empty.
1009-
if !pcx.mcx.tycx.is_exhaustive_patterns_feature_on()
1010-
&& !(pcx.is_scrutinee && matches!(self, Self::NoConstructors))
1011-
{
1012-
// Treat all missing constructors as nonempty.
1013-
// This clears `missing_empty`.
1014-
missing.append(&mut missing_empty);
1015-
}
1016-
10171008
SplitConstructorSet { present, missing, missing_empty }
10181009
}
10191010
}

compiler/rustc_pattern_analysis/src/lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl<'p, 'tcx> PatternColumn<'p, 'tcx> {
5959
) -> Result<SplitConstructorSet<'p, 'tcx>, ErrorGuaranteed> {
6060
let column_ctors = self.patterns.iter().map(|p| p.ctor());
6161
let ctors_for_ty = &pcx.ctors_for_ty()?;
62-
Ok(ctors_for_ty.split(pcx, column_ctors))
62+
Ok(ctors_for_ty.split(column_ctors))
6363
}
6464

6565
/// Does specialization: given a constructor, this takes the patterns from the column that match

compiler/rustc_pattern_analysis/src/usefulness.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -737,15 +737,13 @@ pub(crate) struct PlaceCtxt<'a, Cx: TypeCx> {
737737
pub(crate) mcx: MatchCtxt<'a, Cx>,
738738
/// Type of the place under investigation.
739739
pub(crate) ty: Cx::Ty,
740-
/// Whether the place is the original scrutinee place, as opposed to a subplace of it.
741-
pub(crate) is_scrutinee: bool,
742740
}
743741

744742
impl<'a, Cx: TypeCx> PlaceCtxt<'a, Cx> {
745743
/// A `PlaceCtxt` when code other than `is_useful` needs one.
746744
#[cfg_attr(not(feature = "rustc"), allow(dead_code))]
747745
pub(crate) fn new_dummy(mcx: MatchCtxt<'a, Cx>, ty: Cx::Ty) -> Self {
748-
PlaceCtxt { mcx, ty, is_scrutinee: false }
746+
PlaceCtxt { mcx, ty }
749747
}
750748

751749
pub(crate) fn ctor_arity(&self, ctor: &Constructor<Cx>) -> usize {
@@ -1442,7 +1440,7 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>(
14421440
};
14431441

14441442
debug!("ty: {ty:?}");
1445-
let pcx = &PlaceCtxt { mcx, ty, is_scrutinee: is_top_level };
1443+
let pcx = &PlaceCtxt { mcx, ty };
14461444

14471445
// Whether the place/column we are inspecting is known to contain valid data.
14481446
let place_validity = matrix.place_validity[0];
@@ -1451,7 +1449,17 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>(
14511449
let ctors = matrix.heads().map(|p| p.ctor());
14521450
let ctors_for_ty = pcx.ctors_for_ty()?;
14531451
let is_integers = matches!(ctors_for_ty, ConstructorSet::Integers { .. }); // For diagnostics.
1454-
let split_set = ctors_for_ty.split(pcx, ctors);
1452+
let mut split_set = ctors_for_ty.split(ctors);
1453+
// We have now grouped all the constructors into 3 buckets: present, missing, missing_empty.
1454+
// In the absence of the `exhaustive_patterns` feature however, we don't count nested empty
1455+
// types as empty. Only non-nested `!` or `enum Foo {}` are considered empty.
1456+
if !pcx.mcx.tycx.is_exhaustive_patterns_feature_on()
1457+
&& !(is_top_level && matches!(ctors_for_ty, ConstructorSet::NoConstructors))
1458+
{
1459+
// Treat all missing constructors as nonempty.
1460+
// This clears `missing_empty`.
1461+
split_set.missing.append(&mut split_set.missing_empty);
1462+
}
14551463
let all_missing = split_set.present.is_empty();
14561464

14571465
// Build the set of constructors we will specialize with. It must cover the whole type.

0 commit comments

Comments
 (0)