[
https://issues.apache.org/jira/browse/GROOVY-7713?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15420246#comment-15420246
]
ASF GitHub Bot commented on GROOVY-7713:
----------------------------------------
GitHub user jwagenleitner opened a pull request:
https://github.com/apache/groovy/pull/384
GROOVY-7713: CompileStatic checking fails with null returns from Clos…
…ures
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/jwagenleitner/groovy GROOVY-7713
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/groovy/pull/384.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #384
----
commit cfda1043e1e23d47e7ee6bf1f5928bd34ea0c237
Author: John Wagenleitner <[email protected]>
Date: 2016-08-14T06:34:09Z
GROOVY-7713: CompileStatic checking fails with null returns from Closures
----
> CompileStatic checking fails with null returns
> ----------------------------------------------
>
> Key: GROOVY-7713
> URL: https://issues.apache.org/jira/browse/GROOVY-7713
> Project: Groovy
> Issue Type: Bug
> Components: Static Type Checker
> Affects Versions: 2.4.4, 2.4.5
> Reporter: Scott Douglas
> Priority: Minor
> Attachments: Test1.groovy, Test2.groovy, Test3.groovy, Test4.groovy
>
>
> ---------
> TEST 1
> ---------
> {code}
> package test
> import groovy.transform.CompileStatic
> class TestClass {
> @CompileStatic
> void doTest() {
> Closure<String> closure = {
> return "foo";
> }
> }
> }
> {code}
> Compiles fine.
> ---------
> TEST 2
> ---------
> {code}
> package test
> import groovy.transform.CompileStatic
> class TestClass {
> @CompileStatic
> void doTest() {
> Closure<String> closure = {
> if ("bah".length() == 3) {
> return null
> }
> return "foo";
> }
> }
> }
> {code}
> ...gives...
> {code}
> groovyc Test2.groovy
> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup
> failed:
> Test2.groovy: 9: [Static type checking] - Incompatible generic argument
> types. Cannot assign groovy.lang.Closure <java.lang.Object> to:
> groovy.lang.Closure <String>
> @ line 9, column 29.
> Closure<String> closure = {
> ^
> 1 error
> {code}
> ---------
> TEST 3
> ---------
> {code}
> package test
> import groovy.transform.CompileStatic
> class TestClass {
> @CompileStatic
> void doTest() {
> Closure<String> closure = {
> return null;
> }
> }
> }
> {code}
> ...gives...
> {code}
> groovyc Test3.groovy
> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup
> failed:
> Test3.groovy: 9: [Static type checking] - Incompatible generic argument
> types. Cannot assign groovy.lang.Closure <java.lang.Object> to:
> groovy.lang.Closure <String>
> @ line 9, column 29.
> Closure<String> closure = {
> ^
> 1 error
> {code}
> ---------
> TEST 4
> ---------
> {code}
> package test
> import groovy.transform.CompileStatic
> class TestClass {
> @CompileStatic
> void doTest() {
> Closure<String> closure = {
> }
> }
> }
> {code}
> Compiles fine.
> ---------
> COMMENTS
> ---------
> All files were compiled with 'groovyc Test\[1234\].groovy'. I expected all
> tests to compile. I wouldn't have thought returning null would cause a static
> compilation failure since null is a valid String. Also, I expected test 3 and
> test 4 to be equivalent.
> The workaround appears to be to cast the closure, like:
> {code}
> Closure<String> closure = (Closure<String>) {
> ...
> }
> {code}
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)