[ 
https://issues.apache.org/jira/browse/GROOVY-12509?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18149454#comment-18149454
 ] 

ASF GitHub Bot commented on GROOVY-12509:
-----------------------------------------

codeconsole commented on PR #3050:
URL: https://github.com/apache/groovy/pull/3050#issuecomment-7088466313

   Thank you. All of these are in a851725, with one more in af95df5.
   
   - `findMethod`: its javadoc now states the rules. A private method the 
receiver only inherits is not a candidate (JLS 8.2). It is kept when it is 
declared by the declared type of the receiver variable (known only under static 
compilation), when it is static, when the call is made in its declaring class, 
and when the receiver is an enum constant body or an anonymous class created in 
a static context nested in its declaring class. When the receiver is a class 
whose code this is, or an inner class that is a subclass of one, and none of 
its own methods accepts the argument types, the methods of its outer classes 
are offered too, only the static ones beyond a class with no enclosing 
instance, as `ENCLOSING_INSTANCE_OF_RECEIVER` records. It also says that a 
method pointer or reference is resolved elsewhere, without the exceptions for a 
static method, the declaring class and an instance with no enclosing instance, 
because dynamic Groovy fails on those.
   - `SecondPassExpression`: the class javadoc now explains the declared 
receiver type and the enclosing class, and why the second pass needs them to 
find the same private members. The four-argument constructor documents each 
parameter. Leaving them out of `equals` and `hashCode` is intentional, and the 
javadoc now says why: both follow from the wrapped expression, which is one 
node of one class with one receiver variable, so two wrappers of the same 
expression never differ in them.
   - `isInheritedPrivateMethod`: the javadoc and `@return` now say what the 
body checks: the method is private, is not an extension method, and is declared 
by a class other than the receiver type. It goes on to say that for a method 
found on that type or its superclasses, this is a private method the receiver 
would only inherit.
   - `findVisibleMethods` has javadoc in place of the inline comment, including 
that a static method or a reference made in the declaring class is no exception 
there, unlike a call. `getDeclaredType` says it returns the origin type of a 
variable that is not `this`, `super` or a dynamic variable, or else `null`. The 
two-argument `thisObjectExpression` says it allows an enclosing instance of a 
subclass of `target` and links to the three-argument method.
   - `findMethodWithDeclaredType`, the helper that `findMethodThrough` and the 
second pass go through, had no javadoc either; af95df5 adds it.
   
   I left `ENCLOSING_INSTANCE_OF_RECEIVER` as a one-line comment, since every 
neighbouring marker in `StaticTypesMarker` is one; I can split it if you would 
still like that. I also merged current master, including 4f83940a 
(GROOVY-12727), in 16dddda; there were no conflicts, and since that commit also 
changes `StaticTypeCheckingVisitor`, I ran the core tests, the subproject 
tests, Checkstyle and CodeNarc on the merged tree, and all pass.




> 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)

Reply via email to