codeconsole opened a new pull request, #3174:
URL: https://github.com/apache/groovy/pull/3174
With closure packing enabled (`-Dgroovy.target.closure.pack=true` or
`@PackedClosures`), a local captured by a closure in a constructor or an
instance initializer block, and written after the literal, was copied into the
packed closure by value, so the closure saw a stale value:
```groovy
class R { def r; R() { def c; c = { x -> c == null ? 'stale' : 'set' }; r =
c(1) } }
new R().r // 'stale'; 'set' without packing and in a method
```
`ClosureWriter.holderCaptureNames`, which decides which captures are
threaded as a shared `Reference`, looked the names written after the literal up
in `WriterController.getMethodNode()`, which is null while a constructor is
written. It now uses `ClosureWriter.enclosingMethod()`, which falls back to the
constructor node.
Stacked on #3173 (GROOVY-12652), which adds `enclosingMethod()`; this PR
shows its commit until that is merged. Seen in 6.0.0 too.
## Tests
`PackedClosuresTransformTest.captureWrittenAfterTheLiteralInAConstructorOrInitializerIsShared`
fails without the change. The packing test classes,
`org.codehaus.groovy.classgen.asm.sc.*` and `groovy.transform.stc.*` pass. 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]