Skip to content

GROOVY-12608: SC: call a super trait's static accessor read as a property in a closure with the implementing class - #3135

Closed
codeconsole wants to merge 9 commits into
apache:masterfrom
codeconsole:sc-trait-super-accessor-closure
Closed

codeconsole wants to merge 9 commits into
apache:masterfrom
codeconsole:sc-trait-super-accessor-closure

Conversation

@codeconsole

@codeconsole codeconsole commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Under @CompileStatic, a closure or lambda in trait code that reads a super trait's static accessor or static property as a property calls the super trait interface's static forwarder. The forwarder passes the trait itself to the trait helper as the receiver class, so an accessor that reads trait state fails:

@groovy.transform.CompileStatic
trait T { private static int count = 1; static int getCnt() { count } }
@groovy.transform.CompileStatic
trait U extends T { String m() { def c = { -> cnt }; c() } }
class C implements U {}
assert new C().m() == '1' // MissingMethodException: No signature of static method: T__count$get for class: T

Writing the property in a closure (cnt = n, cnt++, s = 'y') is rejected under @TypeChecked and @CompileStatic with "Cannot set read-only property". Dynamic Groovy and the method form getCnt() work.

A name like this inside a closure is left unchanged by the trait transformation (GROOVY-8049, GROOVY-9386), so that the closure's delegate can still provide it. That is kept.

  • Closures: when the type checker resolves the name to a static method of a trait through the closure's owner, StaticInvocationWriter now calls the trait helper (the GROOVY-12590 path). It passes the class of the closure's thisObject, which the trait transformation sets to $self, or $static$self in a static trait method.
  • Lambdas: a statically compiled lambda has no delegate or thisObject, so TraitReceiverTransformer rewrites the name to $self.name or $static$self.name there, as for a property of the trait itself, and the lambda captures it. In dynamic and @TypeChecked code a lambda is a closure at run time, so it is not rewritten and keeps closure resolution, delegate first: p.with((x) -> cnt) reads p.cnt. The rewrite happens before type checking, so under @CompileStatic it cannot yet tell a lambda compiled as a closure (one passed for a Closure parameter, such as to with) from a native one; such a lambda still reads the trait's property.
  • Setters: in trait code, StaticTypeCheckingVisitor now also finds the static setters of a super trait, since a static interface method is not inherited by the receiver type. Only for the receiver of trait code (implicit this, $self or $static$self): other.cnt = 5 through another receiver is still "Cannot set read-only property". GROOVY-12596: STC: accept "this.name" for a static accessor of a trait in trait code #3123 (GROOVY-12596) adds a broader isTraitReceiver(Expression); whichever lands second can fold hasTraitCodeReceiver into it.

p.with { cnt } still reads p.cnt when p has the property. This PR is stacked on #3115 (GROOVY-12590).

Tests

TraitASTTransformationTest.testSuperTraitStaticAccessorAsPropertyInClosure runs dynamic, @TypeChecked and @CompileStatic. It covers getters, an isser, setters, ++, +=, nested closures, static and instance methods, a grand-super trait, static properties, lambdas, per-class state and delegate-first resolution. The @TypeChecked and @CompileStatic variants fail without the change, and each of the three main-code changes is needed for them to pass. The groovy.transform.stc, org.codehaus.groovy.classgen.asm.sc, org.codehaus.groovy.transform.traitx, gls, groovy.bugs and bugs packages and groovy.ThisAndSuperTest pass. The Checkstyle and CodeNarc gates pass.

…ith the implementing class

Under @CompileStatic, trait code that reads or writes a static accessor of
a trait with property syntax ("ready" for isReady(), "cnt = 1" for
setCnt(int)) called the trait interface's forwarder, which passes the
trait itself as the receiver class, so the accessor failed to reach the
trait's static state. Call the trait helper with $static$self or
$self.getClass() instead, as the method call form already does.
…essor-property

# Conflicts:
#	src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticInvocationWriter.java
…erty in a closure with the implementing class

A closure or lambda in trait code leaves a property name that does not
resolve to a property of the trait itself unchanged (GROOVY-8049,
GROOVY-9386), so that the closure's delegate can provide it. Under
@CompileStatic a static accessor of a super trait reached that way
("cnt" for getCnt()) was called on the super trait interface, which
passes the trait itself to the helper as the receiver class, so an
accessor that reads trait state failed. A static setter written as a
property ("cnt = n") was rejected by the type checker, which looked for
setters on the receiver type only, where a static interface method is
not inherited.

In a closure, call the trait helper with the class of the closure's
thisObject, which is $self, or $static$self in a static trait method.
A lambda has no thisObject, so rewrite the name there to "$self.name"
or "$static$self.name", as for a property of the trait itself. Let trait
code see the static setters of a super trait.
@codecov-commenter

codecov-commenter commented Oct 31, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.41237% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.4200%. Comparing base (0941dba) to head (ee173bf).
⚠️ Report is 121 commits behind head on master.

