Skip to content

Commit 493150d

Browse files
committed
[CSFix] NFC: Add a common base class for all requirement failures
1 parent 4380ee6 commit 493150d

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

include/swift/Sema/CSFix.h

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -597,21 +597,36 @@ class RelabelArguments final
597597
}
598598
};
599599

600+
class RequirementFix : public ConstraintFix {
601+
protected:
602+
Type LHS;
603+
Type RHS;
604+
605+
RequirementFix(ConstraintSystem &cs, FixKind kind, Type lhs, Type rhs,
606+
ConstraintLocator *locator)
607+
: ConstraintFix(cs, kind, locator), LHS(lhs), RHS(rhs) {}
608+
609+
public:
610+
std::string getName() const override = 0;
611+
612+
Type lhsType() const { return LHS; }
613+
Type rhsType() const { return RHS; }
614+
615+
bool diagnose(const Solution &solution,
616+
bool asNote = false) const override = 0;
617+
};
618+
600619
/// Add a new conformance to the type to satisfy a requirement.
601-
class MissingConformance final : public ConstraintFix {
620+
class MissingConformance final : public RequirementFix {
602621
// Determines whether given protocol type comes from the context e.g.
603622
// assignment destination or argument comparison.
604623
bool IsContextual;
605624

606-
Type NonConformingType;
607-
// This could either be a protocol or protocol composition.
608-
Type ProtocolType;
609-
610625
MissingConformance(ConstraintSystem &cs, bool isContextual, Type type,
611626
Type protocolType, ConstraintLocator *locator)
612-
: ConstraintFix(cs, FixKind::AddConformance, locator),
613-
IsContextual(isContextual), NonConformingType(type),
614-
ProtocolType(protocolType) {}
627+
: RequirementFix(cs, FixKind::AddConformance, type, protocolType,
628+
locator),
629+
IsContextual(isContextual) {}
615630

616631
public:
617632
std::string getName() const override {
@@ -630,9 +645,9 @@ class MissingConformance final : public ConstraintFix {
630645
Type protocolType,
631646
ConstraintLocator *locator);
632647

633-
Type getNonConformingType() { return NonConformingType; }
648+
Type getNonConformingType() const { return LHS; }
634649

635-
Type getProtocolType() { return ProtocolType; }
650+
Type getProtocolType() const { return RHS; }
636651

637652
bool isEqual(const ConstraintFix *other) const;
638653

@@ -643,13 +658,11 @@ class MissingConformance final : public ConstraintFix {
643658

644659
/// Skip same-type generic requirement constraint,
645660
/// and assume that types are equal.
646-
class SkipSameTypeRequirement final : public ConstraintFix {
647-
Type LHS, RHS;
648-
661+
class SkipSameTypeRequirement final : public RequirementFix {
649662
SkipSameTypeRequirement(ConstraintSystem &cs, Type lhs, Type rhs,
650663
ConstraintLocator *locator)
651-
: ConstraintFix(cs, FixKind::SkipSameTypeRequirement, locator), LHS(lhs),
652-
RHS(rhs) {}
664+
: RequirementFix(cs, FixKind::SkipSameTypeRequirement, lhs, rhs,
665+
locator) {}
653666

654667
public:
655668
std::string getName() const override {
@@ -658,9 +671,6 @@ class SkipSameTypeRequirement final : public ConstraintFix {
658671

659672
bool diagnose(const Solution &solution, bool asNote = false) const override;
660673

661-
Type lhsType() { return LHS; }
662-
Type rhsType() { return RHS; }
663-
664674
static SkipSameTypeRequirement *create(ConstraintSystem &cs, Type lhs,
665675
Type rhs, ConstraintLocator *locator);
666676

@@ -673,13 +683,11 @@ class SkipSameTypeRequirement final : public ConstraintFix {
673683
///
674684
/// A same shape requirement can be inferred from a generic requirement,
675685
/// or from a pack expansion expression.
676-
class SkipSameShapeRequirement final : public ConstraintFix {
677-
Type LHS, RHS;
678-
686+
class SkipSameShapeRequirement final : public RequirementFix {
679687
SkipSameShapeRequirement(ConstraintSystem &cs, Type lhs, Type rhs,
680688
ConstraintLocator *locator)
681-
: ConstraintFix(cs, FixKind::SkipSameShapeRequirement, locator), LHS(lhs),
682-
RHS(rhs) {}
689+
: RequirementFix(cs, FixKind::SkipSameShapeRequirement, lhs, rhs,
690+
locator) {}
683691

684692
public:
685693
std::string getName() const override {
@@ -688,9 +696,6 @@ class SkipSameShapeRequirement final : public ConstraintFix {
688696

689697
bool diagnose(const Solution &solution, bool asNote = false) const override;
690698

691-
Type lhsType() { return LHS; }
692-
Type rhsType() { return RHS; }
693-
694699
static SkipSameShapeRequirement *create(ConstraintSystem &cs, Type lhs,
695700
Type rhs, ConstraintLocator *locator);
696701

@@ -701,13 +706,11 @@ class SkipSameShapeRequirement final : public ConstraintFix {
701706

702707
/// Skip 'superclass' generic requirement constraint,
703708
/// and assume that types are equal.
704-
class SkipSuperclassRequirement final : public ConstraintFix {
705-
Type LHS, RHS;
706-
709+
class SkipSuperclassRequirement final : public RequirementFix {
707710
SkipSuperclassRequirement(ConstraintSystem &cs, Type lhs, Type rhs,
708711
ConstraintLocator *locator)
709-
: ConstraintFix(cs, FixKind::SkipSuperclassRequirement, locator),
710-
LHS(lhs), RHS(rhs) {}
712+
: RequirementFix(cs, FixKind::SkipSuperclassRequirement, lhs, rhs,
713+
locator) {}
711714

712715
public:
713716
std::string getName() const override {

lib/Sema/CSFix.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,12 +400,13 @@ bool MissingConformance::diagnose(const Solution &solution, bool asNote) const {
400400
auto &cs = solution.getConstraintSystem();
401401
auto context = cs.getContextualTypePurpose(locator->getAnchor());
402402
MissingContextualConformanceFailure failure(
403-
solution, context, NonConformingType, ProtocolType, locator);
403+
solution, context, getNonConformingType(), getProtocolType(), locator);
404404
return failure.diagnose(asNote);
405405
}
406406

407407
MissingConformanceFailure failure(
408-
solution, locator, std::make_pair(NonConformingType, ProtocolType));
408+
solution, locator,
409+
std::make_pair(getNonConformingType(), getProtocolType()));
409410
return failure.diagnose(asNote);
410411
}
411412

@@ -433,8 +434,9 @@ bool MissingConformance::isEqual(const ConstraintFix *other) const {
433434
return false;
434435

435436
return IsContextual == conformanceFix->IsContextual &&
436-
NonConformingType->isEqual(conformanceFix->NonConformingType) &&
437-
ProtocolType->isEqual(conformanceFix->ProtocolType);
437+
getNonConformingType()->isEqual(
438+
conformanceFix->getNonConformingType()) &&
439+
getProtocolType()->isEqual(conformanceFix->getProtocolType());
438440
}
439441

440442
MissingConformance *

0 commit comments

Comments
 (0)