codeconsole opened a new pull request, #3137:
URL: https://github.com/apache/groovy/pull/3137
Under `@TypeChecked`, property access to a private instance field of a
superclass, made through a subclass that is nested in that superclass, compiled
and then failed at run time:
```groovy
class Base {
private String z = 'x'
@groovy.transform.TypeChecked
static class Sub extends Base {
def m() { this.z }
}
}
new Base.Sub().m() // MissingPropertyException: No such property: z for
class: Base$Sub
```
The same happened for `z`, `super.z`, closure and lambda forms, writes,
`that.z`, and access from another nested class of `Base`, in 4.0.33, 5.0.8 and
6.0.0 too. The type checker admitted the access by the nest-mate rule that
static compilation relies on. Type-checked code is dispatched dynamically,
though, and the metaclass of a subclass does not list a private field of its
superclass, so the access could never succeed.
`StaticTypeCheckingVisitor.isFieldAccessible` now rejects such access when
the code is not statically compiled. The rule applies when the field is a
private instance field, the access goes through `super` or a subclass receiver,
and the current class is neither the declaring class nor an inner class whose
chain of enclosing instances leads to it. The error is the one the checker
already gives when `Base` and `Sub` are siblings nested in another class: "No
such property: z for class: Base$Sub". `@CompileStatic` still accesses the
field directly. A non-static inner subclass still compiles, since dynamic
lookup falls back to its enclosing instance. Attribute access (`.@z`) and
access from the declaring class are unchanged.
## Tests
`FieldsAndPropertiesSTCTest.testPrivateFieldOfSuperclassViaStaticNestedSubclass`
fails without the change.
`FieldsAndPropertiesSTCTest.testPrivateFieldOfSuperclassViaInnerSubclassOrDeclaringClass`
and the `FieldsAndPropertiesStaticCompileTest` override cover the shapes that
must keep compiling under type checking and static compilation. The bare `z`
forms are left out of the static compilation test until #3127 (GROOVY-12600)
fixes their VerifyError. `groovy.transform.stc.*`,
`org.codehaus.groovy.classgen.asm.sc.*`, `gls.*`,
`org.codehaus.groovy.transform.traitx.*`, `groovy.ThisAndSuperTest` and
`bugs.*` 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]