Skip to content

Commit

Permalink
[NTI] Fix crash related to properties of unknown THIS.
Browse files Browse the repository at this point in the history
Fixes #2538 on github.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=160576845
  • Loading branch information
dimvar authored and brad4d committed Jun 30, 2017
1 parent 9b55459 commit 9e26885
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/com/google/javascript/jscomp/NewTypeInference.java
Expand Up @@ -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,
Expand Down Expand Up @@ -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()) {
Expand Down
9 changes: 8 additions & 1 deletion test/com/google/javascript/jscomp/NewTypeInferenceTest.java
Expand Up @@ -246,7 +246,7 @@ public void testNewInFunctionJsdoc() {
NewTypeInference.INEXISTENT_PROPERTY);
}

public void testAlhpaRenamingDoesntChangeType() {
public void testAlphaRenamingDoesntChangeType() {
typeCheck(LINE_JOINER.join(
"/**",
" * @param {U} x",
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 9e26885

Please sign in to comment.