Skip to content

Commit 7e19f05

Browse files
committed
[BuilderTransform] Inject buildOptional to top-level if else conditions
1 parent b1c27cb commit 7e19f05

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

lib/Sema/BuilderTransform.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,10 +1237,26 @@ class ResultBuilderTransform
12371237
auto *builderCall =
12381238
buildWrappedChainPayload(branchVarRef, i, numPayloads, isOptional);
12391239

1240+
auto isTopLevel = [&](Stmt *anchor) {
1241+
if (ifStmt->getThenStmt() == anchor)
1242+
return true;
1243+
1244+
// The situation is this:
1245+
//
1246+
// if <cond> {
1247+
// ...
1248+
// } else if <other-cond> {
1249+
// ...
1250+
// }
1251+
if (auto *innerIf = getAsStmt<IfStmt>(ifStmt->getElseStmt()))
1252+
return innerIf->getThenStmt() == anchor;
1253+
1254+
return ifStmt->getElseStmt() == anchor;
1255+
};
1256+
12401257
// The operand should have optional type if we had optional results,
12411258
// so we just need to call `buildIf` now, since we're at the top level.
1242-
if (isOptional && (ifStmt->getThenStmt() == anchor ||
1243-
ifStmt->getElseStmt() == anchor)) {
1259+
if (isOptional && isTopLevel(anchor)) {
12441260
builderCall = buildCallIfWanted(ifStmt->getEndLoc(),
12451261
builder.getBuildOptionalId(),
12461262
builderCall, /*argLabels=*/{});

0 commit comments

Comments
 (0)