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

Type tracked from instanceof condition is not captured in closure

Attach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

    Details

      Description

      I have the following Groovy program

       

       

      @groovy.transform.TypeChecked
      class Test {
        public static void main(String[] args) {
          println(foo(10));
        }
      
        static Integer foo(Object x) {
          if (x instanceof Integer) {
            def bar = {x};
            return bar();
          }
          return 100;
          
        }
      }
      

       

       

      Actual Behavior

      The program does not compile, and I get the following error

       

      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      program.groovy: 10: [Static type checking] - Cannot return value of type java.lang.Object on method returning type java.lang.Integer
       @ line 10, column 14.
               return bar();
                      ^1 error
      
      

      Expected Behavior

      Compile successfully.

      In particular I would expect that the inferred type of the return type of closure is "Integer" and not "Object", as the closure is defined inside a block where the following condition holds.

      x instanceof Integer
      

        Attachments

          Activity

            People

            • Assignee:
              emilles Eric Milles
              Reporter:
              theosot Thodoris Sotiropoulos

              Dates

              • Created:
                Updated:
                Resolved:

                Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 0.5h
                0.5h

                  Issue deployment