Skip to content

Commit

Permalink
Fix missing final label.
Browse files Browse the repository at this point in the history
Fixes #10876
  • Loading branch information
rbuckton committed Sep 12, 2016
1 parent 439fe79 commit 810a680
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/compiler/transformers/generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2629,7 +2629,7 @@ namespace ts {
* Flush the final label of the generator function body.
*/
function flushFinalLabel(operationIndex: number): void {
if (!lastOperationWasCompletion) {
if (isFinalLabelReachable(operationIndex)) {
tryEnterLabel(operationIndex);
withBlockStack = undefined;
writeReturn(/*expression*/ undefined, /*operationLocation*/ undefined);
Expand All @@ -2642,6 +2642,34 @@ namespace ts {
updateLabelExpressions();
}

/**
* Tests whether the final label of the generator function body
* is reachable by user code.
*/
function isFinalLabelReachable(operationIndex: number) {
// if the last operation was *not* a completion (return/throw) then
// the final label is reachable.
if (!lastOperationWasCompletion) {
return true;
}

// if there are no labels defined or referenced, then the final label is
// not reachable.
if (!labelOffsets || !labelExpressions) {
return false;
}

// if the label for this offset is referenced, then the final label
// is reachable.
for (let label = 0; label < labelOffsets.length; label++) {
if (labelOffsets[label] === operationIndex && labelExpressions[label]) {
return true;
}
}

return false;
}

/**
* Appends a case clause for the last label and sets the new label.
*
Expand Down

0 comments on commit 810a680

Please sign in to comment.