Skip to content

Commit 0075a5f

Browse files
committed
Clarify the Wildcard/Missing situation
1 parent 5640f52 commit 0075a5f

File tree

2 files changed

+39
-43
lines changed

2 files changed

+39
-43
lines changed

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

+8-10
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,7 @@
7272
//! The only place where we care about which constructors `Missing` represents is in diagnostics
7373
//! (see `super::usefulness::WitnessMatrix::apply_constructor`).
7474
//!
75-
//! Extra special implementation detail: in fact, in the case where all the constructors are
76-
//! missing, we replace `Missing` with `Wildcard` to signal this. It only makes a difference for
77-
//! diagnostics: for `Missing` we list the missing constructors; for `Wildcard` we only output `_`.
78-
//!
79-
//! FIXME(Nadrieril): maybe `Missing { report_all: bool }` would be less confusing.
80-
//!
81-
//! We choose whether to specialize with `Missing`/`Wildcard` in
75+
//! We choose whether to specialize with `Missing` in
8276
//! `super::usefulness::compute_exhaustiveness_and_reachability`.
8377
//!
8478
//!
@@ -720,8 +714,11 @@ pub(super) enum Constructor<'tcx> {
720714
/// `#[doc(hidden)]` ones.
721715
Hidden,
722716
/// Fake extra constructor for constructors that are not seen in the matrix, as explained at the
723-
/// top of the file.
724-
Missing,
717+
/// top of the file. `report_individual_ctors` is for diagnostics; see
718+
/// `WitnessMatrix::apply_constructor`.
719+
Missing {
720+
report_individual_ctors: bool,
721+
},
725722
}
726723

