Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Restrict types when using expressions with-in goog.asserts.assert, such

as (x != null), etc.

R=nicksantos
DELTA=68  (51 added, 6 deleted, 11 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=4606


git-svn-id: http://closure-compiler.googlecode.com/svn/trunk@1919 b0f006be-c8cd-11de-a2e8-8d36a3108c74
  • Loading branch information...
commit b46500e2041922c442dc83e97129b7754f41a166 1 parent 92b8ef9
johnlenz@google.com authored
View
28 src/com/google/javascript/jscomp/TypeInference.java
@@ -815,27 +815,23 @@ private FlowScope tightenTypesAfterAssertions(FlowScope scope,
String assertedNodeName = assertedNode.getQualifiedName();
// Handle assertions that enforce expressions evaluate to true.
if (assertedType == null) {
- if (assertedNodeName != null) {
- JSType type = getJSType(assertedNode);
- JSType narrowed = type.restrictByNotNullOrUndefined();
- if (type != narrowed) {
- scope = narrowScope(scope, assertedNode, narrowed);
- callNode.setJSType(narrowed);
- }
- } else if (assertedNode.isAnd() ||
- assertedNode.isOr()) {
- BooleanOutcomePair conditionOutcomes =
- traverseWithinShortCircuitingBinOp(assertedNode, scope);
- scope = reverseInterpreter.getPreciserScopeKnowingConditionOutcome(
- assertedNode, conditionOutcomes.getOutcomeFlowScope(
- assertedNode.getType(), true), true);
+ // Handle arbitrary expressions within the assert.
+ scope = reverseInterpreter.getPreciserScopeKnowingConditionOutcome(
+ assertedNode, scope, true);
+ // Build the result of the assertExpression
+ JSType type = getJSType(assertedNode);
+ JSType narrowed = type.restrictByNotNullOrUndefined();
+ if (type != narrowed) {
+ callNode.setJSType(narrowed);
}
- } else if (assertedNodeName != null) {
+ } else {
// Handle assertions that enforce expressions are of a certain type.
JSType type = getJSType(assertedNode);
JSType narrowed = type.getGreatestSubtype(getNativeType(assertedType));
if (type != narrowed) {
- scope = narrowScope(scope, assertedNode, narrowed);
+ if (assertedNodeName != null) {
+ scope = narrowScope(scope, assertedNode, narrowed);
+ }
callNode.setJSType(narrowed);
}
}
View
49 test/com/google/javascript/jscomp/TypeInferenceTest.java
@@ -212,6 +212,12 @@ public void testIf1() {
verifySubtypeOf("y", OBJECT_TYPE);
}
+ public void testIf1a() {
+ assuming("x", createNullableType(OBJECT_TYPE));
+ inFunction("var y = {}; if (x != null) { y = x; }");
+ verifySubtypeOf("y", OBJECT_TYPE);
+ }
+
public void testIf2() {
assuming("x", createNullableType(OBJECT_TYPE));
inFunction("var y = x; if (x) { y = x; } else { y = {}; }");
@@ -232,6 +238,14 @@ public void testAssert1() {
verify("out2", OBJECT_TYPE);
}
+ public void testAssert1a() {
+ JSType startType = createNullableType(OBJECT_TYPE);
+ assuming("x", startType);
+ inFunction("out1 = x; goog.asserts.assert(x !== null); out2 = x;");
+ verify("out1", startType);
+ verify("out2", OBJECT_TYPE);
+ }
+
public void testAssert2() {
JSType startType = createNullableType(OBJECT_TYPE);
assuming("x", startType);
@@ -284,6 +298,32 @@ public void testAssert7() {
verify("out2", OBJECT_TYPE);
}
+ public void disable_testAssert8() { // Fails, out2 is UNKNOWN
+ JSType startType = createNullableType(OBJECT_TYPE);
+ assuming("x", startType);
+ inFunction("out1 = x; out2 = goog.asserts.assert(x != null);");
+ verify("out1", startType);
+ verify("out2", BOOLEAN_TYPE);
+ }
+
+ public void testAssert9() {
+ JSType startType = createNullableType(NUMBER_TYPE);
+ assuming("x", startType);
+ inFunction("out1 = x; out2 = goog.asserts.assert(y = x);");
+ verify("out1", startType);
+ verify("out2", NUMBER_TYPE);
+ }
+
+ public void testAssert10() {
+ JSType startType = createNullableType(OBJECT_TYPE);
+ assuming("x", startType);
+ assuming("y", startType);
+ inFunction("out1 = x; out2 = goog.asserts.assert(x && y); out3 = x;");
+ verify("out1", startType);
+ verify("out2", OBJECT_TYPE);
+ verify("out3", OBJECT_TYPE);
+ }
+
public void testAssertNumber() {
JSType startType = createNullableType(ALL_TYPE);
assuming("x", startType);
@@ -300,6 +340,15 @@ public void testAssertNumber2() {
verify("out1", startType);
}
+ public void testAssertNumber3() {
+ // Make sure it ignores expressions.
+ JSType startType = createNullableType(ALL_TYPE);
+ assuming("x", startType);
+ inFunction("out1 = x; out2 = goog.asserts.assertNumber(x + x);");
+ verify("out1", startType);
+ verify("out2", NUMBER_TYPE);
+ }
+
public void testAssertString() {
JSType startType = createNullableType(ALL_TYPE);
assuming("x", startType);
Please sign in to comment.
Something went wrong with that request. Please try again.