Paul King created GROOVY-12828:
----------------------------------

             Summary: SC: returning an intersection-cast lambda from a closure 
throws NoClassDefFoundError: IntersectionType
                 Key: GROOVY-12828
                 URL: https://issues.apache.org/jira/browse/GROOVY-12828
             Project: Groovy
          Issue Type: Bug
    Affects Versions: 6.0.0
            Reporter: Paul King


Under \{{@CompileStatic}}, a closure that returns a local variable initialized 
with an intersection-cast lambda compiles, but fails at run time.

{code:groovy}
import java.util.function.Supplier

interface Marker {}

@groovy.transform.CompileStatic
class C {
    Supplier<String> m() {
        'x'.with {
            int unused = 0
            Supplier<String> s = (Supplier & Serializable & Marker) () -> 
'value'
            s
        }
    }
}

assert new C().m().get() == 'value'
{code}

Actual result:
{noformat}
java.lang.NoClassDefFoundError: IntersectionType
{noformat}

The error is raised from \{{Class.getDeclaredMethods}} while the closure's 
metaclass is initialized. This suggests that a generated method signature 
refers to the type checker's intersection type instead of an erased type, but 
the cause has not been confirmed.

h3. Versions tested

||Version||Result||
|5.1.3|Rejected at compile time with a static type checking error|
|6.0.0|Compiles, then throws \{{NoClassDefFoundError}}|
|master|Compiles, then throws \{{NoClassDefFoundError}}|

The \{{int unused = 0}} statement stops the parser from treating the typed 
declaration at the start of the closure as a closure parameter.

Assigning the lambda to a property and not returning it from the closure avoids 
the error.



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

Reply via email to