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

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

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

   Thank you. The `++` / `--` gap was real and is fixed in d30b777, with 
`testOperatorOfEnclosingInstanceOfReceiver` next to 
`testCallToMethodOfEnclosingInstanceOfReceiver`.
   
   - `other++`, `++other`, `other--` and `--other`: under `@CompileStatic` they 
called `Box.next()` on `other` itself (`next:self`), where dynamic Groovy and 
`@TypeChecked` call it on the enclosing instance (`next:B`). The type checker 
now records `ENCLOSING_INSTANCE_OF_RECEIVER` on `++` and `--` as 
`findMethodOrFail` does on a binary operator, and 
`StaticTypesBinaryExpressionMultiTypeDispatcher.writePostOrPrefixMethod` passes 
it to the `next()` or `previous()` call, which `StaticInvocationWriter` then 
makes on the enclosing instance. The test covers the four forms, the value of 
`other++`, a `this` operand, an array element `array[0]++`, and a property 
`holder.sub++`.
   - `other += x` is different from what the comment suggests: dynamic Groovy 
never calls a `*Assign` method of an enclosing instance. 
`ScriptBytecodeAdapter.compoundAssign` and `IndyCompoundAssign.resolve` use 
`plusAssign` only when the receiver's own meta-class responds to it 
(`respondsTo`, which does not reach the enclosing instance), and otherwise call 
`plus` through `invokeMethod`, which does. So the type checker now keeps a 
`*Assign` method only if it is one of the receiver itself, and `other += x` 
otherwise becomes `other = other + x`, with `plus` called on the enclosing 
instance as for `other + x`. Before, both `@TypeChecked` and `@CompileStatic` 
called `Box.plusAssign` on `other`. 03be0b1 extends this to a static 
`plusAssign` of an outer class, which `item += 'x'` also picked.
   - A subscript operand was a gap of the same kind: `other[0]++` and `other[0] 
+= x`, with a private `getAt` of `Box` and a `putAt` of `Sub`, read `other[0]` 
through `other`'s own `getAt` under `@CompileStatic`, although a plain 
`other[0]` already read it through the enclosing instance. In 03be0b1 the type 
checker records that `getAt` on an assigned subscript, and the static writer 
reads with it while the write stays `other.putAt`; 
`testSubscriptUpdateWithGetAtOfEnclosingInstance` covers `++`, `--` and `+=`.
   
   Testing the neighbours of these turned up five more cases, each with a test 
that fails without its fix and gives the asserted values when run dynamically:
   
   - b3079e0: `this::helper` and `this.&helper` in an enum constant body for a 
private method of the enum, and `Sub::sm` / `Sub.&sm` for a private static 
method of a class that encloses `Sub`, run in dynamic Groovy and compiled on 
master; this change had rejected them. They compile again; a reference through 
`Sub` to a private static method of a sibling `Box`, which dynamic Groovy fails 
on, stays rejected. The guide no longer says that every private static member 
of `Base` stays reachable through `Sub`, since property syntax `Sub.name` does 
not reach a private static field or getter.
   - 479c57f: `this.im()` in a closure in an anonymous class created in a 
closure cast the outer closure to the outer class under `@CompileStatic`; the 
`this$0` walk now goes through the closure's this object, as the walk for 
another receiver already did.
   - f3f53b8: `Box b = new Sub(); b.p = 'x'` called a private 
`Box.setP(Object)` instead of `Sub.setP(String)` under `@CompileStatic`, unlike 
`b.setP('x')`.
   - 09b256f: the enclosing-instance lookup now goes from the nearest outer 
class out and stops at the first with an applicable method, as dynamic Groovy 
does (`Middle.m(Object)` before `Outer.m(String)`), and in a static method an 
instance method of the class no longer blocks a static method of an outer class 
(`m('x')` calling `Outer.m(String)`, as on master).
   - e9c5645: a private `isReady()` of a superclass no longer hides its public 
`getReady()` from `new Sub().ready`.
   
   The 18,765 core tests, the subproject tests, Checkstyle and CodeNarc pass, 
and every commit compiles on its own. The description is updated for these.
   
   The `findVisibleMethods` javadoc, in 014d4a9 and b3079e0, now states which 
private methods a reference keeps: as for a call, a private method of the enum 
of an enum constant body and a private static method of an enclosing class; 
unlike a call, no other private static method, and none for a reference made in 
the declaring class or on an anonymous class created in a static context.
   
   On the rebase: this branch has been brought up to date by merges so far, and 
the previous replies cite those commits. Rebasing onto master would rewrite 
every commit already pushed and reviewed here and needs a force-push, so their 
ids would no longer exist on the pull request. I have not done that in this 
round; I am happy to do it if you would still like a linear branch, or a squash 
or rebase merge at merge time gives master the same history.




> 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