Skip to content

[Diagnostics] Extract requirement declaration context retrieval into … #18752

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
Aug 16, 2018
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
45 changes: 28 additions & 17 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
//
//===----------------------------------------------------------------------===//

#include "ConstraintSystem.h"
#include "CSDiagnostics.h"
#include "ConstraintSystem.h"
#include "MiscDiagnostics.h"
#include "swift/AST/Expr.h"
#include "swift/AST/GenericSignature.h"
#include "swift/AST/Types.h"
#include "llvm/ADT/ArrayRef.h"

Expand Down Expand Up @@ -67,7 +68,7 @@ Type RequirementFailure::getOwnerType() const {
return getType(getAnchor())->getInOutObjectType()->getMetatypeInstanceType();
}

const Requirement &RequirementFailure::getRequirement() {
const Requirement &RequirementFailure::getRequirement() const {
auto *genericCtx = AffectedDecl->getAsGenericContext();
return genericCtx->getGenericRequirements()[getRequirementIndex()];
}
Expand Down Expand Up @@ -100,6 +101,27 @@ ValueDecl *RequirementFailure::getDeclRef() const {
return ownerType->getAnyGeneric();
}

const DeclContext *RequirementFailure::getRequirementDC() const {
const auto &req = getRequirement();
auto *DC = AffectedDecl->getDeclContext();

do {
auto *D = DC->getInnermostDeclarationDeclContext();
if (!D)
break;

if (auto *GC = D->getAsGenericContext()) {
auto *sig = GC->getGenericSignature();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like you just want DC->getGenericSignatureOfContext() and avoid all this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I can because getGenericSignatureOfContext is going to traverse parents until it finds one with generic signature and it would only return signature from it, but I wouldn't know which parent that was.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getGenericSignatureOfContext() does not traverse parents, but non-generic decls in generic context inherit the generic signature of their parent, so getGenericSignature() has the same behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know, I'll give it a try.

if (sig && sig->isRequirementSatisfied(req))
return DC;
}

DC = DC->getParent();
} while (DC);

return AffectedDecl->getAsGenericContext();
}

bool MissingConformanceFailure::diagnose() {
auto *anchor = getAnchor();
auto ownerType = getOwnerType();
Expand Down Expand Up @@ -175,21 +197,10 @@ bool MissingConformanceFailure::diagnose() {
} else {
const auto &req = getRequirement();
auto *genericCtx = AffectedDecl->getAsGenericContext();
const auto *reqDC = getRequirementDC();

std::function<const DeclContext *(Type)> getAffectedCtx =
[&](Type type) -> const DeclContext * {
if (auto *DMT = type->getAs<DependentMemberType>())
return getAffectedCtx(DMT->getBase());

if (auto *GPT = type->getAs<GenericTypeParamType>())
return GPT->getDecl()->getDeclContext();

return genericCtx;
};

const auto *affected = getAffectedCtx(req.getFirstType());
if (affected != genericCtx) {
auto *NTD = affected->getAsNominalTypeOrNominalTypeExtensionContext();
if (reqDC != genericCtx) {
auto *NTD = reqDC->getAsNominalTypeOrNominalTypeExtensionContext();
emitDiagnostic(anchor->getLoc(), diag::type_does_not_conform_in_decl_ref,
AffectedDecl->getDescriptiveKind(),
AffectedDecl->getFullName(), NTD->getDeclaredType(), type,
Expand All @@ -200,7 +211,7 @@ bool MissingConformanceFailure::diagnose() {
AffectedDecl->getFullName(), type, protocolType);
}

emitDiagnostic(affected->getAsDeclOrDeclExtensionContext(),
emitDiagnostic(reqDC->getAsDeclOrDeclExtensionContext(),
diag::where_type_does_not_conform_type, req.getFirstType(),
type);
}
Expand Down
7 changes: 6 additions & 1 deletion lib/Sema/CSDiagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,12 @@ class RequirementFailure : public FailureDiagnostic {
Type getOwnerType() const;

/// Generic requirement associated with the failure.
const Requirement &getRequirement();
const Requirement &getRequirement() const;

protected:
/// Retrieve declaration contextual where current
/// requirement has been introduced.
const DeclContext *getRequirementDC() const;

private:
/// Retrieve declaration associated with failing generic requirement.
Expand Down