Skip to content

Commit 89d01ba

Browse files
committed
Track row intersections
1 parent d73bd3f commit 89d01ba

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

compiler/rustc_pattern_analysis/src/usefulness.rs

+36-18
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,7 @@
712712
//! I (Nadrieril) prefer to put new tests in `ui/pattern/usefulness` unless there's a specific
713713
//! reason not to, for example if they crucially depend on a particular feature like `or_patterns`.
714714
715+
use rustc_index::bit_set::BitSet;
715716
use smallvec::{smallvec, SmallVec};
716717
use std::fmt;
717718

@@ -911,6 +912,11 @@ struct MatrixRow<'p, Cx: TypeCx> {
911912
/// [`compute_exhaustiveness_and_usefulness`] if the arm is found to be useful.
912913
/// This is reset to `false` when specializing.
913914
useful: bool,
915+
/// Tracks which rows above this one have an intersection with this one, i.e. such that there is
916+
/// a value that matches both rows.
917+
/// Note: Because of relevancy we may miss some intersections. The intersections we do find are
918+
/// correct.
919+
intersects: BitSet<usize>,
914920
}
915921

916922
impl<'p, Cx: TypeCx> MatrixRow<'p, Cx> {
@@ -938,6 +944,7 @@ impl<'p, Cx: TypeCx> MatrixRow<'p, Cx> {
938944
parent_row: self.parent_row,
939945
is_under_guard: self.is_under_guard,
940946
useful: false,
947+
intersects: BitSet::new_empty(0), // Initialized in `Matrix::expand_and_push`.
941948
})
942949
}
943950

@@ -955,6 +962,7 @@ impl<'p, Cx: TypeCx> MatrixRow<'p, Cx> {
955962
parent_row,
956963
is_under_guard: self.is_under_guard,
957964
useful: false,
965+
intersects: BitSet::new_empty(0), // Initialized in `Matrix::expand_and_push`.
958966
}
959967
}
960968
}
@@ -993,13 +1001,15 @@ struct Matrix<'p, Cx: TypeCx> {
9931001
impl<'p, Cx: TypeCx> Matrix<'p, Cx> {
9941002
/// Pushes a new row to the matrix. If the row starts with an or-pattern, this recursively
9951003
/// expands it. Internal method, prefer [`Matrix::new`].
996-
fn expand_and_push(&mut self, row: MatrixRow<'p, Cx>) {
1004+
fn expand_and_push(&mut self, mut row: MatrixRow<'p, Cx>) {
9971005
if !row.is_empty() && row.head().is_or_pat() {
9981006
// Expand nested or-patterns.
999-
for new_row in row.expand_or_pat() {
1007+
for mut new_row in row.expand_or_pat() {
1008+
new_row.intersects = BitSet::new_empty(self.rows.len());
10001009
self.rows.push(new_row);
10011010
}
10021011
} else {
1012+
row.intersects = BitSet::new_empty(self.rows.len());
10031013
self.rows.push(row);
10041014
}
10051015
}
@@ -1019,9 +1029,10 @@ impl<'p, Cx: TypeCx> Matrix<'p, Cx> {
10191029
for (row_id, arm) in arms.iter().enumerate() {
10201030
let v = MatrixRow {
10211031
pats: PatStack::from_pattern(arm.pat),
1022-
parent_row: row_id, // dummy, we won't read it
1032+
parent_row: row_id, // dummy, we don't read it
10231033
is_under_guard: arm.has_guard,
10241034
useful: false,
1035+
intersects: BitSet::new_empty(0), // Initialized in `Matrix::expand_and_push`.
10251036
};
10261037
matrix.expand_and_push(v);
10271038
}
@@ -1349,21 +1360,19 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>(
13491360
let Some(ty) = matrix.head_ty() else {
13501361
// The base case: there are no columns in the matrix. We are morally pattern-matching on ().
13511362
// A row is useful iff it has no (unguarded) rows above it.
1352-
for row in matrix.rows_mut() {
1353-
// All rows are useful until they're not.
1354-
row.useful = true;
1355-
// When there's an unguarded row, the match is exhaustive and any subsequent row is not
1356-
// useful.
1357-
if !row.is_under_guard {
1358-
return Ok(WitnessMatrix::empty());
1359-
}
1363+
let mut useful = true; // Whether the next row is useful.
1364+
for (i, row) in matrix.rows_mut().enumerate() {
1365+
row.useful = useful;
1366+
row.intersects.insert_range(0..i);
1367+
// The next rows stays useful if this one is under a guard.
1368+
useful &= row.is_under_guard;
13601369
}
1361-
// No (unguarded) rows, so the match is not exhaustive. We return a new witness unless
1362-
// irrelevant.
1363-
return if matrix.wildcard_row_is_relevant {
1370+
return if useful && matrix.wildcard_row_is_relevant {
1371+
// The wildcard row is useful; the match is non-exhaustive.
13641372
Ok(WitnessMatrix::unit_witness())
13651373
} else {
1366-
// We choose to not report anything here; see at the top for details.
1374+
// Either the match is exhaustive, or we choose not to report anything because of
1375+
// relevancy. See at the top for details.
13671376
Ok(WitnessMatrix::empty())
13681377
};
13691378
};
@@ -1424,10 +1433,19 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>(
14241433
// Accumulate the found witnesses.
14251434
ret.extend(witnesses);
14261435

1427-
// A parent row is useful if any of its children is.
14281436
for child_row in spec_matrix.rows() {
1429-
let parent_row = &mut matrix.rows[child_row.parent_row];
1430-
parent_row.useful = parent_row.useful || child_row.useful;
1437+
let parent_row_id = child_row.parent_row;
1438+
let parent_row = &mut matrix.rows[parent_row_id];
1439+
// A parent row is useful if any of its children is.
1440+
parent_row.useful |= child_row.useful;
1441+
for child_intersection in child_row.intersects.iter() {
1442+
// Convert the intersecting ids into ids for the parent matrix.
1443+
let parent_intersection = spec_matrix.rows[child_intersection].parent_row;
1444+
// Note: self-intersection can happen with or-patterns.
1445+
if parent_intersection != parent_row_id {
1446+
parent_row.intersects.insert(parent_intersection);
1447+
}
1448+
}
14311449
}
14321450
}
14331451

0 commit comments

Comments
 (0)