Bug 106106 - [1.5][compiler] Compiler error with Arrays.asList in Java 5 mode?
Summary: [1.5][compiler] Compiler error with Arrays.asList in Java 5 mode?
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows 2000
: P3 critical (vote)
Target Milestone: 3.1.1   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-08-04 17:47 EDT by David Mechner CLA
Modified: 2005-09-26 12:02 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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