From 8024907c650c6b194cfc697efe374a54544deb11 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Wed, 23 Sep 2026 17:38:13 +0100 Subject: [PATCH] Leave no half-built ts.If behind when a conditionalValue branch fails MLIRCodeLogicHelper::conditionalValue returned failure with its IfOp still in the IR - an empty or none-typed region - and the insertion point still inside it. Whenever the caller carried on, the failure turned into bad IR further on: "'ts.If' op expects a non-empty block" in place of the branch's own error (seen as `x instanceof C` of a -shared class before #351). It now erases the IfOp and puts the insertion point back before returning. Co-Authored-By: Claude Opus 5.5 --- .../TypeScript/MLIRLogic/MLIRCodeLogic.h | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/tslang/include/TypeScript/MLIRLogic/MLIRCodeLogic.h b/tslang/include/TypeScript/MLIRLogic/MLIRCodeLogic.h index 1fde7013f..198505eb7 100644 --- a/tslang/include/TypeScript/MLIRLogic/MLIRCodeLogic.h +++ b/tslang/include/TypeScript/MLIRLogic/MLIRCodeLogic.h @@ -297,11 +297,25 @@ class MLIRCodeLogicHelper // type will be set later auto ifOp = builder.create(location, builder.getNoneType(), condValue, true); + // A branch that fails leaves nothing behind: not the half-built if (an empty region, or + // a none-typed result), nor an insertion point inside it. Returning with both in place + // turned the failure into bad IR further on - "'ts.If' op expects a non-empty block", or + // code that silently took the other branch - whenever the caller carried on. + auto abandon = [&]() { + builder.setInsertionPoint(ifOp); + ifOp.erase(); + return mlir::failure(); + }; + builder.setInsertionPointToStart(&ifOp.getThenRegion().front()); // value if true auto trueResult = trueValue(); - EXIT_IF_FAILED_OR_NO_VALUE(trueResult) + if (trueResult.failed_or_no_value()) + { + return abandon(); + } + ifOp.getResults().front().setType(trueResult.value.getType()); builder.create(location, mlir::ValueRange{trueResult}); @@ -310,7 +324,11 @@ class MLIRCodeLogicHelper // value if false auto falseResult = falseValue(trueResult.value.getType()); - EXIT_IF_FAILED_OR_NO_VALUE(falseResult) + if (falseResult.failed_or_no_value()) + { + return abandon(); + } + builder.create(location, mlir::ValueRange{falseResult}); builder.setInsertionPointAfter(ifOp);