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

LinkedList<T> is incompatible with intersection type List<T> & Queue<T>

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      generic

      Description

      ADDITIONAL SYSTEM INFORMATION :
      openjdk full version "12-ea+19"

      A DESCRIPTION OF THE PROBLEM :
      I have a simple class, in which a LinkedList is treated as an intersection type of List and Queue. Since LinkedList implements both of these interfaces, I don't see why this wouldn't be possible. However, the given class gives an error on each line, even when explicitly specifying String as the element type of the LinkedList.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile the given class

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Compiles without errors
      ACTUAL -
      .\Issue.java:5: error: incompatible types: cannot infer type arguments for LinkedList<>
      <U, T extends List<U> & Queue<U>> T run1(Class<U> type) { return new LinkedList<>(); }
                                                                                     ^
          reason: no instance(s) of type variable(s) E exist so that LinkedList<E> conforms to T
        where E,T,U are type-variables:
          E extends Object declared in class LinkedList
          T extends List<U>,Queue<U> declared in method <U,T>run1(Class<U>)
          U extends Object declared in method <U,T>run1(Class<U>)
      .\Issue.java:6: error: incompatible types: LinkedList<U> cannot be converted to T
      <U, T extends List<U> & Queue<U>> T run2(Class<U> type) { return new LinkedList<U>(); }
                                                                       ^
        where U,T are type-variables:
          U extends Object declared in method <U,T>run2(Class<U>)
          T extends List<U>,Queue<U> declared in method <U,T>run2(Class<U>)
      .\Issue.java:7: error: incompatible types: LinkedList<String> cannot be converted to T
      <T extends List<String> & Queue<String>> T run3() { return new LinkedList<String>(); }
                                                                 ^
        where T is a type-variable:
          T extends List<String>,Queue<String> declared in method <T>run3()
      .\Issue.java:8: error: incompatible types: LinkedList<String> cannot be converted to T
      <T extends List<String> & Queue<String>> void run4() { T result = new LinkedList<String>(); }
                                                                        ^
        where T is a type-variable:
          T extends List<String>,Queue<String> declared in method <T>run4()
      4 errors

      ---------- BEGIN SOURCE ----------
      import java.util.*;

      class Issue {

      <U, T extends List<U> & Queue<U>> T run1(Class<U> type) { return new LinkedList<>(); }
      <U, T extends List<U> & Queue<U>> T run2(Class<U> type) { return new LinkedList<U>(); }
      <T extends List<String> & Queue<String>> T run3() { return new LinkedList<String>(); }
      <T extends List<String> & Queue<String>> void run4() { T result = new LinkedList<String>(); }

      }

      ---------- END SOURCE ----------

        Attachments

          Activity

            People

            • Assignee:
              pmangal Priyanka Mangal (Inactive)
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: