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

Groovy 2.0.1: Regression in generic type inference

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.0.1
    • 2.0.2
    • Static Type Checker
    • None

    Description

      The following Groovy code compiles with @TypeChecked in 2.0.0, but not in 2.0.1:

      JavaClass.java

      import java.util.List;
      import java.util.ArrayList;
      import java.util.Collection;
      
      public class JavaClass {
      
          public static class Container<T> {
          }
      
          public static class StringContainer extends Container<String> {
          }
      
          public static <T> List<T> unwrap(Collection<? extends Container<T>> list) {
              return null;
          }
      
          public static void main(String[] args) {
              final List<StringContainer> containers = new ArrayList<>();
              final List<String> strings = unwrap(containers);
          }
      }

      GroovyClass.groovy

      import groovy.transform.TypeChecked;
      
      @TypeChecked
      class GroovyClass {
      
          public static void javaCall() {
              final List<JavaClass.StringContainer> containers = new ArrayList<>();
              containers.add(new JavaClass.StringContainer()); 
              final List<String> strings = JavaClass.unwrap(containers);
          }
      
          public static void main(String[] args) { 
              javaCall();
          }
      }

      Groovy 2.0.0 compiles the code without errors or warnings, but 2.0.1 gives the following error message:

      /home/.../GroovyClass.groovy: 8: [Static type checking] - Incompatible generic argument types. Cannot assign java.util.List <JavaClass> to: java.util.List <String>
       @ line 8, column 38.
                 final List<String> strings = JavaClass.unwrap(containers);
                                              ^
      
      1 error

      Somehow Groovy infers the type to the static method's class, instead of the container's type argument.

      Attachments

        Activity

          People

            melix Cédric Champeau
            perplex79 Daniel Lichtenberger
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: