Skip to content
Closed
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
92 changes: 69 additions & 23 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ import {
FlowCondition,
FlowFlags,
FlowLabel,
FlowNode,
FlowReduceLabel,
FlowSwitchClause,
forEach,
forEachChild,
ForInOrOfStatement,
FlowNode,
FlowReduceLabel,
FlowSwitchClause,
forEach,
forEachChild,
ForInOrOfStatement,
ForStatement,
FunctionDeclaration,
FunctionExpression,
Expand Down Expand Up @@ -1551,23 +1551,69 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
currentFlow = finishFlowLabel(postLoopLabel);
}

function bindForInOrForOfStatement(node: ForInOrOfStatement): void {
const preLoopLabel = setContinueTarget(node, createLoopLabel());
const postLoopLabel = createBranchLabel();
bind(node.expression);
addAntecedent(preLoopLabel, currentFlow);
currentFlow = preLoopLabel;
if (node.kind === SyntaxKind.ForOfStatement) {
bind(node.awaitModifier);
}
addAntecedent(postLoopLabel, currentFlow);
bind(node.initializer);
if (node.initializer.kind !== SyntaxKind.VariableDeclarationList) {
bindAssignmentTargetFlow(node.initializer);
}
bindIterativeStatement(node.statement, postLoopLabel, preLoopLabel);
addAntecedent(preLoopLabel, currentFlow);
currentFlow = finishFlowLabel(postLoopLabel);
function checkForUsingShadowingInForOf(node: ForInOrOfStatement): void {
if (node.kind !== SyntaxKind.ForOfStatement || node.initializer.kind !== SyntaxKind.VariableDeclarationList) {
return;
}

const varDeclList = node.initializer as any; // VariableDeclarationList

// Only check if we have using declarations that will be downleveled
if (!(varDeclList.flags & NodeFlags.Using) || languageVersion >= ScriptTarget.ESNext) {
return;
}

// Collect the names of using declarations
const usingNames = new Set<string>();
for (const declaration of varDeclList.declarations) {
if (isIdentifier(declaration.name)) {
usingNames.add(unescapeLeadingUnderscores(declaration.name.escapedText));
}
}

if (usingNames.size === 0) return;

// Check for variable declarations with the same names in the loop body
function checkNode(node: Node): void {
if (isVariableStatement(node)) {
for (const declaration of node.declarationList.declarations) {
if (isIdentifier(declaration.name)) {
const name = unescapeLeadingUnderscores(declaration.name.escapedText);
if (usingNames.has(name)) {
file.bindDiagnostics.push(createDiagnosticForNode(declaration.name, Diagnostics.Duplicate_identifier_0, name));
}
}
}
}

// Recursively check child nodes
forEachChild(node, checkNode);
}

checkNode(node.statement);
}

function bindForInOrForOfStatement(node: ForInOrOfStatement): void {
const preLoopLabel = setContinueTarget(node, createLoopLabel());
const postLoopLabel = createBranchLabel();
bind(node.expression);
addAntecedent(preLoopLabel, currentFlow);
currentFlow = preLoopLabel;
if (node.kind === SyntaxKind.ForOfStatement) {
bind(node.awaitModifier);
}
addAntecedent(postLoopLabel, currentFlow);
bind(node.initializer);
if (node.initializer.kind !== SyntaxKind.VariableDeclarationList) {
bindAssignmentTargetFlow(node.initializer);
}
else {
// Check for using declarations that will be downleveled and shadow variables in the loop body
checkForUsingShadowingInForOf(node);
}
bindIterativeStatement(node.statement, postLoopLabel, preLoopLabel);
addAntecedent(preLoopLabel, currentFlow);
currentFlow = finishFlowLabel(postLoopLabel);
}

function bindIfStatement(node: IfStatement): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
usingDeclarationsForOfShadowing.ts(5,9): error TS2300: Duplicate identifier 'foo'.
usingDeclarationsForOfShadowing.ts(15,7): error TS2300: Duplicate identifier 'x'.
usingDeclarationsForOfShadowing.ts(20,7): error TS2300: Duplicate identifier 'y'.
usingDeclarationsForOfShadowing.ts(20,7): error TS2481: Cannot initialize outer scoped variable 'y' in the same scope as block scoped declaration 'y'.


==== usingDeclarationsForOfShadowing.ts (4 errors) ====
class Foo {}

// Error when downleveling: using declaration shadowed by loop body declaration
for (using foo of [{ [Symbol.dispose]() {} }]) {
const foo = new Foo();
~~~
!!! error TS2300: Duplicate identifier 'foo'.
}

// OK: different names
for (using bar of [{ [Symbol.dispose]() {} }]) {
const baz = new Foo();
}

// Error when downleveling: using declaration shadowed by loop body declaration
for (using x of [{ [Symbol.dispose]() {} }]) {
let x = 1;
~
!!! error TS2300: Duplicate identifier 'x'.
}

// Error when downleveling: using declaration shadowed by loop body declaration
for (using y of [{ [Symbol.dispose]() {} }]) {
var y = "test";
~
!!! error TS2300: Duplicate identifier 'y'.
~
!!! error TS2481: Cannot initialize outer scoped variable 'y' in the same scope as block scoped declaration 'y'.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
//// [tests/cases/compiler/usingDeclarationsForOfShadowing.ts] ////

//// [usingDeclarationsForOfShadowing.ts]
class Foo {}

// Error when downleveling: using declaration shadowed by loop body declaration
for (using foo of [{ [Symbol.dispose]() {} }]) {
const foo = new Foo();
}

// OK: different names
for (using bar of [{ [Symbol.dispose]() {} }]) {
const baz = new Foo();
}

// Error when downleveling: using declaration shadowed by loop body declaration
for (using x of [{ [Symbol.dispose]() {} }]) {
let x = 1;
}

// Error when downleveling: using declaration shadowed by loop body declaration
for (using y of [{ [Symbol.dispose]() {} }]) {
var y = "test";
}

//// [usingDeclarationsForOfShadowing.js]
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
if (value !== null && value !== void 0) {
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
var dispose, inner;
if (async) {
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
dispose = value[Symbol.asyncDispose];
}
if (dispose === void 0) {
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
dispose = value[Symbol.dispose];
if (async) inner = dispose;
}
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
env.stack.push({ value: value, dispose: dispose, async: async });
}
else if (async) {
env.stack.push({ async: true });
}
return value;
};
var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
return function (env) {
function fail(e) {
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
env.hasError = true;
}
var r, s = 0;
function next() {
while (r = env.stack.pop()) {
try {
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
if (r.dispose) {
var result = r.dispose.call(r.value);
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
}
else s |= 1;
}
catch (e) {
fail(e);
}
}
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
if (env.hasError) throw env.error;
}
return next();
};
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
});
class Foo {
}
// Error when downleveling: using declaration shadowed by loop body declaration
for (const foo_1 of [{ [Symbol.dispose]() { } }]) {
const env_1 = { stack: [], error: void 0, hasError: false };
try {
const foo = __addDisposableResource(env_1, foo_1, false);
const foo = new Foo();
}
catch (e_1) {
env_1.error = e_1;
env_1.hasError = true;
}
finally {
__disposeResources(env_1);
}
}
// OK: different names
for (const bar_1 of [{ [Symbol.dispose]() { } }]) {
const env_2 = { stack: [], error: void 0, hasError: false };
try {
const bar = __addDisposableResource(env_2, bar_1, false);
const baz = new Foo();
}
catch (e_2) {
env_2.error = e_2;
env_2.hasError = true;
}
finally {
__disposeResources(env_2);
}
}
// Error when downleveling: using declaration shadowed by loop body declaration
for (const x_1 of [{ [Symbol.dispose]() { } }]) {
const env_3 = { stack: [], error: void 0, hasError: false };
try {
const x = __addDisposableResource(env_3, x_1, false);
let x = 1;
}
catch (e_3) {
env_3.error = e_3;
env_3.hasError = true;
}
finally {
__disposeResources(env_3);
}
}
// Error when downleveling: using declaration shadowed by loop body declaration
for (const y_1 of [{ [Symbol.dispose]() { } }]) {
const env_4 = { stack: [], error: void 0, hasError: false };
try {
const y = __addDisposableResource(env_4, y_1, false);
var y = "test";
}
catch (e_4) {
env_4.error = e_4;
env_4.hasError = true;
}
finally {
__disposeResources(env_4);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//// [tests/cases/compiler/usingDeclarationsForOfShadowing.ts] ////

=== usingDeclarationsForOfShadowing.ts ===
class Foo {}
>Foo : Symbol(Foo, Decl(usingDeclarationsForOfShadowing.ts, 0, 0))

// Error when downleveling: using declaration shadowed by loop body declaration
for (using foo of [{ [Symbol.dispose]() {} }]) {
>foo : Symbol(foo, Decl(usingDeclarationsForOfShadowing.ts, 3, 10))
>[Symbol.dispose] : Symbol([Symbol.dispose], Decl(usingDeclarationsForOfShadowing.ts, 3, 20))
>Symbol.dispose : Symbol(SymbolConstructor.dispose, Decl(lib.esnext.disposable.d.ts, --, --))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --))
>dispose : Symbol(SymbolConstructor.dispose, Decl(lib.esnext.disposable.d.ts, --, --))

const foo = new Foo();
>foo : Symbol(foo, Decl(usingDeclarationsForOfShadowing.ts, 4, 7))
>Foo : Symbol(Foo, Decl(usingDeclarationsForOfShadowing.ts, 0, 0))
}

