Scott Murphy Heiberg created GROOVY-12662:
---------------------------------------------

             Summary: STC rejects an implicit-this call in a closure to an 
overloaded Closure method, such as curry(x), as ambiguous, and ignores argument 
types when choosing the Closure method
                 Key: GROOVY-12662
                 URL: https://issues.apache.org/jira/browse/GROOVY-12662
             Project: Groovy
          Issue Type: Bug
    Affects Versions: 5.0.8, 4.0.33, 6.0.0, 7.0.0-alpha-1
            Reporter: Scott Murphy Heiberg


{code:groovy}
@groovy.transform.TypeChecked // or @CompileStatic
def test() {
    Closure<Object> c = { String a, String b -> b == null ? curry(a) : a + b }
    ((Closure) c('x', null))('y')
}
assert test() == 'xy'
{code}
*Actual:*
{noformat}
[Static type checking] - Reference to method is ambiguous. Cannot choose 
between [groovy.lang.Closure<V> groovy.lang.Closure#curry(java.lang.Object[]), 
groovy.lang.Closure<V> groovy.lang.Closure#curry(java.lang.Object)]
{noformat}
*Expected:* 'xy', as in dynamic Groovy, which calls curry(Object) on the 
enclosing closure. rcurry, ncurry, trampoline(), andThenSelf(), composeSelf() 
and leftShift(Closure) fail the same way.

The selection ignores argument types altogether. If the owner declares 
memoize(String) or getDelegate(int), then memoize('x') or getDelegate(1) inside 
a closure is bound to Closure.memoize() or Closure.getDelegate(). Under 
@CompileStatic the closure's method is called. Under @TypeChecked the inferred 
type is wrong, and the call fails with a GroovyCastException. Dynamic Groovy 
calls the owner's method. Under DELEGATE_ONLY, ClosureMetaClass sends curry, 
ncurry, rcurry, leftShift and rightShift to the delegate, but the type checker 
binds them to the closure.

Cause: for an implicit-this call in a closure, StaticTypeCheckingVisitor takes 
every Closure method of that name (GROOVY-11386) without choosing among them by 
argument type, and never falls back to the delegate or owner when none of them 
accepts the arguments.

Verified: 4.0.33 (ambiguity only), 5.0.8, 6.0.0 and master.



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

Reply via email to