Wed, 01 Sep 2010 03:19:16 -0700
6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
Summary: Allow casts which narrow and then unbox.
Reviewed-by: mcimadamore
1.1 --- a/src/share/classes/com/sun/tools/javac/code/Types.java Mon Aug 30 18:03:35 2010 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java Wed Sep 01 03:19:16 2010 -0700 1.3 @@ -915,7 +915,7 @@ 1.4 return true; 1.5 1.6 if (t.isPrimitive() != s.isPrimitive()) 1.7 - return allowBoxing && isConvertible(t, s, warn); 1.8 + return allowBoxing && (isConvertible(t, s, warn) || isConvertible(s, t, warn)); 1.9 1.10 if (warn != warnStack.head) { 1.11 try {
2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Lower.java Mon Aug 30 18:03:35 2010 -0700 2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Lower.java Wed Sep 01 03:19:16 2010 -0700 2.3 @@ -2889,8 +2889,17 @@ 2.4 /** Unbox an object to a primitive value. */ 2.5 JCExpression unbox(JCExpression tree, Type primitive) { 2.6 Type unboxedType = types.unboxedType(tree.type); 2.7 - // note: the "primitive" parameter is not used. There muse be 2.8 - // a conversion from unboxedType to primitive. 2.9 + if (unboxedType.tag == NONE) { 2.10 + unboxedType = primitive; 2.11 + if (!unboxedType.isPrimitive()) 2.12 + throw new AssertionError(unboxedType); 2.13 + make_at(tree.pos()); 2.14 + tree = make.TypeCast(types.boxedClass(unboxedType).type, tree); 2.15 + } else { 2.16 + // There must be a conversion from unboxedType to primitive. 2.17 + if (!types.isSubtype(unboxedType, primitive)) 2.18 + throw new AssertionError(tree); 2.19 + } 2.20 make_at(tree.pos()); 2.21 Symbol valueSym = lookupMethod(tree.pos(), 2.22 unboxedType.tsym.name.append(names.Value), // x.intValue()
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/6979683/TestCast6979683_BAD34.java Wed Sep 01 03:19:16 2010 -0700 3.3 @@ -0,0 +1,40 @@ 3.4 +/* 3.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. 3.11 + * 3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 + * version 2 for more details (a copy is included in the LICENSE file that 3.16 + * accompanied this code). 3.17 + * 3.18 + * You should have received a copy of the GNU General Public License version 3.19 + * 2 along with this work; if not, write to the Free Software Foundation, 3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 + * 3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 3.23 + * or visit www.oracle.com if you need additional information or have any 3.24 + * questions. 3.25 + */ 3.26 + 3.27 +/* 3.28 + * @test 3.29 + * @bug 6979683 3.30 + * @summary Verify that casts can narrow and unbox at the same time 3.31 + * @author jrose 3.32 + * 3.33 + * @compile/fail/ref=TestCast6979683_BAD34.java.errlog -XDrawDiagnostics TestCast6979683_BAD34.java 3.34 + */ 3.35 + 3.36 +public class TestCast6979683_BAD34 { 3.37 + static boolean zconvBAD1(Number o) { return o; } //BAD 3.38 + //... 3.39 + //... 3.40 + //... 3.41 + //... 3.42 + //... 3.43 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/6979683/TestCast6979683_BAD34.java.errlog Wed Sep 01 03:19:16 2010 -0700 4.3 @@ -0,0 +1,2 @@ 4.4 +TestCast6979683_BAD34.java:34:49: compiler.err.prob.found.req: (compiler.misc.incompatible.types), java.lang.Number, boolean 4.5 +1 error
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/test/tools/javac/6979683/TestCast6979683_BAD35.java Wed Sep 01 03:19:16 2010 -0700 5.3 @@ -0,0 +1,40 @@ 5.4 +/* 5.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 5.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5.7 + * 5.8 + * This code is free software; you can redistribute it and/or modify it 5.9 + * under the terms of the GNU General Public License version 2 only, as 5.10 + * published by the Free Software Foundation. 5.11 + * 5.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 5.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 5.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 5.15 + * version 2 for more details (a copy is included in the LICENSE file that 5.16 + * accompanied this code). 5.17 + * 5.18 + * You should have received a copy of the GNU General Public License version 5.19 + * 2 along with this work; if not, write to the Free Software Foundation, 5.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 5.21 + * 5.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 5.23 + * or visit www.oracle.com if you need additional information or have any 5.24 + * questions. 5.25 + */ 5.26 + 5.27 +/* 5.28 + * @test 5.29 + * @bug 6979683 5.30 + * @summary Verify that casts can narrow and unbox at the same time 5.31 + * @author jrose 5.32 + * 5.33 + * @compile/fail/ref=TestCast6979683_BAD35.java.errlog -XDrawDiagnostics TestCast6979683_BAD35.java 5.34 + */ 5.35 + 5.36 +public class TestCast6979683_BAD35 { 5.37 + //... 5.38 + static int iconvBAD1(Number o) { return o; } //BAD: cast needed 5.39 + //... 5.40 + //... 5.41 + //... 5.42 + //... 5.43 +}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/test/tools/javac/6979683/TestCast6979683_BAD35.java.errlog Wed Sep 01 03:19:16 2010 -0700 6.3 @@ -0,0 +1,2 @@ 6.4 +TestCast6979683_BAD35.java:35:45: compiler.err.prob.found.req: (compiler.misc.incompatible.types), java.lang.Number, int 6.5 +1 error
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/test/tools/javac/6979683/TestCast6979683_BAD36.java Wed Sep 01 03:19:16 2010 -0700 7.3 @@ -0,0 +1,40 @@ 7.4 +/* 7.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 7.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 7.7 + * 7.8 + * This code is free software; you can redistribute it and/or modify it 7.9 + * under the terms of the GNU General Public License version 2 only, as 7.10 + * published by the Free Software Foundation. 7.11 + * 7.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 7.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 7.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 7.15 + * version 2 for more details (a copy is included in the LICENSE file that 7.16 + * accompanied this code). 7.17 + * 7.18 + * You should have received a copy of the GNU General Public License version 7.19 + * 2 along with this work; if not, write to the Free Software Foundation, 7.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 7.21 + * 7.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 7.23 + * or visit www.oracle.com if you need additional information or have any 7.24 + * questions. 7.25 + */ 7.26 + 7.27 +/* 7.28 + * @test 7.29 + * @bug 6979683 7.30 + * @summary Verify that casts can narrow and unbox at the same time 7.31 + * @author jrose 7.32 + * 7.33 + * @compile/fail/ref=TestCast6979683_BAD36.java.errlog -XDrawDiagnostics TestCast6979683_BAD36.java 7.34 + */ 7.35 + 7.36 +public class TestCast6979683_BAD36 { 7.37 + //... 7.38 + //... 7.39 + static int iconvBAD2(Comparable<Integer> o) { return o; } //BAD: cast needed 7.40 + //... 7.41 + //... 7.42 + //... 7.43 +}
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/test/tools/javac/6979683/TestCast6979683_BAD36.java.errlog Wed Sep 01 03:19:16 2010 -0700 8.3 @@ -0,0 +1,2 @@ 8.4 +TestCast6979683_BAD36.java:36:58: compiler.err.prob.found.req: (compiler.misc.incompatible.types), java.lang.Comparable<java.lang.Integer>, int 8.5 +1 error
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 9.2 +++ b/test/tools/javac/6979683/TestCast6979683_BAD37.java Wed Sep 01 03:19:16 2010 -0700 9.3 @@ -0,0 +1,40 @@ 9.4 +/* 9.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 9.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 9.7 + * 9.8 + * This code is free software; you can redistribute it and/or modify it 9.9 + * under the terms of the GNU General Public License version 2 only, as 9.10 + * published by the Free Software Foundation. 9.11 + * 9.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 9.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 9.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 9.15 + * version 2 for more details (a copy is included in the LICENSE file that 9.16 + * accompanied this code). 9.17 + * 9.18 + * You should have received a copy of the GNU General Public License version 9.19 + * 2 along with this work; if not, write to the Free Software Foundation, 9.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 9.21 + * 9.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 9.23 + * or visit www.oracle.com if you need additional information or have any 9.24 + * questions. 9.25 + */ 9.26 + 9.27 +/* 9.28 + * @test 9.29 + * @bug 6979683 9.30 + * @summary Verify that casts can narrow and unbox at the same time 9.31 + * @author jrose 9.32 + * 9.33 + * @compile/fail/ref=TestCast6979683_BAD37.java.errlog -XDrawDiagnostics TestCast6979683_BAD37.java 9.34 + */ 9.35 + 9.36 +public class TestCast6979683_BAD37 { 9.37 + //... 9.38 + //... 9.39 + //... 9.40 + static int iconvBAD3(Comparable<Short> o) { return (int)o; } //BAD: wrong instance 9.41 + //... 9.42 + //... 9.43 +}
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 10.2 +++ b/test/tools/javac/6979683/TestCast6979683_BAD37.java.errlog Wed Sep 01 03:19:16 2010 -0700 10.3 @@ -0,0 +1,2 @@ 10.4 +TestCast6979683_BAD37.java:37:61: compiler.err.prob.found.req: (compiler.misc.inconvertible.types), java.lang.Comparable<java.lang.Short>, int 10.5 +1 error
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 11.2 +++ b/test/tools/javac/6979683/TestCast6979683_BAD38.java Wed Sep 01 03:19:16 2010 -0700 11.3 @@ -0,0 +1,40 @@ 11.4 +/* 11.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 11.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 11.7 + * 11.8 + * This code is free software; you can redistribute it and/or modify it 11.9 + * under the terms of the GNU General Public License version 2 only, as 11.10 + * published by the Free Software Foundation. 11.11 + * 11.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 11.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11.15 + * version 2 for more details (a copy is included in the LICENSE file that 11.16 + * accompanied this code). 11.17 + * 11.18 + * You should have received a copy of the GNU General Public License version 11.19 + * 2 along with this work; if not, write to the Free Software Foundation, 11.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 11.21 + * 11.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 11.23 + * or visit www.oracle.com if you need additional information or have any 11.24 + * questions. 11.25 + */ 11.26 + 11.27 +/* 11.28 + * @test 11.29 + * @bug 6979683 11.30 + * @summary Verify that casts can narrow and unbox at the same time 11.31 + * @author jrose 11.32 + * 11.33 + * @compile/fail/ref=TestCast6979683_BAD38.java.errlog -XDrawDiagnostics TestCast6979683_BAD38.java 11.34 + */ 11.35 + 11.36 +public class TestCast6979683_BAD38 { 11.37 + //... 11.38 + //... 11.39 + //... 11.40 + //... 11.41 + static float cconvBAD1(Comparable<Character> o) { return o; } //BAD 11.42 + //... 11.43 +}
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 12.2 +++ b/test/tools/javac/6979683/TestCast6979683_BAD38.java.errlog Wed Sep 01 03:19:16 2010 -0700 12.3 @@ -0,0 +1,2 @@ 12.4 +TestCast6979683_BAD38.java:38:62: compiler.err.prob.found.req: (compiler.misc.incompatible.types), java.lang.Comparable<java.lang.Character>, float 12.5 +1 error
13.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 13.2 +++ b/test/tools/javac/6979683/TestCast6979683_BAD39.java Wed Sep 01 03:19:16 2010 -0700 13.3 @@ -0,0 +1,40 @@ 13.4 +/* 13.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 13.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 13.7 + * 13.8 + * This code is free software; you can redistribute it and/or modify it 13.9 + * under the terms of the GNU General Public License version 2 only, as 13.10 + * published by the Free Software Foundation. 13.11 + * 13.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 13.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13.15 + * version 2 for more details (a copy is included in the LICENSE file that 13.16 + * accompanied this code). 13.17 + * 13.18 + * You should have received a copy of the GNU General Public License version 13.19 + * 2 along with this work; if not, write to the Free Software Foundation, 13.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 13.21 + * 13.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 13.23 + * or visit www.oracle.com if you need additional information or have any 13.24 + * questions. 13.25 + */ 13.26 + 13.27 +/* 13.28 + * @test 13.29 + * @bug 6979683 13.30 + * @summary Verify that casts can narrow and unbox at the same time 13.31 + * @author jrose 13.32 + * 13.33 + * @compile/fail/ref=TestCast6979683_BAD39.java.errlog -XDrawDiagnostics TestCast6979683_BAD39.java 13.34 + */ 13.35 + 13.36 +public class TestCast6979683_BAD39 { 13.37 + //... 13.38 + //... 13.39 + //... 13.40 + //... 13.41 + //... 13.42 + static float cconvBAD2(Number o) { return (char)o; } //BAD 13.43 +}
14.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 14.2 +++ b/test/tools/javac/6979683/TestCast6979683_BAD39.java.errlog Wed Sep 01 03:19:16 2010 -0700 14.3 @@ -0,0 +1,2 @@ 14.4 +TestCast6979683_BAD39.java:39:53: compiler.err.prob.found.req: (compiler.misc.inconvertible.types), java.lang.Number, char 14.5 +1 error
15.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 15.2 +++ b/test/tools/javac/6979683/TestCast6979683_GOOD.java Wed Sep 01 03:19:16 2010 -0700 15.3 @@ -0,0 +1,111 @@ 15.4 +/* 15.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 15.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 15.7 + * 15.8 + * This code is free software; you can redistribute it and/or modify it 15.9 + * under the terms of the GNU General Public License version 2 only, as 15.10 + * published by the Free Software Foundation. 15.11 + * 15.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 15.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 15.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15.15 + * version 2 for more details (a copy is included in the LICENSE file that 15.16 + * accompanied this code). 15.17 + * 15.18 + * You should have received a copy of the GNU General Public License version 15.19 + * 2 along with this work; if not, write to the Free Software Foundation, 15.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 15.21 + * 15.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 15.23 + * or visit www.oracle.com if you need additional information or have any 15.24 + * questions. 15.25 + */ 15.26 + 15.27 +/* 15.28 + * @test 15.29 + * @bug 6979683 15.30 + * @summary Verify that casts can narrow and unbox at the same time 15.31 + * @author jrose 15.32 + * 15.33 + * @compile TestCast6979683_GOOD.java 15.34 + * @run main TestCast6979683_GOOD 15.35 + */ 15.36 + 15.37 +public class TestCast6979683_GOOD { 15.38 + public static void main(String... av) { 15.39 + bugReportExample(); 15.40 + for (int x = -1; x <= 2; x++) { 15.41 + zconvTests(x != 0); 15.42 + iconvTests(x); 15.43 + bconvTests((byte)x); 15.44 + cconvTests((char)x); 15.45 + } 15.46 + System.out.println("Successfully ran "+tests+" tests."); 15.47 + } 15.48 + 15.49 + static int tests; 15.50 + static void assertEquals(Object x, Object y) { 15.51 + if (!x.equals(y)) { 15.52 + throw new RuntimeException("assertEquals: "+x+" != "+y); 15.53 + } 15.54 + ++tests; 15.55 + } 15.56 + 15.57 + static void bugReportExample() { 15.58 + {} // example in bug report: 15.59 + Object x = (Object)1; 15.60 + int y = (int)x; 15.61 + {} // end example 15.62 + } 15.63 + 15.64 + static boolean zconv1(Boolean o) { return o; } 15.65 + static boolean zconv2(Object o) { return (boolean)o; } 15.66 + static boolean zconv3(Comparable<Boolean> o) { return (boolean)o; } 15.67 + 15.68 + static void zconvTests(boolean x) { 15.69 + assertEquals(x, zconv1(x)); 15.70 + assertEquals(x, zconv2(x)); 15.71 + assertEquals(x, zconv3(x)); 15.72 + } 15.73 + 15.74 + static int iconv1(Integer o) { return o; } 15.75 + static int iconv2(Object o) { return (int)o; } 15.76 + static int iconv3(java.io.Serializable o) { return (int)o; } 15.77 + static int iconv4(Number o) { return (int)o; } 15.78 + static int iconv5(Comparable<Integer> o) { return (int)o; } 15.79 + 15.80 + static void iconvTests(int x) { 15.81 + assertEquals(x, iconv1(x)); 15.82 + assertEquals(x, iconv2(x)); 15.83 + assertEquals(x, iconv3(x)); 15.84 + assertEquals(x, iconv4(x)); 15.85 + assertEquals(x, iconv5(x)); 15.86 + } 15.87 + 15.88 + static float bconv1(Byte o) { return o; } // note type "float" 15.89 + static float bconv2(Object o) { return (byte)o; } 15.90 + static float bconv3(java.io.Serializable o) { return (byte)o; } 15.91 + static float bconv4(Number o) { return (byte)o; } 15.92 + 15.93 + static void bconvTests(byte x) { 15.94 + float xf = x; 15.95 + assertEquals(xf, bconv1(x)); 15.96 + assertEquals(xf, bconv2(x)); 15.97 + assertEquals(xf, bconv3(x)); 15.98 + assertEquals(xf, bconv4(x)); 15.99 + } 15.100 + 15.101 + static float cconv1(Character o) { return o; } // note type "float" 15.102 + static float cconv2(Object o) { return (char)o; } 15.103 + static float cconv3(java.io.Serializable o) { return (char)o; } 15.104 + static float cconv4(Comparable<Character> o) { return (char)o; } 15.105 + 15.106 + static void cconvTests(char x) { 15.107 + float xf = x; 15.108 + assertEquals(xf, cconv1(x)); 15.109 + assertEquals(xf, cconv2(x)); 15.110 + assertEquals(xf, cconv3(x)); 15.111 + assertEquals(xf, cconv4(x)); 15.112 + } 15.113 + 15.114 +}