@@ -734,6 +734,21 @@ struct UsefulnessCtxt<'a, Cx: TypeCx> {
734
734
/// Collect the patterns found useful during usefulness checking. This is used to lint
735
735
/// unreachable (sub)patterns.
736
736
useful_subpatterns : FxHashSet < PatId > ,
737
+ complexity_limit : Option < usize > ,
738
+ complexity_level : usize ,
739
+ }
740
+
741
+ impl < ' a , Cx : TypeCx > UsefulnessCtxt < ' a , Cx > {
742
+ fn increase_complexity_level ( & mut self , complexity_add : usize ) -> Result < ( ) , Cx :: Error > {
743
+ self . complexity_level += complexity_add;
744
+ if self
745
+ . complexity_limit
746
+ . is_some_and ( |complexity_limit| complexity_limit < self . complexity_level )
747
+ {
748
+ return self . tycx . complexity_exceeded ( ) ;
749
+ }
750
+ Ok ( ( ) )
751
+ }
737
752
}
738
753
739
754
/// Context that provides information local to a place under investigation.
@@ -1552,6 +1567,7 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>(
1552
1567
}
1553
1568
1554
1569
let Some ( place) = matrix. head_place ( ) else {
1570
+ mcx. increase_complexity_level ( matrix. rows ( ) . len ( ) ) ?;
1555
1571
// The base case: there are no columns in the matrix. We are morally pattern-matching on ().
1556
1572
// A row is useful iff it has no (unguarded) rows above it.
1557
1573
let mut useful = true ; // Whether the next row is useful.
@@ -1690,8 +1706,14 @@ pub fn compute_match_usefulness<'p, Cx: TypeCx>(
1690
1706
arms : & [ MatchArm < ' p , Cx > ] ,
1691
1707
scrut_ty : Cx :: Ty ,
1692
1708
scrut_validity : ValidityConstraint ,
1709
+ complexity_limit : Option < usize > ,
1693
1710
) -> Result < UsefulnessReport < ' p , Cx > , Cx :: Error > {
1694
- let mut cx = UsefulnessCtxt { tycx, useful_subpatterns : FxHashSet :: default ( ) } ;
1711
+ let mut cx = UsefulnessCtxt {
1712
+ tycx,
1713
+ useful_subpatterns : FxHashSet :: default ( ) ,
1714
+ complexity_limit,
1715
+ complexity_level : 0 ,
1716
+ } ;
1695
1717
let mut matrix = Matrix :: new ( arms, scrut_ty, scrut_validity) ;
1696
1718
let non_exhaustiveness_witnesses = compute_exhaustiveness_and_usefulness ( & mut cx, & mut matrix) ?;
1697
1719
0 commit comments