Skip to content

[TypeChecker] Disallow if #unavailable to produce different opaque … #42456

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
Apr 20, 2022
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
4 changes: 3 additions & 1 deletion lib/Sema/MiscDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2848,7 +2848,9 @@ class OpaqueUnderlyingTypeChecker : public ASTWalker {
// If this is `if #available` statement with no other dynamic
// conditions, let's check if it returns opaque type directly.
if (llvm::all_of(If->getCond(), [&](const auto &condition) {
return condition.getKind() == StmtConditionElement::CK_Availability;
return condition.getKind() ==
StmtConditionElement::CK_Availability &&
!condition.getAvailability()->isUnavailability();
})) {
// Check return statement directly with availability context set.
if (auto *Then = dyn_cast<BraceStmt>(If->getThenStmt())) {
Expand Down
12 changes: 12 additions & 0 deletions test/type/opaque_with_conditional_availability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,15 @@ func test_fail_without_universally_available_type() -> some P {
return Y()
}
}

// Treat `if #unavailable` like regular conditions.
func test_fail_with_unavailability_condition() -> some P {
// expected-error@-1 {{function declares an opaque return type 'some P', but the return statements in its body do not have matching underlying types}}
// expected-note@-2 {{add @available attribute to enclosing global function}}
if #unavailable(macOS 12) {
return X() // expected-error {{'X' is only available in macOS 11.0 or newer}} expected-note {{return statement has underlying type 'X'}}
// expected-note@-1 {{add 'if #available' version check}}
}

return Y() // expected-note {{return statement has underlying type 'Y'}}
}