Repository navigation
GROOVY-12514, GROOVY-12517: qualified this and super in nested classes under static type checking and compilation - #3049
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #3049 +/- ##
==================================================
+ Coverage 74.3343% 74.3489% +0.0146%
- Complexity 44299 44375 +76
==================================================
Files 1699 1699
Lines 150855 150980 +125
Branches 28754 28801 +47
==================================================
+ Hits 112137 112252 +115
+ Misses 28441 28436 -5
- Partials 10277 10292 +15
🚀 New features to boost your workflow:
|
…ed class N.this inside a nested class N is this, as in Java. The type checker rejected it with "No such property: this for Class or static property for class: Box$N", under @TypeChecked and @CompileStatic, for a static nested, inner or enum class, and also inside a closure or lambda there; dynamic Groovy ran it, and javac accepts it. The type checker now types N.this inside N, outside a static context, as N. In a static context ResolveVisitor still reports its error. Top.this in a top-level class keeps its error in every mode (see StaticThisTest, GROOVY-4321).
…ling the same super method Under @CompileStatic a call to super.m() in a closure or lambda calls a generated method super$2$m of the enclosing class. The type checker records the target for every such call, and StaticCompilationMopWriter generated one method per record, so two closures or lambdas calling the same super method produced the method twice and the class failed to load with "ClassFormatError: Duplicate method name super$2$m". Dynamic Groovy and @TypeChecked run the same code. The writer now generates one method per name and descriptor; overloads of the same name keep one method each.
…lass N.super inside a nested class N is super, as in Java (JLS 15.11.2, 15.12.1). Under @TypeChecked and @CompileStatic, N.super.m() and N.super.p failed with "No such property: super for Class or static property for class: Box$N". Dynamic Groovy ran N.super.m(), but read N.super.p and N.super.q as N.this.p and N.this.q, the subclass's properties; javac reads the superclass's. ResolveVisitor now replaces N.super inside a nested class N with super, so every mode gives it the meaning of super: a call or property of the superclass, also inside a closure or lambda, for static nested, inner and enum classes. Under @CompileStatic a closure or lambda then calls the superclass method through the super$ helper (see GROOVY-12516). In a static context ResolveVisitor still reports its error. Top.super in a top-level class keeps its error in every mode (see StaticThisTest, GROOVY-4321), and Outer.super, naming an outer class, is not changed here. In dynamic Groovy, N.super.p inside N now reads the superclass's property instead of N.this.p.
…es, increments and compound assignments Under static compilation a property read or written through super inside a closure or lambda called the superclass accessor with invokespecial from the closure class, which fails verification. A super method call in a closure already goes through a super$ helper of the enclosing class; the type checker now asks for the same helper for the getter and setter of a super property (also for a property whose accessors are generated later), and the property access and assignment transformers call it. super.name++, ++super.name and super.name += value read the property through ScriptBytecodeAdapter.getPropertyOnSuper (and ++ also wrote it through setPropertyOnSuper), which throws MissingPropertyException when the class declares the same property. The call site writer now reads and writes such a super property through the superclass accessor, directly with invokespecial, or through the super$ helpers in a closure or lambda.
In a class nested in Outer that has an enclosing instance of Outer (an
inner class, an anonymous class, a closure or lambda in one of them, or an
inner class of an inner class), Outer.super names the superclass of that
instance (JLS 15.11.2, 15.12.1). The type checker rejected it ("No such
property: super"); it now types Outer.super as the superclass of Outer.
Where the class has no enclosing instance of Outer it stays an error.
Under static compilation, a method or property of Outer.super is called
through a super$ helper of Outer, as a super call in a closure is: the
JVM allows invokespecial on Outer's superclass only from within Outer.
Property reads and writes, increments and compound assignments use the
helpers for the accessors.
Dynamic Groovy read and wrote a property of Outer.super as a property of
Outer.this; it now goes to the superclass property, as Outer.super.m()
already went to the superclass method.
…name++ and compound assignments super.name++, ++super.name, super.name op= value and super.name ?= value wrote the property through the first one-argument setter the superclass declares, so with overloads such as setX(int) and setX(String) the value could go to the wrong one: a GroovyCastException, a character passed as an int, or, in a closure, a lambda or for Outer.super.name, the class's own property written instead. The call site writer now calls the setter the type checker selected for the value, which is also the one it asked the super$ helper for. The lookup of a property node whose accessors "are generated later" is removed: the accessors of every class of the compilation unit exist before bytecode is generated, so it only ran for a property with a non-default accessor name, and there it ignored static and private properties.
…and compound assignments super.name++, super.name op= value and their kin compiled to dynamic getPropertyOnSuper and setPropertyOnSuper calls when an accessor of the property is an extension method, such as getText() and setText(String) of a File, also in a closure, a lambda and for Outer.super.name. The call site writer now calls the extension methods directly: the setter the type checker selected for the value, and the getter it selects when the superclass declares none.
…uperclass Outer.super.x = v, ++ and compound assignments choose among the setters of the superclass, as super.x = v does since GROOVY-12521, in dynamic Groovy, under @TypeChecked and under @CompileStatic.
cd35874 to
cdb685d
Compare
daniellansun
left a comment
There was a problem hiding this comment.
Thanks for the careful multi-ticket stack on qualified this/super — tip cdb685d.
The shape is right: ResolveVisitor rewrites N.super to super (GROOVY-12514) after the existing Class.this/Class.super checks; STC recognizes N.this and types Outer.super as the enclosing type’s superclass (GROOVY-12517); SC sends Outer.super (and super-from-closure) through Outer’s super$ helpers, with InvocationWriter’s existing Outer.this rewrite for the receiver; StaticCompilationMopWriter dedupes super$ by name+descriptor (GROOVY-12516); and the 12518 property / ++ / += / overloaded-setter / extension-accessor paths are backed by strong parameterized tests. CI is green on this tip.
A few items before I would approve:
-
COMPATIBILITY.md for the dynamic meaning change
N.super.p and Outer.super.p / Outer.super.f used to read the subclass / enclosing instance’s own member in dynamic Groovy; they now follow javac (superclass member). That is the same class of silent change we documented for GROOVY-12521. The PR description is clear; please add a short COMPATIBILITY section (Who is affected) before merge — also needed if we cherry-pick to GROOVY_6_0_X. -
Outer.super.@field asymmetry
visitPropertyExpression now uses isSuperOrOuterSuper → get/setPropertyOnSuper, but visitAttributeExpression still keys only on isSuperExpression. Outer.super.@q therefore still hits Outer’s own field. Please mirror the property fix (and test Outer.super.@x / N.super.@x), or file a follow-up JIRA and mention it explicitly so we do not leave the @ form silently wrong. makeSuperPropertySite’s AttributeExpression skip is the SC counterpart of the same gap. -
Landing next to #3071 (GROOVY-12526)
Complementary, not competing: #3071 rejects non-direct Iface.super; this PR enables class N.super / Outer.super. Expect a small ResolveVisitor / STC existsProperty conflict. Keep the interface check as an early path and the N.super rewrite afterward for non-interface nested classes. -
Nit: isSuperOrOuterSuper vs InvocationWriter.isOuterClassSuper
Same predicate in two places (and !isInterface() matters next to #3071). A shared helper would help; not a merge blocker. -
Cherry-pick
After COMPATIBILITY: full tip to GROOVY_6_0_X is appropriate (12516/12518 are pure fixes; 12514/12517 fix STC/SC hard failures and intentionally align dynamic Groovy with javac). Without COMPATIBILITY I would only be comfortable cherry-picking the 12516+12518 commits.
Happy to re-review quickly once COMPATIBILITY is in and the attribute path is either fixed or explicitly deferred with a ticket.
…r and the code generators
…nstance of a subclass of Outer
…r.name in COMPATIBILITY.md
|
@daniellansun thanks. New commits on top of cdb685d, after merging master:
|
|
This also fixes GROOVY-12566 (filed by @paulk-asert): |
# Conflicts: # COMPATIBILITY.md # src/test/groovy/groovy/transform/stc/ClosuresSTCTest.groovy
…ery mode and calls the method of D's superclass
|
Merged master now that #3071 (GROOVY-12526) has landed. The conflicts were textual: COMPATIBILITY.md keeps master's GROOVY-12488 section ahead of the GROOVY-12514/12517 one, and
|
…nclosing instance's private field, like this.name PropertyExpressionTransformer gave only a bare this receiver the GROOVY-12533 treatment, so Sub.this.z (in methods, closures, lambdas and assignments) read and wrote the private field z of this instance while this.z and dynamic Groovy use the enclosing instance. Treat Type.this for the class being rewritten like this.
# Conflicts: # COMPATIBILITY.md
# Conflicts: # COMPATIBILITY.md # src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
# Conflicts: # COMPATIBILITY.md
…erge The note is now in the pull request description. An entry carried on this branch conflicted with every later addition to the end of that file.
…2514 The note is now in the description of apache#3049, which this is stacked on. An entry carried on this branch conflicted with every later addition to the end of that file.
# Conflicts: # src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesBinaryExpressionMultiTypeDispatcher.java
daniellansun
left a comment
There was a problem hiding this comment.
Thanks for the careful follow-ups on tip 9b74da8d55b2. I re-checked the earlier COMMENT against the current diff.
Prior asks are addressed in the code:
- Attribute path.
visitAttributeExpressionnow usesisSuperOrOuterSuper→get/setFieldOnSuper, with SC keeping a direct field access viafallbackAttributeOrPropertySite.InnerClassTest.testQualifiedSuperFieldand the subclass-enclosing-instance coverage lock it. - Shared helper.
ExpressionUtils.isOuterSuperExpressionis shared; interfaceIface.superpaths stay on the dedicated predicates. - #3071 coexistence. Interface-super rejection remains the early path;
N.superrewrite andOuter.supertyping run afterward for non-interface nested classes. The adjustedGroovy12526expectation matches GROOVY-12517. - GROOVY-12566. SC closures/lambdas going through
super$helpers forsuper.xis a welcome extra fix.
CI is green. One merge-time reminder: COMPATIBILITY.md for the dynamic meaning change of N.super / Outer.super is again left off the tip; please restore that section when landing (needed for an honest 6.0.x cherry-pick story as discussed earlier). The optional getter/DIRECT_METHOD_CALL_TARGET hardening can stay a follow-up.
Approving the tip with that COMPATIBILITY restore-at-merge note.
…rom its closures and nested classes At run time, "Outer.super.name()" and "Outer.super.name" from a closure or a nested class reach the member of Outer's superclass through Outer's super$ MOP methods, but Outer had them only for the methods it overrides or calls on super itself. Without one, a dynamic or type checked call invoked the method reflectively, which ran the override of a subclass of Outer that has no super$ method either, such as a statically compiled one. A statically compiled nested class of a dynamic Outer failed with NoSuchMethodError, as it calls the super$ method directly. Outer now generates the super$ methods for the names its closures and nested classes reach through Outer.super, and for the super$ methods that statically compiled code calls.
daniellansun
left a comment
There was a problem hiding this comment.
Thanks Scott — I re-checked tip 3e74e8bf33a3 against my earlier approve of 9b74da8d55b2. The unique commit is the new MopWriter visitor that generates Outer's super$ methods for Outer.super calls from its closures and nested classes.
The helpers are generated on Outer, not on the nested class or the closure, as public synthetic methods that INVOKESPECIAL the superclass — the same shape as the existing MOP methods. The visitor collects the method names, and the get/is/set accessors of the properties, that those other classes reach through Type.super for this class, and the names of super$ helpers already targeted at it, and feeds them into the existing orThis filter. StaticCompilationMopWriter is unchanged: it still emits only SUPER_MOP_METHOD_REQUIRED, which is what a fully statically compiled Outer already records. The new walk is on the dynamic MopWriter path, which is the case that needs it: a dynamic Outer whose statically compiled inner class calls Outer.super, or whose enclosing instance is a statically compiled subclass with no super$ methods of its own.
testQualifiedSuperOfOuterClassWithInstanceOfStaticallyCompiledSubclass and testQualifiedSuperOfDynamicOuterClassFromStaticallyCompiledInnerClass cover those shapes, including property writes, and they assert the members of Base rather than of Sub. CI is green on this tip.
One reminder, unchanged from last time: COMPATIBILITY.md for the dynamic meaning change of N.super / Outer.super is still off this tip and should be restored so a 6.0.x cherry-pick has an honest compatibility note. #3118 has a slightly broader sibling of the same visitor that also covers bare super in closures; if both are applied, that version should be the one that remains.
Approving the tip with that COMPATIBILITY reminder.
|
Merged. Thanks! |
🚨 Jobs failed without executing tests 🚨Failed Jobs without Test Failures❌ jmh / summary 🏷️ Commit: 7e85d89 Rerun ControlsClick the checkbox to trigger a rerun: 🔲 Rerun jobs Learn more about TestLens at testlens.app/docs. |
…ass in a class nested in it (#3114) * GROOVY-12514: STC: allow a qualified this that names the current nested class N.this inside a nested class N is this, as in Java. The type checker rejected it with "No such property: this for Class or static property for class: Box$N", under @TypeChecked and @CompileStatic, for a static nested, inner or enum class, and also inside a closure or lambda there; dynamic Groovy ran it, and javac accepts it. The type checker now types N.this inside N, outside a static context, as N. In a static context ResolveVisitor still reports its error. Top.this in a top-level class keeps its error in every mode (see StaticThisTest, GROOVY-4321). * GROOVY-12516: SC: generate one super$ method for several closures calling the same super method Under @CompileStatic a call to super.m() in a closure or lambda calls a generated method super$2$m of the enclosing class. The type checker records the target for every such call, and StaticCompilationMopWriter generated one method per record, so two closures or lambdas calling the same super method produced the method twice and the class failed to load with "ClassFormatError: Duplicate method name super$2$m". Dynamic Groovy and @TypeChecked run the same code. The writer now generates one method per name and descriptor; overloads of the same name keep one method each. * GROOVY-12514: allow a qualified super that names the current nested class N.super inside a nested class N is super, as in Java (JLS 15.11.2, 15.12.1). Under @TypeChecked and @CompileStatic, N.super.m() and N.super.p failed with "No such property: super for Class or static property for class: Box$N". Dynamic Groovy ran N.super.m(), but read N.super.p and N.super.q as N.this.p and N.this.q, the subclass's properties; javac reads the superclass's. ResolveVisitor now replaces N.super inside a nested class N with super, so every mode gives it the meaning of super: a call or property of the superclass, also inside a closure or lambda, for static nested, inner and enum classes. Under @CompileStatic a closure or lambda then calls the superclass method through the super$ helper (see GROOVY-12516). In a static context ResolveVisitor still reports its error. Top.super in a top-level class keeps its error in every mode (see StaticThisTest, GROOVY-4321), and Outer.super, naming an outer class, is not changed here. In dynamic Groovy, N.super.p inside N now reads the superclass's property instead of N.this.p. * GROOVY-12518: SC: access a super property through accessors in closures, increments and compound assignments Under static compilation a property read or written through super inside a closure or lambda called the superclass accessor with invokespecial from the closure class, which fails verification. A super method call in a closure already goes through a super$ helper of the enclosing class; the type checker now asks for the same helper for the getter and setter of a super property (also for a property whose accessors are generated later), and the property access and assignment transformers call it. super.name++, ++super.name and super.name += value read the property through ScriptBytecodeAdapter.getPropertyOnSuper (and ++ also wrote it through setPropertyOnSuper), which throws MissingPropertyException when the class declares the same property. The call site writer now reads and writes such a super property through the superclass accessor, directly with invokespecial, or through the super$ helpers in a closure or lambda. * GROOVY-12517: allow a qualified super that names an enclosing class In a class nested in Outer that has an enclosing instance of Outer (an inner class, an anonymous class, a closure or lambda in one of them, or an inner class of an inner class), Outer.super names the superclass of that instance (JLS 15.11.2, 15.12.1). The type checker rejected it ("No such property: super"); it now types Outer.super as the superclass of Outer. Where the class has no enclosing instance of Outer it stays an error. Under static compilation, a method or property of Outer.super is called through a super$ helper of Outer, as a super call in a closure is: the JVM allows invokespecial on Outer's superclass only from within Outer. Property reads and writes, increments and compound assignments use the helpers for the accessors. Dynamic Groovy read and wrote a property of Outer.super as a property of Outer.this; it now goes to the superclass property, as Outer.super.m() already went to the superclass method. * GROOVY-12518: SC: use the setter the type checker selected for super.name++ and compound assignments super.name++, ++super.name, super.name op= value and super.name ?= value wrote the property through the first one-argument setter the superclass declares, so with overloads such as setX(int) and setX(String) the value could go to the wrong one: a GroovyCastException, a character passed as an int, or, in a closure, a lambda or for Outer.super.name, the class's own property written instead. The call site writer now calls the setter the type checker selected for the value, which is also the one it asked the super$ helper for. The lookup of a property node whose accessors "are generated later" is removed: the accessors of every class of the compilation unit exist before bytecode is generated, so it only ran for a property with a non-default accessor name, and there it ignored static and private properties. * GROOVY-12518: SC: call extension accessors directly for super.name++ and compound assignments super.name++, super.name op= value and their kin compiled to dynamic getPropertyOnSuper and setPropertyOnSuper calls when an accessor of the property is an extension method, such as getText() and setText(String) of a File, also in a closure, a lambda and for Outer.super.name. The call site writer now calls the extension methods directly: the setter the type checker selected for the value, and the getter it selects when the superclass declares none. * GROOVY-12517: test Outer.super.x = v with overloaded setters of the superclass Outer.super.x = v, ++ and compound assignments choose among the setters of the superclass, as super.x = v does since GROOVY-12521, in dynamic Groovy, under @TypeChecked and under @CompileStatic. * GROOVY-12517: share one check for Outer.super between the type checker and the code generators * GROOVY-12517: read and write Outer.super.@name as the field of the superclass * GROOVY-12517: resolve Outer.super against Outer's superclass for an instance of a subclass of Outer * GROOVY-12517: describe the new meaning of N.super.name and Outer.super.name in COMPATIBILITY.md * GROOVY-12589: STC: resolve super.name to the accessors of the superclass in a class nested in it Within a class nested in its superclass, the type checker resolved "super.name" as the superclass field the nested class may access, so it recorded no accessor and asked for no super$ helper. Static compilation then read and wrote the property dynamically from a closure or lambda, which picks the wrong accessor or fails for a class without MOP methods, or called the accessor with invokespecial from the closure class. The type checker now resolves "super.name" and "Outer.super.name" through the accessors of the superclass, as the code generators and dynamic Groovy do: "isName" before "getName" for a boolean property. * GROOVY-12566: run the super property tests with closures statically compiled too * GROOVY-12517: D.super.value() from an inner class of D compiles in every mode and calls the method of D's superclass * GROOVY-12514: SC: Type.this.name within an inner subclass reads the enclosing instance's private field, like this.name PropertyExpressionTransformer gave only a bare this receiver the GROOVY-12533 treatment, so Sub.this.z (in methods, closures, lambdas and assignments) read and wrote the private field z of this instance while this.z and dynamic Groovy use the enclosing instance. Treat Type.this for the class being rewritten like this. * GROOVY-12514, GROOVY-12517: leave the COMPATIBILITY.md entry to the merge The note is now in the pull request description. An entry carried on this branch conflicted with every later addition to the end of that file. * GROOVY-12589: drop the COMPATIBILITY.md entry inherited from GROOVY-12514 The note is now in the description of #3049, which this is stacked on. An entry carried on this branch conflicted with every later addition to the end of that file. * GROOVY-12517: generate Outer's super$ methods for Outer.super calls from its closures and nested classes At run time, "Outer.super.name()" and "Outer.super.name" from a closure or a nested class reach the member of Outer's superclass through Outer's super$ MOP methods, but Outer had them only for the methods it overrides or calls on super itself. Without one, a dynamic or type checked call invoked the method reflectively, which ran the override of a subclass of Outer that has no super$ method either, such as a statically compiled one. A statically compiled nested class of a dynamic Outer failed with NoSuchMethodError, as it calls the super$ method directly. Outer now generates the super$ methods for the names its closures and nested classes reach through Outer.super, and for the super$ methods that statically compiled code calls. * GROOVY-12589: test the isser and getter of a super property from a class nested in the superclass The test of a boolean property with both "isName" and "getName" used a top-level subclass, where the type checker never sees the field of the superclass as accessible and so never reached the change. The subclass is now nested in its superclass, and an inner class of it also reads "Sub.super.name", so the statically compiled test fails without it. * GROOVY-12517: test Outer.super calls of a static method from an inner class and its closures and lambdas A static target needs no super$ helper of Outer: the call is an invokestatic, valid from any class. Statically compiled, Outer.super.sm() went through a helper that called Base.sm() with invokespecial (IncompatibleClassChangeError), and Outer.super.first() called ArrayList.first() (NoSuchMethodError), until the merge of master gave checkSuperCallFromClosure the GROOVY-12519 check for a static target ahead of the Outer.super case.
This makes qualified
thisandsuperin nested classes (N.this,N.super,Outer.super) work under@TypeCheckedand@CompileStaticas in Java.Outer.superfrom a closure or an inner class goes through thesuper$helper methods that statically compiled closures use forsupercalls, so the two bugs of those helpers that it runs into are fixed first: GROOVY-12516 and GROOVY-12518. Each issue has its own commit (GROOVY-12514 one forthisand one forsuper, GROOVY-12518 two more, for overloaded setters and for extension accessors).Outer.superwithout an enclosing instance, in a static nested class or in an anonymous class in a static method, is a compile error in every mode since GROOVY-12512; the support forOuter.superhere relies on that check.Outer.super.x = v,++and compound assignments with overloaded setters of the superclass choose the setter assuper.x = vdoes since GROOVY-12521;InnerClassTest.testQualifiedSuperOfOuterClassWithOverloadedSetterscovers it in all three modes. Three cases combine this with other open pull requests, and the second of each pair to be merged adds or changes their tests:Outer.super.sm()for a static method thatOuterhides needs GROOVY-12519 (GROOVY-12519: resolve super.name() and super.name to a static member the class hides #3045), assuper.sm()does;super.text += vunder@TypeChecked, where the accessors are extension methods that the class overrides, needs GROOVY-12522 (GROOVY-12522: super.name() and super.name find an extension method that the class overrides #3048);Groovy12526.testEnclosingClassSuperIsOutsideTheInterfaceCheckthatD.super.value()from an inner class ofDis "No such property: super"; with this PR it compiles and returns'C', so that test changes in whichever is merged second.GROOVY-12514:
N.thisandN.superwithin a nested classNN.this, which isthis, failed under@TypeCheckedand@CompileStaticwith "No such property: this", andN.super.m()andN.super.pwith "No such property: super", in static nested, inner and enum classes and in closures and lambdas there. Both now compile, as in javac.N.supermeanssuperin every mode: dynamic Groovy readN.super.pasN.this.p. In a static context they remain errors.Top.thisandTop.superin a top-level class keep their error (GROOVY-4321,StaticThisTest).GROOVY-12517:
Outer.superin a class nested inOuterIn an inner class, an anonymous class, a closure or lambda in one of them, or an inner class of an inner class,
Outer.super.m()andOuter.super.pfailed under@TypeCheckedand@CompileStaticwith "No such property: super". They now call the superclass's member on the enclosingOuterinstance, as in javac. Under@CompileStaticthe calls go throughsuper$helper methods ofOuter, since onlyOutermay call its superclass's methods non-virtually. Property reads and writes,++and compound assignments work too. Dynamic Groovy read and wroteOuter.super.pandOuter.super.@fasOuter.this.pandOuter.this.@f; they now use the superclass's property and field, also when the enclosing instance is of a subclass ofOuter. Without an enclosing instance ofOuterit stays an error.In dynamic and
@TypeCheckedcode these calls reach the superclass throughOuter'ssuper$MOP methods, soOuternow also generates them for the members its closures and nested classes reach throughOuter.super, and for thesuper$methods that statically compiled code calls. Without them, an instance of a@CompileStaticsubclass that overridesmgot its ownmfromOuter.super.m(), and a dynamicOuterwith a@CompileStaticinner class failed withNoSuchMethodError: super$2$m.InnerClassTest.testQualifiedSuperOfOuterClassWithInstanceOfStaticallyCompiledSubclassandtestQualifiedSuperOfDynamicOuterClassFromStaticallyCompiledInnerClasscover both.GROOVY-12516 and GROOVY-12518:
superin closures under@CompileStaticsuper$helper twice, and the class failed to load withClassFormatError. The helper is generated once.super.p,super.nameandsuper.name = vin a closure or lambda failed withVerifyError(the superclass accessor was called withinvokespecialfrom the closure class); they now go throughsuper$helpers, like super method calls.super.n++,++super.nandsuper.n += vread the property through a dynamic call that failed withMissingPropertyExceptionwhen the class declares the same property; they now call the superclass's accessors. When the superclass overloads the setter (setX(int)andsetX(String)), the setter is the one the type checker selected for the value. When an accessor is an extension method, such asgetText()andsetText(String)of aFile,super.text += vcompiled to a dynamic property access; it now calls the extension methods directly.Compatibility
Within a nested class
N,N.superis nowsuper. Within a class nested inOuter,Outer.superis the enclosing instance ofOuterseen as an instance ofOuter's superclass, as in Java. In dynamic Groovy, a property read or write through either —N.super.p,Outer.super.p = v,Outer.super.p++— and a field access —N.super.@f,Outer.super.@f = v— used to reach the instance's own member: the oneNorOuterdeclares, or, for an enclosing instance of a subclass ofOuter, the subclass's. It now reaches the superclass's property, accessor or field, assuper.pandsuper.@fdo.Outer.super.m()already called the superclass's method, except for an enclosing instance of a subclass ofOuter, where, since GROOVY-11611, it called the method thatsuper.m()in the subclass would call (Outer's, ifOuteroverridesm); it now calls the method ofOuter's superclass. Under@TypeCheckedand@CompileStatic, these property and field forms failed to compile ("No such property: super") and now compile with the same meaning.Who is affected. Dynamic code that reads or writes a property or field through
N.superorOuter.superwhereNorOuter, or the class of the enclosing instance, declares or overrides that member. The access now reaches the superclass's member; usethis.p,N.this.porOuter.this.pwhere the class's own member is intended. Classes compiled by an earlier release keep the old behaviour until they are recompiled.What is unchanged. Unqualified
super,Outer.this,Iface.super.m()for interface default methods, method calls throughN.superand throughOuter.superon an instance ofOuteritself, and members that no class below the superclass declares.ExpressionUtils.isOuterSuperExpressionis new; there are no other API changes.Code that failed to compile and now compiles: GROOVY-12514 and GROOVY-12517. Code that failed at run time and now runs under
@CompileStatic: GROOVY-12516 and GROOVY-12518.After merging master (GROOVY-12533, where
this.namein an inner subclass reads the enclosing instance's private field), static compilation still treatedType.this.namedifferently fromthis.namewhenTypeis the current inner subclass: withzprivate inBaseand an innerSub extends Base,Sub.this.zandSub.this.z = v(also in closures and lambdas) usedSub's own instance under@CompileStatic, whilethis.zand dynamic Groovy use the enclosingBaseinstance.PropertyExpressionTransformernow givesType.thisfor the class being compiled the same handling as a barethis.InnerClassTest.testQualifiedThisOfCurrentInnerSubclasscovers reads, writes, closures, lambdas, public fields and properties in dynamic,@TypeCheckedand@CompileStaticmodes.Tests
Each commit has tests that fail without it, marked with its JIRA key:
gls.innerClass.InnerClassTest,ClosuresSTCTest(andClosuresStaticCompileTest),FieldsAndPropertiesSTCTest(andFieldsAndPropertiesStaticCompileTest) and, for the overloaded setters and the extension accessors,FieldsAndPropertiesStaticCompileTestalone. 13,655 tests in thegroovy,gls,bugs,org.codehaus.groovy.classgen,transformandcontrolpackages pass, apart frombugs.Groovy12363, which fails on master too when it runs afterbugs.Groovy12142in the same JVM. The Checkstyle and CodeNarc gates pass.Backport
All commits cherry-pick cleanly onto GROOVY_6_0_X, and the inner-class, closure and fields-and-properties tests pass there. The compatibility note for the changed dynamic meaning of
N.superandOuter.superthen belongs with the 6.0.1 entries, as for GROOVY-12521.