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

Type inference not always working on ternary expressions

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      windows_7

      Description

      FULL PRODUCT VERSION :


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      The following code compiles and runs under Java 7u51, as expected:

      public class Hello {

          public static void main(String[] args) {
              foo((true ? new ArrayList<String>() : new ArrayList<CharSequence>()));
          }

          static <T extends CharSequence> void foo(List<T> l) {
          }
      }

      However, under Java 8 this is now not the case, it refuses to compile with the following error:

      error: method foo in class Hello cannot be applied to given types;
              foo((true ? new ArrayList<String>() : new ArrayList<CharSequence>()));
        required: List<T>
        found: true ? new[...]ce>()
        reason: inferred type does not conform to equality constraint(s)
          inferred: CharSequence
          equality constraints(s): CharSequence,String
        where T is a type-variable:
          T extends CharSequence declared in method <T>foo(List<T>)
      1 error


      It seems like type inference should work here, there's no implicit reason why this should cause an error. For example, the following compiles and runs without issue in both Java 7 and 8:

      public class Hello {

          public static void main(String[] args) {
              ArrayList<? extends CharSequence> x = (true ? new ArrayList<String>() : new ArrayList<CharSequence>());
              foo(x);
          }

          static <T> void foo(List<T> l) {
          }
          
          static <T> T ternary(boolean cond, T a, T b) {
              return cond?a:b;
          }
      }

      Considering the declared type in the first line in the above example is ArrayList<? extends CharSequence>, and this compiles without warning or casting, surely the type should be inferred as such (as it presumably was in Java 7?)

      REGRESSION. Last worked in version 7u51

      ADDITIONAL REGRESSION INFORMATION:
      The above example compiled and ran without issue with every version of Java 7.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Attempt to compile the provided code with Java 8.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Code should compile and run without warning.
      ACTUAL -
      Code does not compile.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      error: method foo in class Hello cannot be applied to given types;
              foo((true ? new ArrayList<String>() : new ArrayList<CharSequence>()));
        required: List<T>
        found: true ? new[...]ce>()
        reason: inferred type does not conform to equality constraint(s)
          inferred: CharSequence
          equality constraints(s): CharSequence,String
        where T is a type-variable:
          T extends CharSequence declared in method <T>foo(List<T>)
      1 error

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.ArrayList;
      import java.util.List;

      public class Hello {

          public static void main(String[] args) {
              foo((true ? new ArrayList<String>() : new ArrayList<CharSequence>()));
          }

          static <T> void foo(List<T> l) {
          }
      }
      ---------- END SOURCE ----------

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                pgovereau Paul Govereau (Inactive)
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: