Skip to content

Commit 6af91cc

Browse files
committed
Sema: Removed unnecessary ValueDecl from AllowProtocolTypeMember
Additionally removed the same unnecessary ValueDecl from AllowProtocolTypeMemberFailure allowing it to extend MissingMemberFailure.
1 parent 049ec0f commit 6af91cc

File tree

6 files changed

+45
-51
lines changed

6 files changed

+45
-51
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6667,7 +6667,7 @@ bool FailureDiagnosis::diagnoseMemberFailures(
66676667
locator = simplifyLocator(CS, locator, memberRange);
66686668

66696669
BaseLoc = baseExpr->getLoc();
6670-
NameLoc = DeclNameLoc(memberRange.Start);
6670+
NameLoc = DeclNameLoc(memberRange.Start);
66716671

66726672
// Retypecheck the anchor type, which is the base of the member expression.
66736673
baseExpr =

lib/Sema/CSDiagnostics.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,7 +1963,7 @@ bool MissingMemberFailure::diagnoseAsError() {
19631963

19641964
bool AllowProtocolTypeMemberFailure::diagnoseAsError() {
19651965
auto *anchor = getRawAnchor();
1966-
1966+
19671967
Expr *baseExpr = getAnchor();
19681968
DeclNameLoc nameLoc;
19691969
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(anchor)) {
@@ -1976,10 +1976,10 @@ bool AllowProtocolTypeMemberFailure::diagnoseAsError() {
19761976
} else if (auto *call = dyn_cast<CallExpr>(anchor)) {
19771977
baseExpr = call->getFn();
19781978
}
1979-
1979+
19801980
emitDiagnostic(getAnchor()->getLoc(),
1981-
diag::could_not_use_member_on_existential,
1982-
getBaseType(), getName())
1981+
diag::could_not_use_member_on_existential, getBaseType(),
1982+
getName())
19831983
.highlight(nameLoc.getSourceRange())
19841984
.highlight(baseExpr->getSourceRange());
19851985
return true;

lib/Sema/CSDiagnostics.h

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ class SubscriptMisuseFailure final : public FailureDiagnostic {
771771
/// let _: Int = s.foo(1, 2) // expected type is `(Int, Int) -> Int`
772772
/// }
773773
/// ```
774-
class MissingMemberFailure final : public FailureDiagnostic {
774+
class MissingMemberFailure : public FailureDiagnostic {
775775
Type BaseType;
776776
DeclName Name;
777777

@@ -787,6 +787,10 @@ class MissingMemberFailure final : public FailureDiagnostic {
787787
static DeclName findCorrectEnumCaseName(Type Ty,
788788
TypoCorrectionResults &corrections,
789789
DeclName memberName);
790+
791+
protected:
792+
Type getBaseType() const { return BaseType; }
793+
DeclName getName() const { return Name; }
790794
};
791795

792796
/// Diagnose cases where a member only accessible on generic constraints
@@ -797,29 +801,19 @@ class MissingMemberFailure final : public FailureDiagnostic {
797801
/// protocol P {
798802
/// var foo: Self { get }
799803
/// }
800-
///
804+
///
801805
/// func bar<X : P>(p: X) {
802806
/// p.foo
803807
/// }
804808
/// ```
805-
class AllowProtocolTypeMemberFailure final : public FailureDiagnostic {
806-
Type BaseType;
807-
ValueDecl *Member;
808-
DeclName Name;
809-
810-
public:
811-
AllowProtocolTypeMemberFailure(Expr *root, ConstraintSystem &cs,
812-
Type baseType, ValueDecl *member, DeclName memberName,
813-
ConstraintLocator *locator)
814-
: FailureDiagnostic(root, cs, locator), BaseType(baseType),
815-
Member(member), Name(memberName) {}
816-
817-
bool diagnoseAsError() override;
818-
819-
private:
820-
Type getBaseType() const { return BaseType; }
821-
ValueDecl *getMember() const { return Member; }
822-
DeclName getName() const { return Name; }
809+
class AllowProtocolTypeMemberFailure final : public MissingMemberFailure {
810+
public:
811+
AllowProtocolTypeMemberFailure(Expr *root, ConstraintSystem &cs,
812+
Type baseType, DeclName memberName,
813+
ConstraintLocator *locator)
814+
: MissingMemberFailure(root, cs, baseType, memberName, locator) {}
815+
816+
bool diagnoseAsError() override;
823817
};
824818

825819
/// Diagnose situations when we use an instance member on a type

lib/Sema/CSFix.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,14 +285,15 @@ DefineMemberBasedOnUse::create(ConstraintSystem &cs, Type baseType,
285285

286286
AllowProtocolTypeMember *
287287
AllowProtocolTypeMember::create(ConstraintSystem &cs, Type baseType,
288-
ValueDecl *member, DeclName memberName, ConstraintLocator *locator) {
288+
ValueDecl *member, DeclName memberName,
289+
ConstraintLocator *locator) {
289290
return new (cs.getAllocator())
290291
AllowProtocolTypeMember(cs, baseType, memberName, member, locator);
291292
}
292293

293294
bool AllowProtocolTypeMember::diagnose(Expr *root, bool asNote) const {
294295
auto failure = AllowProtocolTypeMemberFailure(root, getConstraintSystem(),
295-
BaseType, Member, Name, getLocator());
296+
BaseType, Name, getLocator());
296297
return failure.diagnose(asNote);
297298
}
298299

lib/Sema/CSFix.h

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ enum class FixKind : uint8_t {
124124
/// derived (rather than an arbitrary value of metatype type) or the
125125
/// referenced constructor must be required.
126126
AllowInvalidInitRef,
127-
127+
128128
/// Allow an invalid member access on a value of protocol type as if
129129
/// that protocol type were a generic constraint requiring conformance
130130
/// to that protocol.
@@ -600,30 +600,29 @@ class DefineMemberBasedOnUse final : public ConstraintFix {
600600

601601
class AllowProtocolTypeMember final : public ConstraintFix {
602602
Type BaseType;
603-
ValueDecl *Member;
604603
DeclName Name;
605-
604+
606605
AllowProtocolTypeMember(ConstraintSystem &cs, Type baseType,
607606
DeclName memberName, ValueDecl *member,
608607
ConstraintLocator *locator)
609-
: ConstraintFix(cs, FixKind::AllowProtocolTypeMember,
610-
locator), BaseType(baseType), Member(member), Name(memberName) {}
611-
612-
public:
613-
std::string getName() const override {
614-
llvm::SmallVector<char, 16> scratch;
615-
auto memberName = Name.getString(scratch);
616-
return "allow access to invalid member '" + memberName.str() +
617-
"' on value of protocol type";
618-
}
619-
620-
bool diagnose(Expr *root, bool asNote = false) const override;
621-
622-
static AllowProtocolTypeMember *create(ConstraintSystem &cs,
623-
Type baseType, ValueDecl *member, DeclName memberName,
624-
ConstraintLocator *locator);
608+
: ConstraintFix(cs, FixKind::AllowProtocolTypeMember, locator),
609+
BaseType(baseType), Name(memberName) {}
610+
611+
public:
612+
std::string getName() const override {
613+
llvm::SmallVector<char, 16> scratch;
614+
auto memberName = Name.getString(scratch);
615+
return "allow access to invalid member '" + memberName.str() +
616+
"' on value of protocol type";
617+
}
618+
619+
bool diagnose(Expr *root, bool asNote = false) const override;
620+
621+
static AllowProtocolTypeMember *create(ConstraintSystem &cs, Type baseType,
622+
ValueDecl *member, DeclName memberName,
623+
ConstraintLocator *locator);
625624
};
626-
625+
627626
class AllowTypeOrInstanceMember final : public ConstraintFix {
628627
Type BaseType;
629628
ValueDecl *Member;

lib/Sema/CSSimplify.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4528,12 +4528,12 @@ fixMemberRef(ConstraintSystem &cs, Type baseTy,
45284528
case MemberLookupResult::UR_Inaccessible:
45294529
assert(choice.isDecl());
45304530
return AllowInaccessibleMember::create(cs, choice.getDecl(), locator);
4531-
4531+
45324532
case MemberLookupResult::UR_UnavailableInExistential: {
45334533
return choice.isDecl()
4534-
? AllowProtocolTypeMember::create(
4535-
cs, baseTy, choice.getDecl(), memberName, locator)
4536-
: nullptr;
4534+
? AllowProtocolTypeMember::create(cs, baseTy, choice.getDecl(),
4535+
memberName, locator)
4536+
: nullptr;
45374537
}
45384538

45394539
case MemberLookupResult::UR_MutatingMemberOnRValue:

0 commit comments

Comments
 (0)