Skip to content

[NFC] Refactor DerivedConformances code to reduce diagnostics code duplication #32884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 3 additions & 21 deletions lib/Sema/DerivedConformanceComparable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,28 +343,10 @@ void DerivedConformance::tryDiagnoseFailedComparableDerivation(
DeclContext *DC, NominalTypeDecl *nominal) {
auto &ctx = DC->getASTContext();
auto *comparableProto = ctx.getProtocol(KnownProtocolKind::Comparable);
if (!isa<EnumDecl>(nominal)) {
auto contextDecl = DC->getAsDecl();
ctx.Diags.diagnose(
contextDecl->getLoc(), diag::automatic_protocol_synthesis_unsupported,
comparableProto->getName().str(), isa<StructDecl>(contextDecl));
}

if (auto *enumDecl = dyn_cast<EnumDecl>(nominal)) {
auto nonconformingAssociatedTypes =
DerivedConformance::associatedValuesNotConformingToProtocol(
DC, enumDecl, comparableProto);
for (auto *typeToDiagnose : nonconformingAssociatedTypes) {
SourceLoc reprLoc;
if (auto *repr = typeToDiagnose->getTypeRepr())
reprLoc = repr->getStartLoc();
ctx.Diags.diagnose(
reprLoc, diag::missing_member_type_conformance_prevents_synthesis, 0,
typeToDiagnose->getInterfaceType(),
comparableProto->getDeclaredType(),
nominal->getDeclaredInterfaceType());
}
diagnoseAnyNonConformingMemberTypes(DC, nominal, comparableProto);
diagnoseIfSynthesisUnsupportedForDecl(nominal, comparableProto);

if (auto enumDecl = dyn_cast<EnumDecl>(nominal)) {
if (enumDecl->hasRawType() && !enumDecl->getRawType()->is<ErrorType>()) {
auto rawType = enumDecl->getRawType();
auto rawTypeLoc = enumDecl->getInherited()[0].getSourceRange().Start;
Expand Down
98 changes: 9 additions & 89 deletions lib/Sema/DerivedConformanceEquatableHashable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,49 +31,6 @@

using namespace swift;

enum NonconformingMemberKind {
AssociatedValue,
StoredProperty
};

/// Returns the VarDecl of each stored property in the given struct whose type
/// does not conform to a protocol.
/// \p theStruct The struct whose stored properties should be checked.
/// \p protocol The protocol being requested.
/// \return The VarDecl of each stored property whose type does not conform.
static SmallVector<VarDecl *, 3>
storedPropertiesNotConformingToProtocol(DeclContext *DC, StructDecl *theStruct,
ProtocolDecl *protocol) {
auto storedProperties = theStruct->getStoredProperties();
SmallVector<VarDecl *, 3> nonconformingProperties;
for (auto propertyDecl : storedProperties) {
if (!propertyDecl->isUserAccessible())
continue;

auto type = propertyDecl->getValueInterfaceType();
if (!type)
nonconformingProperties.push_back(propertyDecl);

if (!TypeChecker::conformsToProtocol(DC->mapTypeIntoContext(type), protocol,
DC)) {
nonconformingProperties.push_back(propertyDecl);
}
}
return nonconformingProperties;
}

/// Returns true if every stored property in the given struct conforms to the
/// protocol (or, vacuously, if it has no stored properties).
/// \p theStruct The struct whose stored properties should be checked.
/// \p protocol The protocol being requested.
/// \return True if all stored properties of the struct conform.
static bool allStoredPropertiesConformToProtocol(DeclContext *DC,
StructDecl *theStruct,
ProtocolDecl *protocol) {
return storedPropertiesNotConformingToProtocol(DC, theStruct, protocol)
.empty();
}

/// Common preconditions for Equatable and Hashable.
static bool canDeriveConformance(DeclContext *DC,
NominalTypeDecl *target,
Expand All @@ -86,55 +43,16 @@ static bool canDeriveConformance(DeclContext *DC,
}

if (auto structDecl = dyn_cast<StructDecl>(target)) {
// All stored properties of the struct must conform to the protocol.
return allStoredPropertiesConformToProtocol(DC, structDecl, protocol);
// All stored properties of the struct must conform to the protocol. If
// there are no stored properties, we will vaccously return true.
return DerivedConformance::storedPropertiesNotConformingToProtocol(
DC, structDecl, protocol)
.empty();
}

return false;
}

/// Diagnose failed conformance synthesis caused by a member type not conforming
/// to the same protocol
void diagnoseFailedDerivation(DeclContext *DC, NominalTypeDecl *nominal,
ProtocolDecl *protocol) {
ASTContext &ctx = DC->getASTContext();

if (auto *enumDecl = dyn_cast<EnumDecl>(nominal)) {
auto nonconformingAssociatedTypes =
DerivedConformance::associatedValuesNotConformingToProtocol(DC, enumDecl, protocol);
for (auto *typeToDiagnose : nonconformingAssociatedTypes) {
SourceLoc reprLoc;
if (auto *repr = typeToDiagnose->getTypeRepr())
reprLoc = repr->getStartLoc();
ctx.Diags.diagnose(
reprLoc,
diag::missing_member_type_conformance_prevents_synthesis,
NonconformingMemberKind::AssociatedValue,
typeToDiagnose->getInterfaceType(), protocol->getDeclaredType(),
nominal->getDeclaredInterfaceType());
}
}

if (auto *structDecl = dyn_cast<StructDecl>(nominal)) {
auto nonconformingStoredProperties =
storedPropertiesNotConformingToProtocol(DC, structDecl, protocol);
for (auto *propertyToDiagnose : nonconformingStoredProperties) {
ctx.Diags.diagnose(
propertyToDiagnose->getLoc(),
diag::missing_member_type_conformance_prevents_synthesis,
NonconformingMemberKind::StoredProperty,
propertyToDiagnose->getInterfaceType(), protocol->getDeclaredType(),
nominal->getDeclaredInterfaceType());
}
}

if (auto *classDecl = dyn_cast<ClassDecl>(nominal)) {
ctx.Diags.diagnose(classDecl->getLoc(),
diag::automatic_protocol_synthesis_unsupported,
protocol->getName().str(), 0);
}
}

static std::pair<BraceStmt *, bool>
deriveBodyEquatable_enum_uninhabited_eq(AbstractFunctionDecl *eqDecl, void *) {
auto parentDC = eqDecl->getDeclContext();
Expand Down Expand Up @@ -565,7 +483,8 @@ void DerivedConformance::tryDiagnoseFailedEquatableDerivation(
DeclContext *DC, NominalTypeDecl *nominal) {
ASTContext &ctx = DC->getASTContext();
auto *equatableProto = ctx.getProtocol(KnownProtocolKind::Equatable);
diagnoseFailedDerivation(DC, nominal, equatableProto);
diagnoseAnyNonConformingMemberTypes(DC, nominal, equatableProto);
diagnoseIfSynthesisUnsupportedForDecl(nominal, equatableProto);
}

/// Returns a new \c CallExpr representing
Expand Down Expand Up @@ -1048,7 +967,8 @@ void DerivedConformance::tryDiagnoseFailedHashableDerivation(
DeclContext *DC, NominalTypeDecl *nominal) {
ASTContext &ctx = DC->getASTContext();
auto *hashableProto = ctx.getProtocol(KnownProtocolKind::Hashable);
diagnoseFailedDerivation(DC, nominal, hashableProto);
diagnoseAnyNonConformingMemberTypes(DC, nominal, hashableProto);
diagnoseIfSynthesisUnsupportedForDecl(nominal, hashableProto);
}

ValueDecl *DerivedConformance::deriveHashable(ValueDecl *requirement) {
Expand Down
79 changes: 77 additions & 2 deletions lib/Sema/DerivedConformances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

using namespace swift;

enum NonconformingMemberKind { AssociatedValue, StoredProperty };

DerivedConformance::DerivedConformance(ASTContext &ctx, Decl *conformanceDecl,
NominalTypeDecl *nominal,
ProtocolDecl *protocol)
Expand Down Expand Up @@ -159,15 +161,34 @@ bool DerivedConformance::derivesProtocolConformance(DeclContext *DC,
return false;
}

SmallVector<VarDecl *, 3>
DerivedConformance::storedPropertiesNotConformingToProtocol(
DeclContext *DC, StructDecl *theStruct, ProtocolDecl *protocol) {
auto storedProperties = theStruct->getStoredProperties();
SmallVector<VarDecl *, 3> nonconformingProperties;
for (auto propertyDecl : storedProperties) {
if (!propertyDecl->isUserAccessible())
continue;

auto type = propertyDecl->getValueInterfaceType();
if (!type)
nonconformingProperties.push_back(propertyDecl);

if (!TypeChecker::conformsToProtocol(DC->mapTypeIntoContext(type), protocol,
DC)) {
nonconformingProperties.push_back(propertyDecl);
}
}
return nonconformingProperties;
}

void DerivedConformance::tryDiagnoseFailedDerivation(DeclContext *DC,
NominalTypeDecl *nominal,
ProtocolDecl *protocol) {
auto knownProtocol = protocol->getKnownProtocolKind();
if (!knownProtocol)
return;

// Comparable on eligible type kinds should never fail

if (*knownProtocol == KnownProtocolKind::Equatable) {
tryDiagnoseFailedEquatableDerivation(DC, nominal);
}
Expand All @@ -181,6 +202,60 @@ void DerivedConformance::tryDiagnoseFailedDerivation(DeclContext *DC,
}
}

void DerivedConformance::diagnoseAnyNonConformingMemberTypes(
DeclContext *DC, NominalTypeDecl *nominal, ProtocolDecl *protocol) {
ASTContext &ctx = DC->getASTContext();

if (auto *enumDecl = dyn_cast<EnumDecl>(nominal)) {
auto nonconformingAssociatedTypes =
associatedValuesNotConformingToProtocol(DC, enumDecl, protocol);
for (auto *typeToDiagnose : nonconformingAssociatedTypes) {
SourceLoc reprLoc;
if (auto *repr = typeToDiagnose->getTypeRepr())
reprLoc = repr->getStartLoc();
ctx.Diags.diagnose(
reprLoc, diag::missing_member_type_conformance_prevents_synthesis,
NonconformingMemberKind::AssociatedValue,
typeToDiagnose->getInterfaceType(), protocol->getDeclaredType(),
nominal->getDeclaredInterfaceType());
}
}

if (auto *structDecl = dyn_cast<StructDecl>(nominal)) {
auto nonconformingStoredProperties =
storedPropertiesNotConformingToProtocol(DC, structDecl, protocol);
for (auto *propertyToDiagnose : nonconformingStoredProperties) {
ctx.Diags.diagnose(
propertyToDiagnose->getLoc(),
diag::missing_member_type_conformance_prevents_synthesis,
NonconformingMemberKind::StoredProperty,
propertyToDiagnose->getInterfaceType(), protocol->getDeclaredType(),
nominal->getDeclaredInterfaceType());
}
}
}

void DerivedConformance::diagnoseIfSynthesisUnsupportedForDecl(
NominalTypeDecl *nominal, ProtocolDecl *protocol) {
auto shouldDiagnose = false;

if (protocol->isSpecificProtocol(KnownProtocolKind::Equatable) ||
protocol->isSpecificProtocol(KnownProtocolKind::Hashable)) {
shouldDiagnose = isa<ClassDecl>(nominal);
}

if (protocol->isSpecificProtocol(KnownProtocolKind::Comparable)) {
shouldDiagnose = !isa<EnumDecl>(nominal);
}

if (shouldDiagnose) {
auto &ctx = nominal->getASTContext();
ctx.Diags.diagnose(nominal->getLoc(),
diag::automatic_protocol_synthesis_unsupported,
protocol->getName().str(), isa<StructDecl>(nominal));
}
}

ValueDecl *DerivedConformance::getDerivableRequirement(NominalTypeDecl *nominal,
ValueDecl *requirement) {
// Note: whenever you update this function, also update
Expand Down
32 changes: 32 additions & 0 deletions lib/Sema/DerivedConformances.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ class DerivedConformance {
/// Get the declared type of the protocol that this is conformance is for.
Type getProtocolType() const;

/// Returns the VarDecl of each stored property in the given struct whose type
/// does not conform to a protocol.
/// \p theStruct The struct whose stored properties should be checked.
/// \p protocol The protocol being requested.
/// \return The VarDecl of each stored property whose type does not conform.
static SmallVector<VarDecl *, 3> storedPropertiesNotConformingToProtocol(
DeclContext *DC, StructDecl *theStruct, ProtocolDecl *protocol);

/// True if the type can implicitly derive a conformance for the given
/// protocol.
///
Expand Down Expand Up @@ -80,6 +88,30 @@ class DerivedConformance {
NominalTypeDecl *nominal,
ProtocolDecl *protocol);

/// Diagnose any members which do not conform to the protocol for which
/// we were trying to synthesize the conformance to.
///
/// \param nominal The nominal type for which we would like to diagnose
/// derivation failures
///
/// \param protocol The protocol with requirements we would like to diagnose
/// derivation failures for
static void diagnoseAnyNonConformingMemberTypes(DeclContext *DC,
NominalTypeDecl *nominal,
ProtocolDecl *protocol);

/// Diagnose the declaration for which we were trying to synthesize
/// the conformance for, if the synthesis is not supported for that
/// declaration.
///
/// \param nominal The nominal type for which we would like to diagnose
/// derivation failures
///
/// \param protocol The protocol with requirements we would like to diagnose
/// derivation failures for
static void diagnoseIfSynthesisUnsupportedForDecl(NominalTypeDecl *nominal,
ProtocolDecl *protocol);

/// Determine the derivable requirement that would satisfy the given
/// requirement, if there is one.
///
Expand Down