Skip to content

Commit 71ab19b

Browse files
committed
[Diagnostics] Switch getConstraintLocator variants to accept TypedNode
1 parent f5714bf commit 71ab19b

File tree

4 files changed

+47
-36
lines changed

4 files changed

+47
-36
lines changed

lib/Sema/CSApply.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,17 @@ ConstraintLocator *Solution::getCalleeLocator(ConstraintLocator *locator,
166166
}
167167

168168
ConstraintLocator *
169-
Solution::getConstraintLocator(Expr *anchor,
169+
Solution::getConstraintLocator(const Expr *anchor,
170170
ArrayRef<LocatorPathElt> path) const {
171171
auto &cs = getConstraintSystem();
172-
return cs.getConstraintLocator(anchor, path);
172+
return cs.getConstraintLocator(const_cast<Expr *>(anchor), path);
173+
}
174+
175+
ConstraintLocator *
176+
Solution::getConstraintLocator(ConstraintLocator *base,
177+
ArrayRef<LocatorPathElt> path) const {
178+
auto &cs = getConstraintSystem();
179+
return cs.getConstraintLocator(base, path);
173180
}
174181

175182
/// Return the implicit access kind for a MemberRefExpr with the

lib/Sema/CSDiagnostics.cpp

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ FailureDiagnostic::getArgumentListExprFor(ConstraintLocator *locator) const {
132132
// Form a new locator that ends at the ApplyArgument element, then simplify
133133
// to get the argument list.
134134
auto newPath = ArrayRef<LocatorPathElt>(path.begin(), iter + 1);
135-
auto &cs = getConstraintSystem();
136-
auto argListLoc = cs.getConstraintLocator(locator->getAnchor(), newPath);
135+
auto argListLoc = getConstraintLocator(locator->getAnchor(), newPath);
137136
return simplifyLocatorToAnchor(argListLoc);
138137
}
139138

@@ -218,8 +217,7 @@ ProtocolConformance *RequirementFailure::getConformanceForConditionalReq(
218217
return nullptr;
219218

220219
auto path = locator->getPath();
221-
auto *typeReqLoc = getConstraintLocator(getRawAnchor().get<const Expr *>(),
222-
path.drop_back());
220+
auto *typeReqLoc = getConstraintLocator(getRawAnchor(), path.drop_back());
223221

224222
auto result = llvm::find_if(
225223
cs.CheckedConformances,
@@ -1733,7 +1731,6 @@ bool AssignmentFailure::diagnoseAsError() {
17331731

17341732
std::pair<Expr *, Optional<OverloadChoice>>
17351733
AssignmentFailure::resolveImmutableBase(Expr *expr) const {
1736-
auto &cs = getConstraintSystem();
17371734
auto *DC = getDC();
17381735
expr = expr->getValueProvidingExpr();
17391736

@@ -1763,7 +1760,7 @@ AssignmentFailure::resolveImmutableBase(Expr *expr) const {
17631760
}
17641761

17651762
Optional<OverloadChoice> member = getMemberRef(
1766-
cs.getConstraintLocator(SE, ConstraintLocator::SubscriptMember));
1763+
getConstraintLocator(SE, ConstraintLocator::SubscriptMember));
17671764

17681765
// If it isn't settable, return it.
17691766
if (member) {
@@ -1798,7 +1795,7 @@ AssignmentFailure::resolveImmutableBase(Expr *expr) const {
17981795
// Look through property references.
17991796
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(expr)) {
18001797
// If we found a decl for the UDE, check it.
1801-
auto loc = cs.getConstraintLocator(UDE, ConstraintLocator::Member);
1798+
auto loc = getConstraintLocator(UDE, ConstraintLocator::Member);
18021799

18031800
auto member = getMemberRef(loc);
18041801

@@ -1864,15 +1861,14 @@ AssignmentFailure::getMemberRef(ConstraintLocator *locator) const {
18641861
// If this is a keypath dynamic member lookup, we have to
18651862
// adjust the locator to find member referred by it.
18661863
if (isValidKeyPathDynamicMemberLookup(subscript)) {
1867-
auto &cs = getConstraintSystem();
18681864
// Type has a following format:
18691865
// `(Self) -> (dynamicMember: {Writable}KeyPath<T, U>) -> U`
18701866
auto *fullType = member->openedFullType->castTo<FunctionType>();
18711867
auto *fnType = fullType->getResult()->castTo<FunctionType>();
18721868

18731869
auto paramTy = fnType->getParams()[0].getPlainType();
18741870
auto keyPath = paramTy->getAnyNominal();
1875-
auto memberLoc = cs.getConstraintLocator(
1871+
auto memberLoc = getConstraintLocator(
18761872
locator, LocatorPathElt::KeyPathDynamicMember(keyPath));
18771873

18781874
auto memberRef = getOverloadChoiceIfAvailable(memberLoc);
@@ -2025,7 +2021,7 @@ bool ContextualFailure::diagnoseAsError() {
20252021
auto &cs = getConstraintSystem();
20262022
MissingOptionalUnwrapFailure failure(getSolution(), getType(anchor),
20272023
toType,
2028-
cs.getConstraintLocator(anchor));
2024+
getConstraintLocator(anchor));
20292025
if (failure.diagnoseAsError())
20302026
return true;
20312027
}
@@ -3095,8 +3091,7 @@ bool MissingCallFailure::diagnoseAsError() {
30953091
}
30963092

30973093
case ConstraintLocator::AutoclosureResult: {
3098-
auto loc = getConstraintLocator(getRawAnchor().get<const Expr *>(),
3099-
path.drop_back());
3094+
auto loc = getConstraintLocator(getRawAnchor(), path.drop_back());
31003095
AutoClosureForwardingFailure failure(getSolution(), loc);
31013096
return failure.diagnoseAsError();
31023097
}
@@ -3517,15 +3512,14 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() {
35173512
}
35183513

35193514
auto isCallArgument = [this](Expr *expr) {
3520-
auto &cs = getConstraintSystem();
3521-
auto argExpr = cs.getParentExpr(expr);
3515+
auto argExpr = findParentExpr(expr);
35223516
if (!argExpr)
35233517
return false;
3524-
auto possibleApplyExpr = cs.getParentExpr(expr);
3518+
auto possibleApplyExpr = findParentExpr(expr);
35253519
return possibleApplyExpr && isa<ApplyExpr>(possibleApplyExpr);
35263520
};
35273521

3528-
auto *initCall = cs.getParentExpr(cs.getParentExpr(ctorRef));
3522+
auto *initCall = findParentExpr(findParentExpr(ctorRef));
35293523

35303524
auto isMutable = [&DC](ValueDecl *decl) {
35313525
if (auto *storage = dyn_cast<AbstractStorageDecl>(decl))
@@ -3534,7 +3528,7 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() {
35343528
return true;
35353529
};
35363530

3537-
auto *baseLoc = cs.getConstraintLocator(ctorRef->getBase());
3531+
auto *baseLoc = getConstraintLocator(ctorRef->getBase());
35383532
if (auto selection = getCalleeOverloadChoiceIfAvailable(baseLoc)) {
35393533
OverloadChoice choice = selection->choice;
35403534
if (choice.isDecl() && isMutable(choice.getDecl()) &&
@@ -3563,8 +3557,8 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() {
35633557
}
35643558

35653559
if (getRawAnchor() &&
3566-
cs.DC->getContextKind() == DeclContextKind::Initializer) {
3567-
auto *TypeDC = cs.DC->getParent();
3560+
getDC()->getContextKind() == DeclContextKind::Initializer) {
3561+
auto *TypeDC = getDC()->getParent();
35683562
bool propertyInitializer = true;
35693563
// If the parent context is not a type context, we expect it
35703564
// to be a defaulted parameter in a function declaration.
@@ -3785,13 +3779,13 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() {
37853779

37863780
bool PartialApplicationFailure::diagnoseAsError() {
37873781
auto &cs = getConstraintSystem();
3788-
auto *anchor = cast<UnresolvedDotExpr>(getRawAnchor().get<const Expr *>());
3782+
auto anchor = cast<UnresolvedDotExpr>(getRawAnchor().get<const Expr *>());
37893783

37903784
RefKind kind = RefKind::MutatingMethod;
37913785

37923786
// If this is initializer delegation chain, we have a tailored message.
3793-
if (getOverloadChoiceIfAvailable(cs.getConstraintLocator(
3794-
anchor, ConstraintLocator::ConstructorMember))) {
3787+
if (getOverloadChoiceIfAvailable(
3788+
getConstraintLocator(anchor, ConstraintLocator::ConstructorMember))) {
37953789
kind = anchor->getBase()->isSuperExpr() ? RefKind::SuperInit
37963790
: RefKind::SelfInit;
37973791
} else if (anchor->getBase()->isSuperExpr()) {
@@ -4763,8 +4757,7 @@ bool InaccessibleMemberFailure::diagnoseAsError() {
47634757

47644758
if (baseExpr) {
47654759
auto &cs = getConstraintSystem();
4766-
auto *locator =
4767-
cs.getConstraintLocator(baseExpr, ConstraintLocator::Member);
4760+
auto *locator = getConstraintLocator(baseExpr, ConstraintLocator::Member);
47684761
if (cs.hasFixFor(locator))
47694762
return false;
47704763
}
@@ -5590,7 +5583,7 @@ bool ArgumentMismatchFailure::diagnoseUseOfReferenceEqualityOperator() const {
55905583
// one would cover both arguments.
55915584
if (getAnchor().get<const Expr *>() == rhs && rhsType->is<FunctionType>()) {
55925585
auto &cs = getConstraintSystem();
5593-
if (cs.hasFixFor(cs.getConstraintLocator(
5586+
if (cs.hasFixFor(getConstraintLocator(
55945587
binaryOp, {ConstraintLocator::ApplyArgument,
55955588
LocatorPathElt::ApplyArgToParam(
55965589
0, 0, getParameterFlagsAtIndex(0))})))
@@ -5720,7 +5713,7 @@ bool ArgumentMismatchFailure::diagnoseMisplacedMissingArgument() const {
57205713
auto *fnType = getFnType();
57215714
const auto &param = fnType->getParams()[0];
57225715

5723-
auto *anchor = getRawAnchor().get<const Expr *>();
5716+
auto anchor = getRawAnchor();
57245717

57255718
MissingArgumentsFailure failure(
57265719
solution, {std::make_pair(0, param)},
@@ -5802,8 +5795,8 @@ bool ExtraneousCallFailure::diagnoseAsError() {
58025795
// If this is something like `foo()` where `foo` is a variable
58035796
// or a property, let's suggest dropping `()`.
58045797
auto removeParensFixIt = [&](InFlightDiagnostic &diagnostic) {
5805-
auto *argLoc = getConstraintLocator(getRawAnchor().get<const Expr *>(),
5806-
ConstraintLocator::ApplyArgument);
5798+
auto *argLoc =
5799+
getConstraintLocator(getRawAnchor(), ConstraintLocator::ApplyArgument);
58075800

58085801
if (auto *TE =
58095802
dyn_cast_or_null<TupleExpr>(simplifyLocatorToAnchor(argLoc))) {
@@ -6098,7 +6091,7 @@ bool AssignmentTypeMismatchFailure::diagnoseAsError() {
60986091
}
60996092

61006093
bool AssignmentTypeMismatchFailure::diagnoseAsNote() {
6101-
auto *anchor = getAnchor().get<const Expr *>();
6094+
auto anchor = getAnchor();
61026095

61036096
if (auto overload =
61046097
getCalleeOverloadChoiceIfAvailable(getConstraintLocator(anchor))) {

lib/Sema/CSDiagnostics.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,23 @@ class FailureDiagnostic {
158158
}
159159

160160
ConstraintLocator *
161-
getConstraintLocator(Expr *anchor,
161+
getConstraintLocator(TypedNode anchor,
162162
ConstraintLocator::PathElement element) const {
163-
return S.getConstraintLocator(anchor, {element});
163+
return S.getConstraintLocator(anchor.get<const Expr *>(), {element});
164164
}
165165

166166
/// Retrive the constraint locator for the given anchor and
167167
/// path, uniqued and automatically calculate the summary flags
168168
ConstraintLocator *getConstraintLocator(
169-
Expr *anchor, ArrayRef<ConstraintLocator::PathElement> path = {}) const {
170-
return S.getConstraintLocator(anchor, path);
169+
TypedNode anchor,
170+
ArrayRef<ConstraintLocator::PathElement> path = {}) const {
171+
return S.getConstraintLocator(anchor.get<const Expr *>(), path);
172+
}
173+
174+
ConstraintLocator *
175+
getConstraintLocator(ConstraintLocator *baseLocator,
176+
ConstraintLocator::PathElement element) const {
177+
return S.getConstraintLocator(baseLocator, element);
171178
}
172179

173180
Optional<FunctionArgApplyInfo>

lib/Sema/ConstraintSystem.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,11 @@ class Solution {
999999
bool lookThroughApply = true) const;
10001000

10011001
ConstraintLocator *
1002-
getConstraintLocator(Expr *anchor, ArrayRef<LocatorPathElt> path = {}) const;
1002+
getConstraintLocator(const Expr *anchor,
1003+
ArrayRef<LocatorPathElt> path = {}) const;
1004+
1005+
ConstraintLocator *getConstraintLocator(ConstraintLocator *baseLocator,
1006+
ArrayRef<LocatorPathElt> path) const;
10031007

10041008
void setExprTypes(Expr *expr) const;
10051009

0 commit comments

Comments
 (0)