Skip to content

Commit 133e85b

Browse files
committed
[CSFix] NFC: Extract common base class for all invalid member refs
1 parent abc9b19 commit 133e85b

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

lib/Sema/CSFix.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -342,14 +342,16 @@ AllowMemberRefOnExistential::create(ConstraintSystem &cs, Type baseType,
342342
}
343343

344344
bool AllowMemberRefOnExistential::diagnose(Expr *root, bool asNote) const {
345-
auto failure = InvalidMemberRefOnExistential(root, getConstraintSystem(),
346-
BaseType, Name, getLocator());
345+
auto failure =
346+
InvalidMemberRefOnExistential(root, getConstraintSystem(), getBaseType(),
347+
getMemberName(), getLocator());
347348
return failure.diagnose(asNote);
348349
}
349350

350351
bool AllowTypeOrInstanceMember::diagnose(Expr *root, bool asNote) const {
351352
auto failure = AllowTypeOrInstanceMemberFailure(
352-
root, getConstraintSystem(), BaseType, Member, UsedName, getLocator());
353+
root, getConstraintSystem(), getBaseType(), getMember(), getMemberName(),
354+
getLocator());
353355
return failure.diagnose(asNote);
354356
}
355357

@@ -472,15 +474,17 @@ MoveOutOfOrderArgument *MoveOutOfOrderArgument::create(
472474
}
473475

474476
bool AllowInaccessibleMember::diagnose(Expr *root, bool asNote) const {
475-
InaccessibleMemberFailure failure(root, getConstraintSystem(), Member,
477+
InaccessibleMemberFailure failure(root, getConstraintSystem(), getMember(),
476478
getLocator());
477479
return failure.diagnose(asNote);
478480
}
479481

480482
AllowInaccessibleMember *
481-
AllowInaccessibleMember::create(ConstraintSystem &cs, ValueDecl *member,
483+
AllowInaccessibleMember::create(ConstraintSystem &cs, Type baseType,
484+
ValueDecl *member, DeclName name,
482485
ConstraintLocator *locator) {
483-
return new (cs.getAllocator()) AllowInaccessibleMember(cs, member, locator);
486+
return new (cs.getAllocator())
487+
AllowInaccessibleMember(cs, baseType, member, name, locator);
484488
}
485489

486490
bool AllowAnyObjectKeyPathRoot::diagnose(Expr *root, bool asNote) const {

lib/Sema/CSFix.h

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -728,20 +728,37 @@ class DefineMemberBasedOnUse final : public ConstraintFix {
728728
ConstraintLocator *locator);
729729
};
730730

731-
class AllowMemberRefOnExistential final : public ConstraintFix {
731+
class AllowInvalidMemberRef : public ConstraintFix {
732732
Type BaseType;
733+
ValueDecl *Member;
733734
DeclName Name;
734735

736+
protected:
737+
AllowInvalidMemberRef(ConstraintSystem &cs, FixKind kind, Type baseType,
738+
ValueDecl *member, DeclName name,
739+
ConstraintLocator *locator)
740+
: ConstraintFix(cs, kind, locator), BaseType(baseType), Member(member),
741+
Name(name) {}
742+
743+
public:
744+
Type getBaseType() const { return BaseType; }
745+
746+
ValueDecl *getMember() const { return Member; }
747+
748+
DeclName getMemberName() const { return Name; }
749+
};
750+
751+
class AllowMemberRefOnExistential final : public AllowInvalidMemberRef {
735752
AllowMemberRefOnExistential(ConstraintSystem &cs, Type baseType,
736753
DeclName memberName, ValueDecl *member,
737754
ConstraintLocator *locator)
738-
: ConstraintFix(cs, FixKind::AllowMemberRefOnExistential, locator),
739-
BaseType(baseType), Name(memberName) {}
755+
: AllowInvalidMemberRef(cs, FixKind::AllowMemberRefOnExistential,
756+
baseType, member, memberName, locator) {}
740757

741758
public:
742759
std::string getName() const override {
743760
llvm::SmallVector<char, 16> scratch;
744-
auto memberName = Name.getString(scratch);
761+
auto memberName = getMemberName().getString(scratch);
745762
return "allow access to invalid member '" + memberName.str() +
746763
"' on value of protocol type";
747764
}
@@ -754,20 +771,16 @@ class AllowMemberRefOnExistential final : public ConstraintFix {
754771
ConstraintLocator *locator);
755772
};
756773

757-
class AllowTypeOrInstanceMember final : public ConstraintFix {
758-
Type BaseType;
759-
ValueDecl *Member;
760-
DeclName UsedName;
761-
762-
public:
774+
class AllowTypeOrInstanceMember final : public AllowInvalidMemberRef {
763775
AllowTypeOrInstanceMember(ConstraintSystem &cs, Type baseType,
764776
ValueDecl *member, DeclName name,
765777
ConstraintLocator *locator)
766-
: ConstraintFix(cs, FixKind::AllowTypeOrInstanceMember, locator),
767-
BaseType(baseType), Member(member), UsedName(name) {
778+
: AllowInvalidMemberRef(cs, FixKind::AllowTypeOrInstanceMember, baseType,
779+
member, name, locator) {
768780
assert(member);
769781
}
770782

783+
public:
771784
std::string getName() const override {
772785
return "allow access to instance member on type or a type member on instance";
773786
}
@@ -934,13 +947,12 @@ class MoveOutOfOrderArgument final : public ConstraintFix {
934947
ConstraintLocator *locator);
935948
};
936949

937-
class AllowInaccessibleMember final : public ConstraintFix {
938-
ValueDecl *Member;
939-
940-
AllowInaccessibleMember(ConstraintSystem &cs, ValueDecl *member,
950+
class AllowInaccessibleMember final : public AllowInvalidMemberRef {
951+
AllowInaccessibleMember(ConstraintSystem &cs, Type baseType,
952+
ValueDecl *member, DeclName name,
941953
ConstraintLocator *locator)
942-
: ConstraintFix(cs, FixKind::AllowInaccessibleMember, locator),
943-
Member(member) {}
954+
: AllowInvalidMemberRef(cs, FixKind::AllowInaccessibleMember, baseType,
955+
member, name, locator) {}
944956

945957
public:
946958
std::string getName() const override {
@@ -949,8 +961,8 @@ class AllowInaccessibleMember final : public ConstraintFix {
949961

950962
bool diagnose(Expr *root, bool asNote = false) const override;
951963

952-
static AllowInaccessibleMember *create(ConstraintSystem &cs,
953-
ValueDecl *member,
964+
static AllowInaccessibleMember *create(ConstraintSystem &cs, Type baseType,
965+
ValueDecl *member, DeclName name,
954966
ConstraintLocator *locator);
955967
};
956968

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4750,7 +4750,8 @@ fixMemberRef(ConstraintSystem &cs, Type baseTy,
47504750

47514751
case MemberLookupResult::UR_Inaccessible:
47524752
assert(choice.isDecl());
4753-
return AllowInaccessibleMember::create(cs, choice.getDecl(), locator);
4753+
return AllowInaccessibleMember::create(cs, baseTy, choice.getDecl(),
4754+
memberName, locator);
47544755

47554756
case MemberLookupResult::UR_UnavailableInExistential: {
47564757
return choice.isDecl()

0 commit comments

Comments
 (0)