Skip to content

create new lexical environment for the body of converted loop #12831

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 1 commit into from
Dec 12, 2016
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
9 changes: 7 additions & 2 deletions src/compiler/transformers/es2015.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2286,14 +2286,19 @@ namespace ts {
}
}

startLexicalEnvironment();
let loopBody = visitNode(node.statement, visitor, isStatement);
const lexicalEnvironment = endLexicalEnvironment();

const currentState = convertedLoopState;
convertedLoopState = outerConvertedLoopState;

if (loopOutParameters.length) {
if (loopOutParameters.length || lexicalEnvironment) {
const statements = isBlock(loopBody) ? (<Block>loopBody).statements.slice() : [loopBody];
copyOutParameters(loopOutParameters, CopyDirection.ToOutParameter, statements);
if (loopOutParameters.length) {
copyOutParameters(loopOutParameters, CopyDirection.ToOutParameter, statements);
}
addRange(statements, lexicalEnvironment)
loopBody = createBlock(statements, /*location*/ undefined, /*multiline*/ true);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/capturedLetConstInLoop9.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ function foo() {
}
(function () { return b; });
return { value: 100 };
var _a;
};
for (var _c = 0, _d = []; _c < _d.length; _c++) {
var b = _d[_c];
Expand All @@ -221,6 +222,7 @@ function foo() {
}
}
(function () { return a; });
var _b;
};
var arguments_1 = arguments, x, z, x1, z1;
l0: for (var _i = 0, _a = []; _i < _a.length; _i++) {
Expand All @@ -238,7 +240,6 @@ function foo() {
use(z);
use(x1);
use(z1);
var _b, _a;
}
function foo2() {
for (var _i = 0, _a = []; _i < _a.length; _i++) {
Expand Down
31 changes: 31 additions & 0 deletions tests/baselines/reference/newLexicalEnvironmentForConvertedLoop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//// [newLexicalEnvironmentForConvertedLoop.ts]
function baz(x: any) {
return [[x, x]];
}

function foo(set: any) {
for (const [value, i] of baz(set.values)) {
const bar: any = [];
(() => bar);

set.values.push(...[]);
}
};

//// [newLexicalEnvironmentForConvertedLoop.js]
function baz(x) {
return [[x, x]];
}
function foo(set) {
var _loop_1 = function (value, i) {
var bar = [];
(function () { return bar; });
(_a = set.values).push.apply(_a, []);
var _a;
};
for (var _i = 0, _a = baz(set.values); _i < _a.length; _i++) {
var _b = _a[_i], value = _b[0], i = _b[1];
_loop_1(value, i);
}
}
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
=== tests/cases/compiler/newLexicalEnvironmentForConvertedLoop.ts ===
function baz(x: any) {
>baz : Symbol(baz, Decl(newLexicalEnvironmentForConvertedLoop.ts, 0, 0))
>x : Symbol(x, Decl(newLexicalEnvironmentForConvertedLoop.ts, 0, 13))

return [[x, x]];
>x : Symbol(x, Decl(newLexicalEnvironmentForConvertedLoop.ts, 0, 13))
>x : Symbol(x, Decl(newLexicalEnvironmentForConvertedLoop.ts, 0, 13))
}

function foo(set: any) {
>foo : Symbol(foo, Decl(newLexicalEnvironmentForConvertedLoop.ts, 2, 1))
>set : Symbol(set, Decl(newLexicalEnvironmentForConvertedLoop.ts, 4, 13))

for (const [value, i] of baz(set.values)) {
>value : Symbol(value, Decl(newLexicalEnvironmentForConvertedLoop.ts, 5, 14))
>i : Symbol(i, Decl(newLexicalEnvironmentForConvertedLoop.ts, 5, 20))
>baz : Symbol(baz, Decl(newLexicalEnvironmentForConvertedLoop.ts, 0, 0))
>set : Symbol(set, Decl(newLexicalEnvironmentForConvertedLoop.ts, 4, 13))

const bar: any = [];
>bar : Symbol(bar, Decl(newLexicalEnvironmentForConvertedLoop.ts, 6, 9))

(() => bar);
>bar : Symbol(bar, Decl(newLexicalEnvironmentForConvertedLoop.ts, 6, 9))

set.values.push(...[]);
>set : Symbol(set, Decl(newLexicalEnvironmentForConvertedLoop.ts, 4, 13))
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
=== tests/cases/compiler/newLexicalEnvironmentForConvertedLoop.ts ===
function baz(x: any) {
>baz : (x: any) => any[][]
>x : any

return [[x, x]];
>[[x, x]] : any[][]
>[x, x] : any[]
>x : any
>x : any
}

function foo(set: any) {
>foo : (set: any) => void
>set : any

for (const [value, i] of baz(set.values)) {
>value : any
>i : any
>baz(set.values) : any[][]
>baz : (x: any) => any[][]
>set.values : any
>set : any
>values : any

const bar: any = [];
>bar : any
>[] : undefined[]

(() => bar);
>(() => bar) : () => any
>() => bar : () => any
>bar : any

set.values.push(...[]);
>set.values.push(...[]) : any
>set.values.push : any
>set.values : any
>set : any
>values : any
>push : any
>...[] : undefined
>[] : undefined[]
}
};
13 changes: 13 additions & 0 deletions tests/cases/compiler/newLexicalEnvironmentForConvertedLoop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @target: es5
function baz(x: any) {
return [[x, x]];
}

function foo(set: any) {
for (const [value, i] of baz(set.values)) {
const bar: any = [];
(() => bar);

set.values.push(...[]);
}
};