From cb21074df201984fde10af5898f0e2ced622d26f Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Wed, 23 Sep 2026 11:12:48 +0100 Subject: [PATCH 1/2] Fix Windows try/catch: sequential tries, per-class throw info, nested mismatches Four run-time failures, none of them visible at compile time: - A catch followed directly by a try whose body starts with a call got no catchret: Win32ExceptionPass took that invoke for the catch's own end-of-catch invoke, so the next try ran inside the catch funclet. - Every class shared one ThrowInfo/CatchableTypeArray name, so the first class a module threw fixed the descriptors for all of them. - A typed catch nested in another try in the same function, with no call in its body, left nothing unwinding to the outer pad; the pad was deleted and a non-matching exception escaped. The Linux mismatch-rethrow edge now also exists on Windows for that case (never executed: the filter already matched). - The same mismatch skipped its own try's finally; the edge goes there first. Co-Authored-By: Claude Opus 5.5 --- .../LowerToLLVM/LLVMRTTIHelperVCWin32.h | 5 +- .../TypeScript/LowerToLLVM/ThrowLogic.h | 22 +- .../MLIRLogic/MLIRRTTIHelperVCWin32.h | 19 +- .../TypeScript/TypeScriptPassContext.h | 5 + tslang/lib/TypeScript/LowerToAffineLoops.cpp | 39 +++- .../Win32ExceptionPass.cpp | 32 ++- tslang/test/tester/CMakeLists.txt | 3 + .../tests/00try_catch_sequence_and_nesting.ts | 206 ++++++++++++++++++ 8 files changed, 317 insertions(+), 14 deletions(-) create mode 100644 tslang/test/tester/tests/00try_catch_sequence_and_nesting.ts diff --git a/tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCWin32.h b/tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCWin32.h index ea9ed870e..ff8c2f095 100644 --- a/tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCWin32.h +++ b/tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCWin32.h @@ -118,8 +118,9 @@ class LLVMRTTIHelperVCWin32 types.push_back({ClassType::typeName2, ClassType::typeInfoRef2, ClassType::catchableTypeInfoRef2, pointerSize()}); - catchableTypeInfoArrayRef = ClassType::catchableTypeInfoArrayRef; - throwInfoRef = ClassType::throwInfoRef; + // per class, not shared - see MLIRRTTIHelperVCWin32::setClassThrowInfoNames + catchableTypeInfoArrayRef = join(name, ClassType::catchableTypeInfoArrayRef, ClassType::catchableTypeInfoArrayRefSuffix); + throwInfoRef = join(name, ClassType::throwInfoRef, ClassType::throwInfoRefSuffix); } // A pointer-shaped catchable type (a string, an opaque pointer, a class reference) is as diff --git a/tslang/include/TypeScript/LowerToLLVM/ThrowLogic.h b/tslang/include/TypeScript/LowerToLLVM/ThrowLogic.h index 9e4538ec2..eba5849c6 100644 --- a/tslang/include/TypeScript/LowerToLLVM/ThrowLogic.h +++ b/tslang/include/TypeScript/LowerToLLVM/ThrowLogic.h @@ -65,8 +65,16 @@ class ThrowLogic auto i8PtrTy = th.getPtrType(); + // a `null` operand is a rethrow of the exception being handled: _CxxThrowException(0, 0) + auto isRethrow = isa(exceptionType); + // variable mlir::Value value; + if (isRethrow) + { + value = rewriter.create(loc, i8PtrTy); + } + else { OpBuilder::InsertionGuard guard(rewriter); @@ -84,10 +92,18 @@ class ThrowLogic value = rewriter.create(loc, typeConverter->convertType(value.getType()), value); } - rewriter.create(loc, exceptionValue, value); + mlir::Value throwInfoPtr; + if (isRethrow) + { + throwInfoPtr = rewriter.create(loc, throwInfoPtrTy); + } + else + { + rewriter.create(loc, exceptionValue, value); - // throw - auto throwInfoPtr = rttih.throwInfoPtrValue(loc); + // throw + throwInfoPtr = rttih.throwInfoPtrValue(loc); + } auto throwFuncName = "_CxxThrowException"; auto cxxThrowException = ch.getOrInsertFunction(throwFuncName, th.getFunctionType(th.getVoidType(), {i8PtrTy, throwInfoPtrTy})); diff --git a/tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCWin32.h b/tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCWin32.h index ab1466da0..d9b9eceda 100644 --- a/tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCWin32.h +++ b/tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCWin32.h @@ -126,8 +126,7 @@ class MLIRRTTIHelperVCWin32 types.push_back({windows::ClassType::typeName2, windows::ClassType::typeInfoRef2, windows::ClassType::catchableTypeInfoRef2, pointerSize()}); - catchableTypeInfoArrayRef = windows::ClassType::catchableTypeInfoArrayRef; - throwInfoRef = windows::ClassType::throwInfoRef; + setClassThrowInfoNames(names.front()); } void setClassTypeAsCatchType(StringRef name) @@ -140,8 +139,20 @@ class MLIRRTTIHelperVCWin32 types.push_back({windows::ClassType::typeName2, windows::ClassType::typeInfoRef2, windows::ClassType::catchableTypeInfoRef2, pointerSize()}); - catchableTypeInfoArrayRef = windows::ClassType::catchableTypeInfoArrayRef; - throwInfoRef = windows::ClassType::throwInfoRef; + setClassThrowInfoNames(name); + } + + // Each class needs its own CatchableTypeArray and ThrowInfo: they list that class's own + // descriptors (and its bases'), and both are linkonce_odr globals keyed only by name. A + // shared name let the first class a module threw fix the descriptors for every other class + // thrown from it - `throw new B()` then went out as an `A`, and `catch (e: B)` never matched. + // Must agree with LLVMRTTIHelperVCWin32::setClassTypeAsCatchType, which names the ThrowInfo + // a throw site passes to _CxxThrowException. + void setClassThrowInfoNames(StringRef name) + { + catchableTypeInfoArrayRef = + join(name, windows::ClassType::catchableTypeInfoArrayRef, windows::ClassType::catchableTypeInfoArrayRefSuffix); + throwInfoRef = join(name, windows::ClassType::throwInfoRef, windows::ClassType::throwInfoRefSuffix); } // A pointer-shaped catchable type (a string, an opaque pointer, a class reference) is as diff --git a/tslang/include/TypeScript/TypeScriptPassContext.h b/tslang/include/TypeScript/TypeScriptPassContext.h index 68bfc03a8..e5c02fbfa 100644 --- a/tslang/include/TypeScript/TypeScriptPassContext.h +++ b/tslang/include/TypeScript/TypeScriptPassContext.h @@ -125,6 +125,7 @@ struct TSContext unwind.beginRun(); cleanup.beginRun(); parentTryOp.beginRun(); + inParentTryBody.beginRun(); landingBlockOf.beginRun(); leavesCatch.beginRun(); } @@ -138,6 +139,10 @@ struct TSContext OpSideTable unwind{"unwind"}; OpSideTable cleanup{"cleanup"}; OpSideTable parentTryOp{"parentTryOp"}; + // Whether a try sits in its parentTryOp's *body*, where an exception leaving it is the + // parent's to catch - as opposed to the parent's catch or finally, where unwinding to the + // parent's landing pad would loop back into the handler it is already running. + OpSideTable inParentTryBody{"inParentTryBody"}; OpSideTable landingBlockOf{"landingBlockOf"}; // Throws that sit inside a catch clause and therefore have to end the active catch before // they leave it. `return`, `break` and `continue` carry the same meaning in `unwind`, but diff --git a/tslang/lib/TypeScript/LowerToAffineLoops.cpp b/tslang/lib/TypeScript/LowerToAffineLoops.cpp index bcd381a6d..f8315f3e9 100644 --- a/tslang/lib/TypeScript/LowerToAffineLoops.cpp +++ b/tslang/lib/TypeScript/LowerToAffineLoops.cpp @@ -1296,6 +1296,7 @@ struct TryOpLowering : public TsPattern auto module = tryOp->getParentOfType(); auto parentTryOp = tsContext->parentTryOp.lookup(tryOp.getOperation()); mlir::Block *parentTryOpLandingPad = parentTryOp ? tsContext->landingBlockOf.lookup(parentTryOp) : nullptr; + auto parentCatchesThis = parentTryOpLandingPad && tsContext->inParentTryBody.lookup(tryOp.getOperation()); MLIRRTTIHelperVC rttih(rewriter, module, tsContext->compileOptions); auto i8PtrTy = mth.getOpaqueType(); @@ -1331,13 +1332,17 @@ struct TryOpLowering : public TsPattern tryOp.getCatches().walk(visitorCatchContinue); // set TryOp -> child TryOp + auto inBody = false; auto visitorTryOps = [&](Operation *op) { if (auto childTryOp = dyn_cast_or_null(op)) { tsContext->parentTryOp[op] = tryOp.getOperation(); + tsContext->inParentTryBody[op] = inBody; } }; + inBody = true; tryOp.getBody().walk(visitorTryOps); + inBody = false; tryOp.getCatches().walk(visitorTryOps); tryOp.getFinally().walk(visitorTryOps); @@ -1495,6 +1500,7 @@ struct TryOpLowering : public TsPattern if (auto parent = tsContext->parentTryOp.lookup(oldOp)) { tsContext->parentTryOp[newOp] = parent; + tsContext->inParentTryBody[newOp] = tsContext->inParentTryBody.lookup(oldOp); } } }; @@ -1707,7 +1713,23 @@ struct TryOpLowering : public TsPattern auto landingPadOp = rewriter.create(loc, rttih.getLandingPadType(), rewriter.getBoolAttr(false), catchTypes); - if (!tsContext->compileOptions.isWindows && rttih.hasType()) + // On Windows a typed catch that does not match is left by the runtime, through the + // catchswitch's unwind edge - but Win32ExceptionPass can only point that edge at the + // enclosing try's pad if something inside the catch region already unwinds there. + // A catch body with no call has nothing that does, so the enclosing pad has no + // predecessor left, is deleted as unreachable, and an exception the outer try was + // meant to catch leaves the function instead. The mismatch rethrow below is that + // edge: CompareCatchTypeOp is always true on Windows (the filter has already + // matched by the time the catch body runs), so the rethrow never executes, but it + // keeps the enclosing pad alive until the funclet pass has wired it up. + // + // The same goes for this try's own `finally`, which a mismatched exception has to + // run on its way out: without the edge the catchswitch unwinds straight past it. + // + // Only a try in its parent's body has an edge to add (parentCatchesThis): one in the + // parent's catch or finally is already inside the handler that pad leads to. + auto windowsNeedsParentEdge = tsContext->compileOptions.isWindows && (parentCatchesThis || finallyHasOps); + if (rttih.hasType() && (!tsContext->compileOptions.isWindows || windowsNeedsParentEdge)) { if (linuxHasCleanups) { @@ -1867,7 +1889,20 @@ struct TryOpLowering : public TsPattern rewriter.setInsertionPointToStart(rethrowBlock); auto rethrowVal = rewriter.create(loc, mth.getNullType()); auto rethrowOp = rewriter.create(loc, rethrowVal); - if (parentTryOpLandingPad) + if (tsContext->compileOptions.isWindows) + { + // see windowsNeedsParentEdge: the finally's cleanup pad comes before any + // enclosing try's; a rethrow with neither unwinds to the caller + if (finallyHasOps) + { + tsContext->unwind[rethrowOp] = finallyBlock; + } + else if (parentCatchesThis) + { + tsContext->unwind[rethrowOp] = parentTryOpLandingPad; + } + } + else if (parentTryOpLandingPad) { tsContext->unwind[rethrowOp] = parentTryOpLandingPad; } diff --git a/tslang/lib/TypeScriptExceptionPass/Win32ExceptionPass.cpp b/tslang/lib/TypeScriptExceptionPass/Win32ExceptionPass.cpp index 9cf5aaf00..533ade721 100644 --- a/tslang/lib/TypeScriptExceptionPass/Win32ExceptionPass.cpp +++ b/tslang/lib/TypeScriptExceptionPass/Win32ExceptionPass.cpp @@ -79,6 +79,20 @@ struct Win32ExceptionPassCode CI->getCalledFunction()->getName() == "__cxa_end_catch"; } + // The two invokes a catch region can legitimately end on: its own end-of-catch marker, or a + // throw out of it. Either way nothing follows inside the funclet, so it needs no catchret. + static bool isCatchEndingInvoke(llvm::Instruction *I) + { + auto *II = dyn_cast(I); + if (!II || II->getCalledFunction() == nullptr || !II->getCalledFunction()->hasName()) + { + return false; + } + + auto name = II->getCalledFunction()->getName(); + return name == "__cxa_end_catch" || name == "_CxxThrowException"; + } + bool runOnFunction(Function &F) { auto MadeChange = false; @@ -408,14 +422,26 @@ struct Win32ExceptionPassCode llvm::IRBuilder<> Builder(I); llvm::LLVMContext &Ctx = Builder.getContext(); + // A catch region's `end` is otherwise just the first instruction after its + // end-of-catch marker, and that can be an ordinary invoke: once the canonicalizer + // merges a catch block into what follows it, a second try's first call comes + // straight after the first catch. Treated like the end marker, that invoke got no + // catchret, so the second try ran inside the first catch's funclet and crashed. + // It has to be split off like any other instruction instead. + auto *endInvoke = dyn_cast(I); + if (endInvoke && catchRegion.isCatch() && !isCatchEndingInvoke(endInvoke)) + { + endInvoke = nullptr; + } + auto *BI = dyn_cast(I); if (BI) { retBlock = BI->getSuccessor(0); } - else if (auto *II = dyn_cast(I)) + else if (endInvoke) { - retBlock = II->getNormalDest(); + retBlock = endInvoke->getNormalDest(); } else if (dyn_cast(I)) { @@ -448,7 +474,7 @@ struct Win32ExceptionPassCode if (catchRegion.isCatch()) { - if (!dyn_cast(I)) + if (!endInvoke) { assert(catchRegion.catchPad); auto CR = CatchReturnInst::Create(catchRegion.catchPad, retBlock, BI ? BI->getParent() : I->getParent()); diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index a7cbfeeab..8b64a0870 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -387,6 +387,7 @@ tslang_add_test(NAME test-compile-00-owned-locals COMMAND test-runner "${PROJECT tslang_add_test(NAME test-compile-00-try-using-catch COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_using_catch.ts") tslang_add_test(NAME test-compile-00-throw-in-catch COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00throw_in_catch.ts") tslang_add_test(NAME test-compile-00-throw-inlined COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00throw_inlined.ts") +tslang_add_test(NAME test-compile-00-try-catch-sequence-and-nesting COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_catch_sequence_and_nesting.ts") # Windows only: the fix this pins down is in the Win32 RTTI tables. On Linux an untyped catch # still does not match a thrown number (the clause is a void* pointer type, which the Itanium ABI # matches only against pointer exceptions), so this test fails there. See the Linux catch issue. @@ -815,6 +816,7 @@ tslang_add_test(NAME test-jit-00-new-delete COMMAND test-runner -jit "${PROJECT_ tslang_add_test(NAME test-jit-00-owned-locals COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00owned_locals.ts") tslang_add_test(NAME test-jit-00-throw-in-catch COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00throw_in_catch.ts") tslang_add_test(NAME test-jit-00-throw-inlined COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00throw_inlined.ts") +tslang_add_test(NAME test-jit-00-try-catch-sequence-and-nesting COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_catch_sequence_and_nesting.ts") # Windows only, for the reason given at the compile registration above. if (WIN32) tslang_add_test(NAME test-jit-00-throw-number-untyped-catch COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00throw_number_untyped_catch.ts") @@ -1717,6 +1719,7 @@ set(TSLANG_CORPUS 00try_catch_rethrow.ts 00try_catch_return_dispose.ts 00try_catch_return.ts + 00try_catch_sequence_and_nesting.ts 00try_catch.ts 00try_finally_break_continue.ts 00try_finally_return.ts diff --git a/tslang/test/tester/tests/00try_catch_sequence_and_nesting.ts b/tslang/test/tester/tests/00try_catch_sequence_and_nesting.ts new file mode 100644 index 000000000..4f2fcbb15 --- /dev/null +++ b/tslang/test/tester/tests/00try_catch_sequence_and_nesting.ts @@ -0,0 +1,206 @@ +// try/catch shapes that each failed on Windows, all of them silently or with a crash at run time, +// none of them at compile time. Each function exercises one of them. +// +// - Two try/catch statements in a row, the second one's body starting with a call. The +// canonicalizer merges the first catch block into what follows it, so the first instruction +// after the first catch's end marker was the second try's `invoke`. Win32ExceptionPass took +// that invoke for the catch's own end-of-catch invoke and gave the catch no catchret, so the +// second try ran inside the first catch's funclet and the process crashed. +// +// - Two different classes thrown from one module. Every class shared one ThrowInfo and one +// CatchableTypeArray name (`_TI2PEAV` / `_CTA2PEAV`), both linkonce_odr, so the first class +// thrown fixed the descriptors for every other: `throw new B()` went out as an `A`. +// +// - A typed catch that does not match, nested in another try in the same function. On Windows +// the catchswitch learns where to unwind next only from an invoke inside its catch body; with +// no call there the enclosing try's landing pad had no predecessor, was deleted, and the +// exception left the function. +// +// - The same mismatch with a `finally` on the inner try: that finally was skipped. +// +// The value a catch binds is read only for typed class catches, which 00catch_value.ts covers. + +class A { + a = 1; +} + +class B { + b = 2; +} + +class Base { + k = 3; +} + +class Derived extends Base { + d = 4; +} + +function one() { + return 1; +} + +function sequentialTryWithCall() { + let steps = 0; + try { + throw 1; + } catch (e: int) { + steps += 1; + } + + try { + one(); + throw 2; + } catch (e2: int) { + steps += 10; + } + + try { + let a = new A(); + throw 3; + } catch (e3: int) { + steps += 100; + } + + return steps; +} + +function twoClassesThrown() { + let sum = 0; + try { + throw new A(); + } catch (e: A) { + sum += e.a; + } + + try { + throw new B(); + } catch (e2: B) { + sum += e2.b * 10; + } + + try { + throw new Derived(); + } catch (e3: Base) { + sum += e3.k * 100; + } + + return sum; +} + +function nestedMismatchPrimitive() { + let path = 0; + try { + try { + throw 1; + } catch (e: string) { + path = 1; + } + } catch (e2: int) { + path += 10; + } + + return path; +} + +function nestedMismatchClass() { + let path = 0; + try { + try { + throw new B(); + } catch (e: A) { + path = 1; + } + } catch (e2: B) { + path += 10 * e2.b; + } + + return path; +} + +function nestedMatchStaysInner() { + let path = 0; + try { + try { + throw 1; + } catch (e: int) { + path += 1; + } + } catch (e2: int) { + path += 10; + } + + return path; +} + +function threeDeepMismatch() { + let path = 0; + try { + try { + try { + throw 1; + } catch (e: string) { + path += 1; + } + } catch (e2: B) { + path += 10; + } + } catch (e3: int) { + path += 100; + } + + return path; +} + +function mismatchRunsOwnFinally() { + let path = 0; + try { + try { + throw 1; + } catch (e: string) { + path += 1; + } finally { + path += 10; + } + } catch (e2: int) { + path += 100; + } + + return path; +} + +let trail = 0; + +function mismatchInCalleeRunsFinally() { + try { + throw 1; + } catch (e: string) { + trail = trail * 10 + 1; + } finally { + trail = trail * 10 + 2; + } +} + +function callerCatchesAfterFinally() { + trail = 0; + try { + mismatchInCalleeRunsFinally(); + } catch (e: int) { + trail = trail * 10 + 3; + } + + return trail; +} + +function main() { + assert(sequentialTryWithCall() == 111, "every catch in a sequence of try/catch statements runs and returns"); + assert(twoClassesThrown() == 321, "each thrown class reaches its own typed catch"); + assert(nestedMismatchPrimitive() == 10, "a nested catch that does not match leaves the exception to the outer try"); + assert(nestedMismatchClass() == 20, "a nested class catch that does not match leaves the exception to the outer try"); + assert(nestedMatchStaysInner() == 1, "a nested catch that matches keeps the exception"); + assert(threeDeepMismatch() == 100, "an exception passes two non-matching catches to reach a third"); + assert(mismatchRunsOwnFinally() == 110, "a catch that does not match still runs its try's finally"); + assert(callerCatchesAfterFinally() == 23, "the finally runs before the caller's catch"); + + print("done."); +} From d505b83b3c6b5bb6046720418efbf1bc4b38ea37 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Wed, 23 Sep 2026 11:21:20 +0100 Subject: [PATCH 2/2] Fix nested typed-catch mismatches on Linux; scope the catch binding On Linux a typed catch nested in another try of the same function (or with its own finally) listed only its typed clause, so any other exception type never entered the pad and the process called std::terminate. Such a catch now gets a catch-all clause and, on mismatch, begins the catch and rethrows to the finally or the enclosing try - the pattern the nested finally/cleanup paths already use. The Linux LandingPadOp lowering kept only the first clause and silently dropped that catch-all (and the one linuxHasCleanups adds). The catch binding now lives in its own symbol scope, so a second `catch (e)` in the same block is no longer reported as a redeclaration. Co-Authored-By: Claude Opus 5.5 --- tslang/lib/TypeScript/LowerToAffineLoops.cpp | 27 +++++++++++++++---- tslang/lib/TypeScript/LowerToLLVM.cpp | 7 ++--- tslang/lib/TypeScript/MLIRGenStatements.cpp | 4 +++ .../tests/00try_catch_sequence_and_nesting.ts | 4 +++ 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/tslang/lib/TypeScript/LowerToAffineLoops.cpp b/tslang/lib/TypeScript/LowerToAffineLoops.cpp index f8315f3e9..4f7733bbb 100644 --- a/tslang/lib/TypeScript/LowerToAffineLoops.cpp +++ b/tslang/lib/TypeScript/LowerToAffineLoops.cpp @@ -1636,9 +1636,20 @@ struct TryOpLowering : public TsPattern auto linuxCleanupOnlyChainsToParent = linuxHasCleanups && !catchHasOps && !finallyHasOps && parentTryOpLandingPad; + // A typed catch whose exception may belong to someone else in this same function - the + // try's own finally, or the try around it - has to be entered for every exception, not + // only a matching one. An Itanium landingpad that lists just the typed clause is skipped + // by the personality for any other type, and since nothing in this frame unwinds to the + // finally or the enclosing try's pad, the exception is not handled here at all: + // std::terminate. So add a catch-all clause, and let the mismatch path below rethrow to + // where the exception belongs (the same catch-all-and-rethrow the nested finally and + // cleanup cases here already use). linuxHasCleanups adds the clause on its own. + auto linuxTypedCatchChains = !tsContext->compileOptions.isWindows && catchHasOps && rttih.hasType() && + !linuxHasCleanups && (parentCatchesThis || finallyHasOps); + mlir::Value catchAll; if (parentTryOpLandingPad && finallyHasOps || linuxHasCleanups && rttih.hasType() || - linuxCleanupOnlyChainsToParent) + linuxCleanupOnlyChainsToParent || linuxTypedCatchChains) { catchAll = (mlir::Value)rewriter.create(loc, mth.getNullType()); } @@ -1704,7 +1715,7 @@ struct TryOpLowering : public TsPattern rewriter.setInsertionPointToStart(linuxHasCleanups ? cleanupBlock : catchesBlock); SmallVector catchTypes{catch1}; - if (linuxHasCleanups && rttih.hasType()) + if (linuxHasCleanups && rttih.hasType() || linuxTypedCatchChains) { // we need to catch all exceptions for cleanup code catchTypes.push_back(catchAll); @@ -1887,12 +1898,18 @@ struct TryOpLowering : public TsPattern // instead of falling through to normal post-try control flow. auto *rethrowBlock = rewriter.createBlock(continuationBrCmp); rewriter.setInsertionPointToStart(rethrowBlock); + if (linuxTypedCatchChains) + { + // __cxa_rethrow needs the exception to have been caught first + rewriter.create(loc, mth.getOpaqueType(), cmpValue.getDefiningOp()->getOperand(0)); + } + auto rethrowVal = rewriter.create(loc, mth.getNullType()); auto rethrowOp = rewriter.create(loc, rethrowVal); - if (tsContext->compileOptions.isWindows) + if (tsContext->compileOptions.isWindows || linuxTypedCatchChains) { - // see windowsNeedsParentEdge: the finally's cleanup pad comes before any - // enclosing try's; a rethrow with neither unwinds to the caller + // see windowsNeedsParentEdge / linuxTypedCatchChains: the finally's pad comes + // before any enclosing try's if (finallyHasOps) { tsContext->unwind[rethrowOp] = finallyBlock; diff --git a/tslang/lib/TypeScript/LowerToLLVM.cpp b/tslang/lib/TypeScript/LowerToLLVM.cpp index cd54fbae3..bbb391400 100644 --- a/tslang/lib/TypeScript/LowerToLLVM.cpp +++ b/tslang/lib/TypeScript/LowerToLLVM.cpp @@ -4923,10 +4923,11 @@ struct LandingPadOpLowering : public TsLlvmPattern TypeHelper th(rewriter); - auto catch1 = transformed.getCatches().front(); - + // every clause TryOpLowering attached, not just the first: a typed catch's catch-all + // clause comes second (linuxHasCleanups, linuxTypedCatchChains), and dropping it left + // the pad unentered for any other exception type mlir::Type llvmLandingPadTy = getTypeConverter()->convertType(landingPadOp.getType()); - rewriter.replaceOpWithNewOp(landingPadOp, llvmLandingPadTy, false, ValueRange{catch1}); + rewriter.replaceOpWithNewOp(landingPadOp, llvmLandingPadTy, false, transformed.getCatches()); return success(); } diff --git a/tslang/lib/TypeScript/MLIRGenStatements.cpp b/tslang/lib/TypeScript/MLIRGenStatements.cpp index 112e89d0b..6be91ff70 100644 --- a/tslang/lib/TypeScript/MLIRGenStatements.cpp +++ b/tslang/lib/TypeScript/MLIRGenStatements.cpp @@ -1075,6 +1075,10 @@ namespace mlirgen { auto location = loc(tryStatementAST); + // the catch binding belongs to its clause, not the enclosing block: without its own scope + // a second `catch (e)` in the same block was reported as a redeclaration of the first + SymbolTableScopeT catchVarScope(symbolTable); + std::string varName; auto catchClause = tryStatementAST->catchClause; if (catchClause) diff --git a/tslang/test/tester/tests/00try_catch_sequence_and_nesting.ts b/tslang/test/tester/tests/00try_catch_sequence_and_nesting.ts index 4f2fcbb15..c8a923846 100644 --- a/tslang/test/tester/tests/00try_catch_sequence_and_nesting.ts +++ b/tslang/test/tester/tests/00try_catch_sequence_and_nesting.ts @@ -18,6 +18,10 @@ // // - The same mismatch with a `finally` on the inner try: that finally was skipped. // +// The last two failed on Linux as well, for an Itanium reason: the inner landingpad listed only +// its own typed clause, so the personality never entered it for another type, and with nothing +// in the frame unwinding to the finally or the outer try the program called std::terminate. +// // The value a catch binds is read only for typed class catches, which 00catch_value.ts covers. class A {