Debian Codegraph @

test/com/google/javascript/jscomp/TypeInferenceTest.java

raw symbols SCIP
1 /*
2 * Copyright 2008 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 import static com.google.javascript.rhino.jstype.JSTypeNative.ALL_TYPE;
20 import static com.google.javascript.rhino.jstype.JSTypeNative.ARRAY_TYPE;
21 import static com.google.javascript.rhino.jstype.JSTypeNative.BOOLEAN_TYPE;
22 import static com.google.javascript.rhino.jstype.JSTypeNative.CHECKED_UNKNOWN_TYPE;
23 import static com.google.javascript.rhino.jstype.JSTypeNative.FUNCTION_INSTANCE_TYPE;
24 import static com.google.javascript.rhino.jstype.JSTypeNative.NO_RESOLVED_TYPE;
25 import static com.google.javascript.rhino.jstype.JSTypeNative.NULL_TYPE;
26 import static com.google.javascript.rhino.jstype.JSTypeNative.NUMBER_OBJECT_TYPE;
27 import static com.google.javascript.rhino.jstype.JSTypeNative.NUMBER_TYPE;
28 import static com.google.javascript.rhino.jstype.JSTypeNative.OBJECT_TYPE;
29 import static com.google.javascript.rhino.jstype.JSTypeNative.STRING_OBJECT_TYPE;
30 import static com.google.javascript.rhino.jstype.JSTypeNative.STRING_TYPE;
31 import static com.google.javascript.rhino.jstype.JSTypeNative.UNKNOWN_TYPE;
32 import static com.google.javascript.rhino.jstype.JSTypeNative.VOID_TYPE;
33
34 import com.google.common.base.Joiner;
35 import com.google.common.collect.ImmutableList;
36 import com.google.common.collect.Maps;
37 import com.google.javascript.jscomp.CodingConvention.AssertionFunctionSpec;
38 import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
39 import com.google.javascript.jscomp.DataFlowAnalysis.BranchedFlowState;
40 import com.google.javascript.jscomp.type.FlowScope;
41 import com.google.javascript.jscomp.type.ReverseAbstractInterpreter;
42 import com.google.javascript.rhino.Node;
43 import com.google.javascript.rhino.jstype.EnumType;
44 import com.google.javascript.rhino.jstype.JSType;
45 import com.google.javascript.rhino.jstype.JSTypeNative;
46 import com.google.javascript.rhino.jstype.JSTypeRegistry;
47 import com.google.javascript.rhino.jstype.ObjectType;
48 import com.google.javascript.rhino.jstype.StaticSlot;
49 import com.google.javascript.rhino.testing.Asserts;
50
51 import junit.framework.TestCase;
52
53 import java.util.Map;
54
55 /**
56 * Tests {@link TypeInference}.
57 *
58 */
59 public class TypeInferenceTest extends TestCase {
60
61 private Compiler compiler;
62 private JSTypeRegistry registry;
63 private Map<String, JSType> assumptions;
64 private JSType assumedThisType;
65 private FlowScope returnScope;
66 private static final Map<String, AssertionFunctionSpec>
67 ASSERTION_FUNCTION_MAP = Maps.newHashMap();
68 static {
69 for (AssertionFunctionSpec func :
70 new ClosureCodingConvention().getAssertionFunctions()) {
71 ASSERTION_FUNCTION_MAP.put(func.getFunctionName(), func);
72 }
73 }
74
75 @Override
76 public void setUp() {
77 compiler = new Compiler();
78 CompilerOptions options = new CompilerOptions();
79 options.setClosurePass(true);
80 options.setLanguageIn(LanguageMode.ECMASCRIPT5);
81 compiler.initOptions(options);
82 registry = compiler.getTypeRegistry();
83 assumptions = Maps.newHashMap();
84 returnScope = null;
85 }
86
87 private void assumingThisType(JSType type) {
88 assumedThisType = type;
89 }
90
91 private void assuming(String name, JSType type) {
92 assumptions.put(name, type);
93 }
94
95 private void assuming(String name, JSTypeNative type) {
96 assuming(name, registry.getNativeType(type));
97 }
98
99 private void inFunction(String js) {
100 // Parse the body of the function.
101 String thisBlock = assumedThisType == null
102 ? ""
103 : "/** @this {" + assumedThisType + "} */";
104 Node root = compiler.parseTestCode(
105 "(" + thisBlock + " function() {" + js + "});");
106 assertEquals("parsing error: " +
107 Joiner.on(", ").join(compiler.getErrors()),
108 0, compiler.getErrorCount());
109
110 Node n = root.getFirstChild().getFirstChild();
111 // Create the scope with the assumptions.
112 TypedScopeCreator scopeCreator = new TypedScopeCreator(compiler);
113 Scope assumedScope = scopeCreator.createScope(
114 n, scopeCreator.createScope(root, null));
115 for (Map.Entry<String,JSType> entry : assumptions.entrySet()) {
116 assumedScope.declare(entry.getKey(), null, entry.getValue(), null, false);
117 }
118 // Create the control graph.
119 ControlFlowAnalysis cfa = new ControlFlowAnalysis(compiler, false, false);
120 cfa.process(null, n);
121 ControlFlowGraph<Node> cfg = cfa.getCfg();
122 // Create a simple reverse abstract interpreter.
123 ReverseAbstractInterpreter rai = compiler.getReverseAbstractInterpreter();
124 // Do the type inference by data-flow analysis.
125 TypeInference dfa = new TypeInference(compiler, cfg, rai, assumedScope,
126 ASSERTION_FUNCTION_MAP);
127 dfa.analyze();
128 // Get the scope of the implicit return.
129 BranchedFlowState<FlowScope> rtnState =
130 cfg.getImplicitReturn().getAnnotation();
131 returnScope = rtnState.getIn();
132 }
133
134 private JSType getType(String name) {
135 assertTrue("The return scope should not be null.", returnScope != null);
136 StaticSlot<JSType> var = returnScope.getSlot(name);
137 assertTrue("The variable " + name + " is missing from the scope.",
138 var != null);
139 return var.getType();
140 }
141
142 private void verify(String name, JSType type) {
143 Asserts.assertTypeEquals("Mismatch for " + name, type, getType(name));
144 }
145
146 private void verify(String name, JSTypeNative type) {
147 verify(name, registry.getNativeType(type));
148 }
149
150 private void verifySubtypeOf(String name, JSType type) {
151 JSType varType = getType(name);
152 assertTrue("The variable " + name + " is missing a type.", varType != null);
153 assertTrue("The type " + varType + " of variable " + name +
154 " is not a subtype of " + type +".", varType.isSubtype(type));
155 }
156
157 private void verifySubtypeOf(String name, JSTypeNative type) {
158 verifySubtypeOf(name, registry.getNativeType(type));
159 }
160
161 private EnumType createEnumType(String name, JSTypeNative elemType) {
162 return createEnumType(name, registry.getNativeType(elemType));
163 }
164
165 private EnumType createEnumType(String name, JSType elemType) {
166 return registry.createEnumType(name, null, elemType);
167 }
168
169 private JSType createUndefinableType(JSTypeNative type) {
170 return registry.createUnionType(
171 registry.getNativeType(type), registry.getNativeType(VOID_TYPE));
172 }
173
174 private JSType createNullableType(JSTypeNative type) {
175 return createNullableType(registry.getNativeType(type));
176 }
177
178 private JSType createNullableType(JSType type) {
179 return registry.createNullableType(type);
180 }
181
182 private JSType createUnionType(JSTypeNative type1, JSTypeNative type2) {
183 return registry.createUnionType(
184 registry.getNativeType(type1), registry.getNativeType(type2));
185 }
186
187 public void testAssumption() {
188 assuming("x", NUMBER_TYPE);
189 inFunction("");
190 verify("x", NUMBER_TYPE);
191 }
192
193 public void testVar() {
194 inFunction("var x = 1;");
195 verify("x", NUMBER_TYPE);
196 }
197
198 public void testEmptyVar() {
199 inFunction("var x;");
200 verify("x", VOID_TYPE);
201 }
202
203 public void testAssignment() {
204 assuming("x", OBJECT_TYPE);
205 inFunction("x = 1;");
206 verify("x", NUMBER_TYPE);
207 }
208
209 public void testExprWithinCast() {
210 assuming("x", OBJECT_TYPE);
211 inFunction("/** @type {string} */ (x = 1);");
212 verify("x", NUMBER_TYPE);
213 }
214
215 public void testGetProp() {
216 assuming("x", createNullableType(OBJECT_TYPE));
217 inFunction("x.y();");
218 verify("x", OBJECT_TYPE);
219 }
220
221 public void testGetElemDereference() {
222 assuming("x", createUndefinableType(OBJECT_TYPE));
223 inFunction("x['z'] = 3;");
224 verify("x", OBJECT_TYPE);
225 }
226
227 public void testIf1() {
228 assuming("x", createNullableType(OBJECT_TYPE));
229 inFunction("var y = {}; if (x) { y = x; }");
230 verifySubtypeOf("y", OBJECT_TYPE);
231 }
232
233 public void testIf1a() {
234 assuming("x", createNullableType(OBJECT_TYPE));
235 inFunction("var y = {}; if (x != null) { y = x; }");
236 verifySubtypeOf("y", OBJECT_TYPE);
237 }
238
239 public void testIf2() {
240 assuming("x", createNullableType(OBJECT_TYPE));
241 inFunction("var y = x; if (x) { y = x; } else { y = {}; }");
242 verifySubtypeOf("y", OBJECT_TYPE);
243 }
244
245 public void testIf3() {
246 assuming("x", createNullableType(OBJECT_TYPE));
247 inFunction("var y = 1; if (x) { y = x; }");
248 verify("y", createUnionType(OBJECT_TYPE, NUMBER_TYPE));
249 }
250
251 public void testPropertyInference1() {
252 ObjectType thisType = registry.createAnonymousObjectType(null);
253 thisType.defineDeclaredProperty("foo",
254 createUndefinableType(STRING_TYPE), null);
255 assumingThisType(thisType);
256 inFunction("var y = 1; if (this.foo) { y = this.foo; }");
257 verify("y", createUnionType(NUMBER_TYPE, STRING_TYPE));
258 }
259
260 public void testPropertyInference2() {
261 ObjectType thisType = registry.createAnonymousObjectType(null);
262 thisType.defineDeclaredProperty("foo",
263 createUndefinableType(STRING_TYPE), null);
264 assumingThisType(thisType);
265 inFunction("var y = 1; this.foo = 'x'; y = this.foo;");
266 verify("y", STRING_TYPE);
267 }
268
269 public void testPropertyInference3() {
270 ObjectType thisType = registry.createAnonymousObjectType(null);
271 thisType.defineDeclaredProperty("foo",
272 createUndefinableType(STRING_TYPE), null);
273 assumingThisType(thisType);
274 inFunction("var y = 1; this.foo = x; y = this.foo;");
275 verify("y", CHECKED_UNKNOWN_TYPE);
276 }
277
278 public void testAssert1() {
279 JSType startType = createNullableType(OBJECT_TYPE);
280 assuming("x", startType);
281 inFunction("out1 = x; goog.asserts.assert(x); out2 = x;");
282 verify("out1", startType);
283 verify("out2", OBJECT_TYPE);
284 }
285
286 public void testAssert1a() {
287 JSType startType = createNullableType(OBJECT_TYPE);
288 assuming("x", startType);
289 inFunction("out1 = x; goog.asserts.assert(x !== null); out2 = x;");
290 verify("out1", startType);
291 verify("out2", OBJECT_TYPE);
292 }
293
294 public void testAssert2() {
295 JSType startType = createNullableType(OBJECT_TYPE);
296 assuming("x", startType);
297 inFunction("goog.asserts.assert(1, x); out1 = x;");
298 verify("out1", startType);
299 }
300
301 public void testAssert3() {
302 JSType startType = createNullableType(OBJECT_TYPE);
303 assuming("x", startType);
304 assuming("y", startType);
305 inFunction("out1 = x; goog.asserts.assert(x && y); out2 = x; out3 = y;");
306 verify("out1", startType);
307 verify("out2", OBJECT_TYPE);
308 verify("out3", OBJECT_TYPE);
309 }
310
311 public void testAssert4() {
312 JSType startType = createNullableType(OBJECT_TYPE);
313 assuming("x", startType);
314 assuming("y", startType);
315 inFunction("out1 = x; goog.asserts.assert(x && !y); out2 = x; out3 = y;");
316 verify("out1", startType);
317 verify("out2", OBJECT_TYPE);
318 verify("out3", NULL_TYPE);
319 }
320
321 public void testAssert5() {
322 JSType startType = createNullableType(OBJECT_TYPE);
323 assuming("x", startType);
324 assuming("y", startType);
325 inFunction("goog.asserts.assert(x || y); out1 = x; out2 = y;");
326 verify("out1", startType);
327 verify("out2", startType);
328 }
329
330 public void testAssert6() {
331 JSType startType = createNullableType(OBJECT_TYPE);
332 assuming("x.y", startType);
333 inFunction("out1 = x.y; goog.asserts.assert(x.y); out2 = x.y;");
334 verify("out1", startType);
335 verify("out2", OBJECT_TYPE);
336 }
337
338 public void testAssert7() {
339 JSType startType = createNullableType(OBJECT_TYPE);
340 assuming("x", startType);
341 inFunction("out1 = x; out2 = goog.asserts.assert(x);");
342 verify("out1", startType);
343 verify("out2", OBJECT_TYPE);
344 }
345
346 public void testAssert8() {
347 JSType startType = createNullableType(OBJECT_TYPE);
348 assuming("x", startType);
349 inFunction("out1 = x; out2 = goog.asserts.assert(x != null);");
350 verify("out1", startType);
351 verify("out2", BOOLEAN_TYPE);
352 }
353
354 public void testAssert9() {
355 JSType startType = createNullableType(NUMBER_TYPE);
356 assuming("x", startType);
357 inFunction("out1 = x; out2 = goog.asserts.assert(y = x);");
358 verify("out1", startType);
359 verify("out2", NUMBER_TYPE);
360 }
361
362 public void testAssert10() {
363 JSType startType = createNullableType(OBJECT_TYPE);
364 assuming("x", startType);
365 assuming("y", startType);
366 inFunction("out1 = x; out2 = goog.asserts.assert(x && y); out3 = x;");
367 verify("out1", startType);
368 verify("out2", OBJECT_TYPE);
369 verify("out3", OBJECT_TYPE);
370 }
371
372 public void testAssertNumber() {
373 JSType startType = createNullableType(ALL_TYPE);
374 assuming("x", startType);
375 inFunction("out1 = x; goog.asserts.assertNumber(x); out2 = x;");
376 verify("out1", startType);
377 verify("out2", NUMBER_TYPE);
378 }
379
380 public void testAssertNumber2() {
381 // Make sure it ignores expressions.
382 JSType startType = createNullableType(ALL_TYPE);
383 assuming("x", startType);
384 inFunction("goog.asserts.assertNumber(x + x); out1 = x;");
385 verify("out1", startType);
386 }
387
388 public void testAssertNumber3() {
389 // Make sure it ignores expressions.
390 JSType startType = createNullableType(ALL_TYPE);
391 assuming("x", startType);
392 inFunction("out1 = x; out2 = goog.asserts.assertNumber(x + x);");
393 verify("out1", startType);
394 verify("out2", NUMBER_TYPE);
395 }
396
397 public void testAssertString() {
398 JSType startType = createNullableType(ALL_TYPE);
399 assuming("x", startType);
400 inFunction("out1 = x; goog.asserts.assertString(x); out2 = x;");
401 verify("out1", startType);
402 verify("out2", STRING_TYPE);
403 }
404
405 public void testAssertFunction() {
406 JSType startType = createNullableType(ALL_TYPE);
407 assuming("x", startType);
408 inFunction("out1 = x; goog.asserts.assertFunction(x); out2 = x;");
409 verify("out1", startType);
410 verifySubtypeOf("out2", FUNCTION_INSTANCE_TYPE);
411 }
412
413 public void testAssertObject() {
414 JSType startType = createNullableType(ALL_TYPE);
415 assuming("x", startType);
416 inFunction("out1 = x; goog.asserts.assertObject(x); out2 = x;");
417 verify("out1", startType);
418 verifySubtypeOf("out2", OBJECT_TYPE);
419 }
420
421 public void testAssertObject2() {
422 JSType startType = createNullableType(ARRAY_TYPE);
423 assuming("x", startType);
424 inFunction("out1 = x; goog.asserts.assertObject(x); out2 = x;");
425 verify("out1", startType);
426 verify("out2", ARRAY_TYPE);
427 }
428
429 public void testAssertObject3() {
430 JSType startType = createNullableType(OBJECT_TYPE);
431 assuming("x.y", startType);
432 inFunction("out1 = x.y; goog.asserts.assertObject(x.y); out2 = x.y;");
433 verify("out1", startType);
434 verify("out2", OBJECT_TYPE);
435 }
436
437 public void testAssertObject4() {
438 JSType startType = createNullableType(ARRAY_TYPE);
439 assuming("x", startType);
440 inFunction("out1 = x; out2 = goog.asserts.assertObject(x);");
441 verify("out1", startType);
442 verify("out2", ARRAY_TYPE);
443 }
444
445 public void testAssertObject5() {
446 JSType startType = createNullableType(ALL_TYPE);
447 assuming("x", startType);
448 inFunction(
449 "out1 = x;" +
450 "out2 = /** @type {!Array} */ (goog.asserts.assertObject(x));");
451 verify("out1", startType);
452 verify("out2", ARRAY_TYPE);
453 }
454
455 public void testAssertArray() {
456 JSType startType = createNullableType(ALL_TYPE);
457 assuming("x", startType);
458 inFunction("out1 = x; goog.asserts.assertArray(x); out2 = x;");
459 verify("out1", startType);
460 verifySubtypeOf("out2", ARRAY_TYPE);
461 }
462
463 public void testAssertInstanceof1() {
464 JSType startType = createNullableType(ALL_TYPE);
465 assuming("x", startType);
466 inFunction("out1 = x; goog.asserts.assertInstanceof(x); out2 = x;");
467 verify("out1", startType);
468 verify("out2", OBJECT_TYPE);
469 }
470
471 public void testAssertInstanceof2() {
472 JSType startType = createNullableType(ALL_TYPE);
473 assuming("x", startType);
474 inFunction("out1 = x; goog.asserts.assertInstanceof(x, String); out2 = x;");
475 verify("out1", startType);
476 verify("out2", STRING_OBJECT_TYPE);
477 }
478
479 public void testAssertInstanceof3() {
480 JSType startType = registry.getNativeType(UNKNOWN_TYPE);
481 assuming("x", startType);
482 inFunction("out1 = x; goog.asserts.assertInstanceof(x, String); out2 = x;");
483 verify("out1", startType);
484 verify("out2", UNKNOWN_TYPE);
485 }
486
487 public void testAssertInstanceof4() {
488 JSType startType = registry.getNativeType(STRING_OBJECT_TYPE);
489 assuming("x", startType);
490 inFunction("out1 = x; goog.asserts.assertInstanceof(x, Object); out2 = x;");
491 verify("out1", startType);
492 verify("out2", STRING_OBJECT_TYPE);
493 }
494
495 public void testAssertInstanceof5() {
496 JSType startType = registry.getNativeType(ALL_TYPE);
497 assuming("x", startType);
498 inFunction(
499 "out1 = x; goog.asserts.assertInstanceof(x, String); var r = x;");
500 verify("out1", startType);
501 verify("x", STRING_OBJECT_TYPE);
502 }
503
504 public void testAssertWithIsDefAndNotNull() {
505 JSType startType = createNullableType(NUMBER_TYPE);
506 assuming("x", startType);
507 inFunction(
508 "out1 = x;" +
509 "goog.asserts.assert(goog.isDefAndNotNull(x));" +
510 "out2 = x;");
511 verify("out1", startType);
512 verify("out2", NUMBER_TYPE);
513 }
514
515 public void testIsDefAndNoResolvedType() {
516 JSType startType = createUndefinableType(NO_RESOLVED_TYPE);
517 assuming("x", startType);
518 inFunction(
519 "out1 = x;" +
520 "if (goog.isDef(x)) { out2a = x; out2b = x.length; out2c = x; }" +
521 "out3 = x;" +
522 "if (goog.isDef(x)) { out4 = x; }");
523 verify("out1", startType);
524 verify("out2a", NO_RESOLVED_TYPE);
525 verify("out2b", CHECKED_UNKNOWN_TYPE);
526 verify("out2c", NO_RESOLVED_TYPE);
527 verify("out3", startType);
528 verify("out4", NO_RESOLVED_TYPE);
529 }
530
531 public void testAssertWithNotIsNull() {
532 JSType startType = createNullableType(NUMBER_TYPE);
533 assuming("x", startType);
534 inFunction(
535 "out1 = x;" +
536 "goog.asserts.assert(!goog.isNull(x));" +
537 "out2 = x;");
538 verify("out1", startType);
539 verify("out2", NUMBER_TYPE);
540 }
541
542 public void testReturn1() {
543 assuming("x", createNullableType(OBJECT_TYPE));
544 inFunction("if (x) { return x; }\nx = {};\nreturn x;");
545 verify("x", OBJECT_TYPE);
546 }
547
548 public void testReturn2() {
549 assuming("x", createNullableType(NUMBER_TYPE));
550 inFunction("if (!x) { x = 0; }\nreturn x;");
551 verify("x", NUMBER_TYPE);
552 }
553
554 public void testWhile1() {
555 assuming("x", createNullableType(NUMBER_TYPE));
556 inFunction("while (!x) { if (x == null) { x = 0; } else { x = 1; } }");
557 verify("x", NUMBER_TYPE);
558 }
559
560 public void testWhile2() {
561 assuming("x", createNullableType(NUMBER_TYPE));
562 inFunction("while (!x) { x = {}; }");
563 verifySubtypeOf("x", createUnionType(OBJECT_TYPE, NUMBER_TYPE));
564 }
565
566 public void testDo() {
567 assuming("x", createNullableType(OBJECT_TYPE));
568 inFunction("do { x = 1; } while (!x);");
569 verify("x", NUMBER_TYPE);
570 }
571
572 public void testFor1() {
573 assuming("y", NUMBER_TYPE);
574 inFunction("var x = null; var i = null; for (i=y; !i; i=1) { x = 1; }");
575 verify("x", createNullableType(NUMBER_TYPE));
576 verify("i", NUMBER_TYPE);
577 }
578
579 public void testFor2() {
580 assuming("y", OBJECT_TYPE);
581 inFunction("var x = null; var i = null; for (i in y) { x = 1; }");
582 verify("x", createNullableType(NUMBER_TYPE));
583 verify("i", createNullableType(STRING_TYPE));
584 }
585
586 public void testFor3() {
587 assuming("y", OBJECT_TYPE);
588 inFunction("var x = null; var i = null; for (var i in y) { x = 1; }");
589 verify("x", createNullableType(NUMBER_TYPE));
590 verify("i", createNullableType(STRING_TYPE));
591 }
592
593 public void testFor4() {
594 assuming("x", createNullableType(OBJECT_TYPE));
595 inFunction("var y = {};\n" +
596 "if (x) { for (var i = 0; i < 10; i++) { break; } y = x; }");
597 verifySubtypeOf("y", OBJECT_TYPE);
598 }
599
600 public void testFor5() {
601 assuming("y", templatize(
602 getNativeObjectType(ARRAY_TYPE),
603 ImmutableList.of(getNativeType(NUMBER_TYPE))));
604 inFunction(
605 "var x = null; for (var i = 0; i < y.length; i++) { x = y[i]; }");
606 verify("x", createNullableType(NUMBER_TYPE));
607 verify("i", NUMBER_TYPE);
608 }
609
610 public void testFor6() {
611 assuming("y", getNativeObjectType(ARRAY_TYPE));
612 inFunction(
613 "var x = null;" +
614 "for (var i = 0; i < y.length; i++) { " +
615 " if (y[i] == 'z') { x = y[i]; } " +
616 "}");
617 verify("x", getNativeType(UNKNOWN_TYPE));
618 verify("i", NUMBER_TYPE);
619 }
620
621 public void testSwitch1() {
622 assuming("x", NUMBER_TYPE);
623 inFunction("var y = null; switch(x) {\n" +
624 "case 1: y = 1; break;\n" +
625 "case 2: y = {};\n" +
626 "case 3: y = {};\n" +
627 "default: y = 0;}");
628 verify("y", NUMBER_TYPE);
629 }
630
631 public void testSwitch2() {
632 assuming("x", ALL_TYPE);
633 inFunction("var y = null; switch (typeof x) {\n" +
634 "case 'string':\n" +
635 " y = x;\n" +
636 " return;" +
637 "default:\n" +
638 " y = 'a';\n" +
639 "}");
640 verify("y", STRING_TYPE);
641 }
642
643 public void testSwitch3() {
644 assuming("x",
645 createNullableType(createUnionType(NUMBER_TYPE, STRING_TYPE)));
646 inFunction("var y; var z; switch (typeof x) {\n" +
647 "case 'string':\n" +
648 " y = 1; z = null;\n" +
649 " return;\n" +
650 "case 'number':\n" +
651 " y = x; z = null;\n" +
652 " return;" +
653 "default:\n" +
654 " y = 1; z = x;\n" +
655 "}");
656 verify("y", NUMBER_TYPE);
657 verify("z", NULL_TYPE);
658 }
659
660 public void testSwitch4() {
661 assuming("x", ALL_TYPE);
662 inFunction("var y = null; switch (typeof x) {\n" +
663 "case 'string':\n" +
664 "case 'number':\n" +
665 " y = x;\n" +
666 " return;\n" +
667 "default:\n" +
668 " y = 1;\n" +
669 "}\n");
670 verify("y", createUnionType(NUMBER_TYPE, STRING_TYPE));
671 }
672
673 public void testCall1() {
674 assuming("x",
675 createNullableType(
676 registry.createFunctionType(registry.getNativeType(NUMBER_TYPE))));
677 inFunction("var y = x();");
678 verify("y", NUMBER_TYPE);
679 }
680
681 public void testNew1() {
682 assuming("x",
683 createNullableType(
684 registry.getNativeType(JSTypeNative.U2U_CONSTRUCTOR_TYPE)));
685 inFunction("var y = new x();");
686 verify("y", UNKNOWN_TYPE);
687 }
688
689 public void testInnerFunction1() {
690 inFunction("var x = 1; function f() { x = null; };");
691 verify("x", NUMBER_TYPE);
692 }
693
694 public void testInnerFunction2() {
695 inFunction("var x = 1; var f = function() { x = null; };");
696 verify("x", NUMBER_TYPE);
697 }
698
699 public void testHook() {
700 assuming("x", createNullableType(OBJECT_TYPE));
701 inFunction("var y = x ? x : {};");
702 verifySubtypeOf("y", OBJECT_TYPE);
703 }
704
705 public void testThrow() {
706 assuming("x", createNullableType(NUMBER_TYPE));
707 inFunction("var y = 1;\n" +
708 "if (x == null) { throw new Error('x is null') }\n" +
709 "y = x;");
710 verify("y", NUMBER_TYPE);
711 }
712
713 public void testTry1() {
714 assuming("x", NUMBER_TYPE);
715 inFunction("var y = null; try { y = null; } finally { y = x; }");
716 verify("y", NUMBER_TYPE);
717 }
718
719 public void testTry2() {
720 assuming("x", NUMBER_TYPE);
721 inFunction("var y = null;\n" +
722 "try { } catch (e) { y = null; } finally { y = x; }");
723 verify("y", NUMBER_TYPE);
724 }
725
726 public void testTry3() {
727 assuming("x", NUMBER_TYPE);
728 inFunction("var y = null; try { y = x; } catch (e) { }");
729 verify("y", NUMBER_TYPE);
730 }
731
732 public void testCatch1() {
733 inFunction("var y = null; try { foo(); } catch (e) { y = e; }");
734 verify("y", UNKNOWN_TYPE);
735 }
736
737 public void testCatch2() {
738 inFunction("var y = null; var e = 3; try { foo(); } catch (e) { y = e; }");
739 verify("y", UNKNOWN_TYPE);
740 }
741
742 public void testUnknownType1() {
743 inFunction("var y = 3; y = x;");
744 verify("y", UNKNOWN_TYPE);
745 }
746
747 public void testUnknownType2() {
748 assuming("x", ARRAY_TYPE);
749 inFunction("var y = 5; y = x[0];");
750 verify("y", UNKNOWN_TYPE);
751 }
752
753 public void testInfiniteLoop1() {
754 assuming("x", createNullableType(OBJECT_TYPE));
755 inFunction("x = {}; while(x != null) { x = {}; }");
756 }
757
758 public void testInfiniteLoop2() {
759 assuming("x", createNullableType(OBJECT_TYPE));
760 inFunction("x = {}; do { x = null; } while (x == null);");
761 }
762
763 public void testJoin1() {
764 JSType unknownOrNull = createUnionType(NULL_TYPE, UNKNOWN_TYPE);
765 assuming("x", BOOLEAN_TYPE);
766 assuming("unknownOrNull", unknownOrNull);
767 inFunction("var y; if (x) y = unknownOrNull; else y = null;");
768 verify("y", unknownOrNull);
769 }
770
771 public void testJoin2() {
772 JSType unknownOrNull = createUnionType(NULL_TYPE, UNKNOWN_TYPE);
773 assuming("x", BOOLEAN_TYPE);
774 assuming("unknownOrNull", unknownOrNull);
775 inFunction("var y; if (x) y = null; else y = unknownOrNull;");
776 verify("y", unknownOrNull);
777 }
778
779 public void testArrayLit() {
780 assuming("x", createNullableType(OBJECT_TYPE));
781 inFunction("var y = 3; if (x) { x = [y = x]; }");
782 verify("x", createUnionType(NULL_TYPE, ARRAY_TYPE));
783 verify("y", createUnionType(NUMBER_TYPE, OBJECT_TYPE));
784 }
785
786 public void testGetElem() {
787 assuming("x", createNullableType(OBJECT_TYPE));
788 inFunction("var y = 3; if (x) { x = x[y = x]; }");
789 verify("x", UNKNOWN_TYPE);
790 verify("y", createUnionType(NUMBER_TYPE, OBJECT_TYPE));
791 }
792
793 public void testEnumRAI1() {
794 JSType enumType = createEnumType("MyEnum", ARRAY_TYPE).getElementsType();
795 assuming("x", enumType);
796 inFunction("var y = null; if (x) y = x;");
797 verify("y", createNullableType(enumType));
798 }
799
800 public void testEnumRAI2() {
801 JSType enumType = createEnumType("MyEnum", NUMBER_TYPE).getElementsType();
802 assuming("x", enumType);
803 inFunction("var y = null; if (typeof x == 'number') y = x;");
804 verify("y", createNullableType(enumType));
805 }
806
807 public void testEnumRAI3() {
808 JSType enumType = createEnumType("MyEnum", NUMBER_TYPE).getElementsType();
809 assuming("x", enumType);
810 inFunction("var y = null; if (x && typeof x == 'number') y = x;");
811 verify("y", createNullableType(enumType));
812 }
813
814 public void testEnumRAI4() {
815 JSType enumType = createEnumType("MyEnum",
816 createUnionType(STRING_TYPE, NUMBER_TYPE)).getElementsType();
817 assuming("x", enumType);
818 inFunction("var y = null; if (typeof x == 'number') y = x;");
819 verify("y", createNullableType(NUMBER_TYPE));
820 }
821
822 public void testShortCircuitingAnd() {
823 assuming("x", NUMBER_TYPE);
824 inFunction("var y = null; if (x && (y = 3)) { }");
825 verify("y", createNullableType(NUMBER_TYPE));
826 }
827
828 public void testShortCircuitingAnd2() {
829 assuming("x", NUMBER_TYPE);
830 inFunction("var y = null; var z = 4; if (x && (y = 3)) { z = y; }");
831 verify("z", NUMBER_TYPE);
832 }
833
834 public void testShortCircuitingOr() {
835 assuming("x", NUMBER_TYPE);
836 inFunction("var y = null; if (x || (y = 3)) { }");
837 verify("y", createNullableType(NUMBER_TYPE));
838 }
839
840 public void testShortCircuitingOr2() {
841 assuming("x", NUMBER_TYPE);
842 inFunction("var y = null; var z = 4; if (x || (y = 3)) { z = y; }");
843 verify("z", createNullableType(NUMBER_TYPE));
844 }
845
846 public void testAssignInCondition() {
847 assuming("x", createNullableType(NUMBER_TYPE));
848 inFunction("var y; if (!(y = x)) { y = 3; }");
849 verify("y", NUMBER_TYPE);
850 }
851
852 public void testInstanceOf1() {
853 assuming("x", OBJECT_TYPE);
854 inFunction("var y = null; if (x instanceof String) y = x;");
855 verify("y", createNullableType(STRING_OBJECT_TYPE));
856 }
857
858 public void testInstanceOf2() {
859 assuming("x", createNullableType(OBJECT_TYPE));
860 inFunction("var y = 1; if (x instanceof String) y = x;");
861 verify("y", createUnionType(STRING_OBJECT_TYPE, NUMBER_TYPE));
862 }
863
864 public void testInstanceOf3() {
865 assuming("x", createUnionType(STRING_OBJECT_TYPE, NUMBER_OBJECT_TYPE));
866 inFunction("var y = null; if (x instanceof String) y = x;");
867 verify("y", createNullableType(STRING_OBJECT_TYPE));
868 }
869
870 public void testInstanceOf4() {
871 assuming("x", createUnionType(STRING_OBJECT_TYPE, NUMBER_OBJECT_TYPE));
872 inFunction("var y = null; if (x instanceof String); else y = x;");
873 verify("y", createNullableType(NUMBER_OBJECT_TYPE));
874 }
875
876 public void testInstanceOf5() {
877 assuming("x", OBJECT_TYPE);
878 inFunction("var y = null; if (x instanceof String); else y = x;");
879 verify("y", createNullableType(OBJECT_TYPE));
880 }
881
882 public void testInstanceOf6() {
883 // Here we are using "instanceof" to restrict the unknown type to
884 // the type of the instance. This has the following problems:
885 // 1) The type may actually be any sub-type
886 // 2) The type may implement any interface
887 // After the instanceof we will require casts for methods that require
888 // sub-type or unrelated interfaces which would not have been required
889 // before.
890 JSType startType = registry.getNativeType(UNKNOWN_TYPE);
891 assuming("x", startType);
892 inFunction("out1 = x; if (x instanceof String) out2 = x;");
893 verify("out1", startType);
894 verify("out2", STRING_OBJECT_TYPE);
895 }
896
897 public void testFlattening() {
898 for (int i = 0; i < LinkedFlowScope.MAX_DEPTH + 1; i++) {
899 assuming("s" + i, ALL_TYPE);
900 }
901 assuming("b", JSTypeNative.BOOLEAN_TYPE);
902 StringBuilder body = new StringBuilder();
903 body.append("if (b) {");
904 for (int i = 0; i < LinkedFlowScope.MAX_DEPTH + 1; i++) {
905 body.append("s");
906 body.append(i);
907 body.append(" = 1;\n");
908 }
909 body.append(" } else { ");
910 for (int i = 0; i < LinkedFlowScope.MAX_DEPTH + 1; i++) {
911 body.append("s");
912 body.append(i);
913 body.append(" = 'ONE';\n");
914 }
915 body.append("}");
916 JSType numberORString = createUnionType(NUMBER_TYPE, STRING_TYPE);
917 inFunction(body.toString());
918
919 for (int i = 0; i < LinkedFlowScope.MAX_DEPTH + 1; i++) {
920 verify("s" + i, numberORString);
921 }
922 }
923
924 public void testUnary() {
925 assuming("x", NUMBER_TYPE);
926 inFunction("var y = +x;");
927 verify("y", NUMBER_TYPE);
928 inFunction("var z = -x;");
929 verify("z", NUMBER_TYPE);
930 }
931
932 public void testAdd1() {
933 assuming("x", NUMBER_TYPE);
934 inFunction("var y = x + 5;");
935 verify("y", NUMBER_TYPE);
936 }
937
938 public void testAdd2() {
939 assuming("x", NUMBER_TYPE);
940 inFunction("var y = x + '5';");
941 verify("y", STRING_TYPE);
942 }
943
944 public void testAdd3() {
945 assuming("x", NUMBER_TYPE);
946 inFunction("var y = '5' + x;");
947 verify("y", STRING_TYPE);
948 }
949
950 public void testAssignAdd() {
951 assuming("x", NUMBER_TYPE);
952 inFunction("x += '5';");
953 verify("x", STRING_TYPE);
954 }
955
956 public void testComparison() {
957 inFunction("var x = 'foo'; var y = (x = 3) < 4;");
958 verify("x", NUMBER_TYPE);
959 inFunction("var x = 'foo'; var y = (x = 3) > 4;");
960 verify("x", NUMBER_TYPE);
961 inFunction("var x = 'foo'; var y = (x = 3) <= 4;");
962 verify("x", NUMBER_TYPE);
963 inFunction("var x = 'foo'; var y = (x = 3) >= 4;");
964 verify("x", NUMBER_TYPE);
965 }
966
967 public void testThrownExpression() {
968 inFunction("var x = 'foo'; "
969 + "try { throw new Error(x = 3); } catch (ex) {}");
970 verify("x", NUMBER_TYPE);
971 }
972
973 public void testObjectLit() {
974 inFunction("var x = {}; var out = x.a;");
975 verify("out", UNKNOWN_TYPE); // Shouldn't this be 'undefined'?
976
977 inFunction("var x = {a:1}; var out = x.a;");
978 verify("out", NUMBER_TYPE);
979
980 inFunction("var x = {a:1}; var out = x.a; x.a = 'string'; var out2 = x.a;");
981 verify("out", NUMBER_TYPE);
982 verify("out2", STRING_TYPE);
983
984 inFunction("var x = { get a() {return 1} }; var out = x.a;");
985 verify("out", UNKNOWN_TYPE);
986
987 inFunction(
988 "var x = {" +
989 " /** @return {number} */ get a() {return 1}" +
990 "};" +
991 "var out = x.a;");
992 verify("out", NUMBER_TYPE);
993
994 inFunction("var x = { set a(b) {} }; var out = x.a;");
995 verify("out", UNKNOWN_TYPE);
996
997 inFunction("var x = { " +
998 "/** @param {number} b */ set a(b) {} };" +
999 "var out = x.a;");
1000 verify("out", NUMBER_TYPE);
1001 }
1002
1003 public void testCast1() {
1004 inFunction("var x = /** @type {Object} */ (this);");
1005 verify("x", createNullableType(OBJECT_TYPE));
1006 }
1007
1008 public void testCast2() {
1009 inFunction(
1010 "/** @return {boolean} */" +
1011 "Object.prototype.method = function() { return true; };" +
1012 "var x = /** @type {Object} */ (this).method;");
1013 verify(
1014 "x",
1015 registry.createFunctionType(
1016 registry.getNativeObjectType(OBJECT_TYPE),
1017 registry.getNativeType(BOOLEAN_TYPE),
1018 ImmutableList.<JSType>of() /* params */));
1019 }
1020
1021 public void testBackwardsInferenceCall() {
1022 inFunction(
1023 "/** @param {{foo: (number|undefined)}} x */" +
1024 "function f(x) {}" +
1025 "var y = {};" +
1026 "f(y);");
1027
1028 assertEquals("{foo: (number|undefined)}", getType("y").toString());
1029 }
1030
1031 public void testBackwardsInferenceNew() {
1032 inFunction(
1033 "/**\n" +
1034 " * @constructor\n" +
1035 " * @param {{foo: (number|undefined)}} x\n" +
1036 " */" +
1037 "function F(x) {}" +
1038 "var y = {};" +
1039 "new F(y);");
1040
1041 assertEquals("{foo: (number|undefined)}", getType("y").toString());
1042 }
1043
1044 public void testNoThisInference() {
1045 JSType thisType = createNullableType(OBJECT_TYPE);
1046 assumingThisType(thisType);
1047 inFunction("var out = 3; if (goog.isNull(this)) out = this;");
1048 verify("out", createUnionType(OBJECT_TYPE, NUMBER_TYPE));
1049 }
1050
1051 public void testRecordInference() {
1052 inFunction(
1053 "/** @param {{a: (boolean|undefined)}|{b: (string|undefined)}} x */" +
1054 "function f(x) {}" +
1055 "var out = {};" +
1056 "f(out);");
1057 assertEquals("{a: (boolean|undefined), b: (string|undefined)}",
1058 getType("out").toString());
1059 }
1060
1061 public void testIssue785() {
1062 inFunction("/** @param {string|{prop: (string|undefined)}} x */" +
1063 "function f(x) {}" +
1064 "var out = {};" +
1065 "f(out);");
1066 assertEquals("{prop: (string|undefined)}", getType("out").toString());
1067 }
1068
1069 public void testAssertTypeofProp() {
1070 assuming("x", createNullableType(OBJECT_TYPE));
1071 inFunction(
1072 "goog.asserts.assert(typeof x.prop != 'undefined');" +
1073 "out = x.prop;");
1074 verify("out", CHECKED_UNKNOWN_TYPE);
1075 }
1076
1077 private ObjectType getNativeObjectType(JSTypeNative t) {
1078 return registry.getNativeObjectType(t);
1079 }
1080
1081 private JSType getNativeType(JSTypeNative t) {
1082 return registry.getNativeType(t);
1083 }
1084
1085 private JSType templatize(ObjectType objType, ImmutableList<JSType> t) {
1086 return registry.createTemplatizedType(objType, t);
1087 }
1088 }