Skip to content

Commit 2bcb862

Browse files
committed
Do not subject protocol Self to accessibility diagnostics
It is currently impossible to declare a protocol as unavailable and still ship valid Swift code because while typechecking protocol Self, we attempt to do an accessibility check on its generic signature and find that, surprise surprise, the protocol is unavailable. Rather than plumb another parameter through Sema, this patch piggybacks on existing behavior of AllowPotentiallyUnavailableProtocol to disable the check for protocol self.
1 parent f98ff6a commit 2bcb862

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,13 +271,18 @@ void TypeChecker::checkInheritanceClause(Decl *decl,
271271
} else if (auto ext = dyn_cast<ExtensionDecl>(decl)) {
272272
DC = ext;
273273
options |= TR_GenericSignature | TR_InheritanceClause;
274-
} else if (isa<GenericTypeParamDecl>(decl)) {
274+
} else if (auto GP = dyn_cast<GenericTypeParamDecl>(decl)) {
275275
// For generic parameters, we want name lookup to look at just the
276276
// signature of the enclosing entity.
277277
DC = decl->getDeclContext();
278278
if (auto nominal = dyn_cast<NominalTypeDecl>(DC)) {
279279
DC = nominal;
280280
options |= TR_GenericSignature;
281+
// When looking up protocol 'Self' accessibility checks are disabled to
282+
// head off spurious unavailable diagnostics.
283+
if (isa<ProtocolDecl>(DC) && GP->isProtocolSelf()) {
284+
options |= TR_AllowUnavailable;
285+
}
281286
} else if (auto ext = dyn_cast<ExtensionDecl>(DC)) {
282287
DC = ext;
283288
options |= TR_GenericSignature;

test/attr/attr_availability.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ typealias int = Int // expected-note {{'int' has been explicitly marked unavaila
3131
@available(*, unavailable, renamed: "Float")
3232
typealias float = Float // expected-note {{'float' has been explicitly marked unavailable here}}
3333

34+
protocol MyNewerProtocol {}
35+
36+
@available(*, unavailable, renamed: "MyNewerProtocol")
37+
protocol MyOlderProtocol {} // expected-note {{'MyOlderProtocol' has been explicitly marked unavailable here}}
38+
39+
extension Int: MyOlderProtocol {} // expected-error {{'MyOlderProtocol' has been renamed to 'MyNewerProtocol'}}
40+
3441
struct MyCollection<Element> {
3542
@available(*, unavailable, renamed: "Element")
3643
typealias T = Element // expected-note 2{{'T' has been explicitly marked unavailable here}}

0 commit comments

Comments
 (0)