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