Repository navigation
GROOVY-12519: resolve super.name() and super.name to a static member the class hides - #3045
Conversation
…the class hides When a class declares a static method with the signature of a static method of its superclass, the meta-class dropped the subclass method from the methods available to super calls, as it does for any method declared by the class itself, and nothing took its place: super.sm() threw MissingMethodException, or picked another overload, and super.sp threw MissingPropertyException for a static property. A super call now finds the hidden static method of the nearest superclass that has one, as javac does; the super property index is built from the same methods, so the static property accessors resolve too. Private static methods of the superclass stay out of reach. Under static compilation, super.sm() in a closure or lambda went through a super$ helper that called the static method with invokespecial (IncompatibleClassChangeError). A static target needs no helper: the call is an invokestatic, valid from any class.
… lambdas The type checker no longer asks for a super$ helper for a static target, and an extension method is a static method too. Statically compiled, super.first() in a closure or super.last() in a lambda of an ArrayList subclass called the extension method through a helper that invoked ArrayList.first() with invokespecial, and failed with NoSuchMethodError; 6.0.0 and master still do. The call is now a direct static call.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3045 +/- ##
==================================================
+ Coverage 74.5886% 74.5899% +0.0013%
Complexity 45386 45386
==================================================
Files 1707 1707
Lines 153238 153250 +12
Branches 29461 29469 +8
==================================================
+ Hits 114298 114309 +11
- Misses 28424 28426 +2
+ Partials 10516 10515 -1
🚀 New features to boost your workflow:
|
daniellansun
left a comment
There was a problem hiding this comment.
Thanks for aligning dynamic super resolution and the SC closure path with javac for hidden static members.
Observations:
-
Replacing a not-super-useful static declared on
cwithgetHiddenStaticMethodin the MOP rewrite is the right place: it feeds both call sites and the super property index without a separate property path. -
Accessibility (public / protected / package-private same package, private out of reach) matches what we want for
superstatic lookup. The private-static test is a useful guard. -
Short-circuiting
checkSuperCallFromClosurefor static targets avoids emitting a uselesssuper$helper and fixes theIncompatibleClassChangeError/ extension-methodNoSuchMethodErrorcases. Treating DGM as static here is consistent. -
The parameterized
ThisAndSuperTestcoverage (methods, properties, Mid→Base vs Low→Hider, closures/lambdas) makes the intended matrix easy to re-check.
Optional (non-blocking): a note in release notes that super.sm() / super.sp for a hidden static now succeed where they previously threw or picked another overload would help anyone who relied on the old failure. I have no blocking concerns on this tip.
|
Added the note to COMPATIBILITY.md in 838b579, after merging master in. Thanks. |
…-member # Conflicts: # src/test/groovy/groovy/ThisAndSuperTest.groovy
…-member # Conflicts: # COMPATIBILITY.md
…-member # Conflicts: # COMPATIBILITY.md
…-member # Conflicts: # COMPATIBILITY.md
…-member # Conflicts: # COMPATIBILITY.md
…-member # Conflicts: # COMPATIBILITY.md
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.
daniellansun
left a comment
There was a problem hiding this comment.
Thanks for following up on tip 380d55696410. I re-checked the earlier COMMENT.
getHiddenStaticMethod in the MOP rewrite, the accessibility rules, short-circuiting checkSuperCallFromClosure for static targets, and the parameterized ThisAndSuperTest matrix still look right. CI is green on this tip.
One merge-time reminder only: the optional COMPATIBILITY.md note for the dynamic super.sm() / super.sp behaviour change is not on the current tip (left for the merge). Please restore a short “Who is affected” entry when landing, in the same style as GROOVY-12521, so the silent success path is documented for 6.0.x / 7.x readers.
Approving the code change.
…ethod the class does not hide Under @CompileStatic this failed with IncompatibleClassChangeError before the checkSuperCallFromClosure change, hidden method or not.
In the shape of the GROOVY-12521 entry, with a "Who is affected" paragraph, and including the @CompileStatic closure and lambda case for a static method the class does not hide.
|
Thanks for the re-review. The GROOVY-12519 entry is back in COMPATIBILITY.md in 003a967, after merging master in (984527e) so it sits on the current end of the file. It follows the GROOVY-12521 layout, with a "Who is affected" paragraph. While writing it I found that the |
daniellansun
left a comment
There was a problem hiding this comment.
Thanks, Scott. I re-reviewed tip 003a967bcc78703ee3ee32464f8c36ab23ec9319 against my earlier approval on 380d55696410.
The production change is unchanged: getHiddenStaticMethod still feeds both super.sm() and super.sp, accessibility is the same (a private static method of the superclass stays out of reach), and checkSuperCallFromClosure still returns early for a static target so SC does not emit a super$ helper.
Thank you for restoring the COMPATIBILITY.md entry, and for testSuperDotStaticMethodInClosure. I agree the @CompileStatic closure and lambda path applies to any static method of the superclass called through super, not only a hidden one, and the new test plus the "Who is affected" wording make that clear. The earlier reminder is settled.
CI is green on this tip. Looks good to me.
…-member Conflicts: - MetaClassImpl: master added superMethodOfAnotherRuntimePackage (GROOVY-12570) and getHiddenExtensionMethod (GROOVY-12522) where this branch added getHiddenStaticMethod. The first two take only instance methods, the last only static ones, so a method that is not fit for super usage now tries all three in turn, and all three helpers are kept. - ThisAndSuperTest: both sides added tests at the same place; both kept. - COMPATIBILITY.md: both sides appended entries; master's come first, then the GROOVY-12519 entry.
…en the class overrides it The GROOVY-12522 test left @CompileStatic out, which failed with NoSuchMethodError there. With no super$ helper for a static target, the closure calls the extension method directly.
|
Merged master in (9f46536). |
This comment has been minimized.
This comment has been minimized.
daniellansun
left a comment
There was a problem hiding this comment.
Thanks Scott — I re-checked the published tip ea730c795eac. It does include the merge you named: 9f465365af is the parent, against master ca71425976, and ea730c795e then adds @CompileStatic to the GROOVY-12522 super.first() closure test.
The unique change is still correct. getHiddenStaticMethod still feeds both super.sm() and super.sp, a private static method of the superclass stays out of reach, and checkSuperCallFromClosure still returns early for a static target so SC does not emit a super$ helper. The MetaClassImpl resolution against GROOVY-12570 and GROOVY-12522 looks right: superMethodOfAnotherRuntimePackage and getHiddenExtensionMethod take only instance methods, getHiddenStaticMethod takes only static methods, and a method that is not fit for super now tries all three in turn. I also agree that super.first() from a @CompileStatic closure of a class that overrides first() should be in ThisAndSuperTest; with no super$ helper for a static target, the closure can call the extension method directly.
GitHub still reports a conflict because this tip's merge is against ca71425976, not current master. The only conflict with e3b8697e97 is COMPATIBILITY.md: both sides appended a Groovy 7 section at the same place (this PR's GROOVY-12519 entry, and master's GROOVY-12562 entry from 097708c30c). MetaClassImpl and ThisAndSuperTest do not conflict. Please merge current master and keep both entries, with the 12562 section first and then 12519, as you did on the previous compatibility-file conflict.
CI on this tip is red on the same four trait/lambda tests that were red on master after #3244. Those are fixed on current master in f1cf705346 and e3b8697e97. I am not adding another approval on a conflicting, red tip; the existing approval of the 12519 change still stands once the conflict is resolved and CI is green.
One compatibility note, not a request to change the entry: it is already written as a Groovy 7 behaviour change, which is right. In dynamic Groovy and under @TypeChecked, super.sm() / super.sp for a hidden static member now succeed, or bind differently, where they previously threw or called another overload with null. I would not cherry-pick this as a unit to GROOVY_6_0_X unless that new success path is accepted for 6.0.x as well. The @CompileStatic closure and lambda fix is a separate bug fix (it used to fail with IncompatibleClassChangeError or NoSuchMethodError).
|
Merged. Thanks! |
When a class declares a static method or property with the same signature as its superclass,
super.sm()threwMissingMethodExceptionin dynamic Groovy and under@TypeChecked, or called another overload withnull, andsuper.spthrewMissingPropertyException. Under@CompileStatic,super.sm()in a closure or lambda failed withIncompatibleClassChangeErrorfor any static method of the superclass, hidden or not. All of them now reach the superclass's static member, as in javac. Private static methods of the superclass stay out of reach. The same change fixessuper.first()in a closure or lambda of anArrayListsubclass under@CompileStatic, which failed withNoSuchMethodError, also when the subclass overridesfirst(): an extension method is a static method too, so it needs nosuper$helper either.How
The meta-class drops a method the class declares itself from the methods available to
supercalls, unless something takes its place: a package-private method of another run-time package (GROOVY-12570) or an extension method the class overrides (GROOVY-12522). Nothing took the place of a static method that hides one of the superclass. Asupercall now finds the hidden static method of the nearest superclass that has one; thesuperproperty index is built from the same methods, so the static property accessors resolve too. Under@CompileStatic, a static target needs nosuper$helper in a closure: the call is aninvokestatic, valid from any class.Compatibility
COMPATIBILITY.md has the entry, "Groovy 7 —
superreaches a static member the class hides (GROOVY-12519)". When a class declares a static method or property with the same signature as one of its superclass,super.sm()andsuper.spwithin the class now call the superclass's member, in dynamic Groovy and under@TypeChecked, as javac and@CompileStaticdo. They used to throwMissingMethodExceptionorMissingPropertyException, or call another overload of the superclass withnull. Under@CompileStatic,super.sm()in a closure or lambda for any static method of the superclass, and a call of an extension method throughsuperthere, no longer fails withIncompatibleClassChangeErrororNoSuchMethodError.Who is affected. Code that relied on the old failure or on the other overload being called. A private static method of the superclass stays out of reach. There are no API changes.
Tests
groovy.ThisAndSuperTest, in all three modes, including closures and lambdas, a static property read and written throughsuper, a hidden method two levels up, a static method the class does not hide called from a closure and a lambda, the private case, and extension methods called throughsuperfrom a closure and a lambda, also when the class overrides them. They fail without the change. On the tip, 14,482 tests in thegroovy,bugs,gls,org.codehaus.groovy.classgenandgroovy.transform.stcpackages pass, includingbugs.Groovy12570andgls.innerClass.InnerClassTest, which cover the code master changed beside this. Before the latest merges of master, thetransform,runtime,reflection,vmplugin,controlandastpackages and the stub generator tests passed too. So didgroovy-contracts,groovy-typecheckers,groovy-ginq,groovy-macro-library,groovy-json,groovy-xmlandgroovy-templates, every subproject compiled its main and test sources, and the Checkstyle and CodeNarc gates passed.