From 5cd8362d4ebfed5ea88ad017579eed6cbecc9fae Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sat, 21 Nov 2026 14:35:46 +0000 Subject: [PATCH 1/4] Generator return(value) and throw(e); a throw after a resumed yield is caught by its try A generator typed Generator gets return(value) and throw(e), as JavaScript's: paused at a yield, it resumes there finishing (its finally blocks run) or throwing e (its own try/catch may catch it); one not started or finished does not run. Under -mm=own return() only for a T that owns nothing. Fixed on the way, in generators: a throw or a call after a resumed yield in a `try` went past it (only the first block of the try's body got unwind edges); its catch-type constants did not dominate a resumed turn; a `let` in a `try` read after a yield did not compile (its storage was put in front of the try, not of the generator's switch); a catch variable was a field of the generator object and its use crashed. A yield in a catch block is now an error at the yield (it crashed in LLVM's instruction selection). Co-Authored-By: Claude Opus 5.5 --- tslang/include/TypeScript/Defines.h | 4 + .../TypeScript/MLIRLogic/MLIRCodeLogic.h | 1 + .../TypeScript/MLIRLogic/MLIRGenContext.h | 5 + tslang/lib/TypeScript/LowerToAffineLoops.cpp | 42 +++- tslang/lib/TypeScript/MLIRGenExpressions.cpp | 15 ++ tslang/lib/TypeScript/MLIRGenFunctions.cpp | 210 ++++++++++++++++++ tslang/lib/TypeScript/MLIRGenImpl.h | 13 +- tslang/lib/TypeScript/MLIRGenStatements.cpp | 8 + tslang/test/tester/CMakeLists.txt | 13 ++ .../generator_yield_in_catch.ts | 14 ++ .../tester/tests/00generator_return_throw.ts | 100 +++++++++ tslang/ts-new-parser/enums.h | 8 +- 12 files changed, 423 insertions(+), 10 deletions(-) create mode 100644 tslang/test/tester/diagnostic-location/generator_yield_in_catch.ts create mode 100644 tslang/test/tester/tests/00generator_return_throw.ts diff --git a/tslang/include/TypeScript/Defines.h b/tslang/include/TypeScript/Defines.h index 5185d8f08..aff8a1588 100644 --- a/tslang/include/TypeScript/Defines.h +++ b/tslang/include/TypeScript/Defines.h @@ -565,6 +565,10 @@ // next(value) of a generator typed Generator: the value, the parameter of its next(), is what // the `yield` it resumes evaluates to #define GENERATOR_SENT_NAME ".sent" +// return(v) / throw(e) of a generator typed Generator<...>: how the paused generator resumes - 0 as next() asks, +// 1 throwing GENERATOR_THROWN_NAME at its yield, 2 returning there (its finally blocks run) +#define GENERATOR_MODE_NAME ".mode" +#define GENERATOR_THROWN_NAME ".thrown" #define OBJECT_DICTIONARY_CLASS_NAME "ObjectDictionary" // the default library's function a spread of a dictionary into a new one calls #define OBJECT_DICTIONARY_ASSIGN_NAME "objectDictionaryAssign" diff --git a/tslang/include/TypeScript/MLIRLogic/MLIRCodeLogic.h b/tslang/include/TypeScript/MLIRLogic/MLIRCodeLogic.h index d5cee6cfc..595d7d571 100644 --- a/tslang/include/TypeScript/MLIRLogic/MLIRCodeLogic.h +++ b/tslang/include/TypeScript/MLIRLogic/MLIRCodeLogic.h @@ -1124,6 +1124,7 @@ class MLIRCustomMethods // __switchstate call in the caller's scope - see GenContext const_cast(genContext).allocateVarsOutsideOfOperation = true; const_cast(genContext).currentOperation = switchStateOp; + const_cast(genContext).generatorSwitchOperation = switchStateOp; return mlir::success(); } diff --git a/tslang/include/TypeScript/MLIRLogic/MLIRGenContext.h b/tslang/include/TypeScript/MLIRLogic/MLIRGenContext.h index f55e9e1e4..d95e8a4b5 100644 --- a/tslang/include/TypeScript/MLIRLogic/MLIRGenContext.h +++ b/tslang/include/TypeScript/MLIRLogic/MLIRGenContext.h @@ -111,6 +111,7 @@ struct GenContext unwindCleanupOp = nullptr; currentOperation = nullptr; + generatorSwitchOperation = nullptr; allocateVarsOutsideOfOperation = false; allocateScopeOwnedVarsOutsideOfOperation = false; } @@ -200,6 +201,10 @@ struct GenContext bool discoverParamsOnly = false; bool insertIntoParentScope = false; mlir::Operation *currentOperation = nullptr; + // a generator's `__switchstate` (armed with currentOperation): a local of its body is a field of the + // generator object, and its storage goes in front of the switch, which every resume point follows - not in + // front of a `try` it is declared in (currentOperation there), which the resume points inside it don't + mlir::Operation *generatorSwitchOperation = nullptr; mlir_ts::FuncOp funcOp; // funcOp's `return` statements evaluate their value and drop it (an arrow given to a function // type that returns nothing, see FunctionPrototypeDOM::discardsReturnValue) diff --git a/tslang/lib/TypeScript/LowerToAffineLoops.cpp b/tslang/lib/TypeScript/LowerToAffineLoops.cpp index 793632312..89d336798 100644 --- a/tslang/lib/TypeScript/LowerToAffineLoops.cpp +++ b/tslang/lib/TypeScript/LowerToAffineLoops.cpp @@ -1449,6 +1449,21 @@ struct TryOpLowering : public TsPattern auto module = tryOp->getParentOfType(); auto parentTryOp = tsContext->parentTryOp.lookup(tryOp.getOperation()); + // a generator's next(): its switch resumes turns in the middle of the body (see "Body:catch vars") + mlir::Block *generatorEntryBlock = nullptr; + if (auto funcOp = tryOp->getParentOfType()) + { + auto resumes = funcOp->walk([](mlir::Operation *op) { + return isa(op) + ? WalkResult::interrupt() + : WalkResult::advance(); + }); + if (resumes.wasInterrupted()) + { + generatorEntryBlock = &funcOp.getBody().front(); + } + } + // Where an exception this try does not catch has to go next within the function: the // parent's landing pad for a try in the parent's body, the parent's finally for one in // the parent's catch clause (whose exceptions the parent does not catch, but whose @@ -2190,20 +2205,31 @@ struct TryOpLowering : public TsPattern tsFuncContext->accessorUnwinds.push_back({op, landingBlock}); } }; - auto it = bodyBlock; - do + // every block of the body: a generator's yield splits it at its label, and a throw or a call after a + // resumed yield went past the try (only the first block was walked - `continue` in a `do ... while + // (false)` ends it): its finally did not run, its catch did not catch + for (auto it = bodyBlock;; it = it->getNextNode()) { it->walk(visitorCallOpContinue); - if (it != bodyBlockLast) + if (it == bodyBlockLast) { - it = it->getNextNode(); - continue; + break; } - } while (false); + } } - // Body:catch vars - rewriter.setInsertionPointToStart(bodyBlock); + // Body:catch vars - constants the pads below read. In a generator's next() they go to the function's entry: + // a turn resuming inside the body (its switch jumps to the yield's label) does not pass the body's start, + // and the pad a throw there unwinds to read a constant that did not dominate it ("operand #0 does not + // dominate this use" at the `try`) + if (generatorEntryBlock) + { + rewriter.setInsertionPointToStart(generatorEntryBlock); + } + else + { + rewriter.setInsertionPointToStart(bodyBlock); + } auto catch1 = rttih.hasType() ? (mlir::Value)rttih.typeInfoPtrValue(loc) : /*catch all*/ (mlir::Value)rewriter.create(loc, mth.getNullType()); diff --git a/tslang/lib/TypeScript/MLIRGenExpressions.cpp b/tslang/lib/TypeScript/MLIRGenExpressions.cpp index 09dc464a0..0ed15340c 100644 --- a/tslang/lib/TypeScript/MLIRGenExpressions.cpp +++ b/tslang/lib/TypeScript/MLIRGenExpressions.cpp @@ -450,6 +450,14 @@ namespace mlirgen return mlir::failure(); } + if (!!(yieldExpressionAST->internalFlags & InternalFlags::YieldInCatch)) + { + emitError(location, "a yield in a catch block of a generator is not supported: the generator can't resume " + "inside the block's exception handler; keep what the catch needs in a variable and " + "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 @@ -513,6 +521,13 @@ namespace mlirgen label << GENERATOR_STATELABELPREFIX << state; builder.create(location, label.str()); + // resumed by return() or throw() of a generator typed Generator<...>: it finishes or throws here + if (!!(yieldExpressionAST->internalFlags & InternalFlags::GeneratorResumable) + && evaluateProperty(nf.createToken(SyntaxKind::ThisKeyword), GENERATOR_MODE_NAME, genContext)) + { + EXIT_IF_FAILED(mlirGenGeneratorResumeChecks(location, genContext)); + } + // The value passed to next(value) - next()'s parameter, which a generator typed Generator has - or undefined. It is read after the label: the turn that resumes here is the one that uses it. if (auto sent = resolveIdentifier(location, GENERATOR_SENT_NAME, genContext)) diff --git a/tslang/lib/TypeScript/MLIRGenFunctions.cpp b/tslang/lib/TypeScript/MLIRGenFunctions.cpp index ed99b8da4..902c4ed77 100644 --- a/tslang/lib/TypeScript/MLIRGenFunctions.cpp +++ b/tslang/lib/TypeScript/MLIRGenFunctions.cpp @@ -1157,6 +1157,186 @@ namespace mlirgen return std::get<0>(res); } + // return(value: T) and throw(e) of a generator typed Generator, as JavaScript's: a generator paused at a + // yield resumes there - through next() - finishing (`return`, so its finally blocks run) or throwing e (which + // its own try/catch may catch, and then it goes on to its next yield); one not started yet, or finished, does + // not run: return(value) gives `{ value, done: true }`, throw(e) throws e. Either finishes the generator + // (step -1), unless its body caught the throw. GENERATOR_MODE_NAME tells the yield how to resume + // (mlirGenGeneratorResumeChecks). A finally block that yields again is not followed: return() still finishes. + void MLIRGenImpl::addGeneratorResumeMethods(NodeFactory &nf, FunctionLikeDeclarationBase functionLikeDeclarationBaseAST, + TypeNode yieldType, NodeArray &generatorObjectProperties) + { + // the yields of the body - not of a function inside it - resume checking the mode + FilterVisitorSkipFuncsAST yieldsVisitor(SyntaxKind::YieldExpression, [&](auto yieldExpression) { + if (!yieldExpression->asteriskToken) + { + yieldExpression->internalFlags |= InternalFlags::GeneratorResumable; + } + }); + + if (functionLikeDeclarationBaseAST->body) + { + yieldsVisitor.visit(functionLikeDeclarationBaseAST->body); + } + + auto thisProp = [&](const char *name) { + return nf.createPropertyAccessExpression(nf.createToken(SyntaxKind::ThisKeyword), nf.createIdentifier(stows(std::string(name)))); + }; + auto number = [&](const char *value) { return nf.createNumericLiteral(stows(std::string(value)), TokenFlags::None); }; + auto assign = [&](Expression target, Expression value) { + return nf.createExpressionStatement( + nf.createBinaryExpression(target, nf.createToken(SyntaxKind::EqualsToken), value)); + }; + auto finished = [&]() { + return assign(thisProp(GENERATOR_STEP), + nf.createPrefixUnaryExpression(SyntaxKind::MinusToken, number("1"))); + }; + auto paused = [&]() { + return nf.createBinaryExpression(thisProp(GENERATOR_STEP), nf.createToken(SyntaxKind::GreaterThanToken), + number("0")); + }; + auto resultType = [&]() { + NodeArray members; + members.push_back(nf.createPropertySignature(undefined, nf.createIdentifier(S("value")), undefined, yieldType)); + members.push_back(nf.createPropertySignature(undefined, nf.createIdentifier(S("done")), undefined, + nf.createToken(SyntaxKind::BooleanKeyword).as())); + return nf.createTypeLiteralNode(members); + }; + auto anyType = [&]() { return nf.createToken(SyntaxKind::AnyKeyword).as(); }; + auto callNext = [&]() { + return nf.createCallExpression(thisProp(ITERATOR_NEXT), undefined, NodeArray()); + }; + + // the mode, and what throw() was given + generatorObjectProperties.push_back( + nf.createPropertyAssignment(nf.createIdentifier(S(GENERATOR_MODE_NAME)), number("0"))); + generatorObjectProperties.push_back(nf.createPropertyAssignment( + nf.createIdentifier(S(GENERATOR_THROWN_NAME)), + nf.createTypeAssertion(anyType(), nf.createIdentifier(S(UNDEFINED_NAME))))); + + auto addMethod = [&](const char *name, NodeArray parameters, NodeArray statements) { + auto method = nf.createMethodDeclaration(undefined, undefined, nf.createIdentifier(stows(std::string(name))), undefined, + undefined, parameters, resultType(), + nf.createBlock(statements, /*multiLine*/ false)); + method->pos = functionLikeDeclarationBaseAST->pos; + method->_end = functionLikeDeclarationBaseAST->_end; + generatorObjectProperties.push_back(method); + }; + + // return(value: T) { + // if (this.step > 0) { this.mode = 2; this.next(); } + // this.step = -1; + // return { value: value, done: true }; + // } + // Under -mm=own only for a T that owns nothing (a number, a boolean): the result would hold the value the + // caller lends ("an object literal that holds a value it owns is moved only into a local ..."), as next(v) + // keeps one; the generator then has no return() (the library declares it optional) + SyntaxKind yieldKind = yieldType; + auto ownsNothing = yieldKind == SyntaxKind::NumberKeyword || yieldKind == SyntaxKind::BooleanKeyword + || yieldKind == SyntaxKind::BigIntKeyword; + if (compileOptions.memoryModel != MemoryModelOwn || ownsNothing) + { + NodeArray parameters; + parameters.push_back( + nf.createParameterDeclaration(undefined, undefined, nf.createIdentifier(S("value")), undefined, yieldType)); + + NodeArray resume; + resume.push_back(assign(thisProp(GENERATOR_MODE_NAME), number("2"))); + resume.push_back(nf.createExpressionStatement(callNext())); + + NodeArray resultProperties; + resultProperties.push_back(nf.createPropertyAssignment(nf.createIdentifier(S("value")), nf.createIdentifier(S("value")))); + resultProperties.push_back(nf.createPropertyAssignment(nf.createIdentifier(S("done")), nf.createToken(SyntaxKind::TrueKeyword))); + + NodeArray statements; + statements.push_back(nf.createIfStatement(paused(), nf.createBlock(resume, false), undefined)); + statements.push_back(finished()); + statements.push_back(nf.createReturnStatement(nf.createObjectLiteralExpression(resultProperties, false))); + addMethod("return", parameters, statements); + } + + // throw(e: any) { + // if (this.step > 0) { + // this.mode = 1; this.thrown = e; + // try { return this.next(); } catch (x) { this.step = -1; throw x; } + // } + // this.step = -1; + // throw e; + // } + { + NodeArray parameters; + parameters.push_back( + nf.createParameterDeclaration(undefined, undefined, nf.createIdentifier(S("e")), undefined, anyType())); + + NodeArray tryStatements; + tryStatements.push_back(nf.createReturnStatement(callNext())); + + NodeArray catchStatements; + catchStatements.push_back(finished()); + catchStatements.push_back(nf.createThrowStatement(nf.createIdentifier(S("x")))); + auto catchClause = nf.createCatchClause( + nf.createVariableDeclaration(nf.createIdentifier(S("x")), undefined, undefined, undefined), + nf.createBlock(catchStatements, false)); + + NodeArray resume; + resume.push_back(assign(thisProp(GENERATOR_MODE_NAME), number("1"))); + resume.push_back(assign(thisProp(GENERATOR_THROWN_NAME), nf.createIdentifier(S("e")))); + resume.push_back(nf.createTryStatement(nf.createBlock(tryStatements, false), catchClause, undefined)); + + NodeArray statements; + statements.push_back(nf.createIfStatement(paused(), nf.createBlock(resume, false), undefined)); + statements.push_back(finished()); + statements.push_back(nf.createThrowStatement(nf.createIdentifier(S("e")))); + addMethod("throw", parameters, statements); + } + } + + // Where a yield of a generator typed Generator<...> resumes (GeneratorResumable): throw() asked it to throw + // there, return() to finish there (addGeneratorResumeMethods). + // if (this.mode == 1) { this.mode = 0; throw this.thrown; } + // if (this.mode == 2) { this.mode = 0; return; } + mlir::LogicalResult MLIRGenImpl::mlirGenGeneratorResumeChecks(mlir::Location location, const GenContext &genContext) + { + NodeFactory nf(NodeFactoryFlags::None); + auto thisProp = [&](const char *name) { + return nf.createPropertyAccessExpression(nf.createToken(SyntaxKind::ThisKeyword), nf.createIdentifier(stows(std::string(name)))); + }; + auto modeIs = [&](const char *value) { + return nf.createBinaryExpression(thisProp(GENERATOR_MODE_NAME), nf.createToken(SyntaxKind::EqualsEqualsToken), + nf.createNumericLiteral(stows(std::string(value)), TokenFlags::None)); + }; + auto resetMode = [&]() { + return nf.createExpressionStatement(nf.createBinaryExpression( + thisProp(GENERATOR_MODE_NAME), nf.createToken(SyntaxKind::EqualsToken), + nf.createNumericLiteral(S("0"), TokenFlags::None))); + }; + + NodeArray throwStatements; + throwStatements.push_back(resetMode()); + throwStatements.push_back(nf.createThrowStatement(thisProp(GENERATOR_THROWN_NAME))); + + auto finish = nf.createReturnStatement(undefined); + finish->internalFlags |= InternalFlags::GeneratorReturn; + NodeArray returnStatements; + returnStatements.push_back(resetMode()); + returnStatements.push_back(finish); + + LocationHelper lh(builder.getContext()); + auto [pos, _end] = lh.getSpan(location); + for (auto [mode, statements] : {std::make_pair("1", throwStatements), std::make_pair("2", returnStatements)}) + { + auto ifStatement = nf.createIfStatement(modeIs(mode), nf.createBlock(statements, false), undefined); + ifStatement->pos = pos; + ifStatement->_end = _end; + if (mlir::failed(mlirGen(ifStatement, genContext))) + { + return mlir::failure(); + } + } + + return mlir::success(); + } + FunctionLikeDeclarationBase MLIRGenImpl::buildGeneratorWrapperDeclaration( FunctionLikeDeclarationBase functionLikeDeclarationBaseAST, mlir::Location location) { @@ -1227,6 +1407,24 @@ namespace mlirgen } } + // a yield in a catch clause can't be resumed (YieldInCatch): said at the yield + FilterVisitorSkipFuncsAST trysVisitor(SyntaxKind::TryStatement, [&](auto tryStatement) { + if (!tryStatement->catchClause || !tryStatement->catchClause->block) + { + return; + } + + FilterVisitorSkipFuncsAST yieldsInCatch(SyntaxKind::YieldExpression, [&](auto yieldExpression) { + yieldExpression->internalFlags |= InternalFlags::YieldInCatch; + }); + yieldsInCatch.visit(tryStatement->catchClause->block); + }); + + if (functionLikeDeclarationBaseAST->body) + { + trysVisitor.visit(functionLikeDeclarationBaseAST->body); + } + // a `return` of the body finishes the generator; one of a nested function is that function's own FilterVisitorSkipFuncsAST returnsVisitor(SyntaxKind::ReturnStatement, [&](auto returnStatement) { returnStatement->internalFlags |= InternalFlags::GeneratorReturn; @@ -1270,6 +1468,8 @@ namespace mlirgen // `yield 1` could not be a Generator ("can't assign '() => {value:s32, ...}'") TypeNode nextResultType = undefined; NodeArray nextParameters; + // Generator: return(value: T) and throw(e) besides next() (generatorResumeMethods) + TypeNode resumableYieldType = undefined; if (auto declaredType = functionLikeDeclarationBaseAST->type; declaredType && declaredType == SyntaxKind::TypeReference && !hasModifier(functionLikeDeclarationBaseAST, SyntaxKind::AsyncKeyword)) { @@ -1287,6 +1487,11 @@ namespace mlirgen nf.createToken(SyntaxKind::BooleanKeyword).as())); nextResultType = nf.createTypeLiteralNode(members); + if (typeName == "Generator") + { + resumableYieldType = typeReference->typeArguments.front(); + } + // Generator: next(value?: TNext), the value what the resumed `yield` is if (typeName == "Generator" && typeReference->typeArguments.size() >= 3) { @@ -1322,6 +1527,11 @@ namespace mlirgen generatorObjectProperties.push_back(nextMethodDecl); + if (resumableYieldType) + { + addGeneratorResumeMethods(nf, functionLikeDeclarationBaseAST, resumableYieldType, generatorObjectProperties); + } + auto generatorObject = nf.createObjectLiteralExpression(generatorObjectProperties, false); // the generator object has mutable identity (`step` advanced by next()); // it must be a reference type so aliases (params, closures, const bindings) diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index f909ee997..973bf4475 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -2816,7 +2816,13 @@ class MLIRGenImpl mlir::OpBuilder::InsertionGuard insertGuard(builder); if (variableDeclarationInfo.allocateOutsideOfOperation) { - builder.setInsertionPoint(genContext.currentOperation); + // in a generator's next(), in front of its switch, which dominates every resume point - not in front of + // a `try` the turn may resume inside (a `let` in a `try` read after a yield: "operand #0 does not + // dominate this use"). A local of the body is a field of the generator object, kept between turns; + // one of the frame (a catch variable) lives in one turn + builder.setInsertionPoint(genContext.generatorSwitchOperation + ? genContext.generatorSwitchOperation + : genContext.currentOperation); } if (variableDeclarationInfo.allocateInContextThis) @@ -4426,6 +4432,11 @@ class MLIRGenImpl FunctionLikeDeclarationBase buildGeneratorWrapperDeclaration( FunctionLikeDeclarationBase functionLikeDeclarationBaseAST, mlir::Location location); + void addGeneratorResumeMethods(NodeFactory &nf, FunctionLikeDeclarationBase functionLikeDeclarationBaseAST, + TypeNode yieldType, NodeArray &generatorObjectProperties); + + mlir::LogicalResult mlirGenGeneratorResumeChecks(mlir::Location location, const GenContext &genContext); + std::pair registerGenericFunctionLike( FunctionLikeDeclarationBase functionLikeDeclarationBaseAST, bool ignoreFunctionArgsDetection, const GenContext &genContext) diff --git a/tslang/lib/TypeScript/MLIRGenStatements.cpp b/tslang/lib/TypeScript/MLIRGenStatements.cpp index 670976a18..6eccad433 100644 --- a/tslang/lib/TypeScript/MLIRGenStatements.cpp +++ b/tslang/lib/TypeScript/MLIRGenStatements.cpp @@ -2538,6 +2538,10 @@ namespace mlirgen varName = ".caught"; GenContext catchVariableGenContext(genContext); catchVariableGenContext.declaringCatchVariable = true; + // a local of the function's frame even in a generator's next() (not a field of the generator object): + // the exception is copied into it by the catch's handler, which takes a slot of the frame, and it lives + // in one turn only - a yield in a catch is an error (YieldInCatch). As a field, its use crashed + catchVariableGenContext.allocateVarsInContextThis = false; auto declaredType = registerVariable( location, varName, false, VariableType::Let, [&](mlir::Location, const GenContext &) -> TypeValueInitType { @@ -2557,6 +2561,10 @@ namespace mlirgen varName = MLIRHelper::getName(varDecl->name); GenContext catchVariableGenContext(genContext); catchVariableGenContext.declaringCatchVariable = true; + // a local of the function's frame even in a generator's next() (not a field of the generator object): + // the exception is copied into it by the catch's handler, which takes a slot of the frame, and it lives + // in one turn only - a yield in a catch is an error (YieldInCatch). As a field, its use crashed + catchVariableGenContext.allocateVarsInContextThis = false; if (mlir::failed(mlirGen(varDecl, VariableType::Let, catchVariableGenContext))) { return mlir::failure(); diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index b32ad8535..6288fbd97 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -470,6 +470,7 @@ tslang_add_test(NAME test-compile-00-iterables COMMAND test-runner "${PROJECT_SO tslang_add_test(NAME test-compile-00-generator-typed-yield COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_typed_yield.ts") 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-constructor-type-value COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts") tslang_add_test(NAME test-compile-00-string-concat-chain COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00string_concat_chain.ts") tslang_add_test(NAME test-compile-00-arrays COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00array.ts") @@ -1308,6 +1309,9 @@ tslang_add_test(NAME test-jit-own-00-object-method-arity COMMAND test-runner -ji tslang_add_test(NAME test-jit-00-generator-next-value COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_next_value.ts") tslang_add_test(NAME test-jit-rc-00-generator-next-value COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_next_value.ts") tslang_add_test(NAME test-jit-own-00-generator-next-value COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_next_value.ts") +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-constructor-type-value COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts") tslang_add_test(NAME test-jit-rc-00-constructor-type-value COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts") tslang_add_test(NAME test-jit-own-00-constructor-type-value COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts") @@ -4166,6 +4170,15 @@ set_tests_properties(test-compile-diagnostic-location-generator-return-of-other- PROPERTIES PASS_REGULAR_EXPRESSION "generator_return_of_other_type\\.ts:9:5: error: a value returned by a generator typed Generator with a TReturn other than T is not supported" FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed") +# A yield in a catch block of a generator is an error at the yield (it crashed in LLVM's instruction selection). +add_test(NAME test-compile-diagnostic-location-generator-yield-in-catch + COMMAND $ --emit=obj --no-default-lib -mm=none + "${PROJECT_SOURCE_DIR}/test/tester/diagnostic-location/generator_yield_in_catch.ts" + -o "${CMAKE_CURRENT_BINARY_DIR}/diagnostic-location-generator-yield-in-catch.obj") +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 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_catch.ts b/tslang/test/tester/diagnostic-location/generator_yield_in_catch.ts new file mode 100644 index 000000000..bb43a1bcd --- /dev/null +++ b/tslang/test/tester/diagnostic-location/generator_yield_in_catch.ts @@ -0,0 +1,14 @@ +// A yield in a catch block of a generator is an error at the yield: the generator can't resume inside the +// block's exception handler (the compiler crashed in LLVM's instruction selection). +function* g() { + try { + yield 1; + throw "e"; + } catch (e) { + yield 2; + } +} + +function main() { + for (const v of g()) print(v); +} diff --git a/tslang/test/tester/tests/00generator_return_throw.ts b/tslang/test/tester/tests/00generator_return_throw.ts new file mode 100644 index 000000000..79d74158f --- /dev/null +++ b/tslang/test/tester/tests/00generator_return_throw.ts @@ -0,0 +1,100 @@ +// return(value) and throw(e) of a generator typed Generator, as JavaScript's: one paused at a yield +// resumes there finishing (its finally blocks run) or throwing e (its own try/catch may catch it, and it goes on +// to its next yield); one not started yet, or finished, does not run. Fixed on the way: a throw or a call after a +// resumed yield inside a `try` went past it (its catch did not catch, its finally did not run), a `let` in a `try` +// read after a yield and a throw after a yield in a loop in a `try` did not compile, and a catch variable of a +// generator crashed. (-mm=own: return() only for a T that owns nothing - a number, a boolean.) +type IterateResult = { value: T, done: boolean }; +interface Generator { + next: (value?: TNext) => IterateResult; + return: (value: T) => IterateResult; + throw: (e: any) => IterateResult; +} + +let cleaned = 0; +let lastCaught = ""; + +function* counter(): Generator { + try { + let i = 0; + while (true) yield i++; + } finally { + cleaned++; + } +} + +function* catcher(): Generator { + let caught = 0; + while (true) { + try { + yield caught; + } catch (e) { + caught++; + lastCaught = "caught " + e; + } + } +} + +// no return()/throw() involved: the try fixes alone +function* afterYield() { + try { + let n = 1; + yield n; + if (n == 1) throw "after"; + } catch (e) { + lastCaught = "caught " + e; + } + + yield 2; +} + +function main() { + // return() of a paused generator: finishes, its finally runs + const c = counter(); + assert(c.next().value == 0 && c.next().value == 1); + const r = c.return(42); + assert(r.value == 42 && r.done && cleaned == 1, "return() finishes, finally runs"); + assert(c.next().done, "finished after return()"); + + // return() of one not started: does not run + const n = counter(); + const r2 = n.return(7); + assert(r2.value == 7 && r2.done && cleaned == 1, "return() before the start"); + assert(n.next().done); + + // throw() caught by the body: it goes on to its next yield + const k = catcher(); + assert(k.next().value == 0); + assert(k.throw("boom").value == 1, "throw() caught"); + assert(lastCaught == "caught boom"); + assert(k.next().value == 1); + + // throw() not caught: it comes out, the finally runs, the generator is finished + const u = counter(); + u.next(); + let out = ""; + try { + u.throw("out"); + } catch (e) { + out = "thrown " + e; + } + + assert(out == "thrown out" && cleaned == 2, "throw() not caught"); + assert(u.next().done); + + // throw() of one not started: thrown at once + let early = ""; + try { + counter().throw("early"); + } catch (e) { + early = "thrown " + e; + } + + assert(early == "thrown early" && cleaned == 2, "throw() before the start"); + + let total = 0; + for (const v of afterYield()) total += v; + assert(total == 3 && lastCaught == "caught after", "a throw after a resumed yield is caught"); + + print("done."); +} diff --git a/tslang/ts-new-parser/enums.h b/tslang/ts-new-parser/enums.h index 3c04f05ae..9758dc306 100644 --- a/tslang/ts-new-parser/enums.h +++ b/tslang/ts-new-parser/enums.h @@ -584,7 +584,13 @@ enum class InternalFlags : number HoistedLexical = 1 << 19, // `return v` of a generator typed Generator whose TReturn is not T: next() gives `{ value: T }`, and // v has no place in it (it was cast to T without a word: "end" became 0) - GeneratorReturnOfOtherType = 1 << 20 + GeneratorReturnOfOtherType = 1 << 20, + // a `yield` of a generator typed Generator<...>, which has return() and throw(): where it resumes, the + // generator may be asked to finish or to throw there instead + 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 }; ENUM_OPS(InternalFlags) From 221ff2c3f022206e89d74c5ad8077c4ee72ef776 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sat, 21 Nov 2026 14:41:03 +0000 Subject: [PATCH 2/4] TSLANG_ISSUES 153: generator return()/throw() and the try fixes; yield in finally still open Co-Authored-By: Claude Opus 5.5 --- TSLANG_ISSUES.md | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/TSLANG_ISSUES.md b/TSLANG_ISSUES.md index ebe3b8cdc..4f33344d3 100644 --- a/TSLANG_ISSUES.md +++ b/TSLANG_ISSUES.md @@ -2177,9 +2177,32 @@ Tests: `00object_dictionary.ts`, `00own_param_exclusive_moves.ts` (compile, JIT, whose next() takes no value is a Generator whose next takes one, and the other way round. The library's `Generator` declares `next: (value?: TNext)` once a compiler with this is released (a released one without it can't give a plain generator as one). +- return(value) and throw(e) of a generator typed `Generator`, as JavaScript's: paused at a yield, it + resumes there finishing (`return`, so its finally blocks run) or throwing e (its own try/catch may catch it, and + it goes on to its next yield); one not started yet, or finished, does not run - return(value) gives `{ value, + done: true }`, throw(e) throws e. The generator object holds a mode its yields check where they resume + (addGeneratorResumeMethods, mlirGenGeneratorResumeChecks). Under -mm=own return() only for a T that owns + nothing (a number, a boolean): its result would hold the value the caller lends. The library's `Generator` + declares them optional (`return?`, `throw?`: a generator declared otherwise has neither) once a compiler with + them is released. Under -mm=own a call through an optional one is read as moving the generator, so there a + program declaring them required calls them. +- Found on the way, generators and `try` (all memory models): + - A throw or a call after a resumed yield inside a `try` went past it - its catch did not catch, its finally did + not run (`try { yield 1; throw "x"; } catch (e) {...}`: "uncaught exception"). A yield splits the body at its + label, and only the body's first block got unwind edges (TryOpLowering: `continue` in a `do ... while + (false)`). + - A throw after a yield in a loop in a `try` did not compile ("operand #0 does not dominate this use" at the + `try`): the catch-type constants the landing pad reads were made at the start of the body, which a resumed + turn does not pass - in a generator they go to the function's entry. + - A `let` in a `try` read after a yield did not compile, the same error: its storage was put in front of the + try, not of the generator's switch (GenContext::generatorSwitchOperation). + - A catch variable of a generator was a field of the generator object, and its use crashed: the catch handler + 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). Still open, looked at: -- return(v) / throw(e) of a generator. +- A yield in a finally block is skipped (`try { yield 1; } finally { yield 2; }` gives 1 and not 2), as before. - -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 @@ -2194,5 +2217,5 @@ Still open, looked at: `[Symbol.asyncIterator]` of an async generator. Tests: `00iterables.ts`, `00generator_typed_yield.ts` (compile, JIT, rc), -diagnostic-location `generator_return_of_other_type.ts`; `00generator_next_value.ts`, `00object_method_arity.ts` -(compile, JIT, rc, own). +diagnostic-location `generator_return_of_other_type.ts`; `00generator_next_value.ts`, `00object_method_arity.ts`, +`00generator_return_throw.ts` (compile, JIT, rc, own), diagnostic-location `generator_yield_in_catch.ts`. From 4957e7dbcd916d36a014690a31ab979c1f4e8267 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sat, 21 Nov 2026 15:41:13 +0000 Subject: [PATCH 3/4] Generator .thrown starts as 0: undefined in an object literal is released twice under -mm=own The generator object's `.thrown` field was `undefined`. Under -mm=own a field of an object literal holding `undefined` (or `null`) is released twice - the store of its box is not counted as a move - which wasm's checked heap and Linux caught in every generator typed Generator<...> (00generator_next_value, 00generator_return_throw, the default library's iteration_types). The underlying own bug is recorded in TSLANG 153. Co-Authored-By: Claude Opus 5.5 --- tslang/lib/TypeScript/MLIRGenFunctions.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tslang/lib/TypeScript/MLIRGenFunctions.cpp b/tslang/lib/TypeScript/MLIRGenFunctions.cpp index 902c4ed77..35eb09f5c 100644 --- a/tslang/lib/TypeScript/MLIRGenFunctions.cpp +++ b/tslang/lib/TypeScript/MLIRGenFunctions.cpp @@ -1210,9 +1210,12 @@ namespace mlirgen // the mode, and what throw() was given generatorObjectProperties.push_back( nf.createPropertyAssignment(nf.createIdentifier(S(GENERATOR_MODE_NAME)), number("0"))); + // (a number in it, not `undefined`: under -mm=own an object literal's field of `undefined` or + // `null` was released twice - its box stored into the object and given back at the end as well, a + // double free wasm's heap caught; TSLANG 153) generatorObjectProperties.push_back(nf.createPropertyAssignment( nf.createIdentifier(S(GENERATOR_THROWN_NAME)), - nf.createTypeAssertion(anyType(), nf.createIdentifier(S(UNDEFINED_NAME))))); + nf.createTypeAssertion(anyType(), number("0")))); auto addMethod = [&](const char *name, NodeArray parameters, NodeArray statements) { auto method = nf.createMethodDeclaration(undefined, undefined, nf.createIdentifier(stows(std::string(name))), undefined, From 0e4a7a8ed81eb82504edaf64cf61548998cbb229 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sat, 21 Nov 2026 15:41:26 +0000 Subject: [PATCH 4/4] TSLANG_ISSUES 153: own double release of undefined in an object literal field (open) Co-Authored-By: Claude Opus 5.5 --- TSLANG_ISSUES.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/TSLANG_ISSUES.md b/TSLANG_ISSUES.md index 4f33344d3..71382e04a 100644 --- a/TSLANG_ISSUES.md +++ b/TSLANG_ISSUES.md @@ -2202,6 +2202,11 @@ Tests: `00object_dictionary.ts`, `00own_param_exclusive_moves.ts` (compile, JIT, (it crashed in LLVM's instruction selection; before the fixes above the catch did not catch). Still open, looked at: +- -mm=own: an object literal's field of `undefined` or `null` (`{ t: undefined, n: 1 }`) is + released twice: OwnershipInferencePass does not count the store of its box into the object as a move (a box of + `5` it does), so the release at the end of the function stays and the object's release frees the box again. + wasm's heap caught it ("free of ...: not a block the heap gave out"), and Linux under -mm=own; Windows did not. + The generator object's `.thrown` holds `0` for that reason (its first version was `undefined`). - A yield in a finally block is skipped (`try { yield 1; } finally { yield 2; }` gives 1 and not 2), as before. - -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()