Skip to content

GROOVY-12712: number for-await source variables per source unit - #3243

Merged
paulk-asert merged 1 commit into
apache:masterfrom
codeconsole:fix/groovy-12712-for-await-counter
Nov 16, 2026
Merged

paulk-asert merged 1 commit into
apache:masterfrom
codeconsole:fix/groovy-12712-for-await-counter

Conversation

@codeconsole

Copy link
Copy Markdown
Contributor

The parser desugars for await (x in source) into a block that declares the iterable in a variable named $__forAwaitSource_<n> (AsyncTransformHelper.wrapForAwaitLoop, called from AstBuilder.visitForStmtAlt). The number came from a static AtomicLong shared by every compilation in the JVM, so the same file compiled to different class bytes depending on what was compiled before it in the same groovyc, build daemon or IDE session (the name reaches the local variable table), and two parses of one file in one JVM gave two different CONVERSION-phase ASTs. That breaks reproducible builds, confuses output-keyed build caches, and makes any tool that re-parses a file and compares ASTs (a formatter checking that it preserved the AST) report a difference for every file with a for await loop.

AstBuilder now counts the for await loops of its source unit and passes the ordinal to a new wrapForAwaitLoop(ForStatement, long) overload, so the name depends on the source alone. The one-argument overload keeps the global counter for callers outside the parser (it is public, and AsyncTransformHelperTest uses it).

Tests: AsyncTransformHelperTest gains a test for the indexed overload and one that parses a source with two loops twice through a CompilationUnit and asserts both parses declare $__forAwaitSource_1 and $__forAwaitSource_2; AsyncAwaitTest (274 tests) and AsyncTransformHelperTest pass on JDK 21.

Reproducer from the issue, on 6.0.0: groovyc -d one Beta.groovy gives $__forAwaitSource_1, groovyc -d two Alpha.groovy Beta.groovy gives $__forAwaitSource_2 for the same Beta.groovy, and cmp one/Beta.class two/Beta.class differs. With this change both compilations declare $__forAwaitSource_1.

Candidate for GROOVY_6_0_X as well.

@daniellansun daniellansun left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build-and-test is red with the two ParserNegativeSyntaxTest failures that were broken on master after GROOVY-12641 (GEP-19). Those fixtures have been fixed on current master (see #3247).

Please rebase onto the latest master and push so CI can re-run against that tip — that should clear this noise. If anything is still red after the rebase, we can dig into it then.

The parser desugars `for await (x in source)` into a block that declares the
iterable in a variable named `$__forAwaitSource_<n>`. The number came from a
static AtomicLong shared by every compilation in the JVM, so the same file
compiled to different class bytes (the name is in the local variable table)
depending on what was compiled before it, and two parses of one file in one
JVM gave different ASTs. AstBuilder now counts the loops of its source unit
and passes the ordinal to a new wrapForAwaitLoop(ForStatement, long); the
one-argument overload keeps the global counter for callers outside the parser.
@codeconsole
codeconsole force-pushed the fix/groovy-12712-for-await-counter branch from e20b8f8 to 3f8e070 Compare November 6, 2026 21:51
@codeconsole

Copy link
Copy Markdown
Contributor Author

Rebased onto current master (5170b46, which has #3247) and pushed; the rebase was clean and AsyncTransformHelperTest and AsyncAwaitTest pass locally on the rebased branch.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.3370%. Comparing base (5170b46) to head (3f8e070).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##               master      #3243        +/-   ##
==================================================
- Coverage     74.3428%   74.3370%   -0.0058%     
+ Complexity      44310      44306         -4     
==================================================
  Files            1699       1699                
  Lines          150862     150863         +1     
  Branches        28756      28756                
==================================================
- Hits           112155     112147         -8     
- Misses          28435      28440         +5     
- Partials        10272      10276         +4     
Files with missing lines Coverage Δ
...va/org/apache/groovy/parser/antlr4/AstBuilder.java 87.3391% <100.0000%> (ø)
...odehaus/groovy/transform/AsyncTransformHelper.java 85.5072% <100.0000%> (+0.2131%) ⬆️

... and 10 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@testlens-app

testlens-app Bot commented Nov 6, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 3f8e070
▶️ Tests: 120784 executed
⚪️ Checks: 26/26 completed


Learn more about TestLens at testlens.app/docs.

@daniellansun daniellansun left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for rebasing onto current master (with #3247) and confirming the local async tests. Tip 3f8e07083d7e is green across LTS / Analyze, and the GROOVY-12712 change reads cleanly.

Numbering $__forAwaitSource_<n> from an AstBuilder-local counter (with the existing AtomicLong overload kept for API compatibility) is the right fix: the name that reaches the LVT no longer depends on JVM-wide compile order. The parser-driven ordinal plus testForAwaitSourceNamesDependOnTheSourceUnitOnly / testWrapForAwaitLoopWithIndexUsesThatIndex lock the contract well.

No further concerns from me on this tip.

@paulk-asert
paulk-asert merged commit 676988f into apache:master Nov 16, 2026
27 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.

5 participants