Skip to content

Fix variable scoping in extracted for-loop updates - #120

Merged
AjayBrahmakshatriya merged 1 commit into
BuildIt-lang:masterfrom
AjayBrahmakshatriya:master
Sep 23, 2026
Merged

AjayBrahmakshatriya merged 1 commit into
BuildIt-lang:masterfrom
AjayBrahmakshatriya:master

Conversation

@AjayBrahmakshatriya

@AjayBrahmakshatriya AjayBrahmakshatriya commented Sep 23, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #118.

Summary

for_loop_finder could move an update expression into a generated for
header while leaving variables referenced by that expression declared
inside the loop body. This produced invalid generated code.

For example:

while (i < n) {
    dyn_var<int> one = 1;
    i = i + one;
}

could generate:

  for (...; i < n; i = i + one) {
      int one = 1;
  }

Changes

  • Find loop-body declarations referenced by an extracted update.

  • Hoist those declarations to the scope immediately surrounding the
    generated for.

  • Replace their original declarations with assignments, preserving the
    original initialization timing and side effects.

  • Apply the same behavior recursively for nested loops.

  • Keep the original while when a declaration cannot safely be split,
    including references, arrays, function types, top-level const, and
    nonautomatic declarations.

  • Require fallthrough and explicit-continue updates to be structurally
    equivalent before extracting them.

  • Correct validation of update statements preceding continue.

  • Add sample66 coverage for:

    • A simple loop.
    • Nested loops.
    • A loop containing an early return.
  • Add golden outputs for normal and recovered-variable-name modes.

Validation

  • Built and ran sample66 in both extraction modes.

  • Verified both generated outputs against their golden files.

  • Verified the default output with GCC C11 syntax checking.

  • Tested updates containing multiple body-local variables and external
    variables.

  • Tested recursive hoisting through nested loops.

  • Confirmed differing branch updates remain represented as a while.

  • Confirmed all 63 existing golden-tested samples remain unchanged.

  • Ran formatting and git diff --check.

@AjayBrahmakshatriya
AjayBrahmakshatriya merged commit 2c4292c into BuildIt-lang:master Sep 23, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generated for-loop updates can reference variables declared only in the loop body

1 participant