Bug 106106

Summary: [1.5][compiler] Compiler error with Arrays.asList in Java 5 mode?
Product: [Eclipse Project] JDT Reporter: David Mechner <eclipse>
Component: CoreAssignee: Philipe Mulet <philippe_mulet>
Status: VERIFIED FIXED QA Contact:
Severity: critical    
Priority: P3 CC: bmiller, kent_johnson
Version: 3.2   
Target Milestone: 3.1.1   
Hardware: PC   
OS: Windows 2000   
Whiteboard:

Description David Mechner CLA 2005-08-04 17:47:33 EDT
There seems to be a problem with the compiler (I think) that shows up in the
Arrays.toList method. 

The following program displays the problem:  

import java.util.*; 

public class foo {
  public static void main(String[] args) {
    double[][] d = { { 1 } , { 2 } }; 
    List l = Arrays.asList(d); 
    System.out.println("List size: " + l.size());
  }
}

In eclipse, I get the output: 

   List size: 1

Using the Java 5 compiler I get the expected: 

   List size: 2


It seems that the Eclipse compiler doesn't recognize a double[][] as an Object[]
and so wraps it in a new Object[] containing the double[][]. If I add an
explicit cast to Object[] in eclipse I get the expected result: 

public class foo {
  public static void main(String[] args) {
    double[][] d = { { 1 } , { 2 } }; 
    List l = Arrays.asList((Object[]) d); 
    System.out.println("List size: " + l.size());
  }
}

List size: 2
Comment 1 Olivier Thomann CLA 2005-08-05 08:56:04 EDT
If I change the code to look like this:
import java.util.*; 

public class X {
  public static void main(String[] args) {
    double[][] d = { { 1 } , { 2 } }; 
    List<double[]> l = Arrays.asList(d); 
    System.out.println("List size: " + l.size());
  }
}

The problem is more obvious. It doesn't compile and report:
----------
1. ERROR in d:\tests_sources\X.java
 (at line 6)
	List<double[]> l = Arrays.asList(d); 
	                   ^^^^^^^^^^^^^^^^
Type mismatch: cannot convert from List<double[][]> to List<double[]>
----------
1 problem (1 error)
Arrays.asList(d) is resolved to a binding where the parameter type is
double[][][] instead of double[][].
The varargs should "consume" one dimension.

If compiled using 1.4 compliance, it works fine.
Comment 2 Olivier Thomann CLA 2005-08-05 09:46:23 EDT
It seems that it fails only with base type array.
The problem might come from the if statement in
org.eclipse.jdt.internal.compiler.lookup.ParameterizedGenericMethodBinding line 134.
Comment 3 Philipe Mulet CLA 2005-08-12 06:14:14 EDT
Problem indeed comes from abusive removal of one vararg type dimension before
launching inference. Removal should have only occur for a mono dimensional array
type.

Fixed both in 3.1.1 and 3.2 stream. Added VarargsTest#test034.
Comment 4 David Audel CLA 2005-09-21 10:22:39 EDT
Verified in I20050921-0010 for 3.2M2
Comment 5 David Audel CLA 2005-09-26 12:02:09 EDT
Verified using M20050923-1430 for 3.1.1