Skip to content

Commit

Permalink
Don't leak EvolvingArray out of code flow (#49943)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey authored Jul 19, 2022
1 parent 3863cc4 commit 05d2076
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11896,13 +11896,19 @@ namespace ts {
else if ((type as MappedType).objectFlags & ObjectFlags.Mapped) {
resolveMappedTypeMembers(type as MappedType);
}
else {
Debug.fail("Unhandled object type " + Debug.formatObjectFlags(type.objectFlags));
}
}
else if (type.flags & TypeFlags.Union) {
resolveUnionTypeMembers(type as UnionType);
}
else if (type.flags & TypeFlags.Intersection) {
resolveIntersectionTypeMembers(type as IntersectionType);
}
else {
Debug.fail("Unhandled type " + Debug.formatTypeFlags(type.flags));
}
}
return type as ResolvedType;
}
Expand Down Expand Up @@ -24613,7 +24619,7 @@ namespace ts {
}
// for (const _ in ref) acts as a nonnull on ref
if (isVariableDeclaration(node) && node.parent.parent.kind === SyntaxKind.ForInStatement && isMatchingReference(reference, node.parent.parent.expression)) {
return getNonNullableTypeIfNeeded(getTypeFromFlowType(getTypeAtFlowNode(flow.antecedent)));
return getNonNullableTypeIfNeeded(finalizeEvolvingArrayType(getTypeFromFlowType(getTypeAtFlowNode(flow.antecedent))));
}
// Assignment doesn't affect reference
return undefined;
Expand Down
18 changes: 18 additions & 0 deletions tests/baselines/reference/evolvingArrayResolvedAssert.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
tests/cases/compiler/evolvingArrayResolvedAssert.ts(1,5): error TS7034: Variable 'C' implicitly has type 'any[]' in some locations where its type cannot be determined.
tests/cases/compiler/evolvingArrayResolvedAssert.ts(2,15): error TS7005: Variable 'C' implicitly has an 'any[]' type.
tests/cases/compiler/evolvingArrayResolvedAssert.ts(3,9): error TS7005: Variable 'C' implicitly has an 'any[]' type.


==== tests/cases/compiler/evolvingArrayResolvedAssert.ts (3 errors) ====
var C = [];
~
!!! error TS7034: Variable 'C' implicitly has type 'any[]' in some locations where its type cannot be determined.
for (var a in C) {
~
!!! error TS7005: Variable 'C' implicitly has an 'any[]' type.
if (C.hasOwnProperty(a)) {
~
!!! error TS7005: Variable 'C' implicitly has an 'any[]' type.
}
}

15 changes: 15 additions & 0 deletions tests/baselines/reference/evolvingArrayResolvedAssert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//// [evolvingArrayResolvedAssert.ts]
var C = [];
for (var a in C) {
if (C.hasOwnProperty(a)) {
}
}


//// [evolvingArrayResolvedAssert.js]
"use strict";
var C = [];
for (var a in C) {
if (C.hasOwnProperty(a)) {
}
}
16 changes: 16 additions & 0 deletions tests/baselines/reference/evolvingArrayResolvedAssert.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/compiler/evolvingArrayResolvedAssert.ts ===
var C = [];
>C : Symbol(C, Decl(evolvingArrayResolvedAssert.ts, 0, 3))

for (var a in C) {
>a : Symbol(a, Decl(evolvingArrayResolvedAssert.ts, 1, 8))
>C : Symbol(C, Decl(evolvingArrayResolvedAssert.ts, 0, 3))

if (C.hasOwnProperty(a)) {
>C.hasOwnProperty : Symbol(Object.hasOwnProperty, Decl(lib.es5.d.ts, --, --))
>C : Symbol(C, Decl(evolvingArrayResolvedAssert.ts, 0, 3))
>hasOwnProperty : Symbol(Object.hasOwnProperty, Decl(lib.es5.d.ts, --, --))
>a : Symbol(a, Decl(evolvingArrayResolvedAssert.ts, 1, 8))
}
}

18 changes: 18 additions & 0 deletions tests/baselines/reference/evolvingArrayResolvedAssert.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/compiler/evolvingArrayResolvedAssert.ts ===
var C = [];
>C : any[]
>[] : never[]

for (var a in C) {
>a : string
>C : any[]

if (C.hasOwnProperty(a)) {
>C.hasOwnProperty(a) : boolean
>C.hasOwnProperty : (v: PropertyKey) => boolean
>C : any[]
>hasOwnProperty : (v: PropertyKey) => boolean
>a : string
}
}

7 changes: 7 additions & 0 deletions tests/cases/compiler/evolvingArrayResolvedAssert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @strict: true

var C = [];
for (var a in C) {
if (C.hasOwnProperty(a)) {
}
}

0 comments on commit 05d2076

Please sign in to comment.