OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 package com.google.dart.compiler.resolver; |
| 6 |
| 7 import com.google.common.base.Joiner; |
| 8 import com.google.dart.compiler.DartCompilerErrorCode; |
| 9 |
| 10 /** |
| 11 * Tests the code in {@link CompileTimeConstantVisitor} |
| 12 */ |
| 13 public class CompileTimeConstantTest extends ResolverTestCase{ |
| 14 |
| 15 // TODO(zundel) This test should pass, but the compiler doesn't currently |
| 16 // recursively resolve types in CompileTimeConstVisitor |
| 17 public void disabledTestForwardLookupExpressions() { |
| 18 resolveAndTest(Joiner.on("\n").join( |
| 19 "class Object {}", |
| 20 "class A {", |
| 21 " static final value1 = value2 * 2;", |
| 22 " static final value2 = value3 * 4;", |
| 23 " static final value3 = 8;", |
| 24 "}")); |
| 25 } |
| 26 |
| 27 public void testConstantBinaryExpression() { |
| 28 resolveAndTest(Joiner.on("\n").join( |
| 29 "class Object {}", |
| 30 "class A {", |
| 31 " static final INT_LIT = 5;", |
| 32 " static final INT_LIT_REF = INT_LIT;", |
| 33 " static final DOUBLE_LIT = 1.5;", |
| 34 " static final BOOL_LIT = true;", |
| 35 " static final STRING_LIT = \"Hello\";", |
| 36 " static final BOP1_0 = INT_LIT + 1;", |
| 37 " static final BOP1_1 = 1 + INT_LIT;", |
| 38 " static final BOP1_2 = INT_LIT - 1;", |
| 39 " static final BOP1_3 = 1 - INT_LIT;", |
| 40 " static final BOP1_4 = INT_LIT * 1;", |
| 41 " static final BOP1_5 = 1 * INT_LIT;", |
| 42 " static final BOP1_6 = INT_LIT / 1;", |
| 43 " static final BOP1_7 = 1 / INT_LIT;", |
| 44 " static final BOP2_0 = DOUBLE_LIT + 1.5;", |
| 45 " static final BOP2_1 = 1.5 + DOUBLE_LIT;", |
| 46 " static final BOP2_2 = DOUBLE_LIT - 1.5;", |
| 47 " static final BOP2_3 = 1.5 - DOUBLE_LIT;", |
| 48 " static final BOP2_4 = DOUBLE_LIT * 1.5;", |
| 49 " static final BOP2_5 = 1.5 * DOUBLE_LIT;", |
| 50 " static final BOP2_6 = DOUBLE_LIT / 1.5;", |
| 51 " static final BOP2_7 = 1.5 / DOUBLE_LIT;", |
| 52 " static final BOP3_0 = 2 < INT_LIT;", |
| 53 " static final BOP3_1 = INT_LIT < 2;", |
| 54 " static final BOP3_2 = 2 > INT_LIT;", |
| 55 " static final BOP3_3 = INT_LIT > 2;", |
| 56 " static final BOP3_4 = 2 < DOUBLE_LIT;", |
| 57 " static final BOP3_5 = DOUBLE_LIT < 2;", |
| 58 " static final BOP3_6 = 2 > DOUBLE_LIT;", |
| 59 " static final BOP3_7 = DOUBLE_LIT > 2;", |
| 60 " static final BOP3_8 = 2 <= INT_LIT;", |
| 61 " static final BOP3_9 = INT_LIT <= 2;", |
| 62 " static final BOP3_10 = 2 >= INT_LIT;", |
| 63 " static final BOP3_11 = INT_LIT >= 2;", |
| 64 " static final BOP3_12 = 2.0 <= DOUBLE_LIT;", |
| 65 " static final BOP3_13 = DOUBLE_LIT <= 2.0;", |
| 66 " static final BOP3_14 = 2.0 >= DOUBLE_LIT;", |
| 67 " static final BOP3_15 = DOUBLE_LIT >= 2;", |
| 68 " static final BOP4_0 = 5 % INT_LIT;", |
| 69 " static final BOP4_1 = INT_LIT % 5;", |
| 70 " static final BOP4_2 = 5.0 % DOUBLE_LIT;", |
| 71 " static final BOP4_3 = DOUBLE_LIT % 5.0;", |
| 72 " static final BOP5_0 = 0x80 & 0x04;", |
| 73 " static final BOP5_1 = 0x80 | 0x04;", |
| 74 " static final BOP5_2 = 0x80 << 0x04;", |
| 75 " static final BOP5_3 = 0x80 >> 0x04;", |
| 76 " static final BOP5_4 = 0x80 ~/ 0x04;", |
| 77 " static final BOP5_5 = 0x80 ^ 0x04;", |
| 78 " static final BOP6 = BOOL_LIT && true;", |
| 79 " static final BOP7 = false || BOOL_LIT;", |
| 80 " static final BOP8 = STRING_LIT == \"World!\";", |
| 81 " static final BOP9 = \"Hello\" != STRING_LIT;", |
| 82 " static final BOP10 = INT_LIT === INT_LIT_REF;", |
| 83 " static final BOP11 = BOOL_LIT !== true;", |
| 84 "}")); |
| 85 |
| 86 resolveAndTest(Joiner.on("\n").join( |
| 87 "class Object {}", |
| 88 "class int {}", |
| 89 "class A {", |
| 90 " static int foo() { return 1; }", |
| 91 "}", |
| 92 "class B {", |
| 93 " static final BOP1 = A.foo() * 1;", |
| 94 " static final BOP2 = 1 * A.foo();", |
| 95 "}"), |
| 96 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, |
| 97 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, |
| 98 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, |
| 99 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER); |
| 100 |
| 101 resolveAndTest(Joiner.on("\n").join( |
| 102 "class Object {}", |
| 103 "class int {}", |
| 104 "class String {}", |
| 105 "class A {", |
| 106 " static int foo() { return 1; }", |
| 107 " static String bar() { return \"1\"; }", |
| 108 "}", |
| 109 "class B {", |
| 110 " static final BOP1 = 2 < A.foo();", |
| 111 " static final BOP2 = A.foo() < 2;", |
| 112 " static final BOP3 = 2 < A.bar();", |
| 113 " static final BOP4 = A.bar() < 2;", |
| 114 "}"), |
| 115 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, |
| 116 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, |
| 117 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, |
| 118 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, |
| 119 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, |
| 120 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, |
| 121 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, |
| 122 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER); |
| 123 |
| 124 resolveAndTest(Joiner.on("\n").join( |
| 125 "class Object {}", |
| 126 "class int {}", |
| 127 "class double {}", |
| 128 "class num {}", |
| 129 "class A {", |
| 130 " static final BOP1 = 0x80 & 2.0;", |
| 131 " static final BOP2 = 2.0 & 0x80;", |
| 132 "}"), |
| 133 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_INT, |
| 134 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_INT); |
| 135 |
| 136 resolveAndTest(Joiner.on("\n").join( |
| 137 "class Object {}", |
| 138 "class bool {}", |
| 139 "class int {}", |
| 140 "class double {}", |
| 141 "class num {}", |
| 142 "class A {", |
| 143 " static bool foo() { return true; }", |
| 144 "}", |
| 145 " class B {", |
| 146 " static final BOP3 = 45 && true;", |
| 147 " static final BOP4 = true || 45;", |
| 148 " static final BOP5 = true && A.foo();", |
| 149 " static final BOP6 = A.foo() && false;", |
| 150 "}"), |
| 151 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN, |
| 152 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN, |
| 153 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, |
| 154 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN, |
| 155 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, |
| 156 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN); |
| 157 |
| 158 resolveAndTest(Joiner.on("\n").join( |
| 159 "class Object {}", |
| 160 "class bool {}", |
| 161 "class int {}", |
| 162 "class double {}", |
| 163 "class num {}", |
| 164 "class A {", |
| 165 " static Object foo() { return true; }", |
| 166 "}", |
| 167 "class B {", |
| 168 " const B();", |
| 169 " static final OBJECT_LIT = const B();", |
| 170 " static final INT_LIT = 1;", |
| 171 " static final STRING_LIT = \"true\";", |
| 172 " static final BOP1 = STRING_LIT && true;", |
| 173 " static final BOP2 = false || STRING_LIT;", |
| 174 " static final BOP3 = 59 == OBJECT_LIT;", |
| 175 " static final BOP4 = OBJECT_LIT != 59;", |
| 176 " static final BOP5 = INT_LIT === OBJECT_LIT;", |
| 177 " static final BOP6 = OBJECT_LIT !== true;", |
| 178 "}"), |
| 179 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN, |
| 180 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN, |
| 181 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_STRING_NUMBER_BOOL, |
| 182 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_STRING_NUMBER_BOOL, |
| 183 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_STRING_NUMBER_BOOL, |
| 184 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_STRING_NUMBER_BOOL); |
| 185 |
| 186 resolveAndTest(Joiner.on("\n").join( |
| 187 "class Object {}", |
| 188 "class A {", |
| 189 " static final INT_LIT = 5;", |
| 190 " static final INT_LIT_REF = INT_LIT;", |
| 191 " static final DOUBLE_LIT = 1.5;", |
| 192 " static final BOOL_LIT = true;", |
| 193 " // Multiple binary expresions", |
| 194 " static final BOP1 = 1 * INT_LIT / 3 + INT_LIT + 9;", |
| 195 " // Parenthized expression", |
| 196 " static final BOP2 = ( 1 > 2 );", |
| 197 " static final BOP3 = (1 * 2) + 3;", |
| 198 " static final BOP4 = 3 + (1 * 2);", |
| 199 "}")); |
| 200 |
| 201 // Negative Tests |
| 202 resolveAndTest(Joiner.on("\n").join( |
| 203 "class Object {}", |
| 204 "class A {", |
| 205 " static final INT_LIT = 5;", |
| 206 " static final DOUBLE_LIT = 1.5;", |
| 207 " const A();", |
| 208 " static final OBJECT_LIT = const A();", |
| 209 " // Multiple binary expresions", |
| 210 " static final BOP1_0 = 0 + 1 + OBJECT_LIT;", |
| 211 " static final BOP1_1 = 0 + OBJECT_LIT + 1;", |
| 212 " static final BOP1_2 = OBJECT_LIT + 3 + 9;", |
| 213 "}"), |
| 214 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, |
| 215 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, |
| 216 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, |
| 217 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, |
| 218 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER); |
| 219 |
| 220 resolveAndTest(Joiner.on("\n").join( |
| 221 "class Object {}", |
| 222 "class A {", |
| 223 " static final INT_LIT = 5;", |
| 224 " static final DOUBLE_LIT = 1.5;", |
| 225 " const A();", |
| 226 " static final OBJECT_LIT = new A();", |
| 227 " // Multiple binary expresions", |
| 228 " static final PP0 = 0 - (1 + OBJECT_LIT);", |
| 229 " static final PP1 = 0 + (OBJECT_LIT + 1);", |
| 230 " static final PP2 = (OBJECT_LIT + 3) + 9;", |
| 231 " static final PP3 = (OBJECT_LIT) + 3 + 9;", |
| 232 " static final PP4 = (OBJECT_LIT + 3 + 9);", |
| 233 " static final PP5 = OBJECT_LIT + (3 + 9);", |
| 234 "}"), |
| 235 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, |
| 236 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, |
| 237 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, |
| 238 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, |
| 239 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, |
| 240 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, |
| 241 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, |
| 242 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, |
| 243 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, |
| 244 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, |
| 245 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER, |
| 246 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_NUMBER); |
| 247 } |
| 248 |
| 249 public void testConstantUnaryExpression() { |
| 250 resolveAndTest(Joiner.on("\n").join( |
| 251 "class Object {}", |
| 252 "class A {", |
| 253 " // Unary expression", |
| 254 " static final BOOL_LIT = true;", |
| 255 " static final INT_LIT = 123;", |
| 256 " static final DOUBLE_LIT = 12.3;", |
| 257 " static final UOP1_0 = !BOOL_LIT;", |
| 258 " static final UOP1_1 = BOOL_LIT || !true;", |
| 259 " static final UOP1_2 = !BOOL_LIT || true;", |
| 260 " static final UOP1_3 = !(BOOL_LIT && true);", |
| 261 " static final UOP2_0 = ~0xf0;", |
| 262 " static final UOP2_1 = ~INT_LIT;", |
| 263 " static final UOP2_2 = ~INT_LIT & 123;", |
| 264 " static final UOP2_3 = ~(INT_LIT | 0xff);", |
| 265 " static final UOP3_0 = -0xf0;", |
| 266 " static final UOP3_1 = -INT_LIT;", |
| 267 " static final UOP3_2 = -INT_LIT + 123;", |
| 268 " static final UOP3_3 = -(INT_LIT * 0xff);", |
| 269 " static final UOP3_4 = -0xf0;", |
| 270 " static final UOP3_5 = -DOUBLE_LIT;", |
| 271 " static final UOP3_6 = -DOUBLE_LIT + 123;", |
| 272 " static final UOP3_7 = -(DOUBLE_LIT * 0xff);", |
| 273 "}")); |
| 274 |
| 275 resolveAndTest(Joiner.on("\n").join( |
| 276 "class Object {}", |
| 277 "class int {}", |
| 278 "class A {", |
| 279 " // Unary expression", |
| 280 " static final BOOL_LIT = true;", |
| 281 " static int foo() { return 3; }", |
| 282 " static final UOP1 = !5;", |
| 283 " static final UOP2 = !foo();", |
| 284 " static final UOP3 = !(5);", |
| 285 " static final UOP4 = !(foo());", |
| 286 "}"), |
| 287 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN, |
| 288 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, |
| 289 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN, |
| 290 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN, |
| 291 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, |
| 292 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION_BOOLEAN); |
| 293 } |
| 294 |
| 295 public void testConstantConstructorAssign() { |
| 296 |
| 297 resolveAndTest(Joiner.on("\n").join( |
| 298 "class Object {}", |
| 299 "class A {", |
| 300 " const A();", |
| 301 "}", |
| 302 "class B {", |
| 303 " static final a = const A();", // Constant constructor |
| 304 "}")); |
| 305 |
| 306 // Negative tests |
| 307 resolveAndTest(Joiner.on("\n").join( |
| 308 "class Object {}", |
| 309 "class A {", |
| 310 " const A();", |
| 311 " static final a = new A();", // Error: not a constant constructor |
| 312 "}"), |
| 313 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION); |
| 314 } |
| 315 |
| 316 public void testConstantLiteralAssign() { |
| 317 |
| 318 resolveAndTest(Joiner.on("\n").join( |
| 319 "class Object {}", |
| 320 "class A {", |
| 321 " static final b = true;", |
| 322 " static final s = \"apple\";", // string literal |
| 323 " static final i = 1;", // integer literal |
| 324 " static final d = 3.3;", // double literal |
| 325 " static final h = 0xf;", // hex literal |
| 326 " static final n = null;", // null |
| 327 "}")); |
| 328 |
| 329 // Negative tests |
| 330 resolveAndTest(Joiner.on("\n").join( |
| 331 "class Object {}", |
| 332 "class A {", |
| 333 " foo() { return \"Eve\";}", |
| 334 " static final person = \"earthling\";", |
| 335 " static final s = \"Hello ${foo()}!\";", |
| 336 "}"), |
| 337 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION); |
| 338 } |
| 339 |
| 340 public void testConstantTypedLiteralAssign() { |
| 341 resolveAndTest(Joiner.on("\n").join( |
| 342 "class Object {}", |
| 343 "class List<T> {}", |
| 344 "class Map<K,V> {}", |
| 345 "class A {", |
| 346 " static final aList = const[1, 2, 3];", // array literal |
| 347 " static final map = const { \"1\": \"one\", \"2\": \"banana\" };", //
map literal |
| 348 " static final val = aList[2];", |
| 349 "}")); |
| 350 |
| 351 // Negative tests, on literals that are not compile time constants. |
| 352 resolveAndTest(Joiner.on("\n").join( |
| 353 "class Object {}", |
| 354 "class List<T> {}", |
| 355 "class A {", |
| 356 " // array literal not const", |
| 357 " static final aList= [1, 2, 3];", |
| 358 "}"), |
| 359 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION); |
| 360 |
| 361 resolveAndTest(Joiner.on("\n").join( |
| 362 "class Object {}", |
| 363 "class List<T> {}", |
| 364 "class A {", |
| 365 " static foo() { return 1; }", |
| 366 " // const array literal contains non-const member", |
| 367 " static final aList = const [foo(), 2, 3];", |
| 368 "}"), |
| 369 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION); |
| 370 |
| 371 resolveAndTest(Joiner.on("\n").join( |
| 372 "class Object {}", |
| 373 "class Map<K,V> {}", |
| 374 "class A {", |
| 375 " // map literal is not const", |
| 376 " static final aMap = { \"1\": \"one\", \"2\": \"banana\" };", |
| 377 "}"), |
| 378 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION); |
| 379 |
| 380 resolveAndTest(Joiner.on("\n").join( |
| 381 "class Object {}", |
| 382 "class String {}", |
| 383 "class Map<K,V> {}", |
| 384 "class A {", |
| 385 " static String foo() { return \"one\"; }", |
| 386 " static final String s = \"apple\";", |
| 387 " // map literal contains non-const member", |
| 388 " static final map = const { \"1\":foo(), \"2\": \"banana\" };", |
| 389 " static final stringInterp = \"It was that woman who gave me the ${s}\
";", |
| 390 "}"), |
| 391 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION, |
| 392 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION); |
| 393 } |
| 394 |
| 395 public void testConstantVariableAssign() { |
| 396 resolveAndTest(Joiner.on("\n").join( |
| 397 "class Object {}", |
| 398 "class A {", |
| 399 " static final a = 1;", |
| 400 "}", |
| 401 "class B {", |
| 402 " static final i = 1;", |
| 403 " static final j = i;", // variable that is a compile-time constant |
| 404 " static final k = A.a;", // variable that is a compile-time constant |
| 405 "}")); |
| 406 |
| 407 // Negative tests |
| 408 resolveAndTest(Joiner.on("\n").join( |
| 409 "class Object {}", |
| 410 "class A {", |
| 411 " static foo() {return 1;}", |
| 412 " static final i = foo();", // Error: not a constant integer |
| 413 "}"), |
| 414 DartCompilerErrorCode.EXPECTED_CONSTANT_EXPRESSION); |
| 415 |
| 416 resolveAndTest(Joiner.on("\n").join( |
| 417 "class Object {}", |
| 418 "class A {", |
| 419 " static final foo;", |
| 420 "}"), |
| 421 DartCompilerErrorCode.STATIC_FINAL_REQUIRES_VALUE); |
| 422 } |
| 423 } |
OLD | NEW |