// OK: different names
for (using bar of [{ [Symbol.dispose]() {} }]) {
>bar : Symbol(bar, Decl(usingDeclarationsForOfShadowing.ts, 8, 10))
>[Symbol.dispose] : Symbol([Symbol.dispose], Decl(usingDeclarationsForOfShadowing.ts, 8, 20))
>Symbol.dispose : Symbol(SymbolConstructor.dispose, Decl(lib.esnext.disposable.d.ts, --, --))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --))
>dispose : Symbol(SymbolConstructor.dispose, Decl(lib.esnext.disposable.d.ts, --, --))

const baz = new Foo();
>baz : Symbol(baz, Decl(usingDeclarationsForOfShadowing.ts, 9, 7))
>Foo : Symbol(Foo, Decl(usingDeclarationsForOfShadowing.ts, 0, 0))
}

// Error when downleveling: using declaration shadowed by loop body declaration
for (using x of [{ [Symbol.dispose]() {} }]) {
>x : Symbol(x, Decl(usingDeclarationsForOfShadowing.ts, 13, 10))
>[Symbol.dispose] : Symbol([Symbol.dispose], Decl(usingDeclarationsForOfShadowing.ts, 13, 18))
>Symbol.dispose : Symbol(SymbolConstructor.dispose, Decl(lib.esnext.disposable.d.ts, --, --))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --))
>dispose : Symbol(SymbolConstructor.dispose, Decl(lib.esnext.disposable.d.ts, --, --))

let x = 1;
>x : Symbol(x, Decl(usingDeclarationsForOfShadowing.ts, 14, 5))
}

// Error when downleveling: using declaration shadowed by loop body declaration
for (using y of [{ [Symbol.dispose]() {} }]) {
>y : Symbol(y, Decl(usingDeclarationsForOfShadowing.ts, 18, 10))
>[Symbol.dispose] : Symbol([Symbol.dispose], Decl(usingDeclarationsForOfShadowing.ts, 18, 18))
>Symbol.dispose : Symbol(SymbolConstructor.dispose, Decl(lib.esnext.disposable.d.ts, --, --))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --))
>dispose : Symbol(SymbolConstructor.dispose, Decl(lib.esnext.disposable.d.ts, --, --))

var y = "test";
>y : Symbol(y, Decl(usingDeclarationsForOfShadowing.ts, 19, 5))
}
Loading