Skip to content

Commit 8ed2a95

Browse files
authored
Merge pull request #36918 from slavapestov/error-existential-metatype
Sema: Handle Error self-conformance in TypeChecker::containsProtocol()
2 parents 07b568d + 508dc8c commit 8ed2a95

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4990,6 +4990,14 @@ TypeChecker::containsProtocol(Type T, ProtocolDecl *Proto, DeclContext *DC,
49904990
// Existential types don't need to conform, i.e., they only need to
49914991
// contain the protocol.
49924992
if (T->isExistentialType()) {
4993+
// Handle the special case of the Error protocol, which self-conforms
4994+
// *and* has a witness table.
4995+
if (T->isEqual(Proto->getDeclaredInterfaceType()) &&
4996+
Proto->requiresSelfConformanceWitnessTable()) {
4997+
auto &ctx = DC->getASTContext();
4998+
return ProtocolConformanceRef(ctx.getSelfConformance(Proto));
4999+
}
5000+
49935001
auto layout = T->getExistentialLayout();
49945002

49955003
// First, if we have a superclass constraint, the class may conform
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %target-swift-frontend -emit-ir %s
2+
3+
public struct X {
4+
public subscript(_ key: String, as type: Error.Type = Error.self) -> Error? {
5+
get {
6+
return nil
7+
}
8+
}
9+
}
10+
11+
let x = X()
12+
_ = x["hi"]

0 commit comments

Comments
 (0)