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

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

codeconsole opened a new pull request, #3181:
URL: https://github.com/apache/groovy/pull/3181

   Under `@CompileStatic` with closure packing (`@PackedClosures` or 
`-Dgroovy.target.closure.pack=true`), a closure body is hoisted into a method 
of the owner class. A call that the type checker resolved against a closure 
object then went to the owner:
   
   ```groovy
   @groovy.transform.CompileStatic
   class C {
       String toString() { 'C' }
       static Object self(@DelegatesTo(value=String, strategy=Closure.TO_SELF) 
Closure c) { c.resolveStrategy = Closure.TO_SELF; c() }
       Object a() { self { -> toString() } }                                    
                      // packed: 'C'
       Object b() { Closure<String> outer = { -> { -> toString() }() }; 
[outer(), outer.toString()] } // packed: MissingPropertyException: owner
       Object c() { Closure c = { -> memoize() }; c() }                         
                      // packed: VerifyError
   }
   ```
   
   Without packing, and in dynamic Groovy, `a()` returns the closure's own 
`toString()`, `b()` returns the outer closure's `toString()` twice, and `c()` 
returns the memoized closure. Seen in 6.0.0 too.
   
   `StaticTypesClosureWriter.callsClosureMethod` now makes the packing decision 
decline when an implicit-this call in the body, nested closures included, 
targets a method that `Closure` declares, has the receiver type `Closure` that 
STC records under `TO_SELF`, or has the receiver path `"owner"` that STC 
records for an enclosing closure. `WARN` and `STRICT` report the reason. Calls 
resolved against the owner class still pack.
   
   ## Tests
   
   `PackedClosuresTransformTest.closuresCallingObjectMethodsOnAClosureDecline`, 
`closuresCallingClosureMethodsDecline` and 
`strictModeReportsClosureReceiverDecline` fail without the change. With 
`-Pgroovy.target.closure.pack=true`, 
`ClosuresStaticCompileTest.testClosureObjectMethodResolveStrategy` now passes. 
The packing test classes, `org.codehaus.groovy.classgen.asm.sc.*` and 
`groovy.transform.stc.*` pass without the flag; with it, the only failures are 
ones that also fail on master and have their own issues. The Checkstyle and 
CodeNarc gates pass.
   




> SC closure packing calls methods resolved against a closure object on the 
> owner class (TO_SELF, nested closures, Closure methods)
> ---------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12660
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12660
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 7.0.0-alpha-1, 6.0.0
>            Reporter: Scott Murphy Heiberg
>            Priority: Major
>
> With closure packing ({{@PackedClosures}} or 
> {{-Dgroovy.target.closure.pack=true}}), a statically compiled closure body is 
> hoisted into a method of the owner class. Calls that the type checker 
> resolved against a closure object are then compiled as if they targeted the 
> owner.
> {code:groovy}
> @groovy.transform.CompileStatic
> class C {
>     String toString() { 'C' }
>     static Object self(@DelegatesTo(value=String, strategy=Closure.TO_SELF) 
> Closure c) { c.resolveStrategy = Closure.TO_SELF; c() }
>     Object a() { self { -> toString() } }
>     Object b() { Closure<String> outer = { -> { -> toString() }() }; 
> [outer(), outer.toString()] }
>     Object c() { Closure c = { -> memoize() }; c() }
> }
> {code}
> *Actual*, with packing:
> * {{a()}} returns {{C}}.
> * {{b()}} throws {{MissingPropertyException: No such property: owner for 
> class: C}} (on 6.0.0 it returns {{C}} for the inner call).
> * Loading {{C}} fails with {{VerifyError: Bad type on operand stack ... Type 
> 'C' is not assignable to 'groovy/lang/Closure'}}.
> *Expected*, as without packing and in dynamic Groovy: {{a()}} returns the 
> closure's own toString(), {{b()}} returns the outer closure's toString() 
> twice, {{c()}} returns the memoized closure.
> Cause: the packing decision only checks for delegate-resolved names and for 
> the named closure pseudo-properties. It does not decline a call resolved 
> against the closure itself: an Object method under TO_SELF, an Object method 
> in a nested closure (resolved against the enclosing closure since 
> GROOVY-12576), or a method that Closure declares.
> Verified on 6.0.0 and master.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to