Skip to content

Commit

Permalink
[NTI] Improvements to handling of truthy types.
Browse files Browse the repository at this point in the history
Fixes github issue #1857
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=127561403
  • Loading branch information
dimvar authored and blickly committed Jul 15, 2016
1 parent d733713 commit cdf0641
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/NewTypeInference.java
Expand Up @@ -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()
Expand Down
3 changes: 1 addition & 2 deletions src/com/google/javascript/jscomp/newtypes/JSType.java
Expand Up @@ -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;
Expand Down Expand Up @@ -749,7 +748,7 @@ boolean unifyWithSubtype(JSType other, List<String> 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
Expand Down
22 changes: 21 additions & 1 deletion test/com/google/javascript/jscomp/NewTypeInferenceTest.java
Expand Up @@ -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;",
Expand All @@ -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<T>} arr",
" * @param {function(T)} cb",
" * @template T",
" */",
"function forEach(arr, cb) {}"));
}

public void testIObjectExternMissing() {
Expand Down

0 comments on commit cdf0641

Please sign in to comment.