Skip to content

Commit 4f93aba

Browse files
committed
Don't warn an empty pattern unreachable if we're not sure the data is valid
1 parent 2bf1896 commit 4f93aba

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
@@ -788,9 +788,6 @@ pub(super) enum Constructor<'tcx> {
788788
}
789789

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

996996
impl ConstructorSet {
@@ -1139,10 +1139,10 @@ impl ConstructorSet {
11391139
// Constructors in `ctors`, except wildcards and opaques.
11401140
let mut seen = Vec::new();
11411141
for ctor in ctors.cloned() {
1142-
if let Constructor::Opaque(..) = ctor {
1143-
present.push(ctor);
1144-
} else if !ctor.is_wildcard() {
1145-
seen.push(ctor);
1142+
match ctor {
1143+
Opaque(..) => present.push(ctor),
1144+
Wildcard => {} // discard wildcards
1145+
_ => seen.push(ctor),
11461146
}
11471147
}
11481148

@@ -1246,16 +1246,24 @@ impl ConstructorSet {
12461246
missing.push(NonExhaustive);
12471247
}
12481248
ConstructorSet::NoConstructors => {
1249-
if !pcx.is_top_level {
1250-
missing_empty.push(NonExhaustive);
1251-
}
1249+
// In a `MaybeInvalid` place even an empty pattern may be reachable. We therefore
1250+
// add a dummy empty constructor here, which will be ignored if the place is
1251+
// `ValidOnly`.
1252+
missing_empty.push(NonExhaustive);
12521253
}
12531254
}
12541255

1255-
if !pcx.cx.tcx.features().exhaustive_patterns {
1256-
missing.extend(missing_empty);
1256+
// We have now grouped all the constructors into 3 buckets: present, missing, missing_empty.
1257+
// In the absence of the `exhaustive_patterns` feature however, we don't count nested empty
1258+
// types as empty. Only non-nested `!` or `enum Foo {}` are considered empty.
1259+
if !pcx.cx.tcx.features().exhaustive_patterns
1260+
&& !(pcx.is_top_level && matches!(self, Self::NoConstructors))
1261+
{
1262+
// Treat all missing constructors as nonempty.
1263+
missing.extend(missing_empty.drain(..));
12571264
}
1258-
SplitConstructorSet { present, missing }
1265+
1266+
SplitConstructorSet { present, missing, missing_empty }
12591267
}
12601268
}
12611269

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
}
@@ -1198,9 +1222,9 @@ fn compute_exhaustiveness_and_usefulness<'p, 'tcx>(
11981222
for row in matrix.rows_mut() {
11991223
// All rows are useful until they're not.
12001224
row.useful = true;
1225+
// When there's an unguarded row, the match is exhaustive and any subsequent row is not
1226+
// useful.
12011227
if !row.is_under_guard {
1202-
// There's an unguarded row, so the match is exhaustive, and any subsequent row is
1203-
// unreachable.
12041228
return WitnessMatrix::empty();
12051229
}
12061230
}
@@ -1211,26 +1235,37 @@ fn compute_exhaustiveness_and_usefulness<'p, 'tcx>(
12111235
debug!("ty: {ty:?}");
12121236
let pcx = &PatCtxt { cx, ty, is_top_level };
12131237

1238+
// Whether the place/column we are inspecting is known to contain valid data.
1239+
let place_validity = matrix.place_validity[0];
1240+
// For backwards compability we allow omitting some empty arms that we ideally shouldn't.
1241+
let place_validity = place_validity.allow_omitting_side_effecting_arms();
1242+
12141243
// Analyze the constructors present in this column.
12151244
let ctors = matrix.heads().map(|p| p.ctor());
1216-
let split_set = ConstructorSet::for_ty(pcx.cx, pcx.ty).split(pcx, ctors);
1217-
1245+
let split_set = ConstructorSet::for_ty(cx, ty).split(pcx, ctors);
12181246
let all_missing = split_set.present.is_empty();
1219-
let always_report_all = is_top_level && !IntRange::is_integral(pcx.ty);
1220-
// Whether we should report "Enum::A and Enum::C are missing" or "_ is missing".
1221-
let report_individual_missing_ctors = always_report_all || !all_missing;
12221247

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

12361271
let mut ret = WitnessMatrix::empty();
@@ -1242,11 +1277,19 @@ fn compute_exhaustiveness_and_usefulness<'p, 'tcx>(
12421277
compute_exhaustiveness_and_usefulness(cx, &mut spec_matrix, false)
12431278
});
12441279

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

0 commit comments

Comments
 (0)