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

Questionable method resolution based on type inference

    XMLWordPrintable

Details

    • Enhancement
    • Resolution: Unresolved
    • P4
    • None
    • None
    • tools
    • None

    Description

      Based on an Eclipse compiler issue: https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1753.

      The following code

      public class X {

          interface Data {}

          @SuppressWarnings("unchecked")
          public <T extends Data> T getData() {
              return (T) new Data() {};
          }
          
      public static void assertEquals(Short expected, Short actual) {}

      public static void assertEquals(Byte expected, Byte actual) {}

      public static void assertEquals(Object expected, Object actual) {}

          public void testData() {
             assertEquals(getData(), getData());
          }
      }

      gives this compilation error:
      reference to assertEquals is ambiguous assertEquals(getData(), getData()); both method assertEquals(Short,Short) in X and method assertEquals(Byte,Byte) in X match

      I argue that despite this being excused by the JLS (see linked issue above), the error message is not good. `Short` or `Byte` should never be inferred, as the method returns <T extends Data>. These are 2 separate type hierarchies. Furthermore, the compiler has all the information it needs to resolve correctly to the Object overload method.

      Attachments

        Activity

          People

            Unassigned Unassigned
            nlisker Nir Lisker
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: