Skip to content

Commit c506747

Browse files
authored
[Serialization] Drop inherited conformances on classes (swiftlang#23347)
These can be recreated if needed in a client library. To do this, I've added a new ConformanceLookupKind::NonInherited, which can also be used elsewhere in the project where we're already filtering out inherited conformances some other way. Note that this doesn't drop inherited conformances from the entire serialized interface, just from the list that a class explicitly declares. They still get referenced sometimes. rdar://problem/50541451 and possibly others
1 parent c648a71 commit c506747

File tree

7 files changed

+34
-11
lines changed

7 files changed

+34
-11
lines changed

include/swift/AST/DeclContext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ enum class ConformanceLookupKind : unsigned {
149149
All,
150150
/// Only the explicit conformance.
151151
OnlyExplicit,
152+
/// All conformances except for inherited ones.
153+
NonInherited,
152154
};
153155

154156
/// Describes a diagnostic for a conflict between two protocol

lib/AST/ConformanceLookupTable.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -989,10 +989,30 @@ void ConformanceLookupTable::lookupConformances(
989989
return true;
990990

991991
// If we are to filter out this result, do so now.
992-
if (lookupKind == ConformanceLookupKind::OnlyExplicit &&
993-
entry->getKind() != ConformanceEntryKind::Explicit &&
994-
entry->getKind() != ConformanceEntryKind::Synthesized)
995-
return false;
992+
switch (lookupKind) {
993+
case ConformanceLookupKind::OnlyExplicit:
994+
switch (entry->getKind()) {
995+
case ConformanceEntryKind::Explicit:
996+
case ConformanceEntryKind::Synthesized:
997+
break;
998+
case ConformanceEntryKind::Implied:
999+
case ConformanceEntryKind::Inherited:
1000+
return false;
1001+
}
1002+
break;
1003+
case ConformanceLookupKind::NonInherited:
1004+
switch (entry->getKind()) {
1005+
case ConformanceEntryKind::Explicit:
1006+
case ConformanceEntryKind::Synthesized:
1007+
case ConformanceEntryKind::Implied:
1008+
break;
1009+
case ConformanceEntryKind::Inherited:
1010+
return false;
1011+
}
1012+
break;
1013+
case ConformanceLookupKind::All:
1014+
break;
1015+
}
9961016

9971017
// Record the protocol.
9981018
if (protocols)

lib/SILGen/SILGenDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1436,7 +1436,7 @@ void SILGenModule::emitExternalDefinition(Decl *d) {
14361436
case DeclKind::Class: {
14371437
// Emit witness tables.
14381438
auto nom = cast<NominalTypeDecl>(d);
1439-
for (auto c : nom->getLocalConformances(ConformanceLookupKind::All,
1439+
for (auto c : nom->getLocalConformances(ConformanceLookupKind::NonInherited,
14401440
nullptr)) {
14411441
auto *proto = c->getProtocol();
14421442
if (Lowering::TypeConverter::protocolRequiresWitnessTable(proto) &&

lib/SILGen/SILGenType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ class SILGenType : public TypeMemberVisitor<SILGenType> {
976976
// Emit witness tables for conformances of concrete types. Protocol types
977977
// are existential and do not have witness tables.
978978
for (auto *conformance : theType->getLocalConformances(
979-
ConformanceLookupKind::All, nullptr)) {
979+
ConformanceLookupKind::NonInherited, nullptr)) {
980980
if (conformance->isComplete()) {
981981
if (auto *normal = dyn_cast<NormalProtocolConformance>(conformance))
982982
SGM.getWitnessTable(normal);

lib/Serialization/Serialization.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3217,7 +3217,7 @@ void Serializer::writeDecl(const Decl *D) {
32173217
auto contextID = addDeclContextRef(theClass->getDeclContext());
32183218

32193219
auto conformances = theClass->getLocalConformances(
3220-
ConformanceLookupKind::All, nullptr);
3220+
ConformanceLookupKind::NonInherited, nullptr);
32213221

32223222
SmallVector<TypeID, 4> inheritedTypes;
32233223
for (auto inherited : theClass->getInherited()) {

lib/TBDGen/TBDGen.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ void TBDGenVisitor::addBaseConformanceDescriptor(
114114
}
115115

116116
void TBDGenVisitor::addConformances(DeclContext *DC) {
117-
for (auto conformance : DC->getLocalConformances()) {
117+
for (auto conformance : DC->getLocalConformances(
118+
ConformanceLookupKind::NonInherited)) {
118119
auto protocol = conformance->getProtocol();
119120
auto needsWTable =
120121
Lowering::TypeConverter::protocolRequiresWitnessTable(protocol);
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-build-swift -emit-sil -emit-module-path %t/SwiftLib.swiftmodule -I %S/Inputs/conformance-removed/ %S/Inputs/conformance-removed/SwiftLib.swift -Xcc -DUSE_PROTO
3-
// RUN: not --crash %target-build-swift -typecheck -I %t -I %S/Inputs/custom-modules/ %s 2>&1 | %FileCheck %s
2+
// RUN: %target-build-swift -emit-module -emit-module-path %t/SwiftLib.swiftmodule -I %S/Inputs/conformance-removed/ %S/Inputs/conformance-removed/SwiftLib.swift -Xcc -DUSE_PROTO
3+
// RUN: not grep SomeProto %t/SwiftLib.swiftmodule
4+
// RUN: %target-build-swift -typecheck -I %t -I %S/Inputs/custom-modules/ %s
45

56
// REQUIRES: objc_interop
67

78
import SwiftLib
89
class Rdar28282310: Sub {}
9-
// CHECK: If you're seeing a crash here, check that your SDK and dependencies are at least as new as the versions used to build 'SwiftLib'

0 commit comments

Comments
 (0)