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

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

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.
   




> SC: a private field of the outer class referenced by its bare name in a 
> static nested subclass throws VerifyError ("not assignable to 
> groovy/lang/Closure")
> -----------------------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12600
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12600
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 5.0.8, 6.0.0
>            Reporter: Scott Murphy Heiberg
>            Priority: Major
>
> {code:groovy}
> class Base {
>     private String z = 'x'
>     @groovy.transform.CompileStatic
>     static class Sub extends Base {
>         def m() { z }
>     }
> }
> assert new Base.Sub().m() == 'x'
> {code}
> Actual: the class fails verification when loaded:
> {noformat}
> java.lang.VerifyError: Bad type on operand stack
>   Location: Base$Sub.m()Ljava/lang/Object; @1: invokevirtual
>   Reason: Type 'Base$Sub' (current frame, stack[0]) is not assignable to 
> 'groovy/lang/Closure'
> {noformat}
> The same happens for assignment, compound assignment and increment of the 
> bare name, inside closures and lambdas, in an anonymous class within the 
> subclass, and for a non-static inner subclass of a static nested class.
> Expected: {{m()}} returns 'x', the field the subclass inherits, as {{this.z}} 
> and {{super.z}} already do under static compilation.
> Cause: the type checker resolves the name to the inherited field as a 
> nestmate private access, but the static compilation transformer always 
> rewrites it to {{Base.this.@z}}. The subclass has no enclosing Base instance, 
> so the class generator falls back to the closure path and emits 
> {{Closure.getThisObject()}} on a non-closure.
> Seen in 5.0.8 and 6.0.0. 4.0.33 compiles it to a dynamic access that throws 
> MissingPropertyException.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)
  • [jira] [Commented] (GROOVY-1... ASF GitHub Bot (Jira) via notifications

Reply via email to