7123100: javac fails with java.lang.StackOverflowError
Summary: Inference of under-constrained type-variables creates erroneous recursive wildcard types
Reviewed-by: jjg
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Infer.java Wed Jan 11 18:23:24 2012 +0000
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Infer.java Thu Jan 12 15:28:34 2012 +0000
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -332,25 +332,29 @@
//replace uninferred type-vars
targs = types.subst(targs,
that.tvars,
- instaniateAsUninferredVars(undetvars, that.tvars));
+ instantiateAsUninferredVars(undetvars, that.tvars));
}
return chk.checkType(warn.pos(), that.inst(targs, types), to);
}
//where
- private List<Type> instaniateAsUninferredVars(List<Type> undetvars, List<Type> tvars) {
+ private List<Type> instantiateAsUninferredVars(List<Type> undetvars, List<Type> tvars) {
+ Assert.check(undetvars.length() == tvars.length());
ListBuffer<Type> new_targs = ListBuffer.lb();
- //step 1 - create syntethic captured vars
+ //step 1 - create synthetic captured vars
for (Type t : undetvars) {
UndetVar uv = (UndetVar)t;
Type newArg = new CapturedType(t.tsym.name, t.tsym, uv.inst, syms.botType, null);
new_targs = new_targs.append(newArg);
}
//step 2 - replace synthetic vars in their bounds
+ List<Type> formals = tvars;
for (Type t : new_targs.toList()) {
CapturedType ct = (CapturedType)t;
ct.bound = types.subst(ct.bound, tvars, new_targs.toList());
- WildcardType wt = new WildcardType(ct.bound, BoundKind.EXTENDS, syms.boundClass);
+ WildcardType wt = new WildcardType(syms.objectType, BoundKind.UNBOUND, syms.boundClass);
+ wt.bound = (TypeVar)formals.head;
ct.wildcard = wt;
+ formals = formals.tail;
}
return new_targs.toList();
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/cast/7123100/T7123100a.java Thu Jan 12 15:28:34 2012 +0000
@@ -0,0 +1,16 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 7123100
+ * @summary javac fails with java.lang.StackOverflowError
+ * @compile/fail/ref=T7123100a.out -Werror -Xlint:unchecked -XDrawDiagnostics T7123100a.java
+ */
+
+class T7123100a {
+ <E extends Enum<E>> E m() {
+ return null;
+ }
+
+ <Z> void test() {
+ Z z = (Z)m();
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/cast/7123100/T7123100a.out Thu Jan 12 15:28:34 2012 +0000
@@ -0,0 +1,4 @@
+T7123100a.java:14:19: compiler.warn.prob.found.req: (compiler.misc.unchecked.cast.to.type), compiler.misc.type.captureof: 1, ?, Z
+- compiler.err.warnings.and.werror
+1 error
+1 warning
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/cast/7123100/T7123100b.java Thu Jan 12 15:28:34 2012 +0000
@@ -0,0 +1,12 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 7123100
+ * @summary javac fails with java.lang.StackOverflowError
+ * @compile/fail/ref=T7123100b.out -Werror -Xlint:unchecked -XDrawDiagnostics T7123100b.java
+ */
+
+class T7123100b {
+ <Z> void test(Enum<?> e) {
+ Z z = (Z)e;
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/cast/7123100/T7123100b.out Thu Jan 12 15:28:34 2012 +0000
@@ -0,0 +1,4 @@
+T7123100b.java:10:18: compiler.warn.prob.found.req: (compiler.misc.unchecked.cast.to.type), java.lang.Enum<compiler.misc.type.captureof: 1, ?>, Z
+- compiler.err.warnings.and.werror
+1 error
+1 warning
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/cast/7123100/T7123100c.java Thu Jan 12 15:28:34 2012 +0000
@@ -0,0 +1,16 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 7123100
+ * @summary javac fails with java.lang.StackOverflowError
+ * @compile/fail/ref=T7123100c.out -Werror -Xlint:unchecked -XDrawDiagnostics T7123100c.java
+ */
+
+class T7123100c {
+ <E> E m(E e) {
+ return null;
+ }
+
+ <Z> void test(Enum<?> e) {
+ Z z = (Z)m(e);
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/cast/7123100/T7123100c.out Thu Jan 12 15:28:34 2012 +0000
@@ -0,0 +1,4 @@
+T7123100c.java:14:19: compiler.warn.prob.found.req: (compiler.misc.unchecked.cast.to.type), java.lang.Enum<compiler.misc.type.captureof: 1, ?>, Z
+- compiler.err.warnings.and.werror
+1 error
+1 warning
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/cast/7123100/T7123100d.java Thu Jan 12 15:28:34 2012 +0000
@@ -0,0 +1,16 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 7123100
+ * @summary javac fails with java.lang.StackOverflowError
+ * @compile/fail/ref=T7123100d.out -Werror -Xlint:unchecked -XDrawDiagnostics T7123100d.java
+ */
+
+class T7123100d {
+ <E extends Enum<E>> E m(Enum<E> e) {
+ return null;
+ }
+
+ <Z> void test(Enum<?> e) {
+ Z z = (Z)m(e);
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/cast/7123100/T7123100d.out Thu Jan 12 15:28:34 2012 +0000
@@ -0,0 +1,4 @@
+T7123100d.java:14:19: compiler.warn.prob.found.req: (compiler.misc.unchecked.cast.to.type), compiler.misc.type.captureof: 1, ?, Z
+- compiler.err.warnings.and.werror
+1 error
+1 warning