Skip to content

Commit ddef5b6

Browse files
committed
Don't warn an empty pattern unreachable if we're not sure the data is valid
1 parent f5dbb54 commit ddef5b6

File tree

9 files changed

+183
-654
lines changed

9 files changed

+183
-654
lines changed

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

+26-18
Original file line numberDiff line numberDiff line change
@@ -787,9 +787,6 @@ pub(super) enum Constructor<'tcx> {
787787
}
788788

789789
impl<'tcx> Constructor<'tcx> {
790-
pub(super) fn is_wildcard(&self) -> bool {
791-
matches!(self, Wildcard)
792-
}
793790
pub(super) fn is_non_exhaustive(&self) -> bool {
794791
matches!(self, NonExhaustive)
795792
}
@@ -973,15 +970,17 @@ pub(super) enum ConstructorSet {
973970
/// constructors that exist in the type but are not present in the column.
974971
///
975972
/// More formally, if we discard wildcards from the column, this respects the following constraints:
976-
/// 1. the union of `present` and `missing` covers the whole type
973+
/// 1. the union of `present`, `missing` and `missing_empty` covers all the constructors of the type
977974
/// 2. each constructor in `present` is covered by something in the column
978-
/// 3. no constructor in `missing` is covered by anything in the column
975+
/// 3. no constructor in `missing` or `missing_empty` is covered by anything in the column
979976
/// 4. each constructor in the column is equal to the union of one or more constructors in `present`
980977
/// 5. `missing` does not contain empty constructors (see discussion about emptiness at the top of
981978
/// the file);
982-
/// 6. constructors in `present` and `missing` are split for the column; in other words, they are
983-
/// either fully included in or fully disjoint from each constructor in the column. In other
984-
/// words, there are no non-trivial intersections like between `0..10` and `5..15`.
979+
/// 6. `missing_empty` contains only empty constructors
980+
/// 7. constructors in `present`, `missing` and `missing_empty` are split for the column; in other
981+
/// words, they are either fully included in or fully disjoint from each constructor in the
982+
/// column. In yet other words, there are no non-trivial intersections like between `0..10` and
983+
/// `5..15`.
985984
///
986985
/// We must be particularly careful with weird constructors like `Opaque`: they're not formally part
987986
/// of the `ConstructorSet` for the type, yet if we forgot to include them in `present` we would be
@@ -990,6 +989,7 @@ pub(super) enum ConstructorSet {
990989
pub(super) struct SplitConstructorSet<'tcx> {
991990
pub(super) present: SmallVec<[Constructor<'tcx>; 1]>,
992991
pub(super) missing: Vec<Constructor<'tcx>>,
992+
pub(super) missing_empty: Vec<Constructor<'tcx>>,
993993
}
994994

995995
impl ConstructorSet {
@@ -1132,10 +1132,10 @@ impl ConstructorSet {
11321132
// Constructors in `ctors`, except wildcards and opaques.
11331133
let mut seen = Vec::new();
11341134
for ctor in ctors.cloned() {
1135-
if let Constructor::Opaque(..) = ctor {
1136-
present.push(ctor);
1137-
} else if !ctor.is_wildcard() {
1138-
seen.push(ctor);
1135+
match ctor {
1136+
Opaque(..) => present.push(ctor),
1137+
Wildcard => {} // discard wildcards
1138+
_ => seen.push(ctor),
11391139
}
11401140
}
11411141

@@ -1239,16 +1239,24 @@ impl ConstructorSet {
12391239
missing.push(NonExhaustive);
12401240
}
12411241
ConstructorSet::NoConstructors => {
1242-
if !pcx.is_top_level {
1243-
missing_empty.push(NonExhaustive);
1244-
}
1242+
// In a `MaybeInvalid` place even an empty pattern may be reachable. We therefore
1243+
// add a dummy empty constructor here, which will be ignored if the place is
1244+
// `ValidOnly`.
1245+
missing_empty.push(NonExhaustive);
12451246
}
12461247
}
12471248

1248-
if !pcx.cx.tcx.features().exhaustive_patterns {
1249-
missing.extend(missing_empty);
1249+
// We have now grouped all the constructors into 3 buckets: present, missing, missing_empty.
1250+
// In the absence of the `exhaustive_patterns` feature however, we don't count nested empty
1251+
// types as empty. Only non-nested `!` or `enum Foo {}` are considered empty.
1252+
if !pcx.cx.tcx.features().exhaustive_patterns
1253+
&& !(pcx.is_top_level && matches!(self, Self::NoConstructors))
1254+
{
1255+
// Treat all missing constructors as nonempty.
1256+
missing.extend(missing_empty.drain(..));
12501257
}
1251-
SplitConstructorSet { present, missing }
1258+
1259+
SplitConstructorSet { present, missing, missing_empty }
12521260
}
12531261
}
12541262

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

+64-21
Original file line numberDiff line numberDiff line change
@@ -637,32 +637,56 @@ impl<'a, 'p, 'tcx> fmt::Debug for PatCtxt<'a, 'p, 'tcx> {
637637
}
638638
}
639639

640-
/// In the matrix, tracks whether a given place (aka column) is known to contain a valid value or
641-
/// not.
640+
/// Serves two purposes:
641+
/// - in a wildcard, tracks whether the wildcard matches only valid values (i.e. is a binding `_a`)
642+
/// or also invalid values (i.e. is a true `_` pattern).
643+
/// - in the matrix, track whether a given place (aka column) is known to contain a valid value or
644+
/// not.
642645
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
643646
pub(super) enum ValidityConstraint {
644647
ValidOnly,
645648
MaybeInvalid,
649+
/// Option for backwards compatibility: the place is not known to be valid but we allow omitting
650+
/// `useful && !reachable` arms anyway.
651+
MaybeInvalidButAllowOmittingArms,
646652
}
647653

648654
impl ValidityConstraint {
649655
pub(super) fn from_bool(is_valid_only: bool) -> Self {
650656
if is_valid_only { ValidOnly } else { MaybeInvalid }
651657
}
652658

659+
fn allow_omitting_side_effecting_arms(self) -> Self {
660+
match self {
661+
MaybeInvalid | MaybeInvalidButAllowOmittingArms => MaybeInvalidButAllowOmittingArms,
662+
// There are no side-effecting empty arms here, nothing to do.
663+
ValidOnly => ValidOnly,
664+
}
665+
}
666+
667+
pub(super) fn is_known_valid(self) -> bool {
668+
matches!(self, ValidOnly)
669+
}
670+
pub(super) fn allows_omitting_empty_arms(self) -> bool {
671+
matches!(self, ValidOnly | MaybeInvalidButAllowOmittingArms)
672+
}
673+
653674
/// If the place has validity given by `self` and we read that the value at the place has
654675
/// constructor `ctor`, this computes what we can assume about the validity of the constructor
655676
/// fields.
656677
///
657678
/// Pending further opsem decisions, the current behavior is: validity is preserved, except
658-
/// under `&` where validity is reset to `MaybeInvalid`.
679+
/// inside `&` and union fields where validity is reset to `MaybeInvalid`.
659680
pub(super) fn specialize<'tcx>(
660681
self,
661682
pcx: &PatCtxt<'_, '_, 'tcx>,
662683
ctor: &Constructor<'tcx>,
663684
) -> Self {
664-
// We preserve validity except when we go under a reference.
665-
if matches!(ctor, Constructor::Single) && matches!(pcx.ty.kind(), ty::Ref(..)) {
685+
// We preserve validity except when we go inside a reference or a union field.
686+
if matches!(ctor, Constructor::Single)
687+
&& (matches!(pcx.ty.kind(), ty::Ref(..))
688+
|| matches!(pcx.ty.kind(), ty::Adt(def, ..) if def.is_union()))
689+
{
666690
// Validity of `x: &T` does not imply validity of `*x: T`.
667691
MaybeInvalid
668692
} else {
@@ -675,7 +699,7 @@ impl fmt::Display for ValidityConstraint {
675699
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
676700
let s = match self {
677701
ValidOnly => "✓",
678-
MaybeInvalid => "?",
702+
MaybeInvalid | MaybeInvalidButAllowOmittingArms => "?",
679703
};
680704
write!(f, "{s}")
681705
}
@@ -1202,9 +1226,9 @@ fn compute_exhaustiveness_and_usefulness<'p, 'tcx>(
12021226
for row in matrix.rows_mut() {
12031227
// All rows are useful until they're not.
12041228
row.useful = true;
1229+
// When there's an unguarded row, the match is exhaustive and any subsequent row is not
1230+
// useful.
12051231
if !row.is_under_guard {
1206-
// There's an unguarded row, so the match is exhaustive, and any subsequent row is
1207-
// unreachable.
12081232
return WitnessMatrix::empty();
12091233
}
12101234
}
@@ -1215,26 +1239,37 @@ fn compute_exhaustiveness_and_usefulness<'p, 'tcx>(
12151239
debug!("ty: {ty:?}");
12161240
let pcx = &PatCtxt { cx, ty, is_top_level };
12171241

1242+
// Whether the place/column we are inspecting is known to contain valid data.
1243+
let place_validity = matrix.place_validity[0];
1244+
// For backwards compability we allow omitting some empty arms that we ideally shouldn't.
1245+
let place_validity = place_validity.allow_omitting_side_effecting_arms();
1246+
12181247
// Analyze the constructors present in this column.
12191248
let ctors = matrix.heads().map(|p| p.ctor());
1220-
let split_set = ConstructorSet::for_ty(pcx.cx, pcx.ty).split(pcx, ctors);
1221-
1249+
let split_set = ConstructorSet::for_ty(cx, ty).split(pcx, ctors);
12221250
let all_missing = split_set.present.is_empty();
1223-
let always_report_all = is_top_level && !IntRange::is_integral(pcx.ty);
1224-
// Whether we should report "Enum::A and Enum::C are missing" or "_ is missing".
1225-
let report_individual_missing_ctors = always_report_all || !all_missing;
12261251

1252+
// Build the set of constructors we will specialize with. It must cover the whole type.
12271253
let mut split_ctors = split_set.present;
1228-
let mut only_report_missing = false;
12291254
if !split_set.missing.is_empty() {
12301255
// We need to iterate over a full set of constructors, so we add `Missing` to represent the
12311256
// missing ones. This is explained under "Constructor Splitting" at the top of this file.
12321257
split_ctors.push(Constructor::Missing);
1233-
// For diagnostic purposes we choose to only report the constructors that are missing. Since
1234-
// `Missing` matches only the wildcard rows, it matches fewer rows than any normal
1235-
// constructor and is therefore guaranteed to result in more witnesses. So skipping the
1236-
// other constructors does not jeopardize correctness.
1237-
only_report_missing = true;
1258+
} else if !split_set.missing_empty.is_empty() && !place_validity.is_known_valid() {
1259+
// The missing empty constructors are reachable if the place can contain invalid data.
1260+
split_ctors.push(Constructor::Missing);
1261+
}
1262+
1263+
// Decide what constructors to report.
1264+
let always_report_all = is_top_level && !IntRange::is_integral(pcx.ty);
1265+
// Whether we should report "Enum::A and Enum::C are missing" or "_ is missing".
1266+
let report_individual_missing_ctors = always_report_all || !all_missing;
1267+
// Which constructors are considered missing. We ensure that `!missing_ctors.is_empty() =>
1268+
// split_ctors.contains(Missing)`. The converse usually holds except in the
1269+
// `MaybeInvalidButAllowOmittingArms` backwards-compatibility case.
1270+
let mut missing_ctors = split_set.missing;
1271+
if !place_validity.allows_omitting_empty_arms() {
1272+
missing_ctors.extend(split_set.missing_empty);
12381273
}
12391274

12401275
let mut ret = WitnessMatrix::empty();
@@ -1246,11 +1281,19 @@ fn compute_exhaustiveness_and_usefulness<'p, 'tcx>(
12461281
compute_exhaustiveness_and_usefulness(cx, &mut spec_matrix, false)
12471282
});
12481283

1249-
if !only_report_missing || matches!(ctor, Constructor::Missing) {
1284+
let counts_for_exhaustiveness = match ctor {
1285+
Constructor::Missing => !missing_ctors.is_empty(),
1286+
// If there are missing constructors we'll report those instead. Since `Missing` matches
1287+
// only the wildcard rows, it matches fewer rows than this constructor, and is therefore
1288+
// guaranteed to result in the same or more witnesses. So skipping this does not
1289+
// jeopardize correctness.
1290+
_ => missing_ctors.is_empty(),
1291+
};
1292+
if counts_for_exhaustiveness {
12501293
// Transform witnesses for `spec_matrix` into witnesses for `matrix`.
12511294
witnesses.apply_constructor(
12521295
pcx,
1253-
&split_set.missing,
1296+
&missing_ctors,
12541297
&ctor,
12551298
report_individual_missing_ctors,
12561299
);

0 commit comments

Comments
 (0)