Skip to content

Commit a860b02

Browse files
committed
Sema: Diagnose unavailability of associated type decls.
It should not be possible to mark an associated type declaration unavailable since doing so does not have an effect on how a conformance interacts with its associated types.
1 parent 23e9719 commit a860b02

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4982,6 +4982,11 @@ TypeChecker::diagnosticIfDeclCannotBeUnavailable(const Decl *D) {
49824982
return Diagnostic(diag::availability_decl_no_unavailable, D);
49834983
}
49844984

4985+
// The conformance checker does not know what to do with unavailable
4986+
// associated types.
4987+
if (auto *AT = dyn_cast<AssociatedTypeDecl>(D))
4988+
return Diagnostic(diag::availability_decl_no_unavailable, D);
4989+
49854990
if (auto *VD = dyn_cast<VarDecl>(D)) {
49864991
if (!VD->hasStorageOrWrapsStorage())
49874992
return std::nullopt;

test/decl/protocol/associated_type_availability.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,11 @@ extension ModelP2 {
8484
// expected-error@-1{{'B' is only available in macOS 14 or newer}}
8585
// expected-note@-2{{add @available attribute to enclosing instance method}}
8686
}
87+
88+
protocol P3 {
89+
@available(macOS, obsoleted: 12) // expected-error{{associated type cannot be marked unavailable with '@available'}}
90+
associatedtype A1
91+
92+
@available(macOS, obsoleted: 99) // FIXME: this should probably be diagnosed
93+
associatedtype A2
94+
}

test/decl/protocol/req/unavailable.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,11 @@ struct SelfT: Unavail { // expected-error {{type 'SelfT' does not conform to pro
9191
// expected-error@-1 {{unavailable instance method 'req()' was used to satisfy a requirement of protocol 'Unavail': write it yourself}}
9292
typealias T = SelfT
9393
}
94+
95+
protocol UnavailableAssoc {
96+
@available(*, unavailable) // expected-error {{associated type cannot be marked unavailable with '@available'}}
97+
associatedtype A1
98+
99+
@available(swift, introduced: 99) // expected-error {{associated type cannot be marked unavailable with '@available'}}
100+
associatedtype A2
101+
}

0 commit comments

Comments
 (0)