codeconsole opened a new pull request, #3112:
URL: https://github.com/apache/groovy/pull/3112
Inside a trait method, reading a static trait property through an explicit
`this` fails static type checking, while the unqualified reference and dynamic
Groovy work:
```groovy
@groovy.transform.CompileStatic
trait T {
static boolean flag = true
static boolean sm() { this.flag }
boolean im() { this.flag }
}
class C implements T {}
assert C.sm() && new C().im()
```
```
[Static type checking] - Cannot find matching method
java.lang.Class#T__flag$get() or static method T#T__flag$get().
[Static type checking] - Cannot find matching method
T$Trait$FieldHelper#T__flag$get().
```
`TraitReceiverTransformer` rewrote `this.<field>` with a placeholder
`FieldNode` that is always non-static, so the accessor was called on the field
helper (instance method; static accessors are on the static field helper) or on
the `Class` receiver (static method), without the `DO_DYNAMIC` marker that
static trait fields get. It now uses the trait's declared field, as the
unqualified reference does: a static property is accessed through the
implementing class, and the read is typed by the field's declared type. This
also covers compound assignments such as `this.count += 1`, and `this.x` inside
closures and lambdas.
## Tests
`TraitASTTransformationTest.testTraitStaticPropertyThroughThis` fails
without the change under `@TypeChecked` and `@CompileStatic`; it covers reads
and writes from static and instance methods, compound assignments, a
non-boolean property, per-implementer copies, and closure and lambda access.
The `org.codehaus.groovy.transform.traitx`, `groovy.transform.stc`,
`org.codehaus.groovy.classgen.asm.sc` and `gls` packages and
`groovy.ThisAndSuperTest` pass. The Checkstyle and CodeNarc gates pass.
Seen in 4.0.33, 5.0.8 and 6.0.0.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]