v4 Semantics: loop rules and scope

  • Track loop legality within the current function only.
  • Separate initializer scope from body block scope.
  • Analyze both operands and unreachable-looking statements before execution.
1 / 6

Readiness

  • Trace source-order scopes and v4 for AST fields.
2 / 6

Worked reconstruction

while (true) {
  fn inner() { break; }
  continue;
}
Traversal point Function context Loop depth Decision
outer while body top-level 1 loop control allowed
enter inner function inner 0 reset loop context
inner break inner 0 break outside loop
leave inner top-level 1 restore outer context
outer continue top-level 1 valid

Merely counting lexical loop nodes without respecting function boundaries would accept the invalid break. A loop declared inside inner would give inner its own valid target.

3 / 6

Diagnose before implementing

An analyzer uses one loopDepth counter across nested function calls during traversal. It accepts the worked example, then rejects the outer continue after setting depth to zero. What two operations must be paired?

4 / 6

Independent transfer

Construct a for loop whose body shadows its initializer variable and a post clause that uses a body-only variable. Predict acceptance of the first and the undefined-name diagnostic for the second without executing either program.

5 / 6

Carry forward

Carry a placement-valid AST into lowering. Preserve tests for function boundaries, scope shadowing, and static traversal of skipped runtime branches.

6 / 6
←/→ or click edges to navigate · ? help · N notes · F fullscreen