Skip to content

Commit dbf1cb2

Browse files
authored
Merge pull request #42456 from xedin/ban-unavailable-cond-from-opaque
[TypeChecker] Disallow `if #unavailable` to produce different opaque …
2 parents 8b9248a + de4cfdf commit dbf1cb2

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/Sema/MiscDiagnostics.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2848,7 +2848,9 @@ class OpaqueUnderlyingTypeChecker : public ASTWalker {
28482848
// If this is `if #available` statement with no other dynamic
28492849
// conditions, let's check if it returns opaque type directly.
28502850
if (llvm::all_of(If->getCond(), [&](const auto &condition) {
2851-
return condition.getKind() == StmtConditionElement::CK_Availability;
2851+
return condition.getKind() ==
2852+
StmtConditionElement::CK_Availability &&
2853+
!condition.getAvailability()->isUnavailability();
28522854
})) {
28532855
// Check return statement directly with availability context set.
28542856
if (auto *Then = dyn_cast<BraceStmt>(If->getThenStmt())) {

test/type/opaque_with_conditional_availability.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,15 @@ func test_fail_without_universally_available_type() -> some P {
106106
return Y()
107107
}
108108
}
109+
110+
// Treat `if #unavailable` like regular conditions.
111+
func test_fail_with_unavailability_condition() -> some P {
112+
// expected-error@-1 {{function declares an opaque return type 'some P', but the return statements in its body do not have matching underlying types}}
113+
// expected-note@-2 {{add @available attribute to enclosing global function}}
114+
if #unavailable(macOS 12) {
115+
return X() // expected-error {{'X' is only available in macOS 11.0 or newer}} expected-note {{return statement has underlying type 'X'}}
116+
// expected-note@-1 {{add 'if #available' version check}}
117+
}
118+
119+
return Y() // expected-note {{return statement has underlying type 'Y'}}
120+
}

0 commit comments

Comments
 (0)