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

   A statically compiled static nested subclass that refers to a private field 
of its outer (and super) class by bare name fails verification:
   
   ```groovy
   class Base {
       private String z = 'x'
       @groovy.transform.CompileStatic
       static class Sub extends Base {
           def m() { z }
       }
   }
   new Base.Sub().m()  // java.lang.VerifyError: ... Type 'Base$Sub' ... is not 
assignable to 'groovy/lang/Closure'
   ```
   
   The same happens for writes, compound assignments and `z++`, in closures and 
lambdas, in an anonymous class inside the subclass, and for a non-static inner 
subclass of a static nested class. 5.0.8 and 6.0.0 fail the same way. `this.z` 
and `super.z` already return the field.
   
   The type checker resolves `z` as `this.z` and marks it as a nestmate private 
field access. `VariableExpressionTransformer` then always rewrote such an 
instance field to `DeclaringClass.this.@z`. The subclass has no enclosing 
instance of the declaring class, so `AsmClassGenerator` took its closure branch 
and called `Closure.getThisObject()` on a non-closure. The transformer now 
walks the `this$0` chain from the current class. If it reaches the declaring 
class, `DeclaringClass.this.@z` is kept unchanged, so a non-static inner 
subclass still uses the enclosing instance's field, as dynamic Groovy and 
`@TypeChecked` do. Otherwise it uses the innermost instance in that chain that 
is a subclass of the declaring class: `this`, or `Sub.this` from an anonymous 
class.
   
   ## Tests
   
   `testPrivateFieldOfOuterClassInStaticNestedSubclass` and 
`testPrivateFieldOfOuterClassInInnerSubclassOfStaticNestedClass` in 
`FieldsAndPropertiesStaticCompileTest` fail without the change with the 
VerifyError. `testPrivateFieldOfOuterClassInInnerSubclass` checks that an inner 
subclass keeps using the enclosing instance. `groovy.transform.stc.*`, 
`org.codehaus.groovy.classgen.asm.sc.*`, `bugs.*`, `groovy.bugs.*`, `gls.*`, 
`groovy.ThisAndSuperTest` and `org.codehaus.groovy.transform.traitx.*` 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