Tue, 23 Nov 2010 11:08:43 +0000
6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
Summary: 15.12.2.8 should use boxing when expected type in assignment context is a primitive type
Reviewed-by: jjg
1.1 --- a/src/share/classes/com/sun/tools/javac/code/Types.java Thu Nov 18 16:13:11 2010 -0800 1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java Tue Nov 23 11:08:43 2010 +0000 1.3 @@ -2772,6 +2772,8 @@ 1.4 public Type glb(Type t, Type s) { 1.5 if (s == null) 1.6 return t; 1.7 + else if (t.isPrimitive() || s.isPrimitive()) 1.8 + return syms.errType; 1.9 else if (isSubtypeNoCapture(t, s)) 1.10 return t; 1.11 else if (isSubtypeNoCapture(s, t)) 1.12 @@ -2928,6 +2930,15 @@ 1.13 } 1.14 1.15 /** 1.16 + * Return the boxed type if 't' is primitive, otherwise return 't' itself. 1.17 + */ 1.18 + public Type boxedTypeOrType(Type t) { 1.19 + return t.isPrimitive() ? 1.20 + boxedClass(t).type : 1.21 + t; 1.22 + } 1.23 + 1.24 + /** 1.25 * Return the primitive type corresponding to a boxed type. 1.26 */ 1.27 public Type unboxedType(Type t) {
2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Infer.java Thu Nov 18 16:13:11 2010 -0800 2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Infer.java Tue Nov 23 11:08:43 2010 +0000 2.3 @@ -305,7 +305,8 @@ 2.4 uv.hibounds = hibounds.toList(); 2.5 } 2.6 Type qtype1 = types.subst(that.qtype, that.tvars, undetvars); 2.7 - if (!types.isSubtype(qtype1, to)) { 2.8 + if (!types.isSubtype(qtype1, 2.9 + qtype1.tag == UNDETVAR ? types.boxedTypeOrType(to) : to)) { 2.10 throw unambiguousNoInstanceException 2.11 .setMessage("infer.no.conforming.instance.exists", 2.12 that.tvars, that.qtype, to);
3.1 --- a/test/tools/javac/generics/inference/6638712/T6638712a.java Thu Nov 18 16:13:11 2010 -0800 3.2 +++ b/test/tools/javac/generics/inference/6638712/T6638712a.java Tue Nov 23 11:08:43 2010 +0000 3.3 @@ -10,7 +10,7 @@ 3.4 3.5 class T6638712a { 3.6 3.7 - <T> Comparator<T> compound(Iterable<? extends Comparator<? super T>> it) {} 3.8 + <T> Comparator<T> compound(Iterable<? extends Comparator<? super T>> it) { return null; } 3.9 3.10 public void test(List<Comparator<?>> x) { 3.11 Comparator<String> c3 = compound(x);
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/generics/inference/6995200/T6995200.java Tue Nov 23 11:08:43 2010 +0000 4.3 @@ -0,0 +1,67 @@ 4.4 +/* 4.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 + * 4.8 + * This code is free software; you can redistribute it and/or modify it 4.9 + * under the terms of the GNU General Public License version 2 only, as 4.10 + * published by the Free Software Foundation. 4.11 + * 4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4.15 + * version 2 for more details (a copy is included in the LICENSE file that 4.16 + * accompanied this code). 4.17 + * 4.18 + * You should have received a copy of the GNU General Public License version 4.19 + * 2 along with this work; if not, write to the Free Software Foundation, 4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4.21 + * 4.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 4.23 + * or visit www.oracle.com if you need additional information or have any 4.24 + * questions. 4.25 + */ 4.26 + 4.27 +/* 4.28 + * @test 4.29 + * @bug 6995200 4.30 + * 4.31 + * @summary JDK 7 compiler crashes when type-variable is inferred from expected primitive type 4.32 + * @author mcimadamore 4.33 + * @compile T6995200.java 4.34 + * 4.35 + */ 4.36 + 4.37 +import java.util.List; 4.38 + 4.39 +class T6995200 { 4.40 + static <T> T getValue() { 4.41 + return null; 4.42 + } 4.43 + 4.44 + <X> void test() { 4.45 + byte v1 = getValue(); 4.46 + short v2 = getValue(); 4.47 + int v3 = getValue(); 4.48 + long v4 = getValue(); 4.49 + float v5 = getValue(); 4.50 + double v6 = getValue(); 4.51 + String v7 = getValue(); 4.52 + String[] v8 = getValue(); 4.53 + List<String> v9 = getValue(); 4.54 + List<String>[] v10 = getValue(); 4.55 + List<? extends String> v11 = getValue(); 4.56 + List<? extends String>[] v12 = getValue(); 4.57 + List<? super String> v13 = getValue(); 4.58 + List<? super String>[] v14 = getValue(); 4.59 + List<?> v15 = getValue(); 4.60 + List<?>[] v16 = getValue(); 4.61 + X v17 = getValue(); 4.62 + X[] v18 = getValue(); 4.63 + List<X> v19 = getValue(); 4.64 + List<X>[] v20 = getValue(); 4.65 + List<? extends X> v21 = getValue(); 4.66 + List<? extends X>[] v22 = getValue(); 4.67 + List<? super X> v23 = getValue(); 4.68 + List<? super X>[] v24 = getValue(); 4.69 + } 4.70 +}