Skip to content

Commit 9671ab3

Browse files
committed
Sema: Change DerivedConformance::derivesProtocolConformance() to take the conformance
1 parent b2d34ce commit 9671ab3

File tree

4 files changed

+32
-29
lines changed

4 files changed

+32
-29
lines changed

lib/Sema/AssociatedTypeInference.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2653,12 +2653,11 @@ AssociatedTypeInference::computeDerivedTypeWitness(
26532653
return std::make_pair(Type(), nullptr);
26542654

26552655
// Can we derive conformances for this protocol and adoptee?
2656-
NominalTypeDecl *derivingTypeDecl = dc->getSelfNominalTypeDecl();
2657-
if (!DerivedConformance::derivesProtocolConformance(dc, derivingTypeDecl,
2658-
proto))
2656+
if (!DerivedConformance::derivesProtocolConformance(conformance))
26592657
return std::make_pair(Type(), nullptr);
26602658

26612659
// Try to derive the type witness.
2660+
NominalTypeDecl *derivingTypeDecl = dc->getSelfNominalTypeDecl();
26622661
auto result = deriveTypeWitness(conformance, derivingTypeDecl, assocType);
26632662
if (!result.first)
26642663
return std::make_pair(Type(), nullptr);

lib/Sema/DerivedConformance/DerivedConformance.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,10 @@ Type DerivedConformance::getProtocolType() const {
7272
return Protocol->getDeclaredInterfaceType();
7373
}
7474

75-
bool DerivedConformance::derivesProtocolConformance(DeclContext *DC,
76-
NominalTypeDecl *Nominal,
77-
ProtocolDecl *Protocol) {
75+
bool DerivedConformance::derivesProtocolConformance(
76+
NormalProtocolConformance *Conformance) {
77+
auto *Protocol = Conformance->getProtocol();
78+
7879
const auto derivableKind = Protocol->getKnownDerivableProtocolKind();
7980
if (!derivableKind)
8081
return false;
@@ -85,6 +86,9 @@ bool DerivedConformance::derivesProtocolConformance(DeclContext *DC,
8586
return false;
8687
}
8788

89+
auto *DC = Conformance->getDeclContext();
90+
auto *Nominal = DC->getSelfNominalTypeDecl();
91+
8892
if (*derivableKind == KnownDerivableProtocolKind::Hashable) {
8993
// We can always complete a partial Hashable implementation, and we can
9094
// synthesize a full Hashable implementation for structs and enums with
@@ -296,10 +300,10 @@ ValueDecl *DerivedConformance::getDerivableRequirement(NominalTypeDecl *nominal,
296300

297301
auto conformance = lookupConformance(
298302
nominal->getDeclaredInterfaceType(), proto);
299-
if (conformance) {
300-
auto DC = conformance.getConcrete()->getDeclContext();
303+
if (conformance.isConcrete()) {
301304
// Check whether this nominal type derives conformances to the protocol.
302-
if (!DerivedConformance::derivesProtocolConformance(DC, nominal, proto))
305+
if (!DerivedConformance::derivesProtocolConformance(
306+
conformance.getConcrete()->getRootNormalConformance()))
303307
return nullptr;
304308
}
305309

lib/Sema/DerivedConformance/DerivedConformance.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,11 @@ class DerivedConformance {
9595
/// declarations for requirements of the protocol that are not satisfied by
9696
/// the type's explicit members.
9797
///
98-
/// \param nominal The nominal type for which we are determining whether to
99-
/// derive a witness.
100-
///
101-
/// \param protocol The protocol whose requirements are being derived.
98+
/// \param conformance The conformance.
10299
///
103100
/// \return True if the type can implicitly derive a conformance for the
104101
/// given protocol.
105-
static bool derivesProtocolConformance(DeclContext *DC,
106-
NominalTypeDecl *nominal,
107-
ProtocolDecl *protocol);
102+
static bool derivesProtocolConformance(NormalProtocolConformance *conformance);
108103

109104
/// Diagnose problems, if any, preventing automatic derivation of protocol
110105
/// requirements

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4731,9 +4731,8 @@ ResolveWitnessResult ConformanceChecker::resolveWitnessViaDerivation(
47314731

47324732
// Find the declaration that derives the protocol conformance.
47334733
NominalTypeDecl *derivingTypeDecl = nullptr;
4734-
auto *nominal = DC->getSelfNominalTypeDecl();
4735-
if (DerivedConformance::derivesProtocolConformance(DC, nominal, Proto))
4736-
derivingTypeDecl = nominal;
4734+
if (DerivedConformance::derivesProtocolConformance(Conformance))
4735+
derivingTypeDecl = DC->getSelfNominalTypeDecl();
47374736

47384737
if (!derivingTypeDecl) {
47394738
return ResolveWitnessResult::Missing;
@@ -6871,16 +6870,22 @@ void TypeChecker::checkConformancesInContext(IterableDeclContext *idc) {
68716870
// is implied for enums which already declare a raw type.
68726871
if (auto enumDecl = dyn_cast<EnumDecl>(existingDecl)) {
68736872
if (diag.Protocol->isSpecificProtocol(
6874-
KnownProtocolKind::RawRepresentable) &&
6875-
DerivedConformance::derivesProtocolConformance(dc, enumDecl,
6876-
diag.Protocol) &&
6877-
enumDecl->hasRawType() &&
6878-
enumDecl->getInherited().getStartLoc().isValid()) {
6879-
auto inheritedLoc = enumDecl->getInherited().getStartLoc();
6880-
Context.Diags.diagnose(
6881-
inheritedLoc, diag::enum_declares_rawrep_with_raw_type,
6882-
dc->getDeclaredInterfaceType(), enumDecl->getRawType());
6883-
continue;
6873+
KnownProtocolKind::RawRepresentable)) {
6874+
auto conformance = lookupConformance(
6875+
enumDecl->getDeclaredInterfaceType(), diag.Protocol);
6876+
if (!conformance.isConcrete())
6877+
continue;
6878+
6879+
if (DerivedConformance::derivesProtocolConformance(
6880+
conformance.getConcrete()->getRootNormalConformance()) &&
6881+
enumDecl->hasRawType() &&
6882+
enumDecl->getInherited().getStartLoc().isValid()) {
6883+
auto inheritedLoc = enumDecl->getInherited().getStartLoc();
6884+
Context.Diags.diagnose(
6885+
inheritedLoc, diag::enum_declares_rawrep_with_raw_type,
6886+
dc->getDeclaredInterfaceType(), enumDecl->getRawType());
6887+
continue;
6888+
}
68846889
}
68856890
}
68866891

0 commit comments

Comments
 (0)