Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7713

CompileStatic checking fails with null returns

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.4.4, 2.4.5
    • 2.4.8
    • Static Type Checker
    • None

    Description

      ---------
      TEST 1
      ---------

      package test
      
      import groovy.transform.CompileStatic
      
      class TestClass {
      
      	@CompileStatic
      	void doTest() {
      		Closure<String> closure = {
      			return "foo";
      		}
      	}
      
      }
      

      Compiles fine.

      ---------
      TEST 2
      ---------

      package test
      
      import groovy.transform.CompileStatic
      
      class TestClass {
      
      	@CompileStatic
      	void doTest() {
      		Closure<String> closure = {
      			if ("bah".length() == 3) {
      				return null
      			}
      			return "foo";
      		}
      	}
      
      }
      

      ...gives...

      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
      

      ---------
      TEST 3
      ---------

      package test
      
      import groovy.transform.CompileStatic
      
      class TestClass {
      
      	@CompileStatic
      	void doTest() {
      		Closure<String> closure = {
      			return null;
      		}
      	}
      
      }
      

      ...gives...

      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
      

      ---------
      TEST 4
      ---------

      package test
      
      import groovy.transform.CompileStatic
      
      class TestClass {
      
      	@CompileStatic
      	void doTest() {
      		Closure<String> closure = {
      
      		}
      	}
      
      }
      

      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:

      Closure<String> closure = (Closure<String>) {
          ...
      }
      

      Attachments

        1. Test1.groovy
          0.2 kB
          Scott Douglas
        2. Test2.groovy
          0.2 kB
          Scott Douglas
        3. Test3.groovy
          0.2 kB
          Scott Douglas
        4. Test4.groovy
          0.1 kB
          Scott Douglas

        Issue Links

          Activity

            People

              jwagenleitner John Wagenleitner
              uberska Scott Douglas
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: