Skip to content

GROOVY-12820: SC: a subscript on an inner class instance without getAt uses the getAt of its enclosing instance - #3331

Open
codeconsole wants to merge 2 commits into
apache:masterfrom
codeconsole:sc-compound-subscript-enclosing-instance
Open

codeconsole wants to merge 2 commits into
apache:masterfrom
codeconsole:sc-compound-subscript-enclosing-instance

Conversation

@codeconsole

Copy link
Copy Markdown
Contributor

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.

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.

…ript of an inner class instance without one

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, and the
type checker accepts it under @CompileStatic too. Code generation then
found no getAt on In and failed with GroovyBugError in class
generation, for other[2] and for the reads in other[2] += v, other[2]++
and other[2] ?= v. A plain write, other[2] = v, already worked, because
static compilation calls putAt dynamically.

The subscript writer now follows the receiver's this$0 chain, through a
closure's this object where the class was created in a closure, to the
first enclosing instance with a getAt, and calls it there; a safe
subscript stays null-safe along the chain. A class without an enclosing
instance is unchanged.
@codecov-commenter

codecov-commenter commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 65.21739% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.9507%. Comparing base (09e84fa) to head (6bcdff1).
⚠️ Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
...ovy/classgen/asm/sc/StaticTypesCallSiteWriter.java 65.2174% 5 Missing and 3 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##               master      #3331        +/-   ##
==================================================
- Coverage     74.9518%   74.9507%   -0.0011%     
- Complexity      48908      48933        +25     
==================================================
  Files            1768       1768                
  Lines          161213     161278        +65     
  Branches        31728      31747        +19     
==================================================
+ Hits           120832     120879        +47     
- Misses          28968      28979        +11     
- Partials        11413      11420         +7     
Files with missing lines Coverage Δ
...ovy/classgen/asm/sc/StaticTypesCallSiteWriter.java 76.2976% <65.2174%> (-0.2790%) ⬇️

... and 11 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@testlens-app

testlens-app Bot commented Nov 23, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 6bcdff1
▶️ Tests: 145284 executed
⚪️ Checks: 26/26 completed


Learn more about TestLens at testlens.app/docs.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants