Skip to content

Commit 6611b25

Browse files
committed
AST: Assert if attempting to do conformance lookup table things on a protocol
Unlike structs, enums and classes, protocols should not have a conformance lookup table with normal conformances in it.
1 parent 0e156af commit 6611b25

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/AST/ConformanceLookupTable.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,13 +874,18 @@ ConformanceLookupTable::getConformance(NominalTypeDecl *nominal,
874874
entry->Conformance =
875875
ctx.getInheritedConformance(type, inheritedConformance.getConcrete());
876876
} else {
877-
// Create or find the normal conformance.
877+
// Protocols don't have conformance lookup tables. Self-conformance is
878+
// handled directly in lookupConformance().
879+
assert(!isa<ProtocolDecl>(conformingNominal));
880+
assert(!isa<ProtocolDecl>(conformingDC->getSelfNominalTypeDecl()));
878881
Type conformingType = conformingDC->getSelfInterfaceType();
882+
879883
SourceLoc conformanceLoc
880884
= conformingNominal == conformingDC
881885
? conformingNominal->getLoc()
882886
: cast<ExtensionDecl>(conformingDC)->getLoc();
883887

888+
// Create or find the normal conformance.
884889
auto normalConf =
885890
ctx.getConformance(conformingType, protocol, conformanceLoc,
886891
conformingDC, ProtocolConformanceState::Incomplete,

lib/AST/ProtocolConformance.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,9 @@ ProtocolConformance::getInheritedConformance(ProtocolDecl *protocol) const {
12241224

12251225
#pragma mark Protocol conformance lookup
12261226
void NominalTypeDecl::prepareConformanceTable() const {
1227+
assert(!isa<ProtocolDecl>(this) &&
1228+
"Protocols don't have a conformance table");
1229+
12271230
if (ConformanceTable)
12281231
return;
12291232

@@ -1292,6 +1295,10 @@ void NominalTypeDecl::prepareConformanceTable() const {
12921295
bool NominalTypeDecl::lookupConformance(
12931296
ProtocolDecl *protocol,
12941297
SmallVectorImpl<ProtocolConformance *> &conformances) const {
1298+
assert(!isa<ProtocolDecl>(this) &&
1299+
"Self-conformances are only found by the higher-level "
1300+
"ModuleDecl::lookupConformance() entry point");
1301+
12951302
prepareConformanceTable();
12961303
return ConformanceTable->lookupConformance(
12971304
const_cast<NominalTypeDecl *>(this),
@@ -1301,6 +1308,10 @@ bool NominalTypeDecl::lookupConformance(
13011308

13021309
SmallVector<ProtocolDecl *, 2>
13031310
NominalTypeDecl::getAllProtocols(bool sorted) const {
1311+
assert(!isa<ProtocolDecl>(this) &&
1312+
"For inherited protocols, use ProtocolDecl::inheritsFrom() or "
1313+
"ProtocolDecl::getInheritedProtocols()");
1314+
13041315
prepareConformanceTable();
13051316
SmallVector<ProtocolDecl *, 2> result;
13061317
ConformanceTable->getAllProtocols(const_cast<NominalTypeDecl *>(this), result,

0 commit comments

Comments
 (0)