Skip to content

Commit 0f72faf

Browse files
committed
[CSFix] NFC: Add a common base class for all requirement failures
(cherry picked from commit edb6ef0)
1 parent 9ab363b commit 0f72faf

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
@@ -587,21 +587,36 @@ class RelabelArguments final
587587
}
588588
};
589589

590+
class RequirementFix : public ConstraintFix {
591+
protected:
592+
Type LHS;
593+
Type RHS;
594+
595+
RequirementFix(ConstraintSystem &cs, FixKind kind, Type lhs, Type rhs,
596+
ConstraintLocator *locator)
597+
: ConstraintFix(cs, kind, locator), LHS(lhs), RHS(rhs) {}
598+
599+
public:
600+
std::string getName() const override = 0;
601+
602+
Type lhsType() const { return LHS; }
603+
Type rhsType() const { return RHS; }
604+
605+
bool diagnose(const Solution &solution,
606+
bool asNote = false) const override = 0;
607+
};
608+
590609
/// Add a new conformance to the type to satisfy a requirement.
591-
class MissingConformance final : public ConstraintFix {
610+
class MissingConformance final : public RequirementFix {
592611
// Determines whether given protocol type comes from the context e.g.
593612
// assignment destination or argument comparison.
594613
bool IsContextual;
595614

596-
Type NonConformingType;
597-
// This could either be a protocol or protocol composition.
598-
Type ProtocolType;
599-
600615
MissingConformance(ConstraintSystem &cs, bool isContextual, Type type,
601616
Type protocolType, ConstraintLocator *locator)
602-
: ConstraintFix(cs, FixKind::AddConformance, locator),
603-
IsContextual(isContextual), NonConformingType(type),
604-
ProtocolType(protocolType) {}
617+
: RequirementFix(cs, FixKind::AddConformance, type, protocolType,
618+
locator),
619+
IsContextual(isContextual) {}
605620

606621
public:
607622
std::string getName() const override {
@@ -620,9 +635,9 @@ class MissingConformance final : public ConstraintFix {
620635
Type protocolType,
621636
ConstraintLocator *locator);
622637

623-
Type getNonConformingType() { return NonConformingType; }
638+
Type getNonConformingType() const { return LHS; }
624639

625-
Type getProtocolType() { return ProtocolType; }
640+
Type getProtocolType() const { return RHS; }
626641

627642
bool isEqual(const ConstraintFix *other) const;
628643

@@ -633,13 +648,11 @@ class MissingConformance final : public ConstraintFix {
633648

634649
/// Skip same-type generic requirement constraint,
635650
/// and assume that types are equal.
636-
class SkipSameTypeRequirement final : public ConstraintFix {
637-
Type LHS, RHS;
638-
651+
class SkipSameTypeRequirement final : public RequirementFix {
639652
SkipSameTypeRequirement(ConstraintSystem &cs, Type lhs, Type rhs,
640653
ConstraintLocator *locator)
641-
: ConstraintFix(cs, FixKind::SkipSameTypeRequirement, locator), LHS(lhs),
642-
RHS(rhs) {}
654+
: RequirementFix(cs, FixKind::SkipSameTypeRequirement, lhs, rhs,
655+
locator) {}
643656

644657
public:
645658
std::string getName() const override {
@@ -648,9 +661,6 @@ class SkipSameTypeRequirement final : public ConstraintFix {
648661

649662
bool diagnose(const Solution &solution, bool asNote = false) const override;
650663

651-
Type lhsType() { return LHS; }
652-
Type rhsType() { return RHS; }
653-
654664
static SkipSameTypeRequirement *create(ConstraintSystem &cs, Type lhs,
655665
Type rhs, ConstraintLocator *locator);
656666

@@ -661,13 +671,11 @@ class SkipSameTypeRequirement final : public ConstraintFix {
661671

662672
/// Skip same-shape generic requirement constraint,
663673
/// and assume that types are equal.
664-
class SkipSameShapeRequirement final : public ConstraintFix {
665-
Type LHS, RHS;
666-
674+
class SkipSameShapeRequirement final : public RequirementFix {
667675
SkipSameShapeRequirement(ConstraintSystem &cs, Type lhs, Type rhs,
668676
ConstraintLocator *locator)
669-
: ConstraintFix(cs, FixKind::SkipSameShapeRequirement, locator), LHS(lhs),
670-
RHS(rhs) {}
677+
: RequirementFix(cs, FixKind::SkipSameShapeRequirement, lhs, rhs,
678+
locator) {}
671679

672680
public:
673681
std::string getName() const override {
@@ -676,9 +684,6 @@ class SkipSameShapeRequirement final : public ConstraintFix {
676684

677685
bool diagnose(const Solution &solution, bool asNote = false) const override;
678686

679-
Type lhsType() { return LHS; }
680-
Type rhsType() { return RHS; }
681-
682687
static SkipSameShapeRequirement *create(ConstraintSystem &cs, Type lhs,
683688
Type rhs, ConstraintLocator *locator);
684689

@@ -689,13 +694,11 @@ class SkipSameShapeRequirement final : public ConstraintFix {
689694

690695
/// Skip 'superclass' generic requirement constraint,
691696
/// and assume that types are equal.
692-
class SkipSuperclassRequirement final : public ConstraintFix {
693-
Type LHS, RHS;
694-
697+
class SkipSuperclassRequirement final : public RequirementFix {
695698
SkipSuperclassRequirement(ConstraintSystem &cs, Type lhs, Type rhs,
696699
ConstraintLocator *locator)
697-
: ConstraintFix(cs, FixKind::SkipSuperclassRequirement, locator),
698-
LHS(lhs), RHS(rhs) {}
700+
: RequirementFix(cs, FixKind::SkipSuperclassRequirement, lhs, rhs,
701+
locator) {}
699702

700703
public:
701704
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)