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

Method resolution should stop on static error

XMLWordPrintable

    • b26
    • Not verified

      A DESCRIPTION OF THE PROBLEM :
      Try to compile this:

      public class JavacBrokenError {
        public static void main(String[] args) {
          test(5.0);
        }
        
        void test(double d) {}
        /* static */ void test(Double d) {}
      }

      This produces a wrong error. The error produced is:
      > reference to test is ambiguou
      > both method test(Double) in Test and method test(double) in Test match

      but this error is twice wrong: [A] no, it's not ambiguous; JLS 15.12.2.2-4 clearly state that the lowercase-d double variant is appropriate, as it works in the strict context (15.12.2.2) whereas the capital-D variant requires loose context (15.12.2.3). However, [B] there IS an error here; the invocation to test (which is non-static) is then from a static context and that's not allowed.

      So, the correct, expected behaviour is that javac emits:

      > non-static method test(double) cannot be referenced from a static context

      Cross-checking with ecj: Yes, it gets it right. Any tweaking of this example returns to proper behaviour; add the 'static' keywords and javac does not complain and compiles correctly. Remove one of the two methods, and javac produces the appropriate (non-static method cannot be referenced) error.

      Making only the capital-D-Double method static or not is immaterial; the same bug (an erroneous 'method invocation is ambiguous' error) occurs.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      The bug was hard to explain without a code snippet; all that is needed to reproduce can be found in the 'description' section.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Test.java:3: error: non-static method test(double) cannot be referenced from a static context
      test(5.0);
      ^
      1 error

      ACTUAL -
      Test.java:3: error: reference to test is ambiguous
      test(5.0);
      ^
        both method test(Double) in Test and method test(double) in Test match
      1 error

      ---------- BEGIN SOURCE ----------
      public class JavacBrokenError {
        public static void main(String[] args) {
          test(5.0);
        }
        
        void test(double d) {}
        /* static */ void test(Double d) {}
      }


      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      It's an edge case that isn't particularly likely to come up, and can trivially be worked around by... knowing that the error is misleading. priority low, in other words.


      FREQUENCY : always


            vromero Vicente Arturo Romero Zaldivar
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: