codeconsole opened a new pull request, #3193:
URL: https://github.com/apache/groovy/pull/3193
Under `@CompileStatic`, a lambda that reads a closure property it does not
get from the enclosing class throws at run time:
```groovy
@CompileStatic
class C {
def m() { Supplier<Object> s = () -> delegate; s.get() }
}
new C().m() // ClassCastException: class java.lang.Class cannot be cast to
class C$_m_lambda1
```
The same happens for `owner`, `maximumNumberOfParameters`,
`resolveStrategy`, `parameterTypes`, `directive` and `metaClass`, including in
static methods, nested lambdas and lambdas inside closures; assigning
`resolveStrategy` fails too. 6.0.0 is affected; 4.0 and 5.0 do not throw.
Since GROOVY-11905, a lambda that does not touch the enclosing instance gets
a static `doCall`. `StaticTypesLambdaAnalyzer` counted only `this`, `super` and
`thisObject` as such access. The other closure properties are dynamic variables
that the generated code reads from the lambda itself, and in a static `doCall`
that receiver is the enclosing `Class`. The analyzer now treats every dynamic
variable in the body as access to the lambda, as `thisObject` already was.
The lambda keeps its instance and has the enclosing object as owner and
delegate, which is the type the checker infers for them and what dynamic Groovy
returns. This differs from 4.0 and 5.0, where such a lambda got the enclosing
`Class` as owner and delegate unless it touched an instance member, so
`Supplier<C> s = () -> owner` returned a `Class`. In a static method they are
the class, as before.
## Tests
`LambdaTest.testAccessingClosureProperties1` to `5` fail without the change:
four with ClassCastException and one with IncompatibleClassChangeError. They
cover dynamic, `@TypeChecked` and `@CompileStatic`; instance and static
methods; capturing lambdas; nested lambdas; a lambda in a closure; and
assignment to `resolveStrategy`. `groovy.transform.stc.*`,
`org.codehaus.groovy.classgen.asm.sc.*`,
`org.codehaus.groovy.transform.traitx.*`, `gls.innerClass.*`,
`groovy.ThisAndSuperTest` and the packed-closure tests 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]