Skip to content

Commit c4ea02c

Browse files
committed
[BuilderTransform] Rework missing buildWithAvailability detection
Since all of the branches of an `if` statement are joined together and hence produce the same type, that type should be used to check whether `buildWithAvailability` is required but missing regardless of availability condition kind. Resolves: swiftlang#63764
1 parent 57ceb50 commit c4ea02c

File tree

2 files changed

+11
-24
lines changed

2 files changed

+11
-24
lines changed

lib/Sema/CSSyntacticElement.cpp

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,7 +2104,7 @@ class ResultBuilderRewriter : public SyntacticElementSolutionApplication {
21042104
NullablePtr<Stmt> transformIf(IfStmt *ifStmt, TypeJoinExpr *join,
21052105
unsigned index) {
21062106
// FIXME: Turn this into a condition once warning is an error.
2107-
(void)diagnoseMissingBuildWithAvailability(ifStmt);
2107+
(void)diagnoseMissingBuildWithAvailability(ifStmt, join);
21082108

21092109
auto *joinVar = join->getVar();
21102110

@@ -2206,7 +2206,8 @@ class ResultBuilderRewriter : public SyntacticElementSolutionApplication {
22062206
/// have had the chance to adopt buildLimitedAvailability(), we'll upgrade
22072207
/// this warning to an error.
22082208
LLVM_NODISCARD
2209-
bool diagnoseMissingBuildWithAvailability(IfStmt *ifStmt) {
2209+
bool diagnoseMissingBuildWithAvailability(IfStmt *ifStmt,
2210+
TypeJoinExpr *join) {
22102211
auto findAvailabilityCondition =
22112212
[](StmtCondition stmtCond) -> const StmtConditionElement * {
22122213
for (const auto &cond : stmtCond) {
@@ -2230,27 +2231,10 @@ class ResultBuilderRewriter : public SyntacticElementSolutionApplication {
22302231
return false;
22312232

22322233
SourceLoc loc = availabilityCond->getStartLoc();
2233-
Type bodyType;
2234-
if (availabilityCond->getAvailability()->isUnavailability()) {
2235-
BraceStmt *elseBody = nullptr;
2236-
// For #unavailable, we need to check the "else".
2237-
if (auto *innerIf = getAsStmt<IfStmt>(ifStmt->getElseStmt())) {
2238-
elseBody = castToStmt<BraceStmt>(innerIf->getThenStmt());
2239-
} else {
2240-
elseBody = castToStmt<BraceStmt>(ifStmt->getElseStmt());
2241-
}
2242-
2243-
Type elseBodyType =
2244-
solution.simplifyType(solution.getType(elseBody->getLastElement()));
2245-
bodyType = elseBodyType;
2246-
} else {
2247-
auto *thenBody = castToStmt<BraceStmt>(ifStmt->getThenStmt());
2248-
Type thenBodyType =
2249-
solution.simplifyType(solution.getType(thenBody->getLastElement()));
2250-
bodyType = thenBodyType;
2251-
}
2252-
22532234
auto builderType = solution.simplifyType(Transform.builderType);
2235+
// Since all of the branches of `if` statement have to join into the same
2236+
// type we can just use the type of the join variable here.
2237+
Type bodyType = solution.getResolvedType(join->getVar());
22542238

22552239
return bodyType.findIf([&](Type type) {
22562240
auto nominal = type->getAnyNominal();

test/Constraints/result_builder_availability.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ tuplify(true) { cond in
103103
globalFuncAvailableOn10_52()
104104
} else if false {
105105
globalFuncAvailableOn10_52()
106-
} else {
107-
globalFuncAvailableOn10_52()
108106
}
109107
}
110108
}
@@ -166,6 +164,11 @@ tuplifyWithAvailabilityErasure(true) { cond in
166164
} else {
167165
globalFuncAvailableOn10_52()
168166
}
167+
168+
// https://github.com/apple/swift/issues/63764
169+
if #unavailable(OSX 10.52) {
170+
cond // Ok
171+
}
169172
}
170173

171174
// rdar://97533700 – Make sure we can prefer an unavailable buildPartialBlock if

0 commit comments

Comments
 (0)