[
https://issues.apache.org/jira/browse/GROOVY-12820?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18149420#comment-18149420
]
ASF GitHub Bot commented on GROOVY-12820:
-----------------------------------------
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.
> SC: compound assignment to a subscript whose getAt/putAt belong to an
> enclosing instance fails to compile or reads the wrong object
> -----------------------------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12820
> URL: https://issues.apache.org/jira/browse/GROOVY-12820
> Project: Groovy
> Issue Type: Bug
> Reporter: Scott Murphy Heiberg
> Priority: Major
>
> On master (09e84fa), JDK 25.0.4:
> {code: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' } }
> Object in() { new In() }
> }
> def a = new Box('A'), b = new Box('B')
> a.in().t(b.in())
> assert b.id == 'put(gB2w)'
> {code}
> Dynamic Groovy and {{@TypeChecked}} call {{getAt}} and {{putAt}} on
> {{other}}'s enclosing {{Box}}. Under {{@CompileStatic}}, each of these fails
> with GroovyBugError in class generation, at
> {{StaticTypesCallSiteWriter.makeSingleArgumentCall}}:
> {code:groovy}
> other[2] += 'w'
> other[2]++
> other[2] ?= 'e'
> {code}
> With private {{getAt}}/{{putAt}} and an inner {{class Sub extends Box}}, the
> code compiles, but the read uses {{getAt}} on {{other}} itself while the
> write goes to the enclosing instance, so {{b.id}} becomes {{put(gself2w)}}
> instead of {{put(gB2w)}}.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)