diff --git a/TSLANG_ISSUES.md b/TSLANG_ISSUES.md index f53366849..6c2ad3448 100644 --- a/TSLANG_ISSUES.md +++ b/TSLANG_ISSUES.md @@ -2212,9 +2212,18 @@ Tests: `00object_dictionary.ts`, `00own_param_exclusive_moves.ts` (compile, JIT, copies the exception into a slot of the frame. It is a local of the frame now, living in one turn. - A yield in a catch block is an error at the yield: resuming there is resuming inside the exception handler (it crashed in LLVM's instruction selection; before the fixes above the catch did not catch). +- A yield in a finally block was skipped (`try { yield 1; } finally { print("f"); yield 2; }` gave 1 and not 2, + and "f" was not printed): TryOpLowering copies the finally for each way out of the try (the end of its body, the + unwind, each return/break/continue), each copy with the yield's resume label, and the generator's switch + dispatches by position - the copies shifted every label after them, so the resume landed after the yield in + another copy. Now an error at the yield (YieldInFinally), as a yield in a catch is, and a repeated resume label is + an error in the lowering rather than a resume somewhere else. Tests `generator_yield_in_finally.ts` + (diagnostic-location), `00generator_finally.ts`. Still open, looked at: -- A yield in a finally block is skipped (`try { yield 1; } finally { yield 2; }` gives 1 and not 2), as before. +- A yield in a finally block (an error, above): supporting it needs the finally lowered as straight code after the + try when it yields - a catch-all keeping the exception, the way out recorded (end, throw, return, break, + continue, the generator's return()/throw()), and taken again after the finally - not as the try's finally region. - -mm=own: next(value) of a generator sending it an object (`Generator`) is refused ("'this value' is a parameter, kept here, but -mm=own cannot make the callers of this function expression give it up"): next() keeps what it is given, and a method used other than by a call can't make its callers give it up. A number or a diff --git a/tslang/lib/TypeScript/LowerToAffineLoops.cpp b/tslang/lib/TypeScript/LowerToAffineLoops.cpp index 89d336798..5058266de 100644 --- a/tslang/lib/TypeScript/LowerToAffineLoops.cpp +++ b/tslang/lib/TypeScript/LowerToAffineLoops.cpp @@ -22,6 +22,7 @@ #include "mlir/Transforms/DialectConversion.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/Sequence.h" +#include "llvm/ADT/StringSet.h" #include "mlir/IR/Dialect.h" #include "mlir/IR/IRMapping.h" #include "llvm/Support/Debug.h" @@ -3034,6 +3035,20 @@ class SwitchStateOpLowering : public TsPattern switchStateOp->getParentOp()->walk(visitorAllStateLabels); + // A resume point copied along with the code around it (a region cloned for more than one path, as a + // try's finally is) is a second label of the same name: positional dispatch would send that state and + // every one after it to the wrong place. Said here rather than resumed somewhere else. + llvm::StringSet<> labelNames; + for (auto op : stateLabels) + { + auto stateLabelOp = cast(op); + if (!labelNames.insert(stateLabelOp.getLabel()).second) + { + return stateLabelOp.emitError("a generator's resume point (") + << stateLabelOp.getLabel() << ") is in code copied for more than one path; it can't be resumed"; + } + } + { mlir::OpBuilder::InsertionGuard insertGuard(rewriter); for (auto op : stateLabels) diff --git a/tslang/lib/TypeScript/MLIRGenExpressions.cpp b/tslang/lib/TypeScript/MLIRGenExpressions.cpp index 0ed15340c..f017713d2 100644 --- a/tslang/lib/TypeScript/MLIRGenExpressions.cpp +++ b/tslang/lib/TypeScript/MLIRGenExpressions.cpp @@ -458,6 +458,14 @@ namespace mlirgen return mlir::failure(); } + if (!!(yieldExpressionAST->internalFlags & InternalFlags::YieldInFinally)) + { + emitError(location, "a yield in a finally block of a generator is not supported yet: the finally is run " + "from each way out of the try statement, and the generator can't resume in the one " + "that runs while an exception unwinds; yield after the try statement"); + return mlir::failure(); + } + if (yieldExpressionAST->asteriskToken) { // it gives back nothing yet: it can only stand as a statement of its own diff --git a/tslang/lib/TypeScript/MLIRGenFunctions.cpp b/tslang/lib/TypeScript/MLIRGenFunctions.cpp index 35eb09f5c..b5bc4a0c5 100644 --- a/tslang/lib/TypeScript/MLIRGenFunctions.cpp +++ b/tslang/lib/TypeScript/MLIRGenFunctions.cpp @@ -1410,17 +1410,24 @@ namespace mlirgen } } - // a yield in a catch clause can't be resumed (YieldInCatch): said at the yield + // a yield in a catch clause (YieldInCatch) or a finally block (YieldInFinally) can't be resumed: said at the + // yield FilterVisitorSkipFuncsAST trysVisitor(SyntaxKind::TryStatement, [&](auto tryStatement) { - if (!tryStatement->catchClause || !tryStatement->catchClause->block) + if (tryStatement->catchClause && tryStatement->catchClause->block) { - return; + FilterVisitorSkipFuncsAST yieldsInCatch(SyntaxKind::YieldExpression, [&](auto yieldExpression) { + yieldExpression->internalFlags |= InternalFlags::YieldInCatch; + }); + yieldsInCatch.visit(tryStatement->catchClause->block); } - FilterVisitorSkipFuncsAST yieldsInCatch(SyntaxKind::YieldExpression, [&](auto yieldExpression) { - yieldExpression->internalFlags |= InternalFlags::YieldInCatch; - }); - yieldsInCatch.visit(tryStatement->catchClause->block); + if (tryStatement->finallyBlock) + { + FilterVisitorSkipFuncsAST yieldsInFinally(SyntaxKind::YieldExpression, [&](auto yieldExpression) { + yieldExpression->internalFlags |= InternalFlags::YieldInFinally; + }); + yieldsInFinally.visit(tryStatement->finallyBlock); + } }); if (functionLikeDeclarationBaseAST->body) diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 5041260d7..0555c0cb0 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -471,6 +471,7 @@ tslang_add_test(NAME test-compile-00-generator-typed-yield COMMAND test-runner " tslang_add_test(NAME test-compile-00-object-method-arity COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_method_arity.ts") tslang_add_test(NAME test-compile-00-generator-next-value COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_next_value.ts") tslang_add_test(NAME test-compile-00-generator-return-throw COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_return_throw.ts") +tslang_add_test(NAME test-compile-00-generator-finally COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_finally.ts") tslang_add_test(NAME test-compile-00-any-undefined-field COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_undefined_field.ts") tslang_add_test(NAME test-compile-00-array-write-grows COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_write_grows.ts") tslang_add_test(NAME test-compile-00-constructor-type-value COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts") @@ -1314,6 +1315,9 @@ tslang_add_test(NAME test-jit-own-00-generator-next-value COMMAND test-runner -j tslang_add_test(NAME test-jit-00-generator-return-throw COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_return_throw.ts") tslang_add_test(NAME test-jit-rc-00-generator-return-throw COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_return_throw.ts") tslang_add_test(NAME test-jit-own-00-generator-return-throw COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_return_throw.ts") +tslang_add_test(NAME test-jit-00-generator-finally COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_finally.ts") +tslang_add_test(NAME test-jit-rc-00-generator-finally COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_finally.ts") +tslang_add_test(NAME test-jit-own-00-generator-finally COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_finally.ts") tslang_add_test(NAME test-jit-00-any-undefined-field COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_undefined_field.ts") tslang_add_test(NAME test-jit-rc-00-any-undefined-field COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_undefined_field.ts") tslang_add_test(NAME test-jit-own-00-any-undefined-field COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_undefined_field.ts") @@ -4186,6 +4190,16 @@ set_tests_properties(test-compile-diagnostic-location-generator-yield-in-catch PROPERTIES PASS_REGULAR_EXPRESSION "generator_yield_in_catch\\.ts:8:9: error: a yield in a catch block of a generator is not supported" FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed") +# A yield in a finally block of a generator is an error at the yield (the yield was skipped, and the rest of the +# finally resumed from the wrong place). +add_test(NAME test-compile-diagnostic-location-generator-yield-in-finally + COMMAND $ --emit=obj --no-default-lib -mm=none + "${PROJECT_SOURCE_DIR}/test/tester/diagnostic-location/generator_yield_in_finally.ts" + -o "${CMAKE_CURRENT_BINARY_DIR}/diagnostic-location-generator-yield-in-finally.obj") +set_tests_properties(test-compile-diagnostic-location-generator-yield-in-finally + PROPERTIES PASS_REGULAR_EXPRESSION "generator_yield_in_finally\\.ts:9:9: error: a yield in a finally block of a generator is not supported" + FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed") + # A generic method in a type literal is an error at the method. add_test(NAME test-compile-diagnostic-location-generic-method-type-literal COMMAND $ --emit=obj --no-default-lib -mm=none diff --git a/tslang/test/tester/diagnostic-location/generator_yield_in_finally.ts b/tslang/test/tester/diagnostic-location/generator_yield_in_finally.ts new file mode 100644 index 000000000..1f10a3596 --- /dev/null +++ b/tslang/test/tester/diagnostic-location/generator_yield_in_finally.ts @@ -0,0 +1,15 @@ +// A yield in a finally block of a generator is an error at the yield: the finally is copied for each way out of +// the try (one of them inside an exception handler) and a resume landed in one copy only - the yield was skipped +// and the rest of the finally ran from the wrong place. +function* g() { + try { + yield 1; + } finally { + print("in finally"); + yield 2; + } +} + +function main() { + for (const v of g()) print(v); +} diff --git a/tslang/test/tester/tests/00generator_finally.ts b/tslang/test/tester/tests/00generator_finally.ts new file mode 100644 index 000000000..d3a35e664 --- /dev/null +++ b/tslang/test/tester/tests/00generator_finally.ts @@ -0,0 +1,44 @@ +// A generator's try/finally around its yields: the finally runs once, after the last turn in the try, before what +// follows. A yield in the finally itself is an error (diagnostic-location/generator_yield_in_finally.ts); a +// generator declared in a finally block is a function of its own and may yield. +let log = ""; + +function* around() { + try { + yield 1; + yield 2; + } finally { + log += "f"; + } + yield 3; +} + +function* declaredInFinally() { + let inner: () => number = () => 0; + try { + yield 10; + } finally { + function* g() { + yield 20; + yield 30; + } + inner = () => { + let s: number = 0; + for (const v of g()) s += v; + return s; + }; + } + yield inner(); +} + +function main() { + let order = ""; + for (const v of around()) order += v + log + ","; + assert(order == "1,2,3f," && log == "f", "finally once, after the try's last turn"); + + let sum: number = 0; + for (const v of declaredInFinally()) sum += v; + assert(sum == 60, "a generator declared in a finally"); + + print("done."); +} diff --git a/tslang/ts-new-parser/enums.h b/tslang/ts-new-parser/enums.h index 9758dc306..0e73f7b67 100644 --- a/tslang/ts-new-parser/enums.h +++ b/tslang/ts-new-parser/enums.h @@ -590,7 +590,12 @@ enum class InternalFlags : number GeneratorResumable = 1 << 21, // a `yield` in a `catch` clause of a generator: resuming there is resuming inside the clause's exception // handler, which can't be entered again (Windows: a catch funclet; LLVM's instruction selection crashed) - YieldInCatch = 1 << 22 + YieldInCatch = 1 << 22, + // a `yield` in a `finally` block of a generator: the finally is copied once for each way out of the try (the + // end of its body, the unwind, each return/break/continue), the copy on the unwind leg inside an exception + // handler, and a resume lands in one copy only - the yield was skipped, and the rest of the finally ran from the + // wrong place + YieldInFinally = 1 << 23 }; ENUM_OPS(InternalFlags)