727724
impl<'tcx> Constructor<'tcx> {
@@ -809,10 +806,11 @@ impl<'tcx> Constructor<'tcx> {
809806
#[inline]
810807
pub(super) fn is_covered_by<'p>(&self, pcx: &PatCtxt<'_, 'p, 'tcx>, other: &Self) -> bool {
811808
match (self, other) {
809+
(Wildcard, _) => bug!("Constructor splitting should not have returned `Wildcard`"),
812810
// Wildcards cover anything
813811
(_, Wildcard) => true,
814812
// Only a wildcard pattern can match these special constructors.
815-
(Wildcard | Missing { .. } | NonExhaustive | Hidden, _) => false,
813+
(Missing { .. } | NonExhaustive | Hidden, _) => false,
816814

817815
(Single, Single) => true,
818816
(Variant(self_id), Variant(other_id)) => self_id == other_id,

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

+31-33
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ impl<'tcx> WitnessMatrix<'tcx> {
926926
}
927927

928928
/// Reverses specialization by the `Missing` constructor by pushing a whole new pattern.
929-
fn push_pattern(&mut self, pat: &WitnessPat<'tcx>) {
929+
fn push_pattern(&mut self, pat: WitnessPat<'tcx>) {
930930
for witness in self.0.iter_mut() {
931931
witness.push_pattern(pat.clone())
932932
}
@@ -942,31 +942,34 @@ impl<'tcx> WitnessMatrix<'tcx> {
942942
if self.is_empty() {
943943
return;
944944
}
945-
if matches!(ctor, Constructor::Wildcard) {
946-
let pat = WitnessPat::wild_from_ctor(pcx, Constructor::Wildcard);
947-
self.push_pattern(&pat);
948-
} else if matches!(ctor, Constructor::Missing) {
949-
// We got the special `Missing` constructor, so each of the missing constructors gives a
950-
// new pattern that is not caught by the match. We list those patterns and push them
951-
// onto our current witnesses.
952-
if missing_ctors.iter().any(|c| c.is_non_exhaustive()) {
953-
// We only report `_` here; listing other constructors would be redundant.
954-
let pat = WitnessPat::wild_from_ctor(pcx, Constructor::NonExhaustive);
955-
self.push_pattern(&pat);
956-
} else {
957-
let old_witnesses = std::mem::replace(self, Self::empty());
958-
for ctor in missing_ctors {
959-
let pat = WitnessPat::wild_from_ctor(pcx, ctor.clone());
960-
let mut witnesses_with_missing_ctor = old_witnesses.clone();
961-
witnesses_with_missing_ctor.push_pattern(&pat);
962-
self.extend(witnesses_with_missing_ctor)
945+
if let Constructor::Missing { report_individual_ctors } = ctor {
946+
// We got the special `Missing` constructor that stands for the constructors not present
947+
// in the match.
948+
if *report_individual_ctors {
949+
if missing_ctors.iter().any(|c| c.is_non_exhaustive()) {
950+
// We only report `_` here; listing other constructors would be redundant.
951+
let pat = WitnessPat::wild_from_ctor(pcx, Constructor::NonExhaustive);
952+
self.push_pattern(pat);
953+
} else {
954+
let old_witnesses = std::mem::replace(self, Self::empty());
955+
for ctor in missing_ctors {
956+
// For each missing constructor `c`, we push a `c(_, _, _)` witness
957+
// appropriately filled with wildcards.
958+
let pat = WitnessPat::wild_from_ctor(pcx, ctor.clone());
959+
let mut witnesses_with_missing_ctor = old_witnesses.clone();
960+
witnesses_with_missing_ctor.push_pattern(pat);
961+
self.extend(witnesses_with_missing_ctor)
962+
}
963963
}
964+
} else {
965+
// Report only a wildcard.
966+
let pat = WitnessPat::wild_from_ctor(pcx, Constructor::Wildcard);
967+
self.push_pattern(pat);
964968
}
965969
} else if !missing_ctors.is_empty() {
966-
// `ctor` isn't `Wildcard` or `Missing` and some ctors are missing, so we know
967-
// `split_ctors` will contain `Wildcard` or `Missing`.
968-
// For diagnostic purposes we choose to discard witnesses we got under `ctor`, which
969-
// will let only the `Wildcard` or `Missing` be reported.
970+
// `ctor` isn't `Missing` and some ctors are missing, so we know `split_ctors` will
971+
// contain `Missing`. For diagnostic purposes we choose to discard witnesses we got
972+
// under `ctor`, which will let only the `Missing` be reported.
970973
self.0.clear();
971974
} else {
972975
for witness in self.0.iter_mut() {
@@ -1026,18 +1029,13 @@ fn compute_exhaustiveness_and_reachability<'p, 'tcx>(
10261029
let ctors = matrix.heads().map(|p| p.ctor());
10271030
let split_set = ConstructorSet::for_ty(pcx.cx, pcx.ty).split(pcx, ctors);
10281031
let mut split_ctors = split_set.present;
1029-
// We want to iterate over a full set of constructors, so if any is missing we add a wildcard.
1032+
// We want to iterate over a full set of constructors, so if some are missing we add a `Missing`
1033+
// to represent them.
10301034
if !split_set.missing.is_empty() {
10311035
let all_missing = split_ctors.is_empty();
1032-
let always_report_missing = is_top_level && !IntRange::is_integral(pcx.ty);
1033-
let ctor = if all_missing && !always_report_missing {
1034-
Constructor::Wildcard
1035-
} else {
1036-
// Like `Wildcard`, except if it doesn't match a row this will report all the missing
1037-
// constructors instead of just `_`.
1038-
Constructor::Missing
1039-
};
1040-
split_ctors.push(ctor);
1036+
let always_report_all = is_top_level && !IntRange::is_integral(pcx.ty);
1037+
let report_individual_ctors = always_report_all || !all_missing;
1038+
split_ctors.push(Constructor::Missing { report_individual_ctors });
10411039
}
10421040

10431041
let mut ret = WitnessMatrix::empty();

0 commit comments

Comments
 (0)