Skip to content

Commit ca618bb

Browse files
committed
Sema: Change DerivedConformance::derivesProtocolConformance() to take the conformance
1 parent c699553 commit ca618bb

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
@@ -299,10 +303,10 @@ ValueDecl *DerivedConformance::getDerivableRequirement(NominalTypeDecl *nominal,
299303

300304
auto conformance = lookupConformance(
301305
nominal->getDeclaredInterfaceType(), proto);
302-
if (conformance) {
303-
auto DC = conformance.getConcrete()->getDeclContext();
306+
if (conformance.isConcrete()) {
304307
// Check whether this nominal type derives conformances to the protocol.
305-
if (!DerivedConformance::derivesProtocolConformance(DC, nominal, proto))
308+
if (!DerivedConformance::derivesProtocolConformance(
309+
conformance.getConcrete()->getRootNormalConformance()))
306310
return nullptr;
307311
}
308312

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
@@ -4730,9 +4730,8 @@ ResolveWitnessResult ConformanceChecker::resolveWitnessViaDerivation(
47304730

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

47374736
if (!derivingTypeDecl) {
47384737
return ResolveWitnessResult::Missing;
@@ -6890,16 +6889,22 @@ void TypeChecker::checkConformancesInContext(IterableDeclContext *idc) {
68906889
// is implied for enums which already declare a raw type.
68916890
if (auto enumDecl = dyn_cast<EnumDecl>(existingDecl)) {
68926891
if (diag.Protocol->isSpecificProtocol(
6893-
KnownProtocolKind::RawRepresentable) &&
6894-
DerivedConformance::derivesProtocolConformance(dc, enumDecl,
6895-
diag.Protocol) &&
6896-
enumDecl->hasRawType() &&
6897-
enumDecl->getInherited().getStartLoc().isValid()) {
6898-
auto inheritedLoc = enumDecl->getInherited().getStartLoc();
6899-
Context.Diags.diagnose(
6900-
inheritedLoc, diag::enum_declares_rawrep_with_raw_type,
6901-
dc->getDeclaredInterfaceType(), enumDecl->getRawType());
6902-
continue;
6892+
KnownProtocolKind::RawRepresentable)) {
6893+
auto conformance = lookupConformance(
6894+
enumDecl->getDeclaredInterfaceType(), diag.Protocol);
6895+
if (!conformance.isConcrete())
6896+
continue;
6897+
6898+
if (DerivedConformance::derivesProtocolConformance(
6899+
conformance.getConcrete()->getRootNormalConformance()) &&
6900+
enumDecl->hasRawType() &&
6901+
enumDecl->getInherited().getStartLoc().isValid()) {
6902+
auto inheritedLoc = enumDecl->getInherited().getStartLoc();
6903+
Context.Diags.diagnose(
6904+
inheritedLoc, diag::enum_declares_rawrep_with_raw_type,
6905+
dc->getDeclaredInterfaceType(), enumDecl->getRawType());
6906+
continue;
6907+
}
69036908
}
69046909
}
69056910

0 commit comments

Comments
 (0)