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 ;
715716use smallvec:: { smallvec, SmallVec } ;
716717use 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
916922impl < ' 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}
@@ -991,13 +999,15 @@ struct Matrix<'p, Cx: TypeCx> {
991999impl < ' p , Cx : TypeCx > Matrix < ' p , Cx > {
9921000 /// Pushes a new row to the matrix. If the row starts with an or-pattern, this recursively
9931001 /// expands it. Internal method, prefer [`Matrix::new`].
994- fn expand_and_push ( & mut self , row : MatrixRow < ' p , Cx > ) {
1002+ fn expand_and_push ( & mut self , mut row : MatrixRow < ' p , Cx > ) {
9951003 if !row. is_empty ( ) && row. head ( ) . is_or_pat ( ) {
9961004 // Expand nested or-patterns.
997- for new_row in row. expand_or_pat ( ) {
1005+ for mut new_row in row. expand_or_pat ( ) {
1006+ new_row. intersects = BitSet :: new_empty ( self . rows . len ( ) ) ;
9981007 self . rows . push ( new_row) ;
9991008 }
10001009 } else {
1010+ row. intersects = BitSet :: new_empty ( self . rows . len ( ) ) ;
10011011 self . rows . push ( row) ;
10021012 }
10031013 }
@@ -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 }
@@ -1348,21 +1359,19 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>(
13481359 let Some ( ty) = matrix. head_ty ( mcx) else {
13491360 // The base case: there are no columns in the matrix. We are morally pattern-matching on ().
13501361 // A row is useful iff it has no (unguarded) rows above it.
1351- for row in matrix. rows_mut ( ) {
1352- // All rows are useful until they're not.
1353- row. useful = true ;
1354- // When there's an unguarded row, the match is exhaustive and any subsequent row is not
1355- // useful.
1356- if !row. is_under_guard {
1357- return WitnessMatrix :: empty ( ) ;
1358- }
1362+ let mut useful = true ; // Whether the next row is useful.
1363+ for ( i, row) in matrix. rows_mut ( ) . enumerate ( ) {
1364+ row. useful = useful;
1365+ row. intersects . insert_range ( 0 ..i) ;
1366+ // The next rows stays useful if this one is under a guard.
1367+ useful &= row. is_under_guard ;
13591368 }
1360- // No (unguarded) rows, so the match is not exhaustive. We return a new witness unless
1361- // irrelevant.
1362- return if matrix. wildcard_row . relevant {
1369+ return if useful && matrix. wildcard_row . relevant {
1370+ // The wildcard row is useful; the match is non-exhaustive.
13631371 WitnessMatrix :: unit_witness ( )
13641372 } else {
1365- // We choose to not report anything here; see at the top for details.
1373+ // Either the match is exhaustive, or we choose not to report anything because of
1374+ // relevancy. See at the top for details.
13661375 WitnessMatrix :: empty ( )
13671376 } ;
13681377 } ;
@@ -1423,10 +1432,19 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>(
14231432 // Accumulate the found witnesses.
14241433 ret. extend ( witnesses) ;
14251434
1426- // A parent row is useful if any of its children is.
14271435 for child_row in spec_matrix. rows ( ) {
1428- let parent_row = & mut matrix. rows [ child_row. parent_row ] ;
1429- parent_row. useful = parent_row. useful || child_row. useful ;
1436+ let parent_row_id = child_row. parent_row ;
1437+ let parent_row = & mut matrix. rows [ parent_row_id] ;
1438+ // A parent row is useful if any of its children is.
1439+ parent_row. useful |= child_row. useful ;
1440+ for child_intersection in child_row. intersects . iter ( ) {
1441+ // Convert the intersecting ids into ids for the parent matrix.
1442+ let parent_intersection = spec_matrix. rows [ child_intersection] . parent_row ;
1443+ // Note: self-intersection can happen with or-patterns.
1444+ if parent_intersection != parent_row_id {
1445+ parent_row. intersects . insert ( parent_intersection) ;
1446+ }
1447+ }
14301448 }
14311449 }
14321450
0 commit comments