From 9e2688565abb2fa29b7270b509e6bbd8f30cdc8f Mon Sep 17 00:00:00 2001 From: dimvar Date: Thu, 29 Jun 2017 14:50:21 -0700 Subject: [PATCH] [NTI] Fix crash related to properties of unknown THIS. Fixes #2538 on github. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=160576845 --- src/com/google/javascript/jscomp/NewTypeInference.java | 8 ++++++-- .../google/javascript/jscomp/NewTypeInferenceTest.java | 9 ++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/com/google/javascript/jscomp/NewTypeInference.java b/src/com/google/javascript/jscomp/NewTypeInference.java index 1eefe98ac77..571632557c3 100644 --- a/src/com/google/javascript/jscomp/NewTypeInference.java +++ b/src/com/google/javascript/jscomp/NewTypeInference.java @@ -3382,6 +3382,11 @@ private TypeEnv updateLvalueTypeInEnv( String objName = qname.getLeftmostName(); QualifiedName props = qname.getAllButLeftmost(); JSType objType = envGetType(env, objName); + if (objType == null) { + // Don't specialize THIS properties in functions where THIS is unknown. + checkState(objName.equals("this")); + return env; + } // TODO(dimvar): In analyzeNameFwd/Bwd, we are careful to not // specialize namespaces, and we need the same check here. But // currently, stopping specialization here causes tests to fail, @@ -4307,8 +4312,7 @@ private EnvTypePair mayWarnAboutNullableReferenceAndTighten( JSType minusNull = recvType.removeType(NULL_OR_UNDEFINED); if (!minusNull.isBottom()) { if (this.reportNullDeref) { - warnings.add(JSError.make( - obj, NULLABLE_DEREFERENCE, recvType.toString())); + warnings.add(JSError.make(obj, NULLABLE_DEREFERENCE, recvType.toString())); } TypeEnv outEnv = inEnv; if (obj.isQualifiedName()) { diff --git a/test/com/google/javascript/jscomp/NewTypeInferenceTest.java b/test/com/google/javascript/jscomp/NewTypeInferenceTest.java index 509b58af307..6ada1cdf267 100644 --- a/test/com/google/javascript/jscomp/NewTypeInferenceTest.java +++ b/test/com/google/javascript/jscomp/NewTypeInferenceTest.java @@ -246,7 +246,7 @@ public void testNewInFunctionJsdoc() { NewTypeInference.INEXISTENT_PROPERTY); } - public void testAlhpaRenamingDoesntChangeType() { + public void testAlphaRenamingDoesntChangeType() { typeCheck(LINE_JOINER.join( "/**", " * @param {U} x", @@ -20855,4 +20855,11 @@ public void testPrintTypevarIDs() { "Expected a supertype of : T#1", "but found : T#2")); } + + public void testDontCrashOnKnownPropertyOfGlobalThis() { + typeCheck( + "function f(){ this.constructor['fubar'] = 1; }", + NewTypeInference.NULLABLE_DEREFERENCE, + NewTypeInference.GLOBAL_THIS); + } }