Skip to content

[Diagnostics] NFC: Store requirement types directly in ReqirementFailure #26370

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 26, 2019
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
4 changes: 2 additions & 2 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,8 @@ bool RequirementFailure::diagnoseAsError() {
const auto *reqDC = getRequirementDC();
auto *genericCtx = getGenericContext();

auto lhs = resolveType(getLHS());
auto rhs = resolveType(getRHS());
auto lhs = getLHS();
auto rhs = getRHS();

if (auto *OTD = dyn_cast<OpaqueTypeDecl>(AffectedDecl)) {
auto *namingDecl = OTD->getNamingDecl();
Expand Down
47 changes: 15 additions & 32 deletions lib/Sema/CSDiagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,17 @@ class RequirementFailure : public FailureDiagnostic {
/// to diagnose failures related to arguments.
const ApplyExpr *Apply = nullptr;

/// Types associated with requirement constraint this
/// failure originates from.
Type LHS, RHS;

public:
RequirementFailure(ConstraintSystem &cs, Expr *expr, RequirementKind kind,
ConstraintLocator *locator)
Type lhs, Type rhs, ConstraintLocator *locator)
: FailureDiagnostic(expr, cs, locator),
Conformance(getConformanceForConditionalReq(locator)),
Signature(getSignature(locator)), AffectedDecl(getDeclRef()) {
Signature(getSignature(locator)), AffectedDecl(getDeclRef()),
LHS(resolveType(lhs)), RHS(resolveType(rhs)) {
assert(locator);
assert(isConditional() || Signature);
assert(AffectedDecl);
Expand Down Expand Up @@ -267,8 +272,8 @@ class RequirementFailure : public FailureDiagnostic {
/// Generic requirement associated with the failure.
const Requirement &getRequirement() const;

virtual Type getLHS() const = 0;
virtual Type getRHS() const = 0;
Type getLHS() const { return LHS; }
Type getRHS() const { return RHS; }

bool diagnoseAsError() override;
bool diagnoseAsNote() override;
Expand Down Expand Up @@ -358,27 +363,15 @@ class RequirementFailure : public FailureDiagnostic {
/// foo(S())
/// ```
class MissingConformanceFailure final : public RequirementFailure {
Type NonConformingType;
Type ProtocolType;

public:
MissingConformanceFailure(Expr *expr, ConstraintSystem &cs,
ConstraintLocator *locator,
std::pair<Type, Type> conformance)
: RequirementFailure(cs, expr, RequirementKind::Conformance, locator),
NonConformingType(conformance.first), ProtocolType(conformance.second) {
}
: RequirementFailure(cs, expr, RequirementKind::Conformance,
conformance.first, conformance.second, locator) {}

bool diagnoseAsError() override;

private:
/// The type which was expected, by one of the generic requirements,
/// to conform to associated protocol.
Type getLHS() const override { return NonConformingType; }

/// The protocol generic requirement expected associated type to conform to.
Type getRHS() const override { return ProtocolType; }

protected:
DiagOnDecl getDiagnosticOnDecl() const override {
return diag::type_does_not_conform_decl_owner;
Expand Down Expand Up @@ -448,16 +441,11 @@ class GenericArgumentsMismatchFailure final : public FailureDiagnostic {
///
/// `S.T` is not the same type as `Int`, which is required by `foo`.
class SameTypeRequirementFailure final : public RequirementFailure {
Type LHS, RHS;

public:
SameTypeRequirementFailure(Expr *expr, ConstraintSystem &cs, Type lhs,
Type rhs, ConstraintLocator *locator)
: RequirementFailure(cs, expr, RequirementKind::SameType, locator),
LHS(lhs), RHS(rhs) {}

Type getLHS() const override { return LHS; }
Type getRHS() const override { return RHS; }
: RequirementFailure(cs, expr, RequirementKind::SameType, lhs, rhs,
locator) {}

protected:
DiagOnDecl getDiagnosticOnDecl() const override {
Expand Down Expand Up @@ -487,16 +475,11 @@ class SameTypeRequirementFailure final : public RequirementFailure {
///
/// `A` is not the superclass of `B`, which is required by `foo<T>`.
class SuperclassRequirementFailure final : public RequirementFailure {
Type LHS, RHS;

public:
SuperclassRequirementFailure(Expr *expr, ConstraintSystem &cs, Type lhs,
Type rhs, ConstraintLocator *locator)
: RequirementFailure(cs, expr, RequirementKind::Superclass, locator),
LHS(lhs), RHS(rhs) {}

Type getLHS() const override { return LHS; }
Type getRHS() const override { return RHS; }
: RequirementFailure(cs, expr, RequirementKind::Superclass, lhs, rhs,
locator) {}

protected:
DiagOnDecl getDiagnosticOnDecl() const override {
Expand Down