@@ -734,6 +734,21 @@ struct UsefulnessCtxt<'a, Cx: TypeCx> {
734734 /// Collect the patterns found useful during usefulness checking. This is used to lint
735735 /// unreachable (sub)patterns.
736736 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+ }
737752}
738753
739754/// Context that provides information local to a place under investigation.
@@ -1552,6 +1567,7 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>(
15521567 }
15531568
15541569 let Some ( place) = matrix. head_place ( ) else {
1570+ mcx. increase_complexity_level ( matrix. rows ( ) . len ( ) ) ?;
15551571 // The base case: there are no columns in the matrix. We are morally pattern-matching on ().
15561572 // A row is useful iff it has no (unguarded) rows above it.
15571573 let mut useful = true ; // Whether the next row is useful.
@@ -1690,8 +1706,14 @@ pub fn compute_match_usefulness<'p, Cx: TypeCx>(
16901706 arms : & [ MatchArm < ' p , Cx > ] ,
16911707 scrut_ty : Cx :: Ty ,
16921708 scrut_validity : ValidityConstraint ,
1709+ complexity_limit : Option < usize > ,
16931710) -> 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+ } ;
16951717 let mut matrix = Matrix :: new ( arms, scrut_ty, scrut_validity) ;
16961718 let non_exhaustiveness_witnesses = compute_exhaustiveness_and_usefulness ( & mut cx, & mut matrix) ?;
16971719
0 commit comments