Skip to content
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
11 changes: 10 additions & 1 deletion TSLANG_ISSUES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, void, Box>`) 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
Expand Down
15 changes: 15 additions & 0 deletions tslang/lib/TypeScript/LowerToAffineLoops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -3034,6 +3035,20 @@ class SwitchStateOpLowering : public TsPattern<mlir_ts::SwitchStateOp>

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<mlir_ts::StateLabelOp>(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)
Expand Down
8 changes: 8 additions & 0 deletions tslang/lib/TypeScript/MLIRGenExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 14 additions & 7 deletions tslang/lib/TypeScript/MLIRGenFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<TryStatement> trysVisitor(SyntaxKind::TryStatement, [&](auto tryStatement) {
if (!tryStatement->catchClause || !tryStatement->catchClause->block)
if (tryStatement->catchClause && tryStatement->catchClause->block)
{
return;
FilterVisitorSkipFuncsAST<YieldExpression> yieldsInCatch(SyntaxKind::YieldExpression, [&](auto yieldExpression) {
yieldExpression->internalFlags |= InternalFlags::YieldInCatch;
});
yieldsInCatch.visit(tryStatement->catchClause->block);
}

FilterVisitorSkipFuncsAST<YieldExpression> yieldsInCatch(SyntaxKind::YieldExpression, [&](auto yieldExpression) {
yieldExpression->internalFlags |= InternalFlags::YieldInCatch;
});
yieldsInCatch.visit(tryStatement->catchClause->block);
if (tryStatement->finallyBlock)
{
FilterVisitorSkipFuncsAST<YieldExpression> yieldsInFinally(SyntaxKind::YieldExpression, [&](auto yieldExpression) {
yieldExpression->internalFlags |= InternalFlags::YieldInFinally;
});
yieldsInFinally.visit(tryStatement->finallyBlock);
}
});

if (functionLikeDeclarationBaseAST->body)
Expand Down
14 changes: 14 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 $<TARGET_FILE:tslang> --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 $<TARGET_FILE:tslang> --emit=obj --no-default-lib -mm=none
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
44 changes: 44 additions & 0 deletions tslang/test/tester/tests/00generator_finally.ts
Original file line number Diff line number Diff line change
@@ -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.");
}
7 changes: 6 additions & 1 deletion tslang/ts-new-parser/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading