Skip to content

Commit 481bb33

Browse files
committed
Decide which constructors to report earlier.
This gets rid of `report_individual_missing_ctors`
1 parent 0a7b183 commit 481bb33

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

compiler/rustc_pattern_analysis/src/usefulness.rs

+19-21
Original file line numberDiff line numberDiff line change
@@ -1319,28 +1319,16 @@ impl<Cx: TypeCx> WitnessMatrix<Cx> {
13191319
fn apply_constructor(
13201320
&mut self,
13211321
pcx: &PlaceCtxt<'_, Cx>,
1322-
mut missing_ctors: &[Constructor<Cx>],
1322+
missing_ctors: &[Constructor<Cx>],
13231323
ctor: &Constructor<Cx>,
1324-
report_individual_missing_ctors: bool,
13251324
) {
13261325
if self.is_empty() {
13271326
return;
13281327
}
13291328
if matches!(ctor, Constructor::Missing) {
13301329
// We got the special `Missing` constructor that stands for the constructors not present
1331-
// in the match.
1332-
if !missing_ctors.is_empty() && !report_individual_missing_ctors {
1333-
// Report `_` as missing.
1334-
missing_ctors = &[Constructor::Wildcard];
1335-
} else if missing_ctors.iter().any(|c| c.is_non_exhaustive()) {
1336-
// We need to report a `_` anyway, so listing other constructors would be redundant.
1337-
// `NonExhaustive` is displayed as `_` just like `Wildcard`, but it will be picked
1338-
// up by diagnostics to add a note about why `_` is required here.
1339-
missing_ctors = &[Constructor::NonExhaustive];
1340-
}
1341-
1342-
// For each missing constructor `c`, we add a `c(_, _, _)` witness appropriately
1343-
// filled with wildcards.
1330+
// in the match. For each missing constructor `c`, we add a `c(_, _, _)` witness
1331+
// appropriately filled with wildcards.
13441332
let mut ret = Self::empty();
13451333
for ctor in missing_ctors {
13461334
let pat = pcx.wild_from_ctor(ctor.clone());
@@ -1515,11 +1503,6 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>(
15151503
split_ctors.push(Constructor::Missing);
15161504
}
15171505

1518-
// Decide what constructors to report.
1519-
let is_integers = matches!(ctors_for_ty, ConstructorSet::Integers { .. });
1520-
let always_report_all = place.is_scrutinee && !is_integers;
1521-
// Whether we should report "Enum::A and Enum::C are missing" or "_ is missing".
1522-
let report_individual_missing_ctors = always_report_all || !all_missing;
15231506
// Which constructors are considered missing. We ensure that `!missing_ctors.is_empty() =>
15241507
// split_ctors.contains(Missing)`. The converse usually holds except when
15251508
// `!place_validity.is_known_valid()`.
@@ -1528,6 +1511,21 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>(
15281511
missing_ctors.append(&mut split_set.missing_empty);
15291512
}
15301513

1514+
// Decide what constructors to report.
1515+
let is_integers = matches!(ctors_for_ty, ConstructorSet::Integers { .. });
1516+
let always_report_all = place.is_scrutinee && !is_integers;
1517+
// Whether we should report "Enum::A and Enum::C are missing" or "_ is missing".
1518+
let report_individual_missing_ctors = always_report_all || !all_missing;
1519+
if !missing_ctors.is_empty() && !report_individual_missing_ctors {
1520+
// Report `_` as missing.
1521+
missing_ctors = vec![Constructor::Wildcard];
1522+
} else if missing_ctors.iter().any(|c| c.is_non_exhaustive()) {
1523+
// We need to report a `_` anyway, so listing other constructors would be redundant.
1524+
// `NonExhaustive` is displayed as `_` just like `Wildcard`, but it will be picked
1525+
// up by diagnostics to add a note about why `_` is required here.
1526+
missing_ctors = vec![Constructor::NonExhaustive];
1527+
}
1528+
15311529
let mut ret = WitnessMatrix::empty();
15321530
for ctor in split_ctors {
15331531
// Dig into rows that match `ctor`.
@@ -1542,7 +1540,7 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>(
15421540
})?;
15431541

15441542
// Transform witnesses for `spec_matrix` into witnesses for `matrix`.
1545-
witnesses.apply_constructor(pcx, &missing_ctors, &ctor, report_individual_missing_ctors);
1543+
witnesses.apply_constructor(pcx, &missing_ctors, &ctor);
15461544
// Accumulate the found witnesses.
15471545
ret.extend(witnesses);
15481546

0 commit comments

Comments
 (0)