Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 2e78c6b

Browse files
committed
Remove premature shortcutting
1 parent bf1848d commit 2e78c6b

File tree

1 file changed

+12
-26
lines changed

1 file changed

+12
-26
lines changed

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

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -914,22 +914,6 @@ impl<'p, 'tcx> Usefulness<'p, 'tcx> {
914914
}
915915
}
916916

917-
/// When trying several branches and each returns a `Usefulness`, we need to combine the
918-
/// results together.
919-
fn merge(pref: ArmType, usefulnesses: impl Iterator<Item = Self>) -> Self {
920-
let mut ret = Self::new_not_useful(pref);
921-
for u in usefulnesses {
922-
ret.extend(u);
923-
if let NoWitnesses(subpats) = &ret {
924-
if subpats.is_full() {
925-
// Once we reach the full set, more unions won't change the result.
926-
return ret;
927-
}
928-
}
929-
}
930-
ret
931-
}
932-
933917
/// After calculating the usefulness for a branch of an or-pattern, call this to make this
934918
/// usefulness mergeable with those from the other branches.
935919
fn unsplit_or_pat(self, alt_id: usize, alt_count: usize, pat: &'p Pat<'tcx>) -> Self {
@@ -1168,25 +1152,26 @@ fn is_useful<'p, 'tcx>(
11681152
let pcx = PatCtxt { cx, ty, span: v.head().span, is_top_level, is_non_exhaustive };
11691153

11701154
// If the first pattern is an or-pattern, expand it.
1171-
let ret = if is_or_pat(v.head()) {
1155+
let mut ret = Usefulness::new_not_useful(witness_preference);
1156+
if is_or_pat(v.head()) {
11721157
debug!("expanding or-pattern");
11731158
let v_head = v.head();
11741159
let vs: Vec<_> = v.expand_or_pat().collect();
11751160
let alt_count = vs.len();
11761161
// We try each or-pattern branch in turn.
11771162
let mut matrix = matrix.clone();
1178-
let usefulnesses = vs.into_iter().enumerate().map(|(i, v)| {
1163+
for (i, v) in vs.into_iter().enumerate() {
11791164
let usefulness =
11801165
is_useful(cx, &matrix, &v, witness_preference, hir_id, is_under_guard, false);
1166+
let usefulness = usefulness.unsplit_or_pat(i, alt_count, v_head);
1167+
ret.extend(usefulness);
11811168
// If pattern has a guard don't add it to the matrix.
11821169
if !is_under_guard {
11831170
// We push the already-seen patterns into the matrix in order to detect redundant
11841171
// branches like `Some(_) | Some(0)`.
11851172
matrix.push(v);
11861173
}
1187-
usefulness.unsplit_or_pat(i, alt_count, v_head)
1188-
});
1189-
Usefulness::merge(witness_preference, usefulnesses)
1174+
}
11901175
} else {
11911176
let v_ctor = v.head_ctor(cx);
11921177
if let Constructor::IntRange(ctor_range) = &v_ctor {
@@ -1204,7 +1189,7 @@ fn is_useful<'p, 'tcx>(
12041189
// For each constructor, we compute whether there's a value that starts with it that would
12051190
// witness the usefulness of `v`.
12061191
let start_matrix = &matrix;
1207-
let usefulnesses = split_ctors.into_iter().map(|ctor| {
1192+
for ctor in split_ctors {
12081193
debug!("specialize({:?})", ctor);
12091194
// We cache the result of `Fields::wildcards` because it is used a lot.
12101195
let ctor_wild_subpatterns = Fields::wildcards(pcx, &ctor);
@@ -1213,6 +1198,8 @@ fn is_useful<'p, 'tcx>(
12131198
let v = v.pop_head_constructor(&ctor_wild_subpatterns);
12141199
let usefulness =
12151200
is_useful(cx, &spec_matrix, &v, witness_preference, hir_id, is_under_guard, false);
1201+
let usefulness =
1202+
usefulness.apply_constructor(pcx, start_matrix, &ctor, &ctor_wild_subpatterns);
12161203

12171204
// When all the conditions are met we have a match with a `non_exhaustive` enum
12181205
// that has the potential to trigger the `non_exhaustive_omitted_patterns` lint.
@@ -1248,10 +1235,9 @@ fn is_useful<'p, 'tcx>(
12481235
lint_non_exhaustive_omitted_patterns(pcx.cx, pcx.ty, pcx.span, hir_id, patterns);
12491236
}
12501237

1251-
usefulness.apply_constructor(pcx, start_matrix, &ctor, &ctor_wild_subpatterns)
1252-
});
1253-
Usefulness::merge(witness_preference, usefulnesses)
1254-
};
1238+
ret.extend(usefulness);
1239+
}
1240+
}
12551241

12561242
debug!(?ret);
12571243
ret

0 commit comments

Comments
 (0)