-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Changes from all commits
c3c9e51
f36b6ab
90bac23
6a277d9
982926a
d5dd69b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add tests for this case? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
@@ -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("; "); | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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; | ||
} |
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)) | ||
} |
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 | ||
} |
There was a problem hiding this comment.
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.