[
https://issues.apache.org/jira/browse/GROOVY-12509?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18149346#comment-18149346
]
ASF GitHub Bot commented on GROOVY-12509:
-----------------------------------------
codeconsole commented on PR #3050:
URL: https://github.com/apache/groovy/pull/3050#issuecomment-7083259216
Thank you for the careful review.
The non-`this` receiver case is fixed in 07dce78 and generalized in 5adeb0e.
When the type checker selects a method that the receiver lacks and an enclosing
instance of it has, it marks the call with the receiver type. The call writer
then calls the method on that receiver's enclosing instance, through its
`this$0` chain. Your script now gives `[im:A, im:A, im:A, im:B, im:A]` under
`@CompileStatic` too.
The same holds for:
- `Sub.this.im()`;
- a closure, a native lambda, a safe call (also `this?.im()`) and a spread
call;
- a closure delegate (`other.with { im() }`);
- a class nested in `Sub`, and a receiver of a subclass of `Sub`;
- an anonymous class created in a closure;
- a plain inner class, also for a public method of the outer class;
- the operators `other + x`, `other[i]` and `x in other`.
A method of the receiver itself comes first, as dynamic Groovy only turns to
the enclosing instance for a method the receiver lacks: with a public
`Sub.m(Object)`, `other.m('x')` calls it rather than a private `Box.m(String)`.
`new Sub().im()` written in `Box` itself still runs on that `Sub`. `Box x =
other; x.im()` and `((Box) other).im()` keep calling on the object under
`@CompileStatic`, as javac does for the static type `Box` (GROOVY-12513).
`testCallToPrivateMethodOfEnclosingClassThroughOtherReceiverInInnerSubclass`
and `testCallToMethodOfEnclosingInstanceOfReceiver` cover these, and both fail
without the change.
For property syntax, `@TypeChecked` accepts `other.p` and `other.q = v` for
a private accessor of the enclosing class through another receiver of the
calling class (69f2236), where the run time falls back to the enclosing
instance (GROOVY-12621). `@CompileStatic` reports "No such property" for them
instead of using the receiver, as master did. For a private field, `other.f`
under `@CompileStatic` still reads `other`'s own field, as GROOVY-12621's
`testPrivateFieldOfSuperclassViaReceiverOfCallingClass` pins it, so for a
non-`this` receiver methods and fields differ under static compilation.
Aligning the fields would mean changing that master test.
The guide paragraph is reworded in 28583c9 and c51a496: "none of the private
members", the enum constant body and anonymous-subclass-in-a-static-context
exception, `sub.name` limited to `Base` and the classes nested in it, the
GROOVY-12513 flow-typing split restricted to private instance members outside
`Base`, private static members staying reachable, and the inner-class cases.
You are right about `super.im()` and `((Box) sub).im()`: dynamic Groovy and
`@TypeChecked` throw `MissingMethodException` for both, as for a `Box`
parameter that holds a `Sub`. Only `@CompileStatic` and javac run them. The
description is corrected, and it now states the `@CompileStatic` compatibility
break explicitly.
> STC: private members of another class of the same nest are not accessible
> -------------------------------------------------------------------------
>
> Key: GROOVY-12509
> URL: https://issues.apache.org/jira/browse/GROOVY-12509
> Project: Groovy
> Issue Type: Bug
> Components: Static Type Checker
> Affects Versions: 6.0.0
> Reporter: Scott Murphy Heiberg
> Assignee: Paul King
> Priority: Major
> Fix For: 7.0.0-alpha-1, 6.0.1
>
>
> Under static type checking, a class nested in a top-level class cannot use
> the private constructors, methods and accessors of another class nested in
> the same top-level class:
> {code:groovy}
> @groovy.transform.CompileStatic
> class C {
> static class Box {
> private Box(String s) {}
> private static String sm() { 'sm' }
> private String im() { 'im' }
> private String getX() { 'x' }
> }
> static class A { private static class Hidden { public static int x = 1 } }
> static class Other {
> static test(Box box) {
> new Box('a') // Cannot find matching constructor
> C$Box(java.lang.String)
> Box.sm() // Cannot find matching method java.lang.Class#sm()
> or static method C$Box#sm()
> box.im() // Cannot find matching method C$Box#im()
> box.x // No such property: x for class: C$Box
> A.Hidden.x // Cannot access field: x of class: C$A$Hidden from
> class: C$Other
> }
> }
> }
> {code}
> Java allows all of these (JLS 6.6.1: a private member is accessible anywhere
> within the body of the top-level class that encloses its declaration), and
> the JVM does through nestmates. Groovy already writes the {{NestHost}} and
> {{NestMembers}} attributes for these classes, the class generator already
> calls private members of nestmates directly, and the type checker already
> allows private fields of nestmates (GROOVY-12290). Method references such as
> {{Box::new}} and {{Box::sm}} are rejected the same way.
> The modes also disagree. With a private {{Box(String)}} beside a public
> {{Box(Object)}}, {{new Box('s')}} in {{Other}} calls the private constructor
> in dynamic and {{@TypeChecked}} code, as javac does, but the public one under
> {{@CompileStatic}}. The same happens for a private {{B.m(String)}} beside an
> inherited {{A.m(Object)}}.
> A private getter or setter used through property syntax ({{box.x}}) is also
> rejected from the outer class into a nested class.
> The last case, a public member reached through a private nested type
> ({{A.Hidden.x}}, or a constant of a private nested enum), worked in 5.0.8 and
> fails since 6.0.0 (GROOVY-12310), from a sibling and from the outer class.
> The other cases fail in 4.0, 5.0 and 6.0.
> PR: https://github.com/apache/groovy/pull/3044 (the private nested types; the
> other parts follow in separate pull requests)
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)