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

inference: error in type inference for bounded type parameters

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 5.0
    • specification
    • x86
    • windows_xp



      Name: rmT116609 Date: 07/30/2003


      FULL PRODUCT VERSION :
      java version "1.4.2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
      Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)

      FULL OS VERSION :
      Windows XP

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      prototype compiler for generics version 2.2-ea

      A DESCRIPTION OF THE PROBLEM :
      I'm using the prototype compiler for generics version 2.2-ea.

      For a parameterized method the compiler seems to deduce a type that is not within bounds.

      package generics;
      import java.util.*;
      import java.io.Serializable;


      public class ParameterInference3 {

      private static void f(String s) {
      System.out.println("String");
      }

      private static void f(Integer s) {
      System.out.println("String");
      }

      private static void f(Comparable e) {
      System.out.println("Comparable");
      }

      private static void f(Serializable e) {
      System.out.println("Serializable");
      }

      private static void f(Object e) {
      System.out.println("Object");
      }


      private static class Utilities {

      public static <T extends Comparable> T max(T arg1, T arg2) {
      return (arg1.compareTo(arg2)>0)?arg1:arg2;
      }
      }






      public static void main(String[] args) {
      f(Utilities.max("abc",new Integer(10)));
      }
      }


      The compiler complains:

      ParameterInference3.java:42: reference to f is ambiguous, both method f(java.lang.Comparable) in generics.ParameterInference3 and method f(java.io.Serializable) in generics.ParameterInference3 match
      f(Utilities.max("abc",new Integer(10)));
                      ^

      It looks like the compiler infers T:=java.lang.Object&java.io.Serializable&java.lang.Comparable<?>, which leads to the ambiguity when resolving the call among the overloaded version of f().

      However, Object and Serializable are no viable options for the type parameter because they are not within the bound of method max().

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      compile the source code

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      error free compilation
      ACTUAL -
      compile-time error message

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      ParameterInference3.java:42: reference to f is ambiguous, both method f(java.lang.Comparable) in generics.ParameterInference3 and method f(java.io.Serializable) in generics.ParameterInference3 match
      f(Utilities.max("abc",new Integer(10)));
                      ^

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------

      package generics;
      import java.util.*;
      import java.io.Serializable;


      public class ParameterInference3 {

      private static void f(String s) {
      System.out.println("String");
      }

      private static void f(Integer s) {
      System.out.println("String");
      }

      private static void f(Comparable e) {
      System.out.println("Comparable");
      }

      private static void f(Serializable e) {
      System.out.println("Serializable");
      }

      private static void f(Object e) {
      System.out.println("Object");
      }


      private static class Utilities {

      public static <T extends Comparable> T max(T arg1, T arg2) {
      return (arg1.compareTo(arg2)>0)?arg1:arg2;
      }
      }






      public static void main(String[] args) {
      f(Utilities.max("abc",new Integer(10)));
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      explicit type parameter specification

      f(Utilities.<Comparable>max("abc",new Integer(10)));
      (Incident Review ID: 193183)
      ======================================================================

            abuckley Alex Buckley
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: