Repository navigation
Fix variable scoping in extracted for-loop updates - #120
Merged
AjayBrahmakshatriya merged 1 commit intoSep 23, 2026
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #118.
Summary
for_loop_findercould move an update expression into a generatedforheader while leaving variables referenced by that expression declared
inside the loop body. This produced invalid generated code.
For example:
could generate:
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:
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.