@@ -2111,7 +2111,7 @@ class ResultBuilderRewriter : public SyntacticElementSolutionApplication {
21112111 NullablePtr<Stmt> transformIf (IfStmt *ifStmt, TypeJoinExpr *join,
21122112 unsigned index) {
21132113 // FIXME: Turn this into a condition once warning is an error.
2114- (void )diagnoseMissingBuildWithAvailability (ifStmt);
2114+ (void )diagnoseMissingBuildWithAvailability (ifStmt, join );
21152115
21162116 auto *joinVar = join->getVar ();
21172117
@@ -2213,7 +2213,8 @@ class ResultBuilderRewriter : public SyntacticElementSolutionApplication {
22132213 // / have had the chance to adopt buildLimitedAvailability(), we'll upgrade
22142214 // / this warning to an error.
22152215 LLVM_NODISCARD
2216- bool diagnoseMissingBuildWithAvailability (IfStmt *ifStmt) {
2216+ bool diagnoseMissingBuildWithAvailability (IfStmt *ifStmt,
2217+ TypeJoinExpr *join) {
22172218 auto findAvailabilityCondition =
22182219 [](StmtCondition stmtCond) -> const StmtConditionElement * {
22192220 for (const auto &cond : stmtCond) {
@@ -2237,27 +2238,10 @@ class ResultBuilderRewriter : public SyntacticElementSolutionApplication {
22372238 return false ;
22382239
22392240 SourceLoc loc = availabilityCond->getStartLoc ();
2240- Type bodyType;
2241- if (availabilityCond->getAvailability ()->isUnavailability ()) {
2242- BraceStmt *elseBody = nullptr ;
2243- // For #unavailable, we need to check the "else".
2244- if (auto *innerIf = getAsStmt<IfStmt>(ifStmt->getElseStmt ())) {
2245- elseBody = castToStmt<BraceStmt>(innerIf->getThenStmt ());
2246- } else {
2247- elseBody = castToStmt<BraceStmt>(ifStmt->getElseStmt ());
2248- }
2249-
2250- Type elseBodyType =
2251- solution.simplifyType (solution.getType (elseBody->getLastElement ()));
2252- bodyType = elseBodyType;
2253- } else {
2254- auto *thenBody = castToStmt<BraceStmt>(ifStmt->getThenStmt ());
2255- Type thenBodyType =
2256- solution.simplifyType (solution.getType (thenBody->getLastElement ()));
2257- bodyType = thenBodyType;
2258- }
2259-
22602241 auto builderType = solution.simplifyType (Transform.builderType );
2242+ // Since all of the branches of `if` statement have to join into the same
2243+ // type we can just use the type of the join variable here.
2244+ Type bodyType = solution.getResolvedType (join->getVar ());
22612245
22622246 return bodyType.findIf ([&](Type type) {
22632247 auto nominal = type->getAnyNominal ();
0 commit comments