Skip to content

Always create a temporary for iterated expressions in a for-of loop #5477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3614,10 +3614,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
//
// for (let v of arr) { }
//
// we don't want to emit a temporary variable for the RHS, just use it directly.
let rhsIsIdentifier = node.expression.kind === SyntaxKind.Identifier;
// we can't reuse 'arr' because it might be modified within the body of the loop.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One possibility might be to check if the rhs is declared as const, and then we can emit it inline.

let counter = createTempVariable(TempFlags._i);
let rhsReference = rhsIsIdentifier ? <Identifier>node.expression : createTempVariable(TempFlags.Auto);
let rhsReference = createSynthesizedNode(SyntaxKind.Identifier) as Identifier;
rhsReference.text = node.expression.kind === SyntaxKind.Identifier ?
makeUniqueName((<Identifier>node.expression).text) :
makeTempVariableName(TempFlags.Auto);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add tests for this case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have tests for that case


// This is the let keyword for the counter and rhsReference. The let keyword for
// the LHS will be emitted inside the body.
Expand All @@ -3629,15 +3631,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
write(" = 0");
emitEnd(node.expression);

if (!rhsIsIdentifier) {
// , _a = expr
write(", ");
emitStart(node.expression);
emitNodeWithoutSourceMap(rhsReference);
write(" = ");
emitNodeWithoutSourceMap(node.expression);
emitEnd(node.expression);
}
// , _a = expr
write(", ");
emitStart(node.expression);
emitNodeWithoutSourceMap(rhsReference);
write(" = ");
emitNodeWithoutSourceMap(node.expression);
emitEnd(node.expression);

write("; ");

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/ES3For-ofTypeCheck4.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ for (const v of union) { }

