[ 
https://issues.apache.org/jira/browse/GROOVY-11681?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17955058#comment-17955058
 ] 

Eric Milles edited comment on GROOVY-11681 at 5/31/25 3:06 PM:
---------------------------------------------------------------

The method call is visited before the closure. So you could inspect the closure 
AST to find return type. There is returnAdder and returnListener in 
StaticTypeCheckingVisitor that shows how to do it. You can run in no-modify 
mode to get callbacks for returning statements.


was (Author: emilles):
The method call is visited before the closure. So you could inspect the closure 
ast to find return type. There is a return checker in StaticTypeCheckingVisitor 
that shows how to do it. You can run in no-modify mode to get callbacks for 
returning statements. 

> 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
>            Priority: Major
>
>  
> 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