Skip to content

Commit 6a10fb5

Browse files
committed
Reuse length of RecursionGuard::seen set as the current recursion depth
1 parent 570fa98 commit 6a10fb5

File tree

1 file changed

+12
-9
lines changed
  • crates/apollo-compiler/src/validation

1 file changed

+12
-9
lines changed

crates/apollo-compiler/src/validation/mod.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use crate::Node;
2424
use crate::NodeLocation;
2525
use crate::SourceFile;
2626
use crate::SourceMap;
27-
use apollo_parser::LimitTracker;
2827
use indexmap::IndexSet;
2928
use std::fmt;
3029
use std::io;
@@ -492,23 +491,28 @@ struct RecursionLimitError {}
492491
/// Track used names in a recursive function.
493492
struct RecursionStack {
494493
seen: IndexSet<Name>,
495-
limit: LimitTracker,
494+
limit: usize,
496495
}
497496

498497
impl RecursionStack {
498+
fn new(limit: usize) -> Self {
499+
Self {
500+
seen: IndexSet::new(),
501+
limit,
502+
}
503+
}
504+
499505
fn with_root(root: Name, limit: usize) -> Self {
500506
let mut seen = IndexSet::new();
501507
seen.insert(root);
502-
let mut limit = LimitTracker::new(limit);
503-
let _ = limit.check_and_increment();
504508
Self { seen, limit }
505509
}
506510

507511
/// Return the actual API for tracking recursive uses.
508512
pub(crate) fn guard(&mut self) -> RecursionGuard<'_> {
509513
RecursionGuard {
510514
seen: &mut self.seen,
511-
limit: &mut self.limit,
515+
limit: self.limit,
512516
}
513517
}
514518
}
@@ -520,7 +524,7 @@ impl RecursionStack {
520524
/// from the list.
521525
struct RecursionGuard<'a> {
522526
seen: &'a mut IndexSet<Name>,
523-
limit: &'a mut LimitTracker,
527+
limit: usize,
524528
}
525529
impl RecursionGuard<'_> {
526530
/// Mark that we saw a name. If there are too many names, return an error.
@@ -529,7 +533,7 @@ impl RecursionGuard<'_> {
529533
self.seen.insert(name.clone()),
530534
"cannot push the same name twice to RecursionGuard, check contains() first"
531535
);
532-
if self.limit.check_and_increment() {
536+
if self.seen.len() > self.limit {
533537
Err(RecursionLimitError {})
534538
} else {
535539
Ok(RecursionGuard {
@@ -551,8 +555,7 @@ impl RecursionGuard<'_> {
551555
impl Drop for RecursionGuard<'_> {
552556
fn drop(&mut self) {
553557
// 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();
556559
}
557560
}
558561

0 commit comments

Comments
 (0)