codeconsole opened a new pull request, #3183:
URL: https://github.com/apache/groovy/pull/3183
An implicit-this call in a closure to a method that `Closure` declares was
resolved against every overload of that name, without looking at the arguments:
```groovy
@groovy.transform.TypeChecked
def test() {
Closure<Object> c = { String a, String b -> b == null ? curry(a) : a + b
}
((Closure) c('x', null))('y')
}
```
This fails with "Reference to method is ambiguous. Cannot choose between
Closure#curry(Object[]) and Closure#curry(Object)"; dynamic Groovy returns
`xy`. `rcurry`, `ncurry`, `trampoline()`, `andThenSelf()`, `composeSelf()` and
`leftShift(Closure)` failed the same way, in 4.0.33, 5.0.8 and 6.0.0 too. A
call whose arguments fit no `Closure` overload, such as `memoize('x')` or
`getDelegate(1)` where the owner declares those methods, was bound to the
closure's method (5.0.8 and later): under `@CompileStatic` the closure's method
was called, and under `@TypeChecked` the call failed with a
`GroovyCastException`.
The closure's methods are now chosen with `chooseBestMethod` and the
argument types (the GROOVY-11386 block of
`StaticTypeCheckingVisitor.visitMethodCallExpression`). When none matches, the
call goes through the normal delegate/owner lookup, as
`ClosureMetaClass.invokeMethod` does at run time. Under `DELEGATE_ONLY`,
`ClosureMetaClass` sends `curry`, `ncurry`, `rcurry`, `leftShift` and
`rightShift` to the delegate, and the type checker now does the same
(`CLOSURE_DELEGATE_ONLY_METHODS`); without that, the overload fix would have
bound those calls to the closure.
## Tests
`ClosuresSTCTest.testClosureOverloadedMethods`,
`testClosureMethodWithoutMatchingArguments` and
`testClosureDelegateOnlyCurryAndShiftMethods` fail without the change, in both
`ClosuresSTCTest` and `ClosuresStaticCompileTest`. `groovy.transform.stc.*`,
`org.codehaus.groovy.classgen.asm.sc.*`, `gls.closures.*` and
`groovy.ClosureTest` 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]