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

Type inference fails when widening / narrowing conversion is possible

    Details

    • Type: Bug
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 8, 12.0.1, 13, 14
    • Fix Version/s: 15
    • Component/s: tools
    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

      A DESCRIPTION OF THE PROBLEM :
      Type inference fails when multiple methods with the same name and parameters which are widening / narrowing convertable exist, even though the type which is expected to be inferred would exactly match the parameter type of one method without requiring conversion.

      Java 8 is affected as well.

      It looks like Eclipse's JDT compiler has no problems with this.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Try to compile the code provided below.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      It should successfully compile.
      ACTUAL -
      It fails to compile.

      ---------- BEGIN SOURCE ----------
      import java.util.function.Function;

      public class JavacInferenceBug {
          private static void twoPrimitiveParam(int a, int b) { }
          private static void twoPrimitiveParam(long a, long b) { }
          
          private static void twoBoxedParam(Integer a, Integer b) { }
          private static void twoBoxedParam(Long a, Long b) { }
          
          private static void twoMixedParam(int a, Integer b) { }
          private static void twoMixedParam(long a, Long b) { }
          
          private static void twoMixedObjParam(int a, Integer b) { }
          private static void twoMixedObjParam(int a, Object b) { }
          
          private static void singlePrimitiveWidening(int a) { }
          private static void singlePrimitiveWidening(long a) { }
          
          private static void singlePrimitiveNarrowing(int a) { }
          private static void singlePrimitiveNarrowing(short a) { }
          
          private static void singleBoxedWidening(Integer a) { }
          private static void singleBoxedWidening(Long a) { }
          
          private static void singleBoxedNarrowing(Integer a) { }
          private static void singleBoxedNarrowing(Short a) { }
          
          private static <T> T infer(Function<String, T> function) {
              return null;
          }
          
          public static void main(String[] args) {
              twoPrimitiveParam(1, infer(Integer::valueOf));
              twoBoxedParam(1, infer(Integer::valueOf));
              twoMixedObjParam(1, infer(Integer::valueOf));
              
              // javac fails: Ambiguous
              twoMixedParam(1, infer(Integer::valueOf));
              
              // Works fine
              singlePrimitiveWidening(infer(Integer::valueOf));
              // javac fails: Thinks upper bounds are Object and Short
              // In Java 8: no instance(s) of type variable(s) exist so that T conforms to short
              singlePrimitiveNarrowing(infer(Integer::valueOf));
              
              // javac fails: Ambiguous
              singleBoxedWidening(infer(Integer::valueOf));
              // javac fails: Ambiguous
              singleBoxedNarrowing(infer(Integer::valueOf));
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              vromero Vicente Arturo Romero Zaldivar
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: