Skip to content

Commit 147280a

Browse files
committed
Sema: Renamed invalid member ref on existential fix, and refactored inheritance
1 parent 0714dfe commit 147280a

File tree

6 files changed

+50
-43
lines changed

6 files changed

+50
-43
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,8 +757,8 @@ void FailureDiagnosis::diagnoseUnviableLookupResults(
757757
break;
758758

759759
case MemberLookupResult::UR_UnavailableInExistential: {
760-
AllowProtocolTypeMemberFailure failure(baseExpr, CS, instanceTy, memberName,
761-
CS.getConstraintLocator(E));
760+
InvalidMemberRefOnExistential failure(baseExpr, CS, instanceTy, memberName,
761+
CS.getConstraintLocator(E));
762762
failure.diagnoseAsError();
763763
return;
764764
}

lib/Sema/CSDiagnostics.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,7 +1844,7 @@ bool MissingMemberFailure::diagnoseAsError() {
18441844
if (!anchor || !baseExpr)
18451845
return false;
18461846

1847-
if (auto *typeVar = BaseType->getAs<TypeVariableType>()) {
1847+
if (auto *typeVar = getBaseType()->getAs<TypeVariableType>()) {
18481848
auto &CS = getConstraintSystem();
18491849
auto *memberLoc = typeVar->getImpl().getLocator();
18501850
// Don't try to diagnose anything besides first missing
@@ -1856,7 +1856,7 @@ bool MissingMemberFailure::diagnoseAsError() {
18561856
return false;
18571857
}
18581858

1859-
auto baseType = resolveType(BaseType)->getWithoutSpecifierType();
1859+
auto baseType = resolveType(getBaseType())->getWithoutSpecifierType();
18601860

18611861
DeclNameLoc nameLoc(anchor->getStartLoc());
18621862
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(anchor)) {
@@ -1876,22 +1876,22 @@ bool MissingMemberFailure::diagnoseAsError() {
18761876
if (baseType->is<TupleType>())
18771877
diagnostic = diag::could_not_find_tuple_member;
18781878

1879-
emitDiagnostic(anchor->getLoc(), diagnostic, baseType, Name)
1879+
emitDiagnostic(anchor->getLoc(), diagnostic, baseType, getName())
18801880
.highlight(baseExpr->getSourceRange())
18811881
.highlight(nameLoc.getSourceRange());
18821882
};
18831883

1884-
TypoCorrectionResults corrections(TC, Name, nameLoc);
1884+
TypoCorrectionResults corrections(TC, getName(), nameLoc);
18851885
auto tryTypoCorrection = [&] {
18861886
TC.performTypoCorrection(getDC(), DeclRefKind::Ordinary, baseType,
18871887
defaultMemberLookupOptions, corrections);
18881888
};
18891889

1890-
if (Name.getBaseName().getKind() == DeclBaseName::Kind::Subscript) {
1890+
if (getName().getBaseName().getKind() == DeclBaseName::Kind::Subscript) {
18911891
emitDiagnostic(anchor->getLoc(), diag::could_not_find_value_subscript,
18921892
baseType)
18931893
.highlight(baseExpr->getSourceRange());
1894-
} else if (Name.getBaseName() == "deinit") {
1894+
} else if (getName().getBaseName() == "deinit") {
18951895
// Specialised diagnostic if trying to access deinitialisers
18961896
emitDiagnostic(anchor->getLoc(), diag::destructor_not_accessible)
18971897
.highlight(baseExpr->getSourceRange());
@@ -1900,9 +1900,9 @@ bool MissingMemberFailure::diagnoseAsError() {
19001900
tryTypoCorrection();
19011901

19021902
if (DeclName rightName =
1903-
findCorrectEnumCaseName(instanceTy, corrections, Name)) {
1903+
findCorrectEnumCaseName(instanceTy, corrections, getName())) {
19041904
emitDiagnostic(anchor->getLoc(), diag::could_not_find_enum_case,
1905-
instanceTy, Name, rightName)
1905+
instanceTy, getName(), rightName)
19061906
.fixItReplace(nameLoc.getBaseNameLoc(),
19071907
rightName.getBaseIdentifier().str());
19081908
return true;
@@ -1911,7 +1911,7 @@ bool MissingMemberFailure::diagnoseAsError() {
19111911
if (auto correction = corrections.claimUniqueCorrection()) {
19121912
auto diagnostic = emitDiagnostic(
19131913
anchor->getLoc(), diag::could_not_find_type_member_corrected,
1914-
instanceTy, Name, correction->CorrectedName);
1914+
instanceTy, getName(), correction->CorrectedName);
19151915
diagnostic.highlight(baseExpr->getSourceRange())
19161916
.highlight(nameLoc.getSourceRange());
19171917
correction->addFixits(diagnostic);
@@ -1920,14 +1920,14 @@ bool MissingMemberFailure::diagnoseAsError() {
19201920
}
19211921
} else if (auto moduleTy = baseType->getAs<ModuleType>()) {
19221922
emitDiagnostic(baseExpr->getLoc(), diag::no_member_of_module,
1923-
moduleTy->getModule()->getName(), Name)
1923+
moduleTy->getModule()->getName(), getName())
19241924
.highlight(baseExpr->getSourceRange())
19251925
.highlight(nameLoc.getSourceRange());
19261926
return true;
19271927
} else {
19281928
// Check for a few common cases that can cause missing members.
19291929
auto *ED = baseType->getEnumOrBoundGenericEnum();
1930-
if (ED && Name.isSimpleName("rawValue")) {
1930+
if (ED && getName().isSimpleName("rawValue")) {
19311931
auto loc = ED->getNameLoc();
19321932
if (loc.isValid()) {
19331933
emitBasicError(baseType);
@@ -1947,7 +1947,7 @@ bool MissingMemberFailure::diagnoseAsError() {
19471947
if (auto correction = corrections.claimUniqueCorrection()) {
19481948
auto diagnostic = emitDiagnostic(
19491949
anchor->getLoc(), diag::could_not_find_value_member_corrected,
1950-
baseType, Name, correction->CorrectedName);
1950+
baseType, getName(), correction->CorrectedName);
19511951
diagnostic.highlight(baseExpr->getSourceRange())
19521952
.highlight(nameLoc.getSourceRange());
19531953
correction->addFixits(diagnostic);
@@ -1961,7 +1961,7 @@ bool MissingMemberFailure::diagnoseAsError() {
19611961
return true;
19621962
}
19631963

1964-
bool AllowProtocolTypeMemberFailure::diagnoseAsError() {
1964+
bool InvalidMemberRefOnExistential::diagnoseAsError() {
19651965
auto *anchor = getRawAnchor();
19661966

19671967
Expr *baseExpr = getAnchor();

lib/Sema/CSDiagnostics.h

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,21 @@ class SubscriptMisuseFailure final : public FailureDiagnostic {
762762
bool diagnoseAsNote() override;
763763
};
764764

765+
class InvalidMemberRefFailure : public FailureDiagnostic {
766+
Type BaseType;
767+
DeclName Name;
768+
769+
public:
770+
InvalidMemberRefFailure(Expr *root, ConstraintSystem &cs, Type baseType,
771+
DeclName memberName, ConstraintLocator *locator)
772+
: FailureDiagnostic(root, cs, locator),
773+
BaseType(baseType), Name(memberName) {}
774+
775+
protected:
776+
Type getBaseType() const { return BaseType; }
777+
DeclName getName() const { return Name; }
778+
};
779+
765780
/// Diagnose situations when member referenced by name is missing
766781
/// from the associated base type, e.g.
767782
///
@@ -771,26 +786,18 @@ class SubscriptMisuseFailure final : public FailureDiagnostic {
771786
/// let _: Int = s.foo(1, 2) // expected type is `(Int, Int) -> Int`
772787
/// }
773788
/// ```
774-
class MissingMemberFailure : public FailureDiagnostic {
775-
Type BaseType;
776-
DeclName Name;
777-
789+
class MissingMemberFailure final : public InvalidMemberRefFailure {
778790
public:
779791
MissingMemberFailure(Expr *root, ConstraintSystem &cs, Type baseType,
780792
DeclName memberName, ConstraintLocator *locator)
781-
: FailureDiagnostic(root, cs, locator), BaseType(baseType),
782-
Name(memberName) {}
793+
: InvalidMemberRefFailure(root, cs, baseType, memberName, locator) {}
783794

784795
bool diagnoseAsError() override;
785796

786797
private:
787798
static DeclName findCorrectEnumCaseName(Type Ty,
788799
TypoCorrectionResults &corrections,
789800
DeclName memberName);
790-
791-
protected:
792-
Type getBaseType() const { return BaseType; }
793-
DeclName getName() const { return Name; }
794801
};
795802

796803
/// Diagnose cases where a member only accessible on generic constraints
@@ -806,12 +813,12 @@ class MissingMemberFailure : public FailureDiagnostic {
806813
/// p.foo
807814
/// }
808815
/// ```
809-
class AllowProtocolTypeMemberFailure final : public MissingMemberFailure {
816+
class InvalidMemberRefOnExistential final : public InvalidMemberRefFailure {
810817
public:
811-
AllowProtocolTypeMemberFailure(Expr *root, ConstraintSystem &cs,
812-
Type baseType, DeclName memberName,
813-
ConstraintLocator *locator)
814-
: MissingMemberFailure(root, cs, baseType, memberName, locator) {}
818+
InvalidMemberRefOnExistential(Expr *root, ConstraintSystem &cs,
819+
Type baseType, DeclName memberName,
820+
ConstraintLocator *locator)
821+
: InvalidMemberRefFailure(root, cs, baseType, memberName, locator) {}
815822

816823
bool diagnoseAsError() override;
817824
};

lib/Sema/CSFix.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,17 +283,17 @@ DefineMemberBasedOnUse::create(ConstraintSystem &cs, Type baseType,
283283
DefineMemberBasedOnUse(cs, baseType, member, locator);
284284
}
285285

286-
AllowProtocolTypeMember *
287-
AllowProtocolTypeMember::create(ConstraintSystem &cs, Type baseType,
286+
AllowMemberRefOnExistential *
287+
AllowMemberRefOnExistential::create(ConstraintSystem &cs, Type baseType,
288288
ValueDecl *member, DeclName memberName,
289289
ConstraintLocator *locator) {
290290
return new (cs.getAllocator())
291-
AllowProtocolTypeMember(cs, baseType, memberName, member, locator);
291+
AllowMemberRefOnExistential(cs, baseType, memberName, member, locator);
292292
}
293293

294-
bool AllowProtocolTypeMember::diagnose(Expr *root, bool asNote) const {
295-
auto failure = AllowProtocolTypeMemberFailure(root, getConstraintSystem(),
296-
BaseType, Name, getLocator());
294+
bool AllowMemberRefOnExistential::diagnose(Expr *root, bool asNote) const {
295+
auto failure = InvalidMemberRefOnExistential(root, getConstraintSystem(),
296+
BaseType, Name, getLocator());
297297
return failure.diagnose(asNote);
298298
}
299299

lib/Sema/CSFix.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ enum class FixKind : uint8_t {
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.
131-
AllowProtocolTypeMember,
131+
AllowMemberRefOnExistential,
132132

133133
/// If there are fewer arguments than parameters, let's fix that up
134134
/// by adding new arguments to the list represented as type variables.
@@ -598,14 +598,14 @@ class DefineMemberBasedOnUse final : public ConstraintFix {
598598
ConstraintLocator *locator);
599599
};
600600

601-
class AllowProtocolTypeMember final : public ConstraintFix {
601+
class AllowMemberRefOnExistential final : public ConstraintFix {
602602
Type BaseType;
603603
DeclName Name;
604604

605-
AllowProtocolTypeMember(ConstraintSystem &cs, Type baseType,
605+
AllowMemberRefOnExistential(ConstraintSystem &cs, Type baseType,
606606
DeclName memberName, ValueDecl *member,
607607
ConstraintLocator *locator)
608-
: ConstraintFix(cs, FixKind::AllowProtocolTypeMember, locator),
608+
: ConstraintFix(cs, FixKind::AllowMemberRefOnExistential, locator),
609609
BaseType(baseType), Name(memberName) {}
610610

611611
public:
@@ -618,7 +618,7 @@ class AllowProtocolTypeMember final : public ConstraintFix {
618618

619619
bool diagnose(Expr *root, bool asNote = false) const override;
620620

621-
static AllowProtocolTypeMember *create(ConstraintSystem &cs, Type baseType,
621+
static AllowMemberRefOnExistential *create(ConstraintSystem &cs, Type baseType,
622622
ValueDecl *member, DeclName memberName,
623623
ConstraintLocator *locator);
624624
};

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4531,7 +4531,7 @@ fixMemberRef(ConstraintSystem &cs, Type baseTy,
45314531

45324532
case MemberLookupResult::UR_UnavailableInExistential: {
45334533
return choice.isDecl()
4534-
? AllowProtocolTypeMember::create(cs, baseTy, choice.getDecl(),
4534+
? AllowMemberRefOnExistential::create(cs, baseTy, choice.getDecl(),
45354535
memberName, locator)
45364536
: nullptr;
45374537
}
@@ -6643,7 +6643,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
66436643
case FixKind::RelabelArguments:
66446644
case FixKind::RemoveUnwrap:
66456645
case FixKind::DefineMemberBasedOnUse:
6646-
case FixKind::AllowProtocolTypeMember:
6646+
case FixKind::AllowMemberRefOnExistential:
66476647
case FixKind::AllowTypeOrInstanceMember:
66486648
case FixKind::AllowInvalidPartialApplication:
66496649
case FixKind::AllowInvalidInitRef:

0 commit comments

Comments
 (0)