Skip to content

Commit 4794b4b

Browse files
committed
[Runtime] Detect and log redundant subclass/superclass conformances.
When we rely on a protocol conformance, and the type in question has multiple conformances to that protocol in its inheritance chain, emit a runtime warning. It's quite tricky to cause this problem - you need a type in one dylib that is extended to conform to a protocol in another dylib, subclassed in another module and then some subclass has protocol conformance added as well. If they're in the same module, the compiler will give an error and prevent the problem completely. rdar://73364629
1 parent 6ec0bbd commit 4794b4b

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

stdlib/public/runtime/ProtocolConformance.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -906,10 +906,20 @@ swift_conformsToProtocolImpl(const Metadata *const type,
906906
const WitnessTable *foundWitness = nullptr;
907907
const Metadata *foundType = nullptr;
908908
for (auto searchType : iterateMaybeIncompleteSuperclasses(type)) {
909-
foundWitness = foundWitnesses.lookup(searchType);
910-
if (foundWitness) {
911-
foundType = searchType;
912-
break;
909+
const WitnessTable *witness = foundWitnesses.lookup(searchType);
910+
if (witness) {
911+
if (!foundType) {
912+
foundWitness = witness;
913+
foundType = searchType;
914+
} else {
915+
swift::warning(RuntimeErrorFlagNone,
916+
"Warning: '%s' conforms to protocol '%s', but it also "
917+
"inherits conformance from '%s'. Relying on a "
918+
"particular conformance is undefined behaviour.\n",
919+
foundType->getDescription()->Name.get(),
920+
protocol->Name.get(),
921+
searchType->getDescription()->Name.get());
922+
}
913923
}
914924
}
915925

0 commit comments

Comments
 (0)