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

Combining flow typing and type argument inference does not work as expected

    XMLWordPrintableJSON

Details

    Description

      I have the following program

      class A<T> {
        T f;
        public A(T f) {
          this.f = f;
        }
      }
      
      class Foo {}
      class Bar extends Foo {}
      
      public class Main {
        public static void bar(Foo x) {
          if (x instanceof Bar) {
            A<Bar> a = new A<>(x)
          }
        }
      }
      

      Actual Behavior

      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      test.groovy: 15: [Static type checking] - Incompatible generic argument types. Cannot assign A <Foo> to: A <Bar>
       @ line 15, column 18.
               A<Bar> a = new A<>(x)
                          ^1 error
      
      

      Expected Behavior

      Compile successfully

       

      Tested against https://github.com/apache/groovy/commit/359be2b662be646d2d59db8b999412691891231d

      Attachments

        Activity

          People

            emilles Eric Milles
            theosot Thodoris Sotiropoulos
            Votes:
            0 Vote for this issue
            Watchers:
            2 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