[AST] Choose an implied conformance source next to the type, if possible.

If a conformance to a protocol is implied by several other
conformances (i.e. protocol P: Equatable {} and protocol Q: Equatable {} and a
type declares conformance to both P and Q), we should choose a source that's in
the same file as the type, if we can, because automatic synthesis of
conformances (for Equatable, Hashable, etc.) only works in that case.

Fixes rdar://problem/41852654.
This commit is contained in:
Huon Wilson
2018-07-06 17:44:57 +10:00
parent d3dc80da6b
commit 80a74b4dfc
5 changed files with 44 additions and 0 deletions

View File

@@ -661,6 +661,19 @@ ConformanceLookupTable::Ordering ConformanceLookupTable::compareConformances(
: Ordering::After;
}
// If one of the conformances comes from the same file as the type
// declaration, pick that one; this is so that conformance synthesis works if
// there's any implied conformance in the same file as the type.
auto NTD =
lhs->getDeclContext()->getAsNominalTypeOrNominalTypeExtensionContext();
auto typeSF = NTD->getParentSourceFile();
if (typeSF) {
if (typeSF == lhsSF)
return Ordering::Before;
if (typeSF == rhsSF)
return Ordering::After;
}
// Otherwise, pick the earlier file unit.
auto lhsFileUnit
= dyn_cast<FileUnit>(lhs->getDeclContext()->getModuleScopeContext());