Repository navigation
GROOVY-12621: STC: reject more private superclass field accesses that fail at run time under type checking - #3147
codeconsole wants to merge 10 commits into
Conversation
β¦est-mate subclass under type checking Type-checked code is dispatched dynamically, and the metaclass of a subclass does not list a private instance field of a superclass. Property access to such a field through the subclass, from a class of the same nest other than the declaring class, was accepted by the type checker and then failed with a MissingPropertyException. It is now reported as a missing property, as it is for a superclass that encloses neither class. Static compilation still accesses the field directly, and an inner class whose enclosing instances lead to the declaring class still reaches it dynamically.
β¦ance The body of an enum constant is an anonymous class created by the static initializer of the enum, so it has no instance of the enum to fall back to. ClassNodeUtils.hasNoEnclosingInstance did not say so, and the type checker accepted access to a private instance field of the enum from the body, as "z", "this.z" or "super.z", which then failed at run time with a MissingPropertyException. "E.this" in the body is now a compile error instead of a VerifyError.
β¦per in a nested subclass under type checking The runtime falls back to the enclosing instance when a property read or write fails in an inner class, but not for a write through "super" (GROOVY-11823). The type checker accepted "super.z = value", "super.z += value" and "super.z++" for a private field of the enclosing superclass from a non-static inner subclass, which then failed with a MissingPropertyException. They are now reported as a missing property. Static compilation still writes the field directly, and "super.z" reads are unchanged.
β¦er of another class from an inner class under type checking The runtime falls back to the enclosing instance of an inner class only when the receiver is an instance of the calling class. The type checker accepted "that.z", for a private field of an enclosing superclass, from an inner or anonymous class whose enclosing instances lead to the declaring class, also when "that" was not an instance of that class, and the access failed with a MissingPropertyException. It is now reported as a missing property. An implicit "z" that the checker resolves as "Outer.this.z" is still dispatched on "this", so it is unaffected. Static compilation still accesses the field directly.
β¦ compilation After master's GROOVY-12533 change, this.z in an enum constant body is compiled through the enum instance under static compilation and fails verification; that is GROOVY-12626 (apache#3152).
Codecov Reportβ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3147 +/- ##
==================================================
+ Coverage 74.1012% 74.4222% +0.3210%
- Complexity 43600 44399 +799
==================================================
Files 1692 1699 +7
Lines 149254 151084 +1830
Branches 28286 28798 +512
==================================================
+ Hits 110599 112440 +1841
+ Misses 28475 28376 -99
- Partials 10180 10268 +88
π New features to boost your workflow:
|
daniellansun
left a comment
There was a problem hiding this comment.
Looks correct for GROOVY-12621 as a follow-on to GROOVY-12610.
Three remaining TC holes that still failed at run time are closed carefully: (1) enum constant bodies have no enclosing instance (EnumConstantClassNode in hasNoEnclosingInstance, so E.this becomes a compile error instead of VerifyError); (2) writes through super from a nested subclass are rejected (runtime does not fall back for super writes); (3) a receiver that is not derived from the calling class is rejected, with the checker-built implicit Outer.this.z marked so it stays dispatched on this. @CompileStatic continues to access the field directly via the SC overrides.
Related work: same design tension as #3137 vs Paul #3169 (MetaClass nest-mate reachability spike). Please coordinate land order / direction with #3169 before merging this stack, so TC rejection does not outlive a MetaClass change that would make these accesses succeed dynamically. Stacked on #3137 β land that (or an agreed #3169 path) first.
CI is fully green; the new STC / InnerClass / SC tests are clear.
The test used 'x' for the enclosing instance, the inner subclass instance and the static nested subclass instance, so it could not tell which object a read reached. With distinct values it asserts that type checking reads the enclosing instance for this.z, z and super.z, while static compilation reads super.z from this instance.
# Conflicts: # src/test/groovy/org/codehaus/groovy/classgen/asm/sc/FieldsAndPropertiesStaticCompileTest.groovy
β¦alling class test The test used 'x' for every object, so it could not tell which one a read reached. With distinct values, and a receiver whose enclosing instance is not the caller's, it asserts that type checking reads the field of the receiver's enclosing instance, as dynamic Groovy does, while static compilation reads the field of the receiver itself.
β All tests passed βπ·οΈ Commit: 4e75002 Learn more about TestLens at testlens.app/docs. |
|
@daniellansun On the land order: I answered this on #3137 (#3137 (comment)). In short, @paulk-asert's preference on #3169 is path (1) for 6.0.x regardless: keep the run-time behaviour and have type checking report the access. That is #3137 and then this PR, which is stacked on it. So the order I'd suggest is #3137, then #3147, both on master for cherry-picking to GROOVY_6_0_X. If path (2) is taken for Groovy 7, the rule is narrowed or dropped on master then, as Paul describes on #3169. The run of these shapes against #3169 is in #3137 (comment): three of them still fail at run time under #3169 (a receiver that is not an instance of the calling class, a write through |
daniellansun
left a comment
There was a problem hiding this comment.
Thanks Scott β I re-checked tip 4e750024b432 against my earlier comment on 364d46348b4e.
The three type-checking closures are the same as before: an enum constant body has no enclosing instance, a write through super from a nested subclass is rejected, and a receiver that is not derived from the calling class is rejected, with the implicit Outer.this.z the checker builds still dispatched on this. @CompileStatic still accesses the field directly.
The new tests give each object its own value, including a that built on another enclosing instance. Under type checking, that.z and that.with { z } observe 'far' (the enclosing instance of that), not 'that', and the other forms observe 'outer'. The SC override pins the complementary split, where nest-mate access reads the field of the receiver itself. The old shared 'x' could not show either.
On the direction with #3169: as you set out here and on #3137, path (1) for 6.0.x is #3137 and then this PR, on master for cherry-picking to GROOVY_6_0_X. That is enough for me to approve. This one should still follow #3137. CI is green on this tip.
Approving.
Stacked on #3137 (GROOVY-12610). After that change,
@TypeCheckedstill accepted three kinds of private superclass field access that fail at run time with aMissingPropertyException:The enum case fails the same way in 4.0.33, 5.0.8 and 6.0.0.
ClassNodeUtils.hasNoEnclosingInstancenow says so. As a resultE.thisin the body is a compile error instead of aVerifyError.super. The runtime falls back to the enclosing instance for reads and writes in an inner class, but not for a write throughsuper(GROOVY-11823).StaticTypeCheckingVisitor.isFieldAccessiblenow knows whether the access is a write, and rejects asuperwrite from a class other than the declaring class.supermust now be derived from the current class. The exception is theOuter.this.zthat the checker builds for an implicitz: it is still dispatched onthis, so it is marked and left unaffected.@CompileStaticstill accesses the field directly in every case. A non-static inner subclass still reaches the field throughthis.z,z,super.zreads, closures, lambdas and writes. Attribute access and access from the declaring class are unchanged.Tests
FieldsAndPropertiesSTCTest.testPrivateFieldOfEnumViaConstantBody,testPrivateFieldOfSuperclassWrittenThroughSuperInInnerSubclass,testPrivateFieldOfSuperclassViaReceiverOfOtherClassandInnerClassTest.testOuterThisInEnumConstantBodyfail without the change.testPrivateFieldOfSuperclassViaReceiverOfCallingClassand theFieldsAndPropertiesStaticCompileTestoverrides cover the forms that must keep compiling under type checking and static compilation.groovy.transform.stc.*,org.codehaus.groovy.classgen.asm.sc.*,gls.*,org.codehaus.groovy.transform.traitx.*,groovy.ThisAndSuperTest,bugs.*andgroovy.bugs.*pass. The Checkstyle and CodeNarc gates pass.