Skip to content

Commit 924d6cd

Browse files
committed
Tweak how we record missing constructors
This is slower but also not a performance-sensitive path.
1 parent 1280928 commit 924d6cd

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

compiler/rustc_pattern_analysis/src/usefulness.rs

+16-21
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,7 @@ impl<Cx: TypeCx> WitnessMatrix<Cx> {
13191319
fn apply_constructor(
13201320
&mut self,
13211321
pcx: &PlaceCtxt<'_, Cx>,
1322-
missing_ctors: &[Constructor<Cx>],
1322+
mut missing_ctors: &[Constructor<Cx>],
13231323
ctor: &Constructor<Cx>,
13241324
report_individual_missing_ctors: bool,
13251325
) {
@@ -1329,32 +1329,27 @@ impl<Cx: TypeCx> WitnessMatrix<Cx> {
13291329
if matches!(ctor, Constructor::Missing) {
13301330
// We got the special `Missing` constructor that stands for the constructors not present
13311331
// in the match.
1332-
if missing_ctors.is_empty() {
1333-
// Nothing to report.
1334-
*self = Self::empty();
1335-
} else if !report_individual_missing_ctors {
1332+
if !missing_ctors.is_empty() && !report_individual_missing_ctors {
13361333
// Report `_` as missing.
1337-
let pat = pcx.wild_from_ctor(Constructor::Wildcard);
1338-
self.push_pattern(pat);
1334+
missing_ctors = &[Constructor::Wildcard];
13391335
} else if missing_ctors.iter().any(|c| c.is_non_exhaustive()) {
13401336
// We need to report a `_` anyway, so listing other constructors would be redundant.
13411337
// `NonExhaustive` is displayed as `_` just like `Wildcard`, but it will be picked
13421338
// up by diagnostics to add a note about why `_` is required here.
1343-
let pat = pcx.wild_from_ctor(Constructor::NonExhaustive);
1344-
self.push_pattern(pat);
1345-
} else {
1346-
// For each missing constructor `c`, we add a `c(_, _, _)` witness appropriately
1347-
// filled with wildcards.
1348-
let mut ret = Self::empty();
1349-
for ctor in missing_ctors {
1350-
let pat = pcx.wild_from_ctor(ctor.clone());
1351-
// Clone `self` and add `c(_, _, _)` to each of its witnesses.
1352-
let mut wit_matrix = self.clone();
1353-
wit_matrix.push_pattern(pat);
1354-
ret.extend(wit_matrix);
1355-
}
1356-
*self = ret;
1339+
missing_ctors = &[Constructor::NonExhaustive];
1340+
}
1341+
1342+
// For each missing constructor `c`, we add a `c(_, _, _)` witness appropriately
1343+
// filled with wildcards.
1344+
let mut ret = Self::empty();
1345+
for ctor in missing_ctors {
1346+
let pat = pcx.wild_from_ctor(ctor.clone());
1347+
// Clone `self` and add `c(_, _, _)` to each of its witnesses.
1348+
let mut wit_matrix = self.clone();
1349+
wit_matrix.push_pattern(pat);
1350+
ret.extend(wit_matrix);
13571351
}
1352+
*self = ret;
13581353
} else {
13591354
// Any other constructor we unspecialize as expected.
13601355
for witness in self.0.iter_mut() {

0 commit comments

Comments
 (0)