@@ -24,7 +24,6 @@ use crate::Node;
24
24
use crate :: NodeLocation ;
25
25
use crate :: SourceFile ;
26
26
use crate :: SourceMap ;
27
- use apollo_parser:: LimitTracker ;
28
27
use indexmap:: IndexSet ;
29
28
use std:: fmt;
30
29
use std:: io;
@@ -492,23 +491,28 @@ struct RecursionLimitError {}
492
491
/// Track used names in a recursive function.
493
492
struct RecursionStack {
494
493
seen : IndexSet < Name > ,
495
- limit : LimitTracker ,
494
+ limit : usize ,
496
495
}
497
496
498
497
impl RecursionStack {
498
+ fn new ( limit : usize ) -> Self {
499
+ Self {
500
+ seen : IndexSet :: new ( ) ,
501
+ limit,
502
+ }
503
+ }
504
+
499
505
fn with_root ( root : Name , limit : usize ) -> Self {
500
506
let mut seen = IndexSet :: new ( ) ;
501
507
seen. insert ( root) ;
502
- let mut limit = LimitTracker :: new ( limit) ;
503
- let _ = limit. check_and_increment ( ) ;
504
508
Self { seen, limit }
505
509
}
506
510
507
511
/// Return the actual API for tracking recursive uses.
508
512
pub ( crate ) fn guard ( & mut self ) -> RecursionGuard < ' _ > {
509
513
RecursionGuard {
510
514
seen : & mut self . seen ,
511
- limit : & mut self . limit ,
515
+ limit : self . limit ,
512
516
}
513
517
}
514
518
}
@@ -520,7 +524,7 @@ impl RecursionStack {
520
524
/// from the list.
521
525
struct RecursionGuard < ' a > {
522
526
seen : & ' a mut IndexSet < Name > ,
523
- limit : & ' a mut LimitTracker ,
527
+ limit : usize ,
524
528
}
525
529
impl RecursionGuard < ' _ > {
526
530
/// Mark that we saw a name. If there are too many names, return an error.
@@ -529,7 +533,7 @@ impl RecursionGuard<'_> {
529
533
self . seen. insert( name. clone( ) ) ,
530
534
"cannot push the same name twice to RecursionGuard, check contains() first"
531
535
) ;
532
- if self . limit . check_and_increment ( ) {
536
+ if self . seen . len ( ) > self . limit {
533
537
Err ( RecursionLimitError { } )
534
538
} else {
535
539
Ok ( RecursionGuard {
@@ -551,8 +555,7 @@ impl RecursionGuard<'_> {
551
555
impl Drop for RecursionGuard < ' _ > {
552
556
fn drop ( & mut self ) {
553
557
// This may already be empty if it's the original `stack.guard()` result, but that's fine
554
- self . seen . pop ( ) ;
555
- self . limit . decrement ( ) ;
558
+ let _ = self . seen . pop ( ) ;
556
559
}
557
560
}
558
561
0 commit comments