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