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

Confusing error message after ambiguous overload resolution

XMLWordPrintable

      The following class (provided by Stephan Herrmann) fails to compile, as expected, but produces two error messages. The second is unnecessary and confusing. It also depends on the order of the 'execute1' declarations -- apparently the first declaration is arbitrarily chosen if an ambiguity occurs, so reordering eliminates the second error message.

      Source:

      class HermannCompatibility {

         // generic method
         interface ConsumerA {
             <T> void accept(int i);
         }

         // non-generic
         interface ConsumerB {
             void accept(int i);
         }

         void execute1(ConsumerA c) {}
         void execute1(ConsumerB c) {}

         void test() {
             execute1(x -> {});
         }
      }

      Output:

      HermannCompatibility.java:17: error: reference to execute1 is ambiguous
             execute1(x -> {}); // compiles in Eclipse, not with javac
             ^
        both method execute1(ConsumerA) in HermannCompatibility and method execute1(ConsumerB) in HermannCompatibility match

      HermannCompatibility.java:17: error: incompatible types: invalid functional descriptor for lambda expression
             execute1(x -> {}); // compiles in Eclipse, not with javac
                      ^
          method <T>(int)void in interface ConsumerA is generic
        where T is a type-variable:
          T extends Object declared in method <T>accept(int)

            dlsmith Dan Smith
            dlsmith Dan Smith
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: