Track forEach counters in body scope to fix nested-loop shadowing#1944
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
A `forEach` lowers to a Yul for-loop with two synthetic init variables, `__forEach_idx` and `__forEach_count`, that are live across the whole body. The body was compiled with scope `varName :: inScopeNames`, omitting those two counters. A `forEach` nested inside another `forEach` therefore re-derived the same `__forEach_idx`/`__forEach_count` (pickFreshName saw them as unused), and the inner `let` collided with the still-live outer binding. solc rejects the result with "Variable name already taken in this scope", so any contract with nested loops failed to compile. Add `forEachBodyScope`, which extends the body-compile scope with the two synthetic counter names. It is definitionally equal to `idxName :: countName :: varName :: inScopeNames`, so generated Yul for non-nested loops is unchanged; for nested loops the inner counters are now freshened and the output is valid. The proof side (FunctionBody, GenericInduction, EvmYulLeanBodyClosure) is re-threaded through the same handle so the scope-accounting and bridged-body theorems continue to hold. No new axioms (AXIOMS.md stays at 0), no trust-boundary or CI change, so AUDIT.md/TRUST_ASSUMPTIONS.md/AXIOMS.md need no synchronization. `lake build` is green; downstream, this lets solc compile UnlinkPool (which nests loops) cleanly where the unfixed compiler emitted shadowing-invalid Yul.
fd40e1d to
825b5a7
Compare
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.
Summary
A
forEachlowers to a Yul for-loop with two synthetic init variables,__forEach_idxand__forEach_count, that stay live across the whole loop body.The body was compiled with scope
varName :: inScopeNames, which omits those twocounters. A
forEachnested inside anotherforEachtherefore re-derived the same__forEach_idx/__forEach_count(pickFreshNamesaw them as unused), and theinner
letcollided with the still-live outer binding. solc rejects this with"Variable name already taken in this scope", so any contract that nests loops fails
to compile.
What
CompilationModel.forEachBodyScope, which extends the body-compile scope withthe two synthetic counter names. It is definitionally equal to
idxName :: countName :: varName :: inScopeNames, so generated Yul for non-nestedloops is byte-identical; for nested loops the inner counters are now freshened
(
__forEach_idx_...,__forEach_count_...) and the output is valid.FunctionBody,GenericInduction,EvmYulLeanBodyClosure) through the same handle so the scope-accounting andbridged-body theorems continue to hold over the corrected scope.
Audit docs
No new axioms (AXIOMS.md stays at 0 project-level axioms), no trust-boundary change,
no CI-boundary change. Every invariant recorded in AUDIT.md / TRUST_ASSUMPTIONS.md /
AXIOMS.md is preserved (the proofs are re-threaded, not weakened), so per Non-Negotiable
1 those files need no synchronization. Flagging this explicitly so a maintainer can
object if they read it differently.
Testing
lake buildis green on this branch.forEachloops (Unlink's
UnlinkPool) cleanly, where the unfixed compiler emittedshadowing-invalid Yul that solc rejected.
Opened as draft for review.
Note
Medium Risk
Touches core statement compilation and many IR-generation proofs; behavior change is localized to nested
forEachname freshening, but incorrect scope wiring could affect generated Yul or proof soundness.Overview
Fixes nested
forEachlowering so inner loops no longer reuse the outer loop’s__forEach_idx/__forEach_countnames, which solc rejected as duplicate bindings in the same Yul scope.Adds
CompilationModel.forEachBodyScope, which extends the in-scope name list passed intocompileStmtListfor a loop body with the freshly chosen counter names (plus the loop variable).Stmt.forEachcodegen now compiles the body with that scope instead of onlyvarName :: inScopeNames. For a single loop the scope matches the previous inline list, so output stays the same; nested loops get suffixed counter locals and valid Yul.Proof modules (
FunctionBody,GenericInduction,EvmYulLeanBodyClosure) are updated to use the same scope handle in induction and bridged-body theorems so IR-generation proofs still align with the compiler.Reviewed by Cursor Bugbot for commit 825b5a7. Bugbot is set up for automated code reviews on this repo. Configure here.