JDK-6998691 : Vararg method overloading fails
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 6u22
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux_suse_sles_9
  • CPU: x86
  • Submitted: 2010-11-09
  • Updated: 2012-03-20
  • Resolved: 2011-06-04
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7-poolResolved
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
Java(TM) SE Runtime Environment (build 1.6.0_04-b12)
Java HotSpot(TM) 64-Bit Server VM (build 10.0-b19, mixed mode)

A DESCRIPTION OF THE PROBLEM :
The following code is perfectly compilable by itself:

public class OverloadTest {
    public static int min(int elt, int... elts) {
        int min = elt;
        for (int i : elts) {
            if (i < min) {
                min = i;
            }
        }
        return min;
    }
 
    public static long min(long elt, long... elts) {
        long min = elt;
        for (long l: elts) {
            if (l < min) {
                min = l;
            }
        }
        return min;
    }
 
//    public static void main(String[] args) {
//        int m = min(1, 2, 3);
//    }
}

Complier does distiguish the two signatures!
 
However, an attempt to use any of those methods causes compile-time error:
            Error: (27, 17) reference to min is ambiguous, both method min(int, int���) in OverloadTest and min(long, long ���) in OverloadTest match

The problem existed since Java 1.5



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
With main method commented out compile the OverloadTest class - it goes without any errors.
Uncomment the main method and try to compile - compiler will error


REPRODUCIBILITY :
This bug can be reproduced always.

CUSTOMER SUBMITTED WORKAROUND :
Replacing vararg with primitive arrays works

Comments
EVALUATION This fails in other compilers too. The problem is that the compiler performs a 'fake' most specific step between: m(int, int[]) and m(int, long[]) which fails (neither is most specific) because primitive arrays are not covariant. However the JLS most specific algorithm for variable arity methods does not take into account the (synthetic) array type, which means the algorithm succeeds.
01-12-2010