Thu, 03 Feb 2011 09:36:28 +0000
5017953: spurious cascaded diagnostics when name not found
Summary: when an operator is applied to one or more erroneous operands, spurious diagnostics are generated
Reviewed-by: jjg
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | */ |
aoqi@0 | 23 | |
aoqi@0 | 24 | /* |
aoqi@0 | 25 | * @test |
aoqi@0 | 26 | * @bug 7006109 |
aoqi@0 | 27 | * @summary Add test library to simplify the task of writing automated type-system tests |
aoqi@0 | 28 | * @author mcimadamore |
aoqi@0 | 29 | * @library . |
aoqi@0 | 30 | * @run main PrimitiveConversionTest |
aoqi@0 | 31 | */ |
aoqi@0 | 32 | |
aoqi@0 | 33 | import com.sun.tools.javac.code.Type; |
aoqi@0 | 34 | import com.sun.tools.javac.code.Type.*; |
aoqi@0 | 35 | import com.sun.tools.javac.code.Symbol.*; |
aoqi@0 | 36 | import java.lang.reflect.Array; |
aoqi@0 | 37 | import java.util.EnumSet; |
aoqi@0 | 38 | |
aoqi@0 | 39 | /** |
aoqi@0 | 40 | * Check invariants in assignment/method conversion involving primitive types and arrays |
aoqi@0 | 41 | */ |
aoqi@0 | 42 | public class PrimitiveConversionTest extends TypeHarness { |
aoqi@0 | 43 | |
aoqi@0 | 44 | Type[] types1; |
aoqi@0 | 45 | Type[] types2; |
aoqi@0 | 46 | Type[] types3; |
aoqi@0 | 47 | |
aoqi@0 | 48 | enum Result { |
aoqi@0 | 49 | OK_BOTH(true), |
aoqi@0 | 50 | FAIL_BOTH(false), |
aoqi@0 | 51 | OK_ASSIGN_ONLY(true); |
aoqi@0 | 52 | |
aoqi@0 | 53 | boolean value; |
aoqi@0 | 54 | |
aoqi@0 | 55 | Result(boolean value) { |
aoqi@0 | 56 | this.value = value; |
aoqi@0 | 57 | } |
aoqi@0 | 58 | } |
aoqi@0 | 59 | |
aoqi@0 | 60 | enum ConversionKind { |
aoqi@0 | 61 | ASSIGNMENT_CONVERSION(EnumSet.of(Result.OK_BOTH, Result.OK_ASSIGN_ONLY)) { |
aoqi@0 | 62 | @Override |
aoqi@0 | 63 | void check(TypeHarness harness, Type from, Type to, Result expected) { |
aoqi@0 | 64 | harness.assertAssignable(from, to, resSet.contains(expected)); |
aoqi@0 | 65 | } |
aoqi@0 | 66 | }, |
aoqi@0 | 67 | METHOD_CONVERSION(EnumSet.of(Result.OK_BOTH)) { |
aoqi@0 | 68 | @Override |
aoqi@0 | 69 | void check(TypeHarness harness, Type from, Type to, Result expected) { |
aoqi@0 | 70 | harness.assertConvertible(from, to, resSet.contains(expected)); |
aoqi@0 | 71 | } |
aoqi@0 | 72 | }; |
aoqi@0 | 73 | |
aoqi@0 | 74 | EnumSet<Result> resSet; |
aoqi@0 | 75 | |
aoqi@0 | 76 | private ConversionKind(EnumSet<Result> resSet) { |
aoqi@0 | 77 | this.resSet = resSet; |
aoqi@0 | 78 | } |
aoqi@0 | 79 | |
aoqi@0 | 80 | abstract void check(TypeHarness harness, Type from, Type to, Result expected); |
aoqi@0 | 81 | } |
aoqi@0 | 82 | |
aoqi@0 | 83 | enum TestKind { |
aoqi@0 | 84 | SIMPLE { |
aoqi@0 | 85 | @Override |
aoqi@0 | 86 | Type[] getFromTypes(PrimitiveConversionTest harness) { |
aoqi@0 | 87 | return harness.types1; |
aoqi@0 | 88 | } |
aoqi@0 | 89 | @Override |
aoqi@0 | 90 | Type[] getToTypes(PrimitiveConversionTest harness) { |
aoqi@0 | 91 | return harness.types1; |
aoqi@0 | 92 | } |
aoqi@0 | 93 | @Override |
aoqi@0 | 94 | Result[][] getResults(PrimitiveConversionTest harness) { |
aoqi@0 | 95 | return harness.results1; |
aoqi@0 | 96 | } |
aoqi@0 | 97 | }, |
aoqi@0 | 98 | CONSTANT_TYPES { |
aoqi@0 | 99 | @Override |
aoqi@0 | 100 | Type[] getFromTypes(PrimitiveConversionTest harness) { |
aoqi@0 | 101 | return harness.types2; |
aoqi@0 | 102 | } |
aoqi@0 | 103 | @Override |
aoqi@0 | 104 | Type[] getToTypes(PrimitiveConversionTest harness) { |
aoqi@0 | 105 | return harness.types3; |
aoqi@0 | 106 | } |
aoqi@0 | 107 | @Override |
aoqi@0 | 108 | Result[][] getResults(PrimitiveConversionTest harness) { |
aoqi@0 | 109 | return harness.results2; |
aoqi@0 | 110 | } |
aoqi@0 | 111 | }; |
aoqi@0 | 112 | |
aoqi@0 | 113 | abstract Type[] getFromTypes(PrimitiveConversionTest harness); |
aoqi@0 | 114 | abstract Type[] getToTypes(PrimitiveConversionTest harness); |
aoqi@0 | 115 | abstract Result[][] getResults(PrimitiveConversionTest harness); |
aoqi@0 | 116 | } |
aoqi@0 | 117 | |
aoqi@0 | 118 | static final Result T = Result.OK_BOTH; |
aoqi@0 | 119 | static final Result F = Result.FAIL_BOTH; |
aoqi@0 | 120 | static final Result A = Result.OK_ASSIGN_ONLY; |
aoqi@0 | 121 | |
aoqi@0 | 122 | Result[][] results1 = { |
aoqi@0 | 123 | //byte, short, int, long, float, double, char, bool, C1, C2, C3, T , byte[], short[], int[], long[], float[], double[], char[], bool[], C1[], C2[], C3[], T[] |
aoqi@0 | 124 | /*byte*/ { T , T , T , T , T , T , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F }, |
aoqi@0 | 125 | /*short*/ { F , T , T , T , T , T , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F }, |
aoqi@0 | 126 | /*int*/ { F , F , T , T , T , T , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F }, |
aoqi@0 | 127 | /*long*/ { F , F , F , T , T , T , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F }, |
aoqi@0 | 128 | /*float*/ { F , F , F , F , T , T , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F }, |
aoqi@0 | 129 | /*double*/ { F , F , F , F , F , T , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F }, |
aoqi@0 | 130 | /*char*/ { F , F , T , T , T , T , T , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F }, |
aoqi@0 | 131 | /*bool*/ { F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F }, |
aoqi@0 | 132 | /*C1*/ { F , F , F , F , F , F , F , F , T , F , T , F , F , F , F , F , F , F , F , F , F , F , F , F }, |
aoqi@0 | 133 | /*C2*/ { F , F , F , F , F , F , F , F , T , T , T , F , F , F , F , F , F , F , F , F , F , F , F , F }, |
aoqi@0 | 134 | /*C3*/ { F , F , F , F , F , F , F , F , T , F , T , F , F , F , F , F , F , F , F , F , F , F , F , F }, |
aoqi@0 | 135 | /*T*/ { F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , F , F , F , F , F }, |
aoqi@0 | 136 | /*byte[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , F , F , F , F }, |
aoqi@0 | 137 | /*short[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , F , F , F }, |
aoqi@0 | 138 | /*int[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , F , F }, |
aoqi@0 | 139 | /*long[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , F }, |
aoqi@0 | 140 | /*float[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F , F , F , F }, |
aoqi@0 | 141 | /*double[]*/{ F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F , F , F }, |
aoqi@0 | 142 | /*char[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F , F }, |
aoqi@0 | 143 | /*bool[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F }, |
aoqi@0 | 144 | /*C1[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , T , F }, |
aoqi@0 | 145 | /*C2[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , T , T , F }, |
aoqi@0 | 146 | /*C3[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , T , F }, |
aoqi@0 | 147 | /*T[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T }}; |
aoqi@0 | 148 | |
aoqi@0 | 149 | Result[][] results2 = { |
aoqi@0 | 150 | //byte, short, int, long, float, double, char, bool |
aoqi@0 | 151 | /*byte*/ { T , T , T , T , T , T , F , F }, |
aoqi@0 | 152 | /*short*/ { F , T , T , T , T , T , F , F }, |
aoqi@0 | 153 | /*short1*/ { A , T , T , T , T , T , A , F }, |
aoqi@0 | 154 | /*short2*/ { F , T , T , T , T , T , A , F }, |
aoqi@0 | 155 | /*int*/ { F , F , T , T , T , T , F , F }, |
aoqi@0 | 156 | /*int1*/ { A , A , T , T , T , T , A , F }, |
aoqi@0 | 157 | /*int2*/ { F , A , T , T , T , T , A , F }, |
aoqi@0 | 158 | /*int4*/ { F , F , T , T , T , T , F , F }, |
aoqi@0 | 159 | /*long*/ { F , F , F , T , T , T , F , F }, |
aoqi@0 | 160 | /*long1*/ { F , F , F , T , T , T , F , F }, |
aoqi@0 | 161 | /*long2*/ { F , F , F , T , T , T , F , F }, |
aoqi@0 | 162 | /*long4*/ { F , F , F , T , T , T , F , F }, |
aoqi@0 | 163 | /*long8*/ { F , F , F , T , T , T , F , F }, |
aoqi@0 | 164 | /*float*/ { F , F , F , F , T , T , F , F }, |
aoqi@0 | 165 | /*float1*/ { F , F , F , F , T , T , F , F }, |
aoqi@0 | 166 | /*float2*/ { F , F , F , F , T , T , F , F }, |
aoqi@0 | 167 | /*float4*/ { F , F , F , F , T , T , F , F }, |
aoqi@0 | 168 | /*double*/ { F , F , F , F , F , T , F , F }, |
aoqi@0 | 169 | /*double1*/ { F , F , F , F , F , T , F , F }, |
aoqi@0 | 170 | /*double2*/ { F , F , F , F , F , T , F , F }, |
aoqi@0 | 171 | /*double4*/ { F , F , F , F , F , T , F , F }, |
aoqi@0 | 172 | /*double8*/ { F , F , F , F , F , T , F , F }, |
aoqi@0 | 173 | /*char*/ { F , F , T , T , T , T , T , F }, |
aoqi@0 | 174 | /*char1*/ { A , A , T , T , T , T , T , F }, |
aoqi@0 | 175 | /*char2*/ { F , A , T , T , T , T , T , F }, |
aoqi@0 | 176 | /*bool*/ { F , F , F , F , F , F , F , T }}; |
aoqi@0 | 177 | |
aoqi@0 | 178 | PrimitiveConversionTest() { |
aoqi@0 | 179 | Type[] primitiveTypes = new Type[] { |
aoqi@0 | 180 | predef.byteType, |
aoqi@0 | 181 | predef.shortType, |
aoqi@0 | 182 | predef.intType, |
aoqi@0 | 183 | predef.longType, |
aoqi@0 | 184 | predef.floatType, |
aoqi@0 | 185 | predef.doubleType, |
aoqi@0 | 186 | predef.charType, |
aoqi@0 | 187 | predef.booleanType }; |
aoqi@0 | 188 | |
aoqi@0 | 189 | ClassType c1 = fac.Class(fac.TypeVariable()); |
aoqi@0 | 190 | ClassType c2 = fac.Class(); |
aoqi@0 | 191 | c2.supertype_field = subst(c1, |
aoqi@0 | 192 | Mapping(c1.getTypeArguments().head, predef.stringType)); |
aoqi@0 | 193 | Type c3 = erasure(c1); |
aoqi@0 | 194 | |
aoqi@0 | 195 | Type[] referenceTypes = { |
aoqi@0 | 196 | subst(c1, |
aoqi@0 | 197 | Mapping(c1.getTypeArguments().head, predef.stringType)), |
aoqi@0 | 198 | c2, |
aoqi@0 | 199 | c3, |
aoqi@0 | 200 | fac.TypeVariable() }; |
aoqi@0 | 201 | |
aoqi@0 | 202 | Type[] arrayTypes = new Type[primitiveTypes.length + referenceTypes.length]; |
aoqi@0 | 203 | int idx = 0; |
aoqi@0 | 204 | for (Type t : join(Type.class, primitiveTypes, referenceTypes)) { |
aoqi@0 | 205 | arrayTypes[idx++] = fac.Array(t); |
aoqi@0 | 206 | } |
aoqi@0 | 207 | |
aoqi@0 | 208 | types1 = join(Type.class, primitiveTypes, referenceTypes, arrayTypes); |
aoqi@0 | 209 | |
aoqi@0 | 210 | types2 = new Type[] { |
aoqi@0 | 211 | predef.byteType, |
aoqi@0 | 212 | predef.shortType, |
aoqi@0 | 213 | fac.Constant((short)0x0001), |
aoqi@0 | 214 | fac.Constant((short)0x0100), |
aoqi@0 | 215 | predef.intType, |
aoqi@0 | 216 | fac.Constant((int)0x0000_0001), |
aoqi@0 | 217 | fac.Constant((int)0x0000_0100), |
aoqi@0 | 218 | fac.Constant((int)0x0001_0000), |
aoqi@0 | 219 | predef.longType, |
aoqi@0 | 220 | fac.Constant((long)0x0000_0000_0000_0001L), |
aoqi@0 | 221 | fac.Constant((long)0x0000_0000_0000_0100L), |
aoqi@0 | 222 | fac.Constant((long)0x0000_0000_0001_0000L), |
aoqi@0 | 223 | fac.Constant((long)0x0001_0000_0000_0000L), |
aoqi@0 | 224 | predef.floatType, |
aoqi@0 | 225 | fac.Constant((float)0x0000_0001), |
aoqi@0 | 226 | fac.Constant((float)0x0000_0100), |
aoqi@0 | 227 | fac.Constant((float)0x0001_0000), |
aoqi@0 | 228 | predef.doubleType, |
aoqi@0 | 229 | fac.Constant((double)0x0000_0000_0000_0001L), |
aoqi@0 | 230 | fac.Constant((double)0x0000_0000_0000_0100L), |
aoqi@0 | 231 | fac.Constant((double)0x0000_0000_0001_0000L), |
aoqi@0 | 232 | fac.Constant((double)0x0001_0000_0000_0000L), |
aoqi@0 | 233 | predef.charType, |
aoqi@0 | 234 | fac.Constant((char)0x0001), |
aoqi@0 | 235 | fac.Constant((char)0x0100), |
aoqi@0 | 236 | predef.booleanType |
aoqi@0 | 237 | }; |
aoqi@0 | 238 | |
aoqi@0 | 239 | types3 = primitiveTypes; |
aoqi@0 | 240 | } |
aoqi@0 | 241 | |
aoqi@0 | 242 | void testConversion(ConversionKind convKind, TestKind testKind) { |
aoqi@0 | 243 | Type[] rows = testKind.getFromTypes(this); |
aoqi@0 | 244 | Type[] cols = testKind.getToTypes(this); |
aoqi@0 | 245 | for (int i = 0; i < rows.length ; i++) { |
aoqi@0 | 246 | for (int j = 0; j < cols.length ; j++) { |
aoqi@0 | 247 | convKind.check(this, rows[i], cols[j], testKind.getResults(this)[i][j]); |
aoqi@0 | 248 | } |
aoqi@0 | 249 | } |
aoqi@0 | 250 | } |
aoqi@0 | 251 | |
aoqi@0 | 252 | @SuppressWarnings("unchecked") |
aoqi@0 | 253 | <T> T[] join(Class<T> type, T[]... args) { |
aoqi@0 | 254 | int totalLength = 0; |
aoqi@0 | 255 | for (T[] arr : args) { |
aoqi@0 | 256 | totalLength += arr.length; |
aoqi@0 | 257 | } |
aoqi@0 | 258 | T[] new_arr = (T[])Array.newInstance(type, totalLength); |
aoqi@0 | 259 | int idx = 0; |
aoqi@0 | 260 | for (T[] arr : args) { |
aoqi@0 | 261 | System.arraycopy(arr, 0, new_arr, idx, arr.length); |
aoqi@0 | 262 | idx += arr.length; |
aoqi@0 | 263 | } |
aoqi@0 | 264 | return new_arr; |
aoqi@0 | 265 | } |
aoqi@0 | 266 | |
aoqi@0 | 267 | public static void main(String[] args) { |
aoqi@0 | 268 | PrimitiveConversionTest harness = new PrimitiveConversionTest(); |
aoqi@0 | 269 | for (ConversionKind convKind : ConversionKind.values()) { |
aoqi@0 | 270 | for (TestKind testKind : TestKind.values()) { |
aoqi@0 | 271 | harness.testConversion(convKind, testKind); |
aoqi@0 | 272 | } |
aoqi@0 | 273 | } |
aoqi@0 | 274 | } |
aoqi@0 | 275 | } |