[ 
https://issues.apache.org/jira/browse/GROOVY-12587?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18135448#comment-18135448
 ] 

ASF GitHub Bot commented on GROOVY-12587:
-----------------------------------------

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.
   




> STC rejects reading a static trait property through "this" inside a trait 
> method
> --------------------------------------------------------------------------------
>
>                 Key: GROOVY-12587
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12587
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 4.0.33, 5.0.8, 6.0.0
>            Reporter: Scott Murphy Heiberg
>            Priority: Major
>
> {code: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()
> {code}
> Actual, under both @CompileStatic and @TypeChecked:
> {noformat}
> [Static type checking] - Cannot find matching method 
> java.lang.Class#T__flag$get() or static method T#T__flag$get(). Please check 
> if the declared type is correct and if the method exists.
> [Static type checking] - Cannot find matching method 
> T$Trait$FieldHelper#T__flag$get(). Please check if the declared type is 
> correct and if the method exists.
> {noformat}
> The first error is for the static method, the second for the instance method. 
> The same happens for a static trait property or field of any type, for 
> compound assignments such as {{this.count += 1}}, and inside closures and 
> lambdas.
> Expected: the script compiles and the assertion passes, as it does without 
> the annotation and when the property is referenced without {{this.}}.
> Cause: the trait receiver transformer rewrites {{this.flag}} with a 
> placeholder non-static field instead of the trait's declared field, so the 
> accessor call targets the field helper (instance method) or the Class 
> receiver (static method), without the dynamic-dispatch marker that static 
> trait fields get.
> Seen in 4.0.33, 5.0.8, 6.0.0 and master.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to