Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8279556

Generic type inference bug with Stream::toList

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Manjaro Linux, OpenJDK Java 17.0.1 and 18 EA.

      A DESCRIPTION OF THE PROBLEM :
      Compiler rejects use of `Stream::toList` but not `Stream::collect(Collectors.toList())

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Save source code as StreamBug.java, execute `java StreamBug.java`

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      No output. (File compiles and runs.)
      ACTUAL -
      Compiler prints:

      StreamBug.java:8: error: incompatible types: List<Class<CAP#1>> cannot be converted to List<Class<?>>
              List<Class<?>> output = input.stream().map(a -> a).toList();
                                                                       ^
        where CAP#1 is a fresh type-variable:
          CAP#1 extends Object from capture of ?
      1 error
      error: compilation failed

      ---------- BEGIN SOURCE ----------
      import java.util.List;
      import java.util.stream.Collectors;

      public class StreamBug {
          public static void main(String... args) {
              List<Class<?>> input = List.of();
              List<Class<?>> output = input.stream().map(a -> a).toList();
              List<Class<?>> output2 = input.stream().map(a -> a).collect(Collectors.toList());
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      use `collect(Collectors.toList())`

      FREQUENCY : always


            vromero Vicente Arturo Romero Zaldivar
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: