JDK-6222762 : Primitive arrays and varargs inference leads to crash in TreeMaker.Type(TreeMaker.java:531)
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2005-01-28
  • Updated: 2010-04-02
  • Resolved: 2005-11-24
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 6
6Resolved
Related Reports
Duplicate :  
Relates :  
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
The compiler crashes when compiling this code:

public class GenericsBug {
  
  static <T> void varargs(T...args){
    for(T t : args)
      System.out.println(t);
  }
  
  public static void main(String[]args){
    varargs(new int[]{1, 2, 3}, new int[]{4, 5, 6});
  }
  
}


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Just try to compile the code

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Since the method:
static <T> void varargs(T...args)
should be translated by the compiler to:
static void varargs(Object...args)
and arrays are objects then this code should compile, otherwise the compiler should report an error, but not crash!!
ACTUAL -
An exception has occurred in the compiler (1.5.0). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport)  after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report.  Thank you.
java.lang.AssertionError: unexpected type: <nulltype>
	at com.sun.tools.javac.tree.TreeMaker.Type(TreeMaker.java:531)
	at com.sun.tools.javac.tree.TreeMaker.Type(TreeMaker.java:525)
	at com.sun.tools.javac.comp.Lower.boxArgs(Lower.java:2529)
	at com.sun.tools.javac.comp.Lower.visitApply(Lower.java:2439)
	at com.sun.tools.javac.tree.Tree$Apply.accept(Tree.java:819)
	at com.sun.tools.javac.comp.Lower.translate(Lower.java:1893)
	at com.sun.tools.javac.tree.TreeTranslator.visitExec(TreeTranslator.java:227)
	at com.sun.tools.javac.tree.Tree$Exec.accept(Tree.java:734)
	at com.sun.tools.javac.comp.Lower.translate(Lower.java:1893)
	at com.sun.tools.javac.tree.TreeTranslator.translate(TreeTranslator.java:54)
	at com.sun.tools.javac.tree.TreeTranslator.visitBlock(TreeTranslator.java:145)
	at com.sun.tools.javac.comp.Lower.visitBlock(Lower.java:2950)
	at com.sun.tools.javac.tree.Tree$Block.accept(Tree.java:540)
	at com.sun.tools.javac.comp.Lower.translate(Lower.java:1893)
	at com.sun.tools.javac.tree.TreeTranslator.visitMethodDef(TreeTranslator.java:129)
	at com.sun.tools.javac.comp.Lower.visitMethodDefInternal(Lower.java:2287)
	at com.sun.tools.javac.comp.Lower.visitMethodDef(Lower.java:2206)
	at com.sun.tools.javac.tree.Tree$MethodDef.accept(Tree.java:482)
	at com.sun.tools.javac.comp.Lower.translate(Lower.java:1893)
	at com.sun.tools.javac.comp.Lower.visitClassDef(Lower.java:2001)
	at com.sun.tools.javac.tree.Tree$ClassDef.accept(Tree.java:438)
	at com.sun.tools.javac.comp.Lower.translate(Lower.java:1893)
	at com.sun.tools.javac.comp.Lower.translate(Lower.java:1913)
	at com.sun.tools.javac.comp.Lower.translateTopLevelClass(Lower.java:3087)
	at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:462)
	at com.sun.tools.javac.main.Main.compile(Main.java:592)
	at com.sun.tools.javac.main.Main.compile(Main.java:544)
	at com.sun.tools.javac.Main.compile(Main.java:58)
	at com.sun.tools.javac.Main.main(Main.java:48)


ERROR MESSAGES/STACK TRACES THAT OCCUR :
An exception has occurred in the compiler (1.5.0). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport)  after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report.  Thank you.
java.lang.AssertionError: unexpected type: <nulltype>
	at com.sun.tools.javac.tree.TreeMaker.Type(TreeMaker.java:531)
	at com.sun.tools.javac.tree.TreeMaker.Type(TreeMaker.java:525)
	at com.sun.tools.javac.comp.Lower.boxArgs(Lower.java:2529)
	at com.sun.tools.javac.comp.Lower.visitApply(Lower.java:2439)
	at com.sun.tools.javac.tree.Tree$Apply.accept(Tree.java:819)
	at com.sun.tools.javac.comp.Lower.translate(Lower.java:1893)
	at com.sun.tools.javac.tree.TreeTranslator.visitExec(TreeTranslator.java:227)
	at com.sun.tools.javac.tree.Tree$Exec.accept(Tree.java:734)
	at com.sun.tools.javac.comp.Lower.translate(Lower.java:1893)
	at com.sun.tools.javac.tree.TreeTranslator.translate(TreeTranslator.java:54)
	at com.sun.tools.javac.tree.TreeTranslator.visitBlock(TreeTranslator.java:145)
	at com.sun.tools.javac.comp.Lower.visitBlock(Lower.java:2950)
	at com.sun.tools.javac.tree.Tree$Block.accept(Tree.java:540)
	at com.sun.tools.javac.comp.Lower.translate(Lower.java:1893)
	at com.sun.tools.javac.tree.TreeTranslator.visitMethodDef(TreeTranslator.java:129)
	at com.sun.tools.javac.comp.Lower.visitMethodDefInternal(Lower.java:2287)
	at com.sun.tools.javac.comp.Lower.visitMethodDef(Lower.java:2206)
	at com.sun.tools.javac.tree.Tree$MethodDef.accept(Tree.java:482)
	at com.sun.tools.javac.comp.Lower.translate(Lower.java:1893)
	at com.sun.tools.javac.comp.Lower.visitClassDef(Lower.java:2001)
	at com.sun.tools.javac.tree.Tree$ClassDef.accept(Tree.java:438)
	at com.sun.tools.javac.comp.Lower.translate(Lower.java:1893)
	at com.sun.tools.javac.comp.Lower.translate(Lower.java:1913)
	at com.sun.tools.javac.comp.Lower.translateTopLevelClass(Lower.java:3087)
	at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:462)
	at com.sun.tools.javac.main.Main.compile(Main.java:592)
	at com.sun.tools.javac.main.Main.compile(Main.java:544)
	at com.sun.tools.javac.Main.compile(Main.java:58)
	at com.sun.tools.javac.Main.main(Main.java:48)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class GenericsBug {
  
  static <T> void varargs(T...args){
    for(T t : args)
      System.out.println(t);
  }
  
  public static void main(String[]args){
    varargs(new int[]{1, 2, 3}, new int[]{4, 5, 6});
  }
  
}

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

CUSTOMER SUBMITTED WORKAROUND :
I don't have any, but not to use varargs
Hint:  When I used the Integer wrapper, it woked, i.e,
varargs(new Integer[]{1, 2, 3}, new Integer[]{4, 5, 6});
instead of:
varargs(new int[]{1, 2, 3}, new int[]{4, 5, 6});
###@###.### 2005-1-28 06:42:25 GMT

Comments
SUGGESTED FIX Index: src/share/classes/com/sun/tools/javac/code/Types.java ============================================================ @@ -2633,8 +2745,19 @@ case 0: return syms.botType; case ARRAY_BOUND: - // what about int[] and float[]? - return new ArrayType(lub(Type.map(ts, elemTypeFun)), syms.arrayClass); + List<Type> elements = Type.map(ts, elemTypeFun); + for (Type t : elements) { + if (t.isPrimitive()) { + Type first = ts.head; + for (Type s : ts) { + if (!isSameType(first,s)) { + return arraySuperType(); + } + } + return first; + } + } + return new ArrayType(lub(elements), syms.arrayClass); case CLASS_BOUND: List<Type> cl = null; while (ts.nonEmpty()) { @@ -2645,9 +2768,25 @@ } return compoundMin(cl); default: // boundkind == ARRAY_BOUND | CLASS_BOUND - // What about Cloneable, Serializable, etc? - return syms.objectType; + List<Type> classes = Type.emptyList.prepend(arraySuperType()); + // Filter out any arrays + for (Type t : ts) { + if (t.tag != ARRAY) + classes = classes.prepend(t); + } + return lub(classes); + } + } + + private Type arraySuperType = null; + Type arraySuperType() { + if (arraySuperType == null) { + // All arrays implement Cloneable and Serializable. + List<Type> ts = Type.emptyList.prepend(syms.cloneableType); + ts = ts.prepend(syms.serializableType); + arraySuperType = makeCompoundType(ts, syms.noSymbol, null); } + return arraySuperType; } /** Does type have a result type that is a subtype of the ###@###.### 2005-1-31 10:13:11 GMT
31-01-2005

EVALUATION This is clearly a bug. This program crashes in a different way: public class GenericsBug { <T> void varargs(T...args) {} void test(int[] i1, int[] i2) { varargs(i1, i2); } } However, I suspect that these crashes are symptoms of the same problem with inference and arrays. I have added a number of related bugs. ###@###.### 2005-1-30 20:31:36 GMT
30-01-2005