Skip to content

GROOVY-12609: STC: resolve the remapped name of a super trait field in a closure or for an instance field - #3136

Closed
codeconsole wants to merge 2 commits into
apache:masterfrom
codeconsole:stc-super-trait-coded-field
Closed

codeconsole wants to merge 2 commits into
apache:masterfrom
codeconsole:stc-super-trait-coded-field

Conversation

@codeconsole

Copy link
Copy Markdown
Contributor

A sub-trait refers to a public field of a super trait by its remapped name (T__name, see GROOVY-11641 and GROOVY-11663). Under @TypeChecked and @CompileStatic that worked only for a static field outside of closures:

@groovy.transform.TypeChecked
trait T {
    public static String shared = 'x'
    public String f = 'i'
}
@groovy.transform.TypeChecked
trait U extends T {
    static m() { def c = { T__shared }; c() } // The variable [T__shared] is undeclared.
    def n() { T__f }                          // No such property: T__f for class: U
}
class C implements U {}

Dynamic Groovy runs both. 5.0.8 and 6.0.0 fail the same way.

TraitReceiverTransformer turns a dynamic variable of trait code into a property of the trait receiver, but not inside a closure (GROOVY-8049, GROOVY-9386). It now does so in a closure too when the name is the remapped name of a field of a super trait. The check follows a super trait's extends clause when that trait is not transformed yet.

TraitTypeCheckingExtension.handleUnresolvedProperty now resolves the remapped name of an instance field of a super trait through the field helper getter, as it already did for static fields through the static field helper. It does this only on the instance receiver, so an instance field used in a static method is still an error. The type is corrected through the type arguments of the super trait.

Tests

TraitASTTransformationTest.testTraitAccessToInheritedStaticFieldInClosure, testTraitAccessToInheritedInstanceField, testTraitAccessToIndirectlyInheritedFields and testTraitAccessToPrecompiledInheritedFields run dynamic, @TypeChecked and @CompileStatic; their @TypeChecked and @CompileStatic variants fail without the change. testTraitAccessToInheritedInstanceFieldInStaticContext checks that an instance field is still rejected in a static method. The groovy.transform.stc, org.codehaus.groovy.classgen.asm.sc, org.codehaus.groovy.transform.traitx and gls packages and groovy.ThisAndSuperTest pass. The Checkstyle and CodeNarc gates pass.

@codecov-commenter

codecov-commenter commented Oct 31, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.55556% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.4132%. Comparing base (cd944a5) to head (4c72616).
⚠️ Report is 150 commits behind head on master.

Files with missing lines Patch % Lines
...oovy/transform/trait/TraitReceiverTransformer.java 71.4286% 1 Missing and 5 partials ⚠️
...oovy/transform/stc/TraitTypeCheckingExtension.java 93.3333% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##               master      #3136        +/-   ##
==================================================
+ Coverage     74.1016%   74.4132%   +0.3116%     
- Complexity      43596      44388       +792     
==================================================
  Files            1692       1699         +7     
  Lines          149245     151078      +1833     
  Branches        28284      28792       +508     
==================================================
+ Hits           110593     112422      +1829     
+ Misses          28475      28378        -97     
- Partials        10177      10278       +101     
Files with missing lines Coverage Δ
...oovy/transform/stc/TraitTypeCheckingExtension.java 83.7209% <93.3333%> (+2.7083%) ⬆️
...oovy/transform/trait/TraitReceiverTransformer.java 87.0370% <71.4286%> (-1.7955%) ⬇️

... and 144 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.

@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.

LGTM for GROOVY-12609.

Remapped super-trait field names (T__shared, T__f) must remain trait-receiver properties even inside closures (where GROOVY-8049 / 9386 otherwise leave dynamic variables alone), and instance remapped names need the field-helper getter path that static ones already had. isSuperTraitFieldName + collectSuperTraits (including unresolved extends) and the handleUnresolvedProperty instance-field branch with generics correction look careful.

Three-mode tests for static/instance remapped fields, closures, lambdas, and generics are good. CI green.

@testlens-app

testlens-app Bot commented Nov 8, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 4c72616
▶️ Tests: 63335 executed
⚪️ Checks: 26/26 completed


Learn more about TestLens at testlens.app/docs.

@paulk-asert

Copy link
Copy Markdown
Contributor

Merged, potentially with minor changes, as part of #3297.

paulk-asert pushed a commit that referenced this pull request Nov 16, 2026
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.

4 participants