codeconsole opened a new pull request, #3331:
URL: https://github.com/apache/groovy/pull/3331
In an inner class `In` of `Box`, where `Box` has `getAt` and `putAt` and
`In` has neither, `other[2]` for another `In` reads through `Box.getAt` on
`other`'s enclosing instance in dynamic Groovy and under `@TypeChecked`. The
type checker accepts it under `@CompileStatic` too, but code generation found
no `getAt` on `In` and failed with `GroovyBugError` in class generation, for
`other[2]` and for the read in `other[2] += v`, `other[2]++` and `other[2] ?=
v`.
```groovy
class Box {
String id
Box(String id) { this.id = id }
String getAt(int i) { 'g' + id + i }
void putAt(int i, String v) { id = 'put(' + v + ')' }
@groovy.transform.CompileStatic
class In { def t(In other) { other[2] += 'w' } } // BUG! exception in
phase 'class generation'
Object in() { new In() }
}
```
The subscript writer now follows the receiver's `this$0` chain to the first
enclosing instance with a `getAt` and calls it there, through a closure's this
object where the class was created in a closure, as `Outer.this` does. A safe
subscript stays null-safe along the chain. A plain write, `other[2] = v`,
already worked, because static compilation calls `putAt` dynamically. A class
without an enclosing instance is unchanged.
GROOVY-12820 also describes an inner subclass whose outer class declares
private `getAt` and `putAt`, where the compound read uses `getAt` on `other`
itself. On master, static compilation treats that private method as a member of
`other` for `other.getAt(2)` as well, so it is consistent there; #3050 changes
that rule and covers the subscript read.
## Tests
`bugs.Groovy12820` runs one script dynamically, type checked and statically
compiled: a read, a safe read with a receiver and with `null`, `this[2]`, a
class nested two levels, an anonymous class created in a closure, and the three
compound forms. Without the change, the statically compiled run fails with the
`GroovyBugError`. The 18,620 tests in the `groovy`, `gls`, `bugs`,
`org.codehaus.groovy.classgen`, `transform`, `runtime`, `reflection`,
`vmplugin`, `control` and `ast` packages and the stub generator tests pass.
`groovy-contracts`, `groovy-typecheckers`, `groovy-ginq`,
`groovy-macro-library`, `groovy-json`, `groovy-xml` and `groovy-templates` pass
their tests, and every subproject compiles its main and test sources. 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]