Saravanan created GROOVY-11681:
----------------------------------

             Summary: Return statement throws a compile error inside a closure
                 Key: GROOVY-11681
                 URL: https://issues.apache.org/jira/browse/GROOVY-11681
             Project: Groovy
          Issue Type: Bug
          Components: Static compilation
    Affects Versions: 5.0.0-beta-1
            Reporter: Saravanan


 

I am not sure what's going on here, the first call to myOther() succeeds during 
compilation, but the call to the submit method shows this error (after static 
compile was applied, during class gen)
{code:java}

import java.util.concurrent.Callable;
import java.util.concurrent.Executors;

public class RecordTesting {

    public <T> T myOther(Callable<T> callable) {
    }

    public void myFunction() {
        myOther(() -> {
            return "blah";
        })

        Executors.newSingleThreadExecutor().submit(() -> {
            return "blah";
        });
    }
}
{code}
 
{noformat}
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
General error during instruction selection: Cannot use return statement with an 
expression on a method that returns void
. At [17:13] 
testdata/plugins/records/RecordTesting.javaorg.codehaus.groovy.syntax.RuntimeParserException:
 Cannot use return statement with an expression on a method that returns void
. At [17:13] testdata/plugins/records/RecordTesting.java
    at 
org.codehaus.groovy.classgen.AsmClassGenerator.throwException(AsmClassGenerator.java:2450)
    at 
org.codehaus.groovy.classgen.asm.StatementWriter.writeReturn(StatementWriter.java:603)
    at 
org.codehaus.groovy.classgen.AsmClassGenerator.visitReturnStatement(AsmClassGenerator.java:885)
    at 
org.codehaus.groovy.ast.stmt.ReturnStatement.visit(ReturnStatement.java:73)
    at 
org.codehaus.groovy.classgen.asm.StatementWriter.writeBlockStatement(StatementWriter.java:91)
    at 
org.codehaus.groovy.classgen.asm.sc.StaticTypesStatementWriter.writeBlockStatement(StaticTypesStatementWriter.java:79)
    at 
org.codehaus.groovy.classgen.AsmClassGenerator.visitBlockStatement(AsmClassGenerator.java:815)
    at 
org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:72){noformat}
The error goes away if I cast the lambda to a callable like this
{code:java}
Executors.newSingleThreadExecutor().submit((Callable<String>) () -> { return 
"blah"; });
{code}
 



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to