[
https://issues.apache.org/jira/browse/GROOVY-12626?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18135675#comment-18135675
]
ASF GitHub Bot commented on GROOVY-12626:
-----------------------------------------
codeconsole opened a new pull request, #3152:
URL: https://github.com/apache/groovy/pull/3152
Under `@CompileStatic`, a bare private field name in an enum constant body
fails verification:
```groovy
@groovy.transform.CompileStatic
enum E { A { def n() { z } }; private String z = 'x' }
assert E.A.n() == 'x' // java.lang.VerifyError: Bad type on operand stack
```
The same happens for `this.z`, writes, compound assignments, closures,
lambdas and anonymous classes in the constant body, and for an anonymous
subclass created in a static method of the declaring class. 5.0.8 and 6.0.0
fail the same way.
The static compiler rewrote the access as `E.this.z`, because
`StaticCompilationTransformer.hasEnclosingInstanceOf` treated every non-static
inner class as having an enclosing instance. An enum constant body is created
by the enum's static initializer, and an anonymous class in a static method by
that method, so neither has one. Both now count as having no enclosing
instance, using `ClassNodeUtils.hasNoEnclosingInstance` plus a check for
`EnumConstantClassNode`. The field is then accessed through `this`, as for a
static nested subclass (GROOVY-12533). An anonymous class inside the constant
body goes through the constant body's instance. Dynamic Groovy and
`@TypeChecked` are unchanged.
This PR is stacked on #3127 (GROOVY-12600), whose `findEnclosingSubclassOf`
it also uses. #3147 (GROOVY-12621) makes `hasNoEnclosingInstance` return true
for an enum constant body; once both are in, the explicit
`EnumConstantClassNode` check here is redundant.
## Tests
`FieldsAndPropertiesStaticCompileTest.testPrivateFieldOfEnumByBareNameInConstantBody`
and `testPrivateFieldOfOuterClassInAnonymousSubclassInStaticMethod` fail
without the change. `groovy.transform.stc.*`,
`org.codehaus.groovy.classgen.asm.sc.*`, `gls.*`, `groovy.ThisAndSuperTest` and
`org.codehaus.groovy.transform.traitx.*` pass. The Checkstyle and CodeNarc
gates pass.
> SC: a bare private field name in an enum constant body, or in an anonymous
> subclass created in a static method, fails with VerifyError
> --------------------------------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12626
> URL: https://issues.apache.org/jira/browse/GROOVY-12626
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 5.0.8, 6.0.0
> Reporter: Scott Murphy Heiberg
> Priority: Major
>
> {code:groovy}
> @groovy.transform.CompileStatic
> enum E { A { def n() { z } }; private String z = 'x' }
> assert E.A.n() == 'x'
> {code}
> Actual: {{java.lang.VerifyError: Bad type on operand stack}}. The same
> happens for this.z, assignment, compound assignment, a closure, a lambda or
> an anonymous class in the constant body. It also happens for an anonymous
> subclass created in a static method of the declaring class:
> {code:groovy}
> @groovy.transform.CompileStatic
> class B { private String z = 'x'; static B make() { new B() { String
> toString() { z } } } }
> assert B.make().toString() == 'x'
> {code}
> Expected: the field the class inherits is read and written; both scripts pass.
> Cause: the static compiler rewrites the field access through the enclosing E
> instance because it treats the constant body (and the anonymous class) as
> having an enclosing instance of the declaring class. Neither has one: the
> constant body is created by the enum's static initializer, and the anonymous
> class by a static method.
> 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)