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

STC is unable to infer type variable when combining parameterized types and the diamond operator

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 4.0.0-beta-2, 3.0.11
    • None
    • None

    Description

      I have the following program

      class A {
        <T> T foo(C<T> t) { return null; }
      }
      
      
      class C<T> {}
      
      class B {
      
        void test() {
          C<String> x = (new A()).foo(new C<>());
        }
      }
      

      Actual behaviour

      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      test.groovy: 12: [Static type checking] - Cannot assign value of type java.lang.Object to variable of type C<java.lang.String>
       @ line 12, column 19.
             C<String> x = (new A()).foo(new C<>());
                           ^
      
      1 error
      

      Expected behaviour

      Compile successfully

      Note that compilation fails only if the expected return type is parameterized (i.e., `D<String>`). If I replace the declared type of `x` with a regular type (i.e., `String`), the code compiles fine.

      Tested against master.

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              theosot Thodoris Sotiropoulos
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: