Skip to content

Commit d68cd20

Browse files
committed
Merge pull request #8556 from Microsoft/controlFlowLoopAnalysis
Fix control flow loop analysis issue
2 parents 89506c1 + 43691b1 commit d68cd20

File tree

4 files changed

+78
-7
lines changed

4 files changed

+78
-7
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7780,16 +7780,15 @@ namespace ts {
77807780
if (cache[key]) {
77817781
return cache[key];
77827782
}
7783-
// If the type at a particular antecedent path is the declared type and the
7784-
// reference is known to always be assigned (i.e. when declared and initial types
7785-
// are the same), there is no reason to process more antecedents since the only
7786-
// possible outcome is subtypes that will be removed in the final union type anyway.
7787-
if (type === declaredType && declaredType === initialType) {
7788-
return cache[key] = type;
7789-
}
77907783
if (!contains(antecedentTypes, type)) {
77917784
antecedentTypes.push(type);
77927785
}
7786+
// If the type at a particular antecedent path is the declared type there is no
7787+
// reason to process more antecedents since the only possible outcome is subtypes
7788+
// that will be removed in the final union type anyway.
7789+
if (type === declaredType) {
7790+
break;
7791+
}
77937792
}
77947793
return cache[key] = getUnionType(antecedentTypes);
77957794
}

tests/baselines/reference/controlFlowLoopAnalysis.errors.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,23 @@ tests/cases/compiler/controlFlowLoopAnalysis.ts(13,25): error TS2345: Argument o
3636
}
3737
}
3838
}
39+
40+
// Repro from #8511
41+
42+
function mapUntilCant<a, b>(
43+
values: a[],
44+
canTake: (value: a, index: number) => boolean,
45+
mapping: (value: a, index: number) => b
46+
): b[] {
47+
let result: b[] = [];
48+
for (let index = 0, length = values.length; index < length; index++) {
49+
let value = values[index];
50+
if (canTake(value, index)) {
51+
result.push(mapping(value, index));
52+
} else {
53+
return result;
54+
}
55+
}
56+
return result;
57+
}
3958

tests/baselines/reference/controlFlowLoopAnalysis.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ function test2() {
2929
}
3030
}
3131
}
32+
33+
// Repro from #8511
34+
35+
function mapUntilCant<a, b>(
36+
values: a[],
37+
canTake: (value: a, index: number) => boolean,
38+
mapping: (value: a, index: number) => b
39+
): b[] {
40+
let result: b[] = [];
41+
for (let index = 0, length = values.length; index < length; index++) {
42+
let value = values[index];
43+
if (canTake(value, index)) {
44+
result.push(mapping(value, index));
45+
} else {
46+
return result;
47+
}
48+
}
49+
return result;
50+
}
3251

3352

3453
//// [controlFlowLoopAnalysis.js]
@@ -56,3 +75,17 @@ function test2() {
5675
}
5776
}
5877
}
78+
// Repro from #8511
79+
function mapUntilCant(values, canTake, mapping) {
80+
var result = [];
81+
for (var index = 0, length = values.length; index < length; index++) {
82+
var value = values[index];
83+
if (canTake(value, index)) {
84+
result.push(mapping(value, index));
85+
}
86+
else {
87+
return result;
88+
}
89+
}
90+
return result;
91+
}

tests/cases/compiler/controlFlowLoopAnalysis.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// @strictNullChecks: true
2+
// @noImplicitAny: true
23

34
// Repro from #8418
45

@@ -29,3 +30,22 @@ function test2() {
2930
}
3031
}
3132
}
33+
34+
// Repro from #8511
35+
36+
function mapUntilCant<a, b>(
37+
values: a[],
38+
canTake: (value: a, index: number) => boolean,
39+
mapping: (value: a, index: number) => b
40+
): b[] {
41+
let result: b[] = [];
42+
for (let index = 0, length = values.length; index < length; index++) {
43+
let value = values[index];
44+
if (canTake(value, index)) {
45+
result.push(mapping(value, index));
46+
} else {
47+
return result;
48+
}
49+
}
50+
return result;
51+
}

0 commit comments

Comments
 (0)