Skip to content

Commit

Permalink
Reset error variable in downlevel for-await-of loop (microsoft#38170)
Browse files Browse the repository at this point in the history
* Rename forAwait tests

* Reset error var in for-await loop
  • Loading branch information
rbuckton authored Apr 24, 2020
1 parent d28e38f commit 968943f
Show file tree
Hide file tree
Showing 17 changed files with 532 additions and 207 deletions.
163 changes: 137 additions & 26 deletions src/compiler/transformers/es2018.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,38 @@ namespace ts {
AsyncMethodsWithSuper = 1 << 0
}

// Facts we track as we traverse the tree
const enum HierarchyFacts {
None = 0,

//
// Ancestor facts
//

HasLexicalThis = 1 << 0,
IterationContainer = 1 << 1,
// NOTE: do not add more ancestor flags without also updating AncestorFactsMask below.

//
// Ancestor masks
//

AncestorFactsMask = (IterationContainer << 1) - 1,

SourceFileIncludes = HasLexicalThis,
SourceFileExcludes = IterationContainer,
StrictModeSourceFileIncludes = None,

ClassOrFunctionIncludes = HasLexicalThis,
ClassOrFunctionExcludes = IterationContainer,

ArrowFunctionIncludes = None,
ArrowFunctionExcludes = ClassOrFunctionExcludes,

IterationStatementIncludes = IterationContainer,
IterationStatementExcludes = None,
}

export function transformES2018(context: TransformationContext) {
const {
resumeLexicalEnvironment,
Expand All @@ -26,7 +58,7 @@ namespace ts {
let enabledSubstitutions: ESNextSubstitutionFlags;
let enclosingFunctionFlags: FunctionFlags;
let enclosingSuperContainerFlags: NodeCheckFlags = 0;
let hasLexicalThis: boolean;
let hierarchyFacts: HierarchyFacts = 0;

let currentSourceFile: SourceFile;
let taggedTemplateStringDeclarations: VariableDeclaration[];
Expand All @@ -40,6 +72,30 @@ namespace ts {

return chainBundle(transformSourceFile);

function affectsSubtree(excludeFacts: HierarchyFacts, includeFacts: HierarchyFacts) {
return hierarchyFacts !== (hierarchyFacts & ~excludeFacts | includeFacts);
}

/**
* Sets the `HierarchyFacts` for this node prior to visiting this node's subtree, returning the facts set prior to modification.
* @param excludeFacts The existing `HierarchyFacts` to reset before visiting the subtree.
* @param includeFacts The new `HierarchyFacts` to set before visiting the subtree.
*/
function enterSubtree(excludeFacts: HierarchyFacts, includeFacts: HierarchyFacts) {
const ancestorFacts = hierarchyFacts;
hierarchyFacts = (hierarchyFacts & ~excludeFacts | includeFacts) & HierarchyFacts.AncestorFactsMask;
return ancestorFacts;
}

/**
* Restores the `HierarchyFacts` for this node's ancestor after visiting this node's
* subtree.
* @param ancestorFacts The `HierarchyFacts` of the ancestor to restore after visiting the subtree.
*/
function exitSubtree(ancestorFacts: HierarchyFacts) {
hierarchyFacts = ancestorFacts;
}

function recordTaggedTemplateString(temp: Identifier) {
taggedTemplateStringDeclarations = append(
taggedTemplateStringDeclarations,
Expand Down Expand Up @@ -75,11 +131,11 @@ namespace ts {
return node;
}

function doWithLexicalThis<T, U>(cb: (value: T) => U, value: T) {
if (!hasLexicalThis) {
hasLexicalThis = true;
function doWithHierarchyFacts<T, U>(cb: (value: T) => U, value: T, excludeFacts: HierarchyFacts, includeFacts: HierarchyFacts) {
if (affectsSubtree(excludeFacts, includeFacts)) {
const ancestorFacts = enterSubtree(excludeFacts, includeFacts);
const result = cb(value);
hasLexicalThis = false;
exitSubtree(ancestorFacts);
return result;
}
return cb(value);
Expand Down Expand Up @@ -112,26 +168,66 @@ namespace ts {
return visitVariableStatement(node as VariableStatement);
case SyntaxKind.VariableDeclaration:
return visitVariableDeclaration(node as VariableDeclaration);
case SyntaxKind.DoStatement:
case SyntaxKind.WhileStatement:
case SyntaxKind.ForInStatement:
return doWithHierarchyFacts(
visitDefault,
node,
HierarchyFacts.IterationStatementExcludes,
HierarchyFacts.IterationStatementIncludes);
case SyntaxKind.ForOfStatement:
return visitForOfStatement(node as ForOfStatement, /*outermostLabeledStatement*/ undefined);
case SyntaxKind.ForStatement:
return visitForStatement(node as ForStatement);
return doWithHierarchyFacts(
visitForStatement,
node as ForStatement,
HierarchyFacts.IterationStatementExcludes,
HierarchyFacts.IterationStatementIncludes);
case SyntaxKind.VoidExpression:
return visitVoidExpression(node as VoidExpression);
case SyntaxKind.Constructor:
return doWithLexicalThis(visitConstructorDeclaration, node as ConstructorDeclaration);
return doWithHierarchyFacts(
visitConstructorDeclaration,
node as ConstructorDeclaration,
HierarchyFacts.ClassOrFunctionExcludes,
HierarchyFacts.ClassOrFunctionIncludes);
case SyntaxKind.MethodDeclaration:
return doWithLexicalThis(visitMethodDeclaration, node as MethodDeclaration);
return doWithHierarchyFacts(
visitMethodDeclaration,
node as MethodDeclaration,
HierarchyFacts.ClassOrFunctionExcludes,
HierarchyFacts.ClassOrFunctionIncludes);
case SyntaxKind.GetAccessor:
return doWithLexicalThis(visitGetAccessorDeclaration, node as GetAccessorDeclaration);
return doWithHierarchyFacts(
visitGetAccessorDeclaration,
node as GetAccessorDeclaration,
HierarchyFacts.ClassOrFunctionExcludes,
HierarchyFacts.ClassOrFunctionIncludes);
case SyntaxKind.SetAccessor:
return doWithLexicalThis(visitSetAccessorDeclaration, node as SetAccessorDeclaration);
return doWithHierarchyFacts(
visitSetAccessorDeclaration,
node as SetAccessorDeclaration,
HierarchyFacts.ClassOrFunctionExcludes,
HierarchyFacts.ClassOrFunctionIncludes);
case SyntaxKind.FunctionDeclaration:
return doWithLexicalThis(visitFunctionDeclaration, node as FunctionDeclaration);
return doWithHierarchyFacts(
visitFunctionDeclaration,
node as FunctionDeclaration,
HierarchyFacts.ClassOrFunctionExcludes,
HierarchyFacts.ClassOrFunctionIncludes);
case SyntaxKind.FunctionExpression:
return doWithLexicalThis(visitFunctionExpression, node as FunctionExpression);
return doWithHierarchyFacts(
visitFunctionExpression,
node as FunctionExpression,
HierarchyFacts.ClassOrFunctionExcludes,
HierarchyFacts.ClassOrFunctionIncludes);
case SyntaxKind.ArrowFunction:
return visitArrowFunction(node as ArrowFunction);
return doWithHierarchyFacts(
visitArrowFunction,
node as ArrowFunction,
HierarchyFacts.ArrowFunctionExcludes,
HierarchyFacts.ArrowFunctionIncludes);
case SyntaxKind.Parameter:
return visitParameter(node as ParameterDeclaration);
case SyntaxKind.ExpressionStatement:
Expand All @@ -152,7 +248,11 @@ namespace ts {
return visitEachChild(node, visitor, context);
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ClassExpression:
return doWithLexicalThis(visitDefault, node);
return doWithHierarchyFacts(
visitDefault,
node,
HierarchyFacts.ClassOrFunctionExcludes,
HierarchyFacts.ClassOrFunctionIncludes);
default:
return visitEachChild(node, visitor, context);
}
Expand Down Expand Up @@ -231,7 +331,7 @@ namespace ts {
if (statement.kind === SyntaxKind.ForOfStatement && (<ForOfStatement>statement).awaitModifier) {
return visitForOfStatement(<ForOfStatement>statement, node);
}
return restoreEnclosingLabel(visitEachChild(statement, visitor, context), node);
return restoreEnclosingLabel(visitNode(statement, visitor, isStatement, liftToBlock), node);
}
return visitEachChild(node, visitor, context);
}
Expand Down Expand Up @@ -311,14 +411,20 @@ namespace ts {
}

function visitSourceFile(node: SourceFile): SourceFile {
const ancestorFacts = enterSubtree(
HierarchyFacts.SourceFileExcludes,
isEffectiveStrictModeSourceFile(node, compilerOptions) ?
HierarchyFacts.StrictModeSourceFileIncludes :
HierarchyFacts.SourceFileIncludes);
exportedVariableStatement = false;
hasLexicalThis = !isEffectiveStrictModeSourceFile(node, compilerOptions);
const visited = visitEachChild(node, visitor, context);
const statement = concatenate(visited.statements, taggedTemplateStringDeclarations && [
createVariableStatement(/*modifiers*/ undefined,
createVariableDeclarationList(taggedTemplateStringDeclarations))
]);
return updateSourceFileNode(visited, setTextRange(createNodeArray(statement), node.statements));
const result = updateSourceFileNode(visited, setTextRange(createNodeArray(statement), node.statements));
exitSubtree(ancestorFacts);
return result;
}

function visitTaggedTemplateExpression(node: TaggedTemplateExpression) {
Expand Down Expand Up @@ -441,15 +547,15 @@ namespace ts {
* @param node A ForOfStatement.
*/
function visitForOfStatement(node: ForOfStatement, outermostLabeledStatement: LabeledStatement | undefined): VisitResult<Statement> {
const ancestorFacts = enterSubtree(HierarchyFacts.IterationStatementExcludes, HierarchyFacts.IterationStatementIncludes);
if (node.initializer.transformFlags & TransformFlags.ContainsObjectRestOrSpread) {
node = transformForOfStatementWithObjectRest(node);
}
if (node.awaitModifier) {
return transformForAwaitOfStatement(node, outermostLabeledStatement);
}
else {
return restoreEnclosingLabel(visitEachChild(node, visitor, context), outermostLabeledStatement);
}
const result = node.awaitModifier ?
transformForAwaitOfStatement(node, outermostLabeledStatement, ancestorFacts) :
restoreEnclosingLabel(visitEachChild(node, visitor, context), outermostLabeledStatement);
exitSubtree(ancestorFacts);
return result;
}

function transformForOfStatementWithObjectRest(node: ForOfStatement) {
Expand Down Expand Up @@ -528,7 +634,7 @@ namespace ts {
: createAwait(expression);
}

function transformForAwaitOfStatement(node: ForOfStatement, outermostLabeledStatement: LabeledStatement | undefined) {
function transformForAwaitOfStatement(node: ForOfStatement, outermostLabeledStatement: LabeledStatement | undefined, ancestorFacts: HierarchyFacts) {
const expression = visitNode(node.expression, visitor, isExpression);
const iterator = isIdentifier(expression) ? getGeneratedNameForNode(expression) : createTempVariable(/*recordTempVariable*/ undefined);
const result = isIdentifier(expression) ? getGeneratedNameForNode(iterator) : createTempVariable(/*recordTempVariable*/ undefined);
Expand All @@ -544,13 +650,18 @@ namespace ts {
hoistVariableDeclaration(errorRecord);
hoistVariableDeclaration(returnMethod);

// if we are enclosed in an outer loop ensure we reset 'errorRecord' per each iteration
const initializer = ancestorFacts & HierarchyFacts.IterationContainer ?
inlineExpressions([createAssignment(errorRecord, createVoidZero()), callValues]) :
callValues;

const forStatement = setEmitFlags(
setTextRange(
createFor(
/*initializer*/ setEmitFlags(
setTextRange(
createVariableDeclarationList([
setTextRange(createVariableDeclaration(iterator, /*type*/ undefined, callValues), node.expression),
setTextRange(createVariableDeclaration(iterator, /*type*/ undefined, initializer), node.expression),
createVariableDeclaration(result)
]),
node.expression
Expand Down Expand Up @@ -809,7 +920,7 @@ namespace ts {
visitLexicalEnvironment(node.body!.statements, visitor, context, statementOffset)
)
),
hasLexicalThis
!!(hierarchyFacts & HierarchyFacts.HasLexicalThis)
)
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//// [tests/cases/conformance/emitter/es2015/forAwait/emitter.forAwait.es2015.ts] ////
//// [tests/cases/conformance/statements/for-await-ofStatements/emitter.forAwait.ts] ////

//// [file1.ts]
async function f1() {
Expand Down Expand Up @@ -40,6 +40,15 @@ async function* f6() {
continue outer;
}
}
//// [file7.ts]
// https://github.com/microsoft/TypeScript/issues/36166
async function* f7() {
let y: any;
for (;;) {
for await (const x of y) {
}
}
}

//// [file1.js]
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
Expand Down Expand Up @@ -264,3 +273,44 @@ function f6() {
}
});
}
//// [file7.js]
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
};
// https://github.com/microsoft/TypeScript/issues/36166
function f7() {
return __asyncGenerator(this, arguments, function* f7_1() {
var e_1, _a;
let y;
for (;;) {
try {
for (var y_1 = (e_1 = void 0, __asyncValues(y)), y_1_1; y_1_1 = yield __await(y_1.next()), !y_1_1.done;) {
const x = y_1_1.value;
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (y_1_1 && !y_1_1.done && (_a = y_1.return)) yield __await(_a.call(y_1));
}
finally { if (e_1) throw e_1.error; }
}
}
});
}
Loading

0 comments on commit 968943f

Please sign in to comment.