Skip to content

Commit e294b23

Browse files
authored
Merge pull request #18174 from Microsoft/fixContextualTypeLogicalOr
Fix exponential compile time for || operator
2 parents 4c54323 + 42b6606 commit e294b23

File tree

5 files changed

+241
-1
lines changed

5 files changed

+241
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13059,7 +13059,7 @@ namespace ts {
1305913059
// expression has no contextual type, the right operand is contextually typed by the type of the left operand.
1306013060
let type = getContextualType(binaryExpression);
1306113061
if (!type && node === binaryExpression.right) {
13062-
type = getTypeOfExpression(binaryExpression.left);
13062+
type = getTypeOfExpression(binaryExpression.left, /*cache*/ true);
1306313063
}
1306413064
return type;
1306513065
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//// [contextualTypeLogicalOr.ts]
2+
// Repro from #18005
3+
4+
let x = 123;
5+
var a =
6+
x && [1, 2, 3, 4] ||
7+
x && [1, 2, 3, 4] ||
8+
x && [1, 2, 3, 4] ||
9+
x && [1, 2, 3, 4] ||
10+
x && [1, 2, 3, 4] ||
11+
x && [1, 2, 3, 4] ||
12+
x && [1, 2, 3, 4] ||
13+
x && [1, 2, 3, 4] ||
14+
x && [1, 2, 3, 4] ||
15+
x && [1, 2, 3, 4] ||
16+
x && [1, 2, 3, 4] ||
17+
x && [1, 2, 3, 4] ||
18+
x && [1, 2, 3, 4];
19+
20+
21+
//// [contextualTypeLogicalOr.js]
22+
// Repro from #18005
23+
var x = 123;
24+
var a = x && [1, 2, 3, 4] ||
25+
x && [1, 2, 3, 4] ||
26+
x && [1, 2, 3, 4] ||
27+
x && [1, 2, 3, 4] ||
28+
x && [1, 2, 3, 4] ||
29+
x && [1, 2, 3, 4] ||
30+
x && [1, 2, 3, 4] ||
31+
x && [1, 2, 3, 4] ||
32+
x && [1, 2, 3, 4] ||
33+
x && [1, 2, 3, 4] ||
34+
x && [1, 2, 3, 4] ||
35+
x && [1, 2, 3, 4] ||
36+
x && [1, 2, 3, 4];
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
=== tests/cases/compiler/contextualTypeLogicalOr.ts ===
2+
// Repro from #18005
3+
4+
let x = 123;
5+
>x : Symbol(x, Decl(contextualTypeLogicalOr.ts, 2, 3))
6+
7+
var a =
8+
>a : Symbol(a, Decl(contextualTypeLogicalOr.ts, 3, 3))
9+
10+
x && [1, 2, 3, 4] ||
11+
>x : Symbol(x, Decl(contextualTypeLogicalOr.ts, 2, 3))
12+
13+
x && [1, 2, 3, 4] ||
14+
>x : Symbol(x, Decl(contextualTypeLogicalOr.ts, 2, 3))
15+
16+
x && [1, 2, 3, 4] ||
17+
>x : Symbol(x, Decl(contextualTypeLogicalOr.ts, 2, 3))
18+
19+
x && [1, 2, 3, 4] ||
20+
>x : Symbol(x, Decl(contextualTypeLogicalOr.ts, 2, 3))
21+
22+
x && [1, 2, 3, 4] ||
23+
>x : Symbol(x, Decl(contextualTypeLogicalOr.ts, 2, 3))
24+
25+
x && [1, 2, 3, 4] ||
26+
>x : Symbol(x, Decl(contextualTypeLogicalOr.ts, 2, 3))
27+
28+
x && [1, 2, 3, 4] ||
29+
>x : Symbol(x, Decl(contextualTypeLogicalOr.ts, 2, 3))
30+
31+
x && [1, 2, 3, 4] ||
32+
>x : Symbol(x, Decl(contextualTypeLogicalOr.ts, 2, 3))
33+
34+
x && [1, 2, 3, 4] ||
35+
>x : Symbol(x, Decl(contextualTypeLogicalOr.ts, 2, 3))
36+
37+
x && [1, 2, 3, 4] ||
38+
>x : Symbol(x, Decl(contextualTypeLogicalOr.ts, 2, 3))
39+
40+
x && [1, 2, 3, 4] ||
41+
>x : Symbol(x, Decl(contextualTypeLogicalOr.ts, 2, 3))
42+
43+
x && [1, 2, 3, 4] ||
44+
>x : Symbol(x, Decl(contextualTypeLogicalOr.ts, 2, 3))
45+
46+
x && [1, 2, 3, 4];
47+
>x : Symbol(x, Decl(contextualTypeLogicalOr.ts, 2, 3))
48+
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
=== tests/cases/compiler/contextualTypeLogicalOr.ts ===
2+
// Repro from #18005
3+
4+
let x = 123;
5+
>x : number
6+
>123 : 123
7+
8+
var a =
9+
>a : number[]
10+
11+
x && [1, 2, 3, 4] ||
12+
>x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] : number[]
13+
>x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] : number[]
14+
>x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] : number[]
15+
>x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] : number[]
16+
>x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] : number[]
17+
>x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] : number[]
18+
>x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] : number[]
19+
>x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] : number[]
20+
>x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] : number[]
21+
>x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] : number[]
22+
>x && [1, 2, 3, 4] || x && [1, 2, 3, 4] || x && [1, 2, 3, 4] : number[]
23+
>x && [1, 2, 3, 4] || x && [1, 2, 3, 4] : number[]
24+
>x && [1, 2, 3, 4] : number[]
25+
>x : number
26+
>[1, 2, 3, 4] : number[]
27+
>1 : 1
28+
>2 : 2
29+
>3 : 3
30+
>4 : 4
31+
32+
x && [1, 2, 3, 4] ||
33+
>x && [1, 2, 3, 4] : number[]
34+
>x : number
35+
>[1, 2, 3, 4] : number[]
36+
>1 : 1
37+
>2 : 2
38+
>3 : 3
39+
>4 : 4
40+
41+
x && [1, 2, 3, 4] ||
42+
>x && [1, 2, 3, 4] : number[]
43+
>x : number
44+
>[1, 2, 3, 4] : number[]
45+
>1 : 1
46+
>2 : 2
47+
>3 : 3
48+
>4 : 4
49+
50+
x && [1, 2, 3, 4] ||
51+
>x && [1, 2, 3, 4] : number[]
52+
>x : number
53+
>[1, 2, 3, 4] : number[]
54+
>1 : 1
55+
>2 : 2
56+
>3 : 3
57+
>4 : 4
58+
59+
x && [1, 2, 3, 4] ||
60+
>x && [1, 2, 3, 4] : number[]
61+
>x : number
62+
>[1, 2, 3, 4] : number[]
63+
>1 : 1
64+
>2 : 2
65+
>3 : 3
66+
>4 : 4
67+
68+
x && [1, 2, 3, 4] ||
69+
>x && [1, 2, 3, 4] : number[]
70+
>x : number
71+
>[1, 2, 3, 4] : number[]
72+
>1 : 1
73+
>2 : 2
74+
>3 : 3
75+
>4 : 4
76+
77+
x && [1, 2, 3, 4] ||
78+
>x && [1, 2, 3, 4] : number[]
79+
>x : number
80+
>[1, 2, 3, 4] : number[]
81+
>1 : 1
82+
>2 : 2
83+
>3 : 3
84+
>4 : 4
85+
86+
x && [1, 2, 3, 4] ||
87+
>x && [1, 2, 3, 4] : number[]
88+
>x : number
89+
>[1, 2, 3, 4] : number[]
90+
>1 : 1
91+
>2 : 2
92+
>3 : 3
93+
>4 : 4
94+
95+
x && [1, 2, 3, 4] ||
96+
>x && [1, 2, 3, 4] : number[]
97+
>x : number
98+
>[1, 2, 3, 4] : number[]
99+
>1 : 1
100+
>2 : 2
101+
>3 : 3
102+
>4 : 4
103+
104+
x && [1, 2, 3, 4] ||
105+
>x && [1, 2, 3, 4] : number[]
106+
>x : number
107+
>[1, 2, 3, 4] : number[]
108+
>1 : 1
109+
>2 : 2
110+
>3 : 3
111+
>4 : 4
112+
113+
x && [1, 2, 3, 4] ||
114+
>x && [1, 2, 3, 4] : number[]
115+
>x : number
116+
>[1, 2, 3, 4] : number[]
117+
>1 : 1
118+
>2 : 2
119+
>3 : 3
120+
>4 : 4
121+
122+
x && [1, 2, 3, 4] ||
123+
>x && [1, 2, 3, 4] : number[]
124+
>x : number
125+
>[1, 2, 3, 4] : number[]
126+
>1 : 1
127+
>2 : 2
128+
>3 : 3
129+
>4 : 4
130+
131+
x && [1, 2, 3, 4];
132+
>x && [1, 2, 3, 4] : number[]
133+
>x : number
134+
>[1, 2, 3, 4] : number[]
135+
>1 : 1
136+
>2 : 2
137+
>3 : 3
138+
>4 : 4
139+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Repro from #18005
2+
3+
let x = 123;
4+
var a =
5+
x && [1, 2, 3, 4] ||
6+
x && [1, 2, 3, 4] ||
7+
x && [1, 2, 3, 4] ||
8+
x && [1, 2, 3, 4] ||
9+
x && [1, 2, 3, 4] ||
10+
x && [1, 2, 3, 4] ||
11+
x && [1, 2, 3, 4] ||
12+
x && [1, 2, 3, 4] ||
13+
x && [1, 2, 3, 4] ||
14+
x && [1, 2, 3, 4] ||
15+
x && [1, 2, 3, 4] ||
16+
x && [1, 2, 3, 4] ||
17+
x && [1, 2, 3, 4];

0 commit comments

Comments
 (0)