@@ -734,6 +734,22 @@ 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
+ . map ( |complexity_limit| complexity_limit < self . complexity_level )
747
+ . unwrap_or ( false )
748
+ {
749
+ return self . tycx . complexity_exceeded ( ) ;
750
+ }
751
+ Ok ( ( ) )
752
+ }
737
753
}
738
754
739
755
/// Context that provides information local to a place under investigation.
@@ -1552,6 +1568,7 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>(
1552
1568
}
1553
1569
1554
1570
let Some ( place) = matrix. head_place ( ) else {
1571
+ mcx. increase_complexity_level ( matrix. rows ( ) . len ( ) ) ?;
1555
1572
// The base case: there are no columns in the matrix. We are morally pattern-matching on ().
1556
1573
// A row is useful iff it has no (unguarded) rows above it.
1557
1574
let mut useful = true ; // Whether the next row is useful.
@@ -1690,8 +1707,14 @@ pub fn compute_match_usefulness<'p, Cx: TypeCx>(
1690
1707
arms : & [ MatchArm < ' p , Cx > ] ,
1691
1708
scrut_ty : Cx :: Ty ,
1692
1709
scrut_validity : ValidityConstraint ,
1710
+ complexity_limit : Option < usize > ,
1693
1711
) -> Result < UsefulnessReport < ' p , Cx > , Cx :: Error > {
1694
- let mut cx = UsefulnessCtxt { tycx, useful_subpatterns : FxHashSet :: default ( ) } ;
1712
+ let mut cx = UsefulnessCtxt {
1713
+ tycx,
1714
+ useful_subpatterns : FxHashSet :: default ( ) ,
1715
+ complexity_limit,
1716
+ complexity_level : 0 ,
1717
+ } ;
1695
1718
let mut matrix = Matrix :: new ( arms, scrut_ty, scrut_validity) ;
1696
1719
let non_exhaustiveness_witnesses = compute_exhaustiveness_and_usefulness ( & mut cx, & mut matrix) ?;
1697
1720
0 commit comments