closureiters: fix undeclared C identifier when using template/macro bindings in async and/or second operand#25761
Open
emizzle wants to merge 1 commit intonim-lang:develfrom
Open
Conversation
…indings in async and/or second operand `lowerStmtListExprs` handles the second operand of `and`/`or` in closure iterators (i.e., async procs) differently from the first: when the second operand is a `nkStmtListExpr` (produced by template/macro expansion or `await`), its statements were placed in `ifBody` — the body of the short-circuit `if` block — instead of the outer `result` node. This meant that variables declared in those statements (e.g., via an injected `let` from a template like `bindOpt`) were scoped to the short-circuit block and inaccessible in the enclosing `if` body, producing "use of undeclared identifier" errors at the C level. The fix mirrors how the first operand is already handled (line 661): hoist the statements to `result` so they are visible in the enclosing scope. The expression part (`ex`) remains in `ifBody` and continues to be properly short-circuited. Plain (non-stmtListExpr) second operands are unaffected. Note: as a consequence, side-effectful *statements* in the second operand (e.g., variable declarations from template expansion) are no longer short-circuited. This only affects patterns that previously failed to compile.
Member
Unacceptable. Instead keep the "it fails to compile" state. |
Author
It's a trade off. "Unacceptable" seems more like a preference/opinion. |
Author
|
Perhaps this would be more acceptable if it was only enabled with a feature flag? |
Member
|
and/or operators must be short-circuited. |
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
lowerStmtListExprshandles the second operand ofand/orin closure iterators (i.e., async procs) differently from the first: when the second operand is ankStmtListExpr(produced by template/macro expansion orawait), its statements were placed inifBody— the body of the short-circuitifblock — instead of the outerresultnode.This meant that variables declared in those statements (e.g., via an injected
letfrom a template likebindOpt) were scoped to the short-circuit block and inaccessible in the enclosingifbody, producing "use of undeclared identifier" errors at the C level.The fix mirrors how the first operand is already handled (line 661): hoist the statements to
resultso they are visible in the enclosing scope. The expression part (ex) remains inifBodyand continues to be properly short-circuited. Plain (non-stmtListExpr) second operands are unaffected.Note: as a consequence, side-effectful statements in the second operand (e.g., variable declarations from template expansion) are no longer short-circuited. This only affects patterns that previously failed to compile.
Minimal reproducible example
Changes
compiler/closureiters.nim: changeifBody.add(st)toresult.add(st)for the second operand ofand/or, mirroring the existing first-operand handlingtests/async/tasync_and_stmtlistexpr.nim: regression test covering the fixed bug, plus two tests confirming that plain (non-stmtListExpr) second operands remain properly short-circuitedchangelog.md: entry added under "Compiler changes"