Files with missing lines Patch % Lines
...oovy/transform/trait/TraitReceiverTransformer.java 54.5454% 3 Missing and 7 partials ⚠️
...groovy/classgen/asm/sc/StaticInvocationWriter.java 88.3721% 1 Missing and 4 partials ⚠️
...groovy/transform/trait/TraitASTTransformation.java 87.5000% 0 Missing and 2 partials ⚠️
...ovy/classgen/asm/sc/StaticTypesCallSiteWriter.java 66.6667% 0 Missing and 1 partial ⚠️
...roovy/transform/stc/StaticTypeCheckingVisitor.java 92.3077% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##               master      #3135        +/-   ##
==================================================
+ Coverage     74.1533%   74.4200%   +0.2667%     
- Complexity      43751      44435       +684     
==================================================
  Files            1692       1699         +7     
  Lines          149516     151153      +1637     
  Branches        28366      28822       +456     
==================================================
+ Hits           110871     112488      +1617     
+ Misses          28448      28384        -64     
- Partials        10197      10281        +84     
Files with missing lines Coverage Δ
...ovy/classgen/asm/sc/StaticTypesCallSiteWriter.java 78.0083% <66.6667%> (+0.0457%) ⬆️
...roovy/transform/stc/StaticTypeCheckingVisitor.java 88.0738% <92.3077%> (-0.1441%) ⬇️
...groovy/transform/trait/TraitASTTransformation.java 92.2252% <87.5000%> (-0.2956%) ⬇️
...groovy/classgen/asm/sc/StaticInvocationWriter.java 78.3595% <88.3721%> (+1.1732%) ⬆️
...oovy/transform/trait/TraitReceiverTransformer.java 85.1163% <54.5454%> (-3.6592%) ⬇️

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

…ssor-closure

# Conflicts:
#	src/test/groovy/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy

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

Extends the GROOVY-12590 helper-call path to the closure case: a bare cnt left alone for delegate resolution (GROOVY-8049 / 9386) can still be resolved by the type checker to a super-trait static accessor through the closure owner; StaticInvocationWriter then calls the trait helper with the implementing class obtained from Closure.getThisObject() (Class vs instance). The companion STC tweak that accepts writes of those static properties under TC/SC is necessary and tested.

Stacked on #3115 — please land 12590 first (this tip already includes it). CI green. Tests cover closure/lambda reads and writes of super-trait static accessors/properties from instance and static methods.

…er of trait code

The type checker offered the static setter of a super trait for a property
written through any receiver typed as the sub-trait, so other.cnt = 5
compiled where a read-only property error is due, and failed at run time
under static compilation. Offer it only for implicit this, $self and
$static$self, the receivers this change is for.
…y under static compilation

Outside static compilation a lambda is a closure, which resolves a name
through its delegate and owner, so rewriting "cnt" in p.with((x) -> cnt)
to $self.cnt skipped the delegate p and returned the trait's value. Only a
statically compiled lambda lacks a delegate and thisObject, so rewrite the
name there only.
@testlens-app

testlens-app Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: ee173bf
▶️ Tests: 11339 executed
⚪️ Checks: 26/26 completed


Learn more about TestLens at testlens.app/docs.

paulk-asert pushed a commit that referenced this pull request Nov 16, 2026
…erty in a closure with the implementing class (#3135)

A closure or lambda in trait code leaves a property name that does not
resolve to a property of the trait itself unchanged (GROOVY-8049,
GROOVY-9386), so that the closure's delegate can provide it. Under
@CompileStatic a static accessor of a super trait reached that way
("cnt" for getCnt()) was called on the super trait interface, which
passes the trait itself to the helper as the receiver class, so an
accessor that reads trait state failed. A static setter written as a
property ("cnt = n") was rejected by the type checker, which looked for
setters on the receiver type only, where a static interface method is
not inherited.

In a closure, call the trait helper with the class of the closure's
thisObject, which is $self, or $static$self in a static trait method.
A lambda has no thisObject, so rewrite the name there to "$self.name"
or "$static$self.name", as for a property of the trait itself. Let trait
code see the static setters of a super trait.
@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
…erty in a closure with the implementing class (#3135)

A closure or lambda in trait code leaves a property name that does not
resolve to a property of the trait itself unchanged (GROOVY-8049,
GROOVY-9386), so that the closure's delegate can provide it. Under
@CompileStatic a static accessor of a super trait reached that way
("cnt" for getCnt()) was called on the super trait interface, which
passes the trait itself to the helper as the receiver class, so an
accessor that reads trait state failed. A static setter written as a
property ("cnt = n") was rejected by the type checker, which looked for
setters on the receiver type only, where a static interface method is
not inherited.

In a closure, call the trait helper with the class of the closure's
thisObject, which is $self, or $static$self in a static trait method.
A lambda has no thisObject, so rewrite the name there to "$self.name"
or "$static$self.name", as for a property of the trait itself. Let trait
code see the static setters of a super trait.
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