Skip to content

Conversation

@coleleavitt
Copy link

Summary

Add methods to extract user state from ReusableTraverseCtx after traversal:

  • into_state() - consumes ctx and returns just the user state
  • into_state_and_scoping() - consumes ctx and returns both state and Scoping

Motivation

When implementing multi-pass AST transformations, passes often need to:

  1. Accumulate state during traversal (e.g., detect patterns)
  2. Extract that state after traversal
  3. Pass accumulated state to subsequent passes

Currently, the only way to extract state is via unsafe fn unwrap(). These new methods provide a safe API for this common pattern.

Example Use Case

// Pass 1: Detect string arrays
let mut ctx = ReusableTraverseCtx::new(MyState::new(), scoping, &allocator);
traverse_mut_with_ctx(&mut pass1, &mut program, &mut ctx);

// Extract state for next pass
let mut state = ctx.into_state();
pass1.finalize(&mut state);

// Pass 2: Use accumulated state
let scoping = SemanticBuilder::new().build(&program).semantic.into_scoping();
let mut ctx = ReusableTraverseCtx::new(state, scoping, &allocator);
traverse_mut_with_ctx(&mut pass2, &mut program, &mut ctx);

Changes

  • Added pub fn into_state(self) -> State
  • Added pub fn into_state_and_scoping(self) -> (State, Scoping)

…ReusableTraverseCtx

Add methods to extract user state from ReusableTraverseCtx after traversal:
- into_state(): returns just the user state
- into_state_and_scoping(): returns both state and Scoping

This enables multi-pass transformations where state needs to be accumulated
across passes that each require rebuilding scoping (e.g., after AST modifications).
@github-actions github-actions bot added the C-enhancement Category - New feature or request label Jan 14, 2026
@codspeed-hq
Copy link

codspeed-hq bot commented Jan 14, 2026

CodSpeed Performance Report

Merging this PR will not alter performance

Comparing coleleavitt:feat/traverse-ctx-into-state (4d5a794) with main (78fa1ba)1

Summary

✅ 38 untouched benchmarks
⏩ 7 skipped benchmarks2

Footnotes

  1. No successful run was found on main (61091c1) during the generation of this report, so 78fa1ba was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

  2. 7 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant