mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[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:
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user