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

Type from flow typing is not propagated in variable assignments

    XMLWordPrintableJSON

    Details

      Description

      I have the following Groovy program.

      class A {}
      
      @groovy.transform.CompileStatic
      class Main {
      
        static A bar(Object x) {
          if (x instanceof A) {
            def y = x // Inferred Object and not A
            y
          } else {
            new A()
          }
        }
      }
      
      

      Actual Behavior

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

      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      Main.groovy: 9: [Static type checking] - Cannot return value of type java.lang.Object on method returning type A
       @ line 9, column 7.
               y
               ^
      
      1 error
      
      

      Expected Behavior

      Compile successfully.

      Affected Version

      I have also tested it with the compiler from Master (commit: 444f6a5e7f10b75d43d94e7bb4fe771ae384c321).

        Attachments

          Activity

            People

            • Assignee:
              emilles Eric Milles
              Reporter:
              schaliasos Stefanos Chaliasos
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              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