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