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

Handling of "most specific" varargs method inconsistent with JLS

    Details

    • Subcomponent:
    • CPU:
      x86
    • OS:
      linux

      Description

      FULL PRODUCT VERSION :
      $ -> java -version
      java version "1.6.0_14"
      Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
      Java HotSpot(TM) 64-Bit Server VM (build 14.0-b16, mixed mode)

      $ -> javac -version
      javac 1.6.0_14


      ADDITIONAL OS VERSION INFORMATION :
      Linux 2.6.28-15-generic #49-Ubuntu SMP Tue Aug 18 19:25:34 UTC 2009 x86_64 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      When invoking a method with the following overloaded signatures, I expect an ambiguity error (assuming the arguments are compatible with both):

      int f(Object... args);
      int f(int... args);

      javac treats the second as more specific than the first. This behavior is sensible (I prefer it), but is inconsistent with the JLS (15.12.2).

      Specifics from the spec:
      - Per 15.12.2.2, neither method is applicable by subtyping.
      - Per 15.12.2.3, neither method is applicable by method invocation conversion (allowing boxing).
      - Per 15.12.2.4, both methods are applicable as variable arity methods.
      - Per 15.12.2.5, neither is more specific than the other (int <: Object is not true)

      Note that "most specific" is defined in terms of subtyping, not method invocation conversion.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile the example program.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      A compiler error.
      ACTUAL -
      Successful compilation. Running demonstrates that f(int...) is selected.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class VarargsMostSpecific {

        static int f(Object... args) { return 1; }
        static int f(int... args) { return 2; }

        public static void main(String... args) {
          int result = f(1, 2, 3);
          System.out.println("result: " + result);
        }

      }


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

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                dlsmith Dan Smith
                Reporter:
                ndcosta Nelson Dcosta
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: