codeconsole opened a new pull request, #3123:
URL: https://github.com/apache/groovy/pull/3123

   In trait code, reading a static accessor of the trait with `this.` is 
rejected by the type checker:
   
   ```groovy
   @groovy.transform.TypeChecked // or @CompileStatic
   trait T {
       private static boolean flag = true
       static boolean isReady() { flag }
       boolean check() { this.ready }
   }
   class C implements T {}
   assert new C().check()
   ```
   ```
   [Static type checking] - static method of interface T can only be accessed 
with class qualifier
   ```
   
   Dynamic Groovy runs this, and `this.isReady()` compiles. 6.0.0 reports the 
same error. The trait transform turns `this.ready` into `$self.ready` (or 
`$static$self.ready` in a static method), whose receiver has the trait type, or 
the type `Class` of a sub-trait. `checkInterfaceStaticCall` treated that 
receiver as an instance (or the wrong class) qualifying a static interface 
method. The implicit form `ready` hit the same check, but its error was dropped 
because the generated expression has no source position.
   
   `checkInterfaceStaticCall` now skips the check when the method is declared 
by a trait and the receiver is the trait helper's `$self` or `$static$self` 
parameter. A variable of trait type (`T t; t.ready`) is still rejected.
   
   This PR is stacked on #3115 (GROOVY-12590). The type checking change does 
not depend on it, but under `@CompileStatic` an accessor that reads trait state 
is only called with the implementing class once #3115 is in, and the tests 
check that.
   
   ## Tests
   
   `TraitASTTransformationTest.testThisStaticAccessorOfTrait` and 
`testThisStaticAccessorOfTrait2` fail without the change. They cover getters, 
is-getters and setters through `this.`, closures, lambdas, static methods of a 
sub-trait, and per-class trait state, in dynamic, `@TypeChecked` and 
`@CompileStatic` mode. `testStaticAccessorOfTraitThroughInstance` checks that 
access through another instance is still an error. `groovy.transform.stc.*`, 
`org.codehaus.groovy.classgen.asm.sc.*`, 
`org.codehaus.groovy.transform.traitx.*`, `bugs.*`, `groovy.bugs.*`, `gls.*` 
and `groovy.ThisAndSuperTest` pass. The Checkstyle and CodeNarc gates pass.
   


-- 
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]

Reply via email to