Skip to content

[BuilderTransform] Rework missing buildWithLimitedAvailability detection #63768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 6 additions & 22 deletions lib/Sema/CSSyntacticElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2104,7 +2104,7 @@ class ResultBuilderRewriter : public SyntacticElementSolutionApplication {
NullablePtr<Stmt> transformIf(IfStmt *ifStmt, TypeJoinExpr *join,
unsigned index) {
// FIXME: Turn this into a condition once warning is an error.
(void)diagnoseMissingBuildWithAvailability(ifStmt);
(void)diagnoseMissingBuildWithAvailability(ifStmt, join);

auto *joinVar = join->getVar();

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

SourceLoc loc = availabilityCond->getStartLoc();
Type bodyType;
if (availabilityCond->getAvailability()->isUnavailability()) {
BraceStmt *elseBody = nullptr;
// For #unavailable, we need to check the "else".
if (auto *innerIf = getAsStmt<IfStmt>(ifStmt->getElseStmt())) {
elseBody = castToStmt<BraceStmt>(innerIf->getThenStmt());
} else {
elseBody = castToStmt<BraceStmt>(ifStmt->getElseStmt());
}

Type elseBodyType =
solution.simplifyType(solution.getType(elseBody->getLastElement()));
bodyType = elseBodyType;
} else {
auto *thenBody = castToStmt<BraceStmt>(ifStmt->getThenStmt());
Type thenBodyType =
solution.simplifyType(solution.getType(thenBody->getLastElement()));
bodyType = thenBodyType;
}

auto builderType = solution.simplifyType(Transform.builderType);
// Since all of the branches of `if` statement have to join into the same
// type we can just use the type of the join variable here.
Type bodyType = solution.getResolvedType(join->getVar());

return bodyType.findIf([&](Type type) {
auto nominal = type->getAnyNominal();
Expand Down
7 changes: 5 additions & 2 deletions test/Constraints/result_builder_availability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ tuplify(true) { cond in
globalFuncAvailableOn10_52()
} else if false {
globalFuncAvailableOn10_52()
} else {
globalFuncAvailableOn10_52()
}
}
}
Expand Down Expand Up @@ -166,6 +164,11 @@ tuplifyWithAvailabilityErasure(true) { cond in
} else {
globalFuncAvailableOn10_52()
}

// https://github.com/apple/swift/issues/63764
if #unavailable(OSX 10.52) {
cond // Ok
}
}

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