//// [ES3For-ofTypeCheck4.js]
var union;
for (var _i = 0; _i < union.length; _i++) {
var v = union[_i];
for (var _i = 0, union_1 = union; _i < union_1.length; _i++) {
var v = union_1[_i];
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/ES3For-ofTypeCheck6.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ for (var v of union) { }

//// [ES3For-ofTypeCheck6.js]
var union;
for (var _i = 0; _i < union.length; _i++) {
var v = union[_i];
for (var _i = 0, union_1 = union; _i < union_1.length; _i++) {
var v = union_1[_i];
}
6 changes: 3 additions & 3 deletions tests/baselines/reference/ES5For-of24.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ for (var v of a) {

//// [ES5For-of24.js]
var a = [1, 2, 3];
for (var _i = 0; _i < a.length; _i++) {
var v = a[_i];
var a_1 = 0;
for (var _i = 0, a_1 = a; _i < a_1.length; _i++) {
var v = a_1[_i];
var a_2 = 0;
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/ES5For-of25.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/baselines/reference/ES5For-of25.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 25 additions & 25 deletions tests/baselines/reference/ES5For-of25.sourcemap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ sourceFile:ES5For-of25.ts
10> ^
11> ^
12> ^
13> ^^^^^^^^^^^^^^^^^^^^^^->
13> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
1 >
2 >var
3 > a
Expand All @@ -47,58 +47,58 @@ sourceFile:ES5For-of25.ts
11>Emitted(1, 18) Source(1, 18) + SourceIndex(0)
12>Emitted(1, 19) Source(1, 19) + SourceIndex(0)
---
>>>for (var _i = 0; _i < a.length; _i++) {
>>>for (var _i = 0, a_1 = a; _i < a_1.length; _i++) {
1->
2 >^^^
3 > ^
4 > ^
5 > ^^^^^^^^^^
6 > ^^
7 > ^^^^^^^^^^^^^
8 > ^^
9 > ^^^^
10> ^
7 > ^^^^^^^
8 > ^^
9 > ^^^^^^^^^^^^^^^
10> ^^
11> ^^^^
12> ^
1->
>
2 >for
3 >
4 > (var v of
5 > a
6 >
7 > var v
8 >
9 > var v of a
10> )
7 > a
8 >
9 > var v
10>
11> var v of a
12> )
1->Emitted(2, 1) Source(2, 1) + SourceIndex(0)
2 >Emitted(2, 4) Source(2, 4) + SourceIndex(0)
3 >Emitted(2, 5) Source(2, 5) + SourceIndex(0)
4 >Emitted(2, 6) Source(2, 15) + SourceIndex(0)
5 >Emitted(2, 16) Source(2, 16) + SourceIndex(0)
6 >Emitted(2, 18) Source(2, 6) + SourceIndex(0)
7 >Emitted(2, 31) Source(2, 11) + SourceIndex(0)
8 >Emitted(2, 33) Source(2, 6) + SourceIndex(0)
9 >Emitted(2, 37) Source(2, 16) + SourceIndex(0)
10>Emitted(2, 38) Source(2, 17) + SourceIndex(0)
6 >Emitted(2, 18) Source(2, 15) + SourceIndex(0)
7 >Emitted(2, 25) Source(2, 16) + SourceIndex(0)
8 >Emitted(2, 27) Source(2, 6) + SourceIndex(0)
9 >Emitted(2, 42) Source(2, 11) + SourceIndex(0)
10>Emitted(2, 44) Source(2, 6) + SourceIndex(0)
11>Emitted(2, 48) Source(2, 16) + SourceIndex(0)
12>Emitted(2, 49) Source(2, 17) + SourceIndex(0)
---
>>> var v = a[_i];
>>> var v = a_1[_i];
1 >^^^^
2 > ^^^^
3 > ^
4 > ^^^
5 > ^
6 > ^^^^
4 > ^^^^^^^^^^
1 >
2 > var
3 > v
4 > of
5 > a
6 >
4 >
1 >Emitted(3, 5) Source(2, 6) + SourceIndex(0)
2 >Emitted(3, 9) Source(2, 10) + SourceIndex(0)
3 >Emitted(3, 10) Source(2, 11) + SourceIndex(0)
4 >Emitted(3, 13) Source(2, 15) + SourceIndex(0)
5 >Emitted(3, 14) Source(2, 16) + SourceIndex(0)
6 >Emitted(3, 18) Source(2, 11) + SourceIndex(0)
4 >Emitted(3, 20) Source(2, 11) + SourceIndex(0)
---
>>> v;
1 >^^^^
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/ES5For-of30.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ for ([a = 1, b = ""] of tuple) {
//// [ES5For-of30.js]
var a, b;
var tuple = [2, "3"];
for (var _i = 0; _i < tuple.length; _i++) {
_a = tuple[_i], _b = _a[0], a = _b === void 0 ? 1 : _b, _c = _a[1], b = _c === void 0 ? "" : _c;
for (var _i = 0, tuple_1 = tuple; _i < tuple_1.length; _i++) {
_a = tuple_1[_i], _b = _a[0], a = _b === void 0 ? 1 : _b, _c = _a[1], b = _c === void 0 ? "" : _c;
a;
b;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/ES5For-ofTypeCheck11.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ for (v of union) { }
//// [ES5For-ofTypeCheck11.js]
var union;
var v;
for (var _i = 0; _i < union.length; _i++) {
v = union[_i];
for (var _i = 0, union_1 = union; _i < union_1.length; _i++) {
v = union_1[_i];
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/ES5For-ofTypeCheck3.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ for (var v of tuple) { }

//// [ES5For-ofTypeCheck3.js]
var tuple = ["", 0];
for (var _i = 0; _i < tuple.length; _i++) {
var v = tuple[_i];
for (var _i = 0, tuple_1 = tuple; _i < tuple_1.length; _i++) {
var v = tuple_1[_i];
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/ES5For-ofTypeCheck4.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ for (const v of union) { }

//// [ES5For-ofTypeCheck4.js]
var union;
for (var _i = 0; _i < union.length; _i++) {
var v = union[_i];
for (var _i = 0, union_1 = union; _i < union_1.length; _i++) {
var v = union_1[_i];
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/ES5For-ofTypeCheck5.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ for (var v of union) { }

//// [ES5For-ofTypeCheck5.js]
var union;
for (var _i = 0; _i < union.length; _i++) {
var v = union[_i];
for (var _i = 0, union_1 = union; _i < union_1.length; _i++) {
var v = union_1[_i];
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/ES5For-ofTypeCheck6.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ for (var v of union) { }

//// [ES5For-ofTypeCheck6.js]
var union;
for (var _i = 0; _i < union.length; _i++) {
var v = union[_i];
for (var _i = 0, union_1 = union; _i < union_1.length; _i++) {
var v = union_1[_i];
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/ES5For-ofTypeCheck7.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ for (var v of union) { }

//// [ES5For-ofTypeCheck7.js]
var union;
for (var _i = 0; _i < union.length; _i++) {
var v = union[_i];
for (var _i = 0, union_1 = union; _i < union_1.length; _i++) {
var v = union_1[_i];
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/ES5For-ofTypeCheck8.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ for (v of union) { }
//// [ES5For-ofTypeCheck8.js]
var union;
var v;
for (var _i = 0; _i < union.length; _i++) {
v = union[_i];
for (var _i = 0, union_1 = union; _i < union_1.length; _i++) {
v = union_1[_i];
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/ES5For-ofTypeCheck9.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ for (let v of union) { }

//// [ES5For-ofTypeCheck9.js]
var union;
for (var _i = 0; _i < union.length; _i++) {
var v = union[_i];
for (var _i = 0, union_1 = union; _i < union_1.length; _i++) {
var v = union_1[_i];
}
23 changes: 23 additions & 0 deletions tests/baselines/reference/ES5for-of32.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [ES5for-of32.ts]

var array = [1,2,3];
var sum = 0;

for (let num of array) {
if (sum === 0) {
array = [4,5,6]
}

sum += num;
}

//// [ES5for-of32.js]
var array = [1, 2, 3];
var sum = 0;
for (var _i = 0, array_1 = array; _i < array_1.length; _i++) {
var num = array_1[_i];
if (sum === 0) {
array = [4, 5, 6];
}
sum += num;
}
23 changes: 23 additions & 0 deletions tests/baselines/reference/ES5for-of32.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/conformance/statements/for-ofStatements/ES5for-of32.ts ===

var array = [1,2,3];
>array : Symbol(array, Decl(ES5for-of32.ts, 1, 3))

var sum = 0;
>sum : Symbol(sum, Decl(ES5for-of32.ts, 2, 3))

for (let num of array) {
>num : Symbol(num, Decl(ES5for-of32.ts, 4, 8))
>array : Symbol(array, Decl(ES5for-of32.ts, 1, 3))

if (sum === 0) {
>sum : Symbol(sum, Decl(ES5for-of32.ts, 2, 3))

array = [4,5,6]
>array : Symbol(array, Decl(ES5for-of32.ts, 1, 3))
}

sum += num;
>sum : Symbol(sum, Decl(ES5for-of32.ts, 2, 3))
>num : Symbol(num, Decl(ES5for-of32.ts, 4, 8))
}
36 changes: 36 additions & 0 deletions tests/baselines/reference/ES5for-of32.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
=== tests/cases/conformance/statements/for-ofStatements/ES5for-of32.ts ===

var array = [1,2,3];
>array : number[]
>[1,2,3] : number[]
>1 : number
>2 : number
>3 : number

var sum = 0;
>sum : number
>0 : number

for (let num of array) {
>num : number
>array : number[]

if (sum === 0) {
>sum === 0 : boolean
>sum : number
>0 : number

array = [4,5,6]
>array = [4,5,6] : number[]
>array : number[]
>[4,5,6] : number[]
>4 : number
>5 : number
>6 : number
}

sum += num;
>sum += num : number
>sum : number
>num : number
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/argumentsObjectIterator01_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ function doubleAndReturnAsArray(x: number, y: number, z: number): [number, numbe
//// [argumentsObjectIterator01_ES5.js]
function doubleAndReturnAsArray(x, y, z) {
var result = [];
for (var _i = 0; _i < arguments.length; _i++) {
var arg = arguments[_i];
for (var _i = 0, arguments_1 = arguments; _i < arguments_1.length; _i++) {
var arg = arguments_1[_i];
result.push(arg + arg);
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,22 @@
})();
(function () {
var ns = [];
for (var _i = 0; _i < ns.length; _i++) {
var _a = ns[_i];
for (var _i = 0, ns_1 = ns; _i < ns_1.length; _i++) {
var _a = ns_1[_i];
}
for (var _b = 0; _b < ns.length; _b++) {
var _c = ns[_b];
for (var _b = 0, ns_2 = ns; _b < ns_2.length; _b++) {
var _c = ns_2[_b];
}
for (var _d = 0; _d < ns.length; _d++) {
var _e = ns[_d];
for (var _d = 0, ns_3 = ns; _d < ns_3.length; _d++) {
var _e = ns_3[_d];
}
for (var _f = 0; _f < ns.length; _f++) {
var _g = ns[_f];
for (var _f = 0, ns_4 = ns; _f < ns_4.length; _f++) {
var _g = ns_4[_f];
}
for (var _h = 0; _h < ns.length; _h++) {
var _j = ns[_h];
for (var _h = 0, ns_5 = ns; _h < ns_5.length; _h++) {
var _j = ns_5[_h];
}
for (var _k = 0; _k < ns.length; _k++) {
var _l = ns[_k];
for (var _k = 0, ns_6 = ns; _k < ns_6.length; _k++) {
var _l = ns_6[_k];
}
})();
4 changes: 2 additions & 2 deletions tests/baselines/reference/parserES5ForOfStatement10.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ for (const v of X) {
}

//// [parserES5ForOfStatement10.js]
for (var _i = 0; _i < X.length; _i++) {
var v = X[_i];
for (var _i = 0, X_1 = X; _i < X_1.length; _i++) {
var v = X_1[_i];
}
Loading