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

void tail calls in closures break @CompileStatic type inference

    XMLWordPrintableJSON

Details

    Description

      Consider this piece of code:

      void f() {
      }
      
      @CompileStatic
      String test1() {
      	Closure<String> x = {
      		if (true) {
      			"asd"
      		} else {
      			f()
      			null
      		}
      	}
      	x.call()
      }
      
      @CompileStatic
      String test2() {
      	Closure<String> x = { // Cannot assign Closure<Object> to Closure<String>
      		if (true) {
      			"asd"
      		} else {
      			f()
      		}
      	}
      	x.call()
      }
      
      @CompileStatic
      String test3() {
      	if (true) {
      		"asd"
      	} else {
      		f()
      	}
      }
      

      The code in test1 compiles correctly. The type of the closure in test2 is inferred as Closure<Object> and that results in a static type checking error. There is an inconsistency here - in test3 the type inference yields String.

      Basically - null s are not considered when the type is inferred. Calls to void methods should not be either.

      Attachments

        Activity

          People

            emilles Eric Milles
            joan.karadimov Joan Karadimov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: