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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]