Scott Murphy Heiberg created GROOVY-12678:
---------------------------------------------
Summary: STC types owner and delegate as Closure inside a lambda
nested in a closure or lambda, so SC throws GroovyCastException
Key: GROOVY-12678
URL: https://issues.apache.org/jira/browse/GROOVY-12678
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}
import java.util.function.Supplier
@groovy.transform.CompileStatic
class C {
String name = 'n'
def q() {
Supplier<Supplier<Object>> s = () -> { name; (Supplier<Object>) (() ->
{ name; owner.maximumNumberOfParameters }) }
s.get().get()
}
def r() {
def c = { -> Supplier<Object> s = () -> { name;
owner.maximumNumberOfParameters }; s.get() }
c()
}
def t() {
def c = { -> Supplier<String> s = () -> { name; owner.name }; s.get() }
c()
}
}
new C().q()
{code}
*Actual:* {{q()}} and {{r()}} throw {{GroovyCastException: Cannot cast object
'C@...' with class 'C' to class 'groovy.lang.Closure'}}, and {{t()}} fails to
compile with "No such property: name for class: groovy.lang.Closure".
{{delegate}}, {{getOwner()}} and {{getDelegate()}} behave the same.
*Expected:* the owner of a statically compiled native lambda is the enclosing
instance at any depth of nesting, as it is for a top-level lambda. So {{owner}}
and {{delegate}} should be typed as {{C}}: {{t()}} returns 'n', and
{{owner.maximumNumberOfParameters}} is rejected at compile time, as it is in a
top-level lambda.
Cause: {{StaticTypeCheckingVisitor}} types {{owner}} and {{getOwner()}}, and
{{delegate}} without delegation metadata, as {{Closure}} whenever more than one
closure or lambda encloses the reference. A native lambda's owner is the
enclosing {{this}}, not the enclosing function. A closure, or a lambda compiled
as a closure (type-checked code, or no functional-interface target), is still
owned by the enclosing function.
Verified on 4.0.33 (owner reference in a lambda inside a closure), 5.0.8, 6.0.0
and master.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)