Repository navigation
GROOVY-12608: SC: call a super trait's static accessor read as a property in a closure with the implementing class - #3135
codeconsole wants to merge 9 commits into
Conversation
…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.
…n a closure in a static method
Codecov Report❌ Patch coverage is Additional details and impacted files@@ 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
🚀 New features to boost your workflow:
|
…ssor-closure # Conflicts: # src/test/groovy/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
daniellansun
left a comment
There was a problem hiding this comment.
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.
✅ All tests passed ✅🏷️ Commit: ee173bf Learn more about TestLens at testlens.app/docs. |
…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.
|
Merged, potentially with minor changes, as part of #3297. |
…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.
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:Writing the property in a closure (
cnt = n,cnt++,s = 'y') is rejected under@TypeCheckedand@CompileStaticwith "Cannot set read-only property". Dynamic Groovy and the method formgetCnt()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.
StaticInvocationWriternow calls the trait helper (the GROOVY-12590 path). It passes the class of the closure'sthisObject, which the trait transformation sets to$self, or$static$selfin a static trait method.thisObject, soTraitReceiverTransformerrewrites the name to$self.nameor$static$self.namethere, as for a property of the trait itself, and the lambda captures it. In dynamic and@TypeCheckedcode a lambda is a closure at run time, so it is not rewritten and keeps closure resolution, delegate first:p.with((x) -> cnt)readsp.cnt. The rewrite happens before type checking, so under@CompileStaticit cannot yet tell a lambda compiled as a closure (one passed for aClosureparameter, such as towith) from a native one; such a lambda still reads the trait's property.StaticTypeCheckingVisitornow 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 (implicitthis,$selfor$static$self):other.cnt = 5through 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 broaderisTraitReceiver(Expression); whichever lands second can foldhasTraitCodeReceiverinto it.p.with { cnt }still readsp.cntwhenphas the property. This PR is stacked on #3115 (GROOVY-12590).Tests
TraitASTTransformationTest.testSuperTraitStaticAccessorAsPropertyInClosureruns dynamic,@TypeCheckedand@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@TypeCheckedand@CompileStaticvariants fail without the change, and each of the three main-code changes is needed for them to pass. Thegroovy.transform.stc,org.codehaus.groovy.classgen.asm.sc,org.codehaus.groovy.transform.traitx,gls,groovy.bugsandbugspackages andgroovy.ThisAndSuperTestpass. The Checkstyle and CodeNarc gates pass.