From cdf064187bd660fad4b5b65b12be5e2a043779eb Mon Sep 17 00:00:00 2001 From: dimvar Date: Fri, 15 Jul 2016 11:36:35 -0700 Subject: [PATCH] [NTI] Improvements to handling of truthy types. Fixes github issue #1857 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=127561403 --- .../javascript/jscomp/NewTypeInference.java | 2 +- .../javascript/jscomp/newtypes/JSType.java | 3 +-- .../jscomp/NewTypeInferenceTest.java | 22 ++++++++++++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/com/google/javascript/jscomp/NewTypeInference.java b/src/com/google/javascript/jscomp/NewTypeInference.java index dc357c014bd..ac349aa149f 100644 --- a/src/com/google/javascript/jscomp/NewTypeInference.java +++ b/src/com/google/javascript/jscomp/NewTypeInference.java @@ -1600,7 +1600,7 @@ private EnvTypePair analyzeInstanceofFwd( EnvTypePair objPair, ctorPair; // First, evaluate ignoring the specialized context - objPair = analyzeExprFwd(obj, inEnv); + objPair = analyzeExprFwd(obj, inEnv, JSType.TOP_OBJECT); JSType objType = objPair.type; if (!objType.isTop() && !objType.isUnknown() diff --git a/src/com/google/javascript/jscomp/newtypes/JSType.java b/src/com/google/javascript/jscomp/newtypes/JSType.java index b35a9c38975..a291e9325b3 100644 --- a/src/com/google/javascript/jscomp/newtypes/JSType.java +++ b/src/com/google/javascript/jscomp/newtypes/JSType.java @@ -28,7 +28,6 @@ import com.google.javascript.rhino.Node; import com.google.javascript.rhino.ObjectTypeI; import com.google.javascript.rhino.TypeI; - import java.util.Collection; import java.util.LinkedHashSet; import java.util.List; @@ -749,7 +748,7 @@ boolean unifyWithSubtype(JSType other, List typeParameters, && typeParameters.contains(getTypeVar())) { updateTypemap(typeMultimap, getTypeVar(), other); return true; - } else if (other.isUnknown()) { + } else if (other.isUnknown() || other.isTrueOrTruthy()) { return true; } else if (other.isTop()) { // T|number doesn't unify with TOP diff --git a/test/com/google/javascript/jscomp/NewTypeInferenceTest.java b/test/com/google/javascript/jscomp/NewTypeInferenceTest.java index e2b6bc52842..6a45fff6fa0 100644 --- a/test/com/google/javascript/jscomp/NewTypeInferenceTest.java +++ b/test/com/google/javascript/jscomp/NewTypeInferenceTest.java @@ -17218,7 +17218,7 @@ public void testMeetingWithTruthyFalsy() { "function f(x) { if (!x) { return /** @type {number} */ (x); } }"); } - public void testPropAccessOnTruthy() { + public void testUsingTruthy() { typeCheck(LINE_JOINER.join( "function f(/** !Function */ x) {", " return x.superClass_ ? x.superClass_.constructor : null;", @@ -17228,6 +17228,26 @@ public void testPropAccessOnTruthy() { "function f(/** !Function */ x) {", " if (x.superClass_) { x.superClass_.constructor = null; }", "}")); + + typeCheck(LINE_JOINER.join( + "function g(x) { var /** !Object */ y = x; }", + "function f(x) {", + " if (!x) return null;", + " g(x);", + " return x instanceof Array;", + "}")); + + typeCheck(LINE_JOINER.join( + "function f(x) {", + " if (!x) return null;", + " forEach(x, function(y){});", + "}", + "/**", + " * @param {!Array} arr", + " * @param {function(T)} cb", + " * @template T", + " */", + "function forEach(arr, cb) {}")); } public void testIObjectExternMissing() {