Fix infinite recursion in control flow analyzer inlining logic (#45845)

* Move inlineLevel counter to main type checker scope

* Add regression test
This commit is contained in:
Anders Hejlsberg 2021-09-13 11:02:55 -07:00 committed by GitHub
parent e03600f257
commit 5b84512ccc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 91 additions and 2 deletions

View File

@ -333,6 +333,7 @@ namespace ts {
let totalInstantiationCount = 0;
let instantiationCount = 0;
let instantiationDepth = 0;
let inlineLevel = 0;
let currentNode: Node | undefined;
const emptySymbols = createSymbolTable();
@ -23247,7 +23248,6 @@ namespace ts {
let key: string | undefined;
let isKeySet = false;
let flowDepth = 0;
let inlineLevel = 0;
if (flowAnalysisDisabled) {
return errorType;
}

View File

@ -32,9 +32,11 @@ tests/cases/conformance/controlFlow/controlFlowAliasing.ts(267,13): error TS2322
Type 'number' is not assignable to type 'string'.
tests/cases/conformance/controlFlow/controlFlowAliasing.ts(270,13): error TS2322: Type 'string | number' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.
tests/cases/conformance/controlFlow/controlFlowAliasing.ts(280,5): error TS2448: Block-scoped variable 'a' used before its declaration.
tests/cases/conformance/controlFlow/controlFlowAliasing.ts(280,5): error TS2454: Variable 'a' is used before being assigned.
==== tests/cases/conformance/controlFlow/controlFlowAliasing.ts (17 errors) ====
==== tests/cases/conformance/controlFlow/controlFlowAliasing.ts (19 errors) ====
// Narrowing by aliased conditional expressions
function f10(x: string | number) {
@ -358,4 +360,19 @@ tests/cases/conformance/controlFlow/controlFlowAliasing.ts(270,13): error TS2322
!!! error TS2322: Type 'string' is not assignable to type 'number'.
}
}
// Repro from #45830
const obj = {
fn: () => true
};
if (a) { }
~
!!! error TS2448: Block-scoped variable 'a' used before its declaration.
!!! related TS2728 tests/cases/conformance/controlFlow/controlFlowAliasing.ts:282:7: 'a' is declared here.
~
!!! error TS2454: Variable 'a' is used before being assigned.
const a = obj.fn();

View File

@ -271,6 +271,16 @@ function foo({ kind, payload }: Data) {
let t: number = payload;
}
}
// Repro from #45830
const obj = {
fn: () => true
};
if (a) { }
const a = obj.fn();
//// [controlFlowAliasing.js]
@ -522,6 +532,12 @@ function foo(_a) {
var t = payload;
}
}
// Repro from #45830
var obj = {
fn: function () { return true; }
};
if (a) { }
var a = obj.fn();
//// [controlFlowAliasing.d.ts]
@ -657,3 +673,7 @@ declare type Data = {
};
declare function gg2(obj: Data): void;
declare function foo({ kind, payload }: Data): void;
declare const obj: {
fn: () => boolean;
};
declare const a: boolean;

View File

@ -772,3 +772,22 @@ function foo({ kind, payload }: Data) {
}
}
// Repro from #45830
const obj = {
>obj : Symbol(obj, Decl(controlFlowAliasing.ts, 275, 5))
fn: () => true
>fn : Symbol(fn, Decl(controlFlowAliasing.ts, 275, 13))
};
if (a) { }
>a : Symbol(a, Decl(controlFlowAliasing.ts, 281, 5))
const a = obj.fn();
>a : Symbol(a, Decl(controlFlowAliasing.ts, 281, 5))
>obj.fn : Symbol(fn, Decl(controlFlowAliasing.ts, 275, 13))
>obj : Symbol(obj, Decl(controlFlowAliasing.ts, 275, 5))
>fn : Symbol(fn, Decl(controlFlowAliasing.ts, 275, 13))

View File

@ -897,3 +897,26 @@ function foo({ kind, payload }: Data) {
}
}
// Repro from #45830
const obj = {
>obj : { fn: () => boolean; }
>{ fn: () => true} : { fn: () => boolean; }
fn: () => true
>fn : () => boolean
>() => true : () => boolean
>true : true
};
if (a) { }
>a : boolean
const a = obj.fn();
>a : boolean
>obj.fn() : boolean
>obj.fn : () => boolean
>obj : { fn: () => boolean; }
>fn : () => boolean

View File

@ -273,3 +273,13 @@ function foo({ kind, payload }: Data) {
let t: number = payload;
}
}
// Repro from #45830
const obj = {
fn: () => true
};
if (a) { }
const a = obj.fn();