Skip to content

Commit 4b1aa29

Browse files
committed
[ConstraintSystem] NFC: Adjust all uses of locator anchors to work with TypedNode
1 parent 12a63ce commit 4b1aa29

File tree

8 files changed

+209
-201
lines changed

8 files changed

+209
-201
lines changed

lib/Sema/CSApply.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ ConstraintLocator *Solution::getCalleeLocator(ConstraintLocator *locator,
123123
}
124124

125125
ConstraintLocator *
126-
Solution::getConstraintLocator(const Expr *anchor,
126+
Solution::getConstraintLocator(TypedNode anchor,
127127
ArrayRef<LocatorPathElt> path) const {
128128
auto &cs = getConstraintSystem();
129-
return cs.getConstraintLocator(const_cast<Expr *>(anchor), path);
129+
return cs.getConstraintLocator(anchor, path);
130130
}
131131

132132
ConstraintLocator *
@@ -1204,9 +1204,9 @@ namespace {
12041204
// For properties, build member references.
12051205
if (isa<VarDecl>(member)) {
12061206
if (isUnboundInstanceMember) {
1207-
assert(memberLocator.getBaseLocator() &&
1207+
assert(memberLocator.getBaseLocator() &&
12081208
cs.UnevaluatedRootExprs.count(
1209-
memberLocator.getBaseLocator()->getAnchor()) &&
1209+
getAsExpr(memberLocator.getBaseLocator()->getAnchor())) &&
12101210
"Attempt to reference an instance member of a metatype");
12111211
auto baseInstanceTy = cs.getType(base)
12121212
->getInOutObjectType()->getMetatypeInstanceType();
@@ -1618,7 +1618,7 @@ namespace {
16181618

16191619
if (selected.choice.getKind() ==
16201620
OverloadChoiceKind::KeyPathDynamicMemberLookup &&
1621-
!isa<SubscriptExpr>(locator.getAnchor()))
1621+
!isExpr<SubscriptExpr>(locator.getAnchor()))
16221622
locatorKind = ConstraintLocator::Member;
16231623

16241624
newSubscript =
@@ -1744,7 +1744,7 @@ namespace {
17441744
locatorKind = ConstraintLocator::Member;
17451745

17461746
if (choice.getKind() == OverloadChoiceKind::KeyPathDynamicMemberLookup) {
1747-
locatorKind = isa<SubscriptExpr>(locator.getAnchor())
1747+
locatorKind = isExpr<SubscriptExpr>(locator.getAnchor())
17481748
? ConstraintLocator::SubscriptMember
17491749
: ConstraintLocator::Member;
17501750
}
@@ -1898,7 +1898,7 @@ namespace {
18981898
SourceLoc dotLoc,
18991899
ConstraintLocator *memberLoc) {
19001900
auto &ctx = cs.getASTContext();
1901-
auto *anchor = memberLoc->getAnchor();
1901+
auto *anchor = getAsExpr(memberLoc->getAnchor());
19021902

19031903
SmallVector<KeyPathExpr::Component, 2> components;
19041904

@@ -7804,13 +7804,13 @@ namespace {
78047804
explicit CompareExprSourceLocs(SourceManager &sourceMgr)
78057805
: sourceMgr(sourceMgr) { }
78067806

7807-
bool operator()(Expr *lhs, Expr *rhs) const {
7807+
bool operator()(TypedNode lhs, TypedNode rhs) const {
78087808
if (static_cast<bool>(lhs) != static_cast<bool>(rhs)) {
78097809
return static_cast<bool>(lhs);
78107810
}
78117811

7812-
auto lhsLoc = lhs->getLoc();
7813-
auto rhsLoc = rhs->getLoc();
7812+
auto lhsLoc = getLoc(lhs);
7813+
auto rhsLoc = getLoc(rhs);
78147814
if (lhsLoc.isValid() != rhsLoc.isValid())
78157815
return lhsLoc.isValid();
78167816

@@ -7824,26 +7824,27 @@ namespace {
78247824
/// able to emit an error message, or false if none of the fixits worked out.
78257825
bool ConstraintSystem::applySolutionFixes(const Solution &solution) {
78267826
/// Collect the fixes on a per-expression basis.
7827-
llvm::SmallDenseMap<Expr *, SmallVector<ConstraintFix *, 4>> fixesPerExpr;
7827+
llvm::SmallDenseMap<TypedNode, SmallVector<ConstraintFix *, 4>>
7828+
fixesPerAnchor;
78287829
for (auto *fix : solution.Fixes) {
7829-
fixesPerExpr[fix->getAnchor()].push_back(fix);
7830+
fixesPerAnchor[fix->getAnchor()].push_back(fix);
78307831
}
78317832

78327833
// Collect all of the expressions that have fixes, and sort them by
78337834
// source ordering.
7834-
SmallVector<Expr *, 4> exprsWithFixes;
7835-
for (const auto &fix : fixesPerExpr) {
7836-
exprsWithFixes.push_back(fix.getFirst());
7835+
SmallVector<TypedNode, 4> orderedAnchors;
7836+
for (const auto &fix : fixesPerAnchor) {
7837+
orderedAnchors.push_back(fix.getFirst());
78377838
}
7838-
std::sort(exprsWithFixes.begin(), exprsWithFixes.end(),
7839+
std::sort(orderedAnchors.begin(), orderedAnchors.end(),
78397840
CompareExprSourceLocs(Context.SourceMgr));
78407841

78417842
// Walk over each of the expressions, diagnosing fixes.
78427843
bool diagnosedAnyErrors = false;
78437844

7844-
for (auto expr : exprsWithFixes) {
7845+
for (auto anchor : orderedAnchors) {
78457846
// Coalesce fixes with the same locator to avoid duplicating notes.
7846-
auto fixes = fixesPerExpr[expr];
7847+
auto fixes = fixesPerAnchor[anchor];
78477848

78487849
using ConstraintFixVector = llvm::SmallVector<ConstraintFix *, 4>;
78497850
llvm::SmallMapVector<ConstraintLocator *,

lib/Sema/CSBindings.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,8 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) const {
459459
// of the optional type extracted by force unwrap.
460460
bool isOptionalObject = false;
461461
if (auto *locator = typeVar->getImpl().getLocator()) {
462-
auto *anchor = locator->getAnchor();
463-
isOptionalObject = anchor && isa<ForceValueExpr>(anchor);
462+
auto anchor = locator->getAnchor();
463+
isOptionalObject = isExpr<ForceValueExpr>(anchor);
464464
}
465465

466466
// Gather the constraints associated with this type variable.
@@ -1104,7 +1104,7 @@ bool TypeVariableBinding::attempt(ConstraintSystem &cs) const {
11041104
if (cs.recordFix(fix))
11051105
return true;
11061106
} else if (srcLocator->getAnchor() &&
1107-
isa<ObjectLiteralExpr>(srcLocator->getAnchor())) {
1107+
isExpr<ObjectLiteralExpr>(srcLocator->getAnchor())) {
11081108
auto *fix = SpecifyObjectLiteralTypeImport::create(
11091109
cs, TypeVar->getImpl().getLocator());
11101110
if (cs.recordFix(fix))

lib/Sema/CSDiagnostics.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ TypedNode FailureDiagnostic::getAnchor() const {
6161
if (!resolved || !resolved->getAnchor())
6262
return locator->getAnchor();
6363

64-
Expr *anchor = resolved->getAnchor();
64+
auto anchor = resolved->getAnchor();
6565
// FIXME: Work around an odd locator representation that doesn't separate the
6666
// base of a subscript member from the member access.
6767
if (locator->isLastElement<LocatorPathElt::SubscriptMember>()) {
68-
if (auto subscript = dyn_cast<SubscriptExpr>(anchor))
68+
if (auto subscript = getAsExpr<SubscriptExpr>(anchor))
6969
anchor = subscript->getBase();
7070
}
7171

@@ -105,7 +105,7 @@ FailureDiagnostic::getArgumentListExprFor(ConstraintLocator *locator) const {
105105
// to get the argument list.
106106
auto newPath = ArrayRef<LocatorPathElt>(path.begin(), iter + 1);
107107
auto argListLoc = getConstraintLocator(locator->getAnchor(), newPath);
108-
return simplifyLocatorToAnchor(argListLoc);
108+
return getAsExpr(simplifyLocatorToAnchor(argListLoc));
109109
}
110110

111111
Expr *FailureDiagnostic::getBaseExprFor(const Expr *anchor) const {
@@ -409,8 +409,12 @@ bool MissingConformanceFailure::diagnoseAsError() {
409409

410410
auto &cs = getConstraintSystem();
411411
llvm::SmallPtrSet<Expr *, 4> anchors;
412-
for (const auto *fix : cs.getFixes())
413-
anchors.insert(fix->getAnchor());
412+
for (const auto *fix : cs.getFixes()) {
413+
if (auto anchor = fix->getAnchor()) {
414+
if (anchor.is<const Expr *>())
415+
anchors.insert(getAsExpr(anchor));
416+
}
417+
}
414418

415419
bool hasFix = false;
416420
caseExpr->forEachChildExpr([&](Expr *expr) -> Expr * {
@@ -4215,7 +4219,7 @@ bool MissingArgumentsFailure::isMisplacedMissingArgument(
42154219
if (!(fnType && fnType->getNumParams() == 2))
42164220
return false;
42174221

4218-
auto *anchor = locator->getAnchor();
4222+
auto anchor = locator->getAnchor();
42194223

42204224
auto hasFixFor = [&](FixKind kind, ConstraintLocator *locator) -> bool {
42214225
auto fix = llvm::find_if(solution.Fixes, [&](const ConstraintFix *fix) {
@@ -4241,9 +4245,9 @@ bool MissingArgumentsFailure::isMisplacedMissingArgument(
42414245
return false;
42424246

42434247
Expr *argExpr = nullptr;
4244-
if (auto *call = dyn_cast<CallExpr>(anchor)) {
4248+
if (auto *call = getAsExpr<CallExpr>(anchor)) {
42454249
argExpr = call->getArg();
4246-
} else if (auto *subscript = dyn_cast<SubscriptExpr>(anchor)) {
4250+
} else if (auto *subscript = getAsExpr<SubscriptExpr>(anchor)) {
42474251
argExpr = subscript->getIndex();
42484252
} else {
42494253
return false;
@@ -5742,8 +5746,7 @@ bool ExtraneousCallFailure::diagnoseAsError() {
57425746
auto *argLoc =
57435747
getConstraintLocator(getRawAnchor(), ConstraintLocator::ApplyArgument);
57445748

5745-
if (auto *TE =
5746-
dyn_cast_or_null<TupleExpr>(simplifyLocatorToAnchor(argLoc))) {
5749+
if (auto *TE = getAsExpr<TupleExpr>(simplifyLocatorToAnchor(argLoc))) {
57475750
if (TE->getNumElements() == 0) {
57485751
diagnostic.fixItRemove(TE->getSourceRange());
57495752
}

lib/Sema/CSRanking.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ bool CompareDeclSpecializationRequest::evaluate(
505505
ConstraintSystem cs(dc, ConstraintSystemOptions());
506506
bool knownNonSubtype = false;
507507

508-
auto *locator = cs.getConstraintLocator(nullptr);
508+
auto *locator = cs.getConstraintLocator({});
509509
// FIXME: Locator when anchored on a declaration.
510510
// Get the type of a reference to the second declaration.
511511

@@ -769,8 +769,8 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
769769
bool isVarAndNotProtocol2 = false;
770770

771771
auto getWeight = [&](ConstraintLocator *locator) -> unsigned {
772-
if (auto *anchor = locator->getAnchor()) {
773-
auto weight = cs.getExprDepth(anchor);
772+
if (auto *anchor = locator->getAnchor().dyn_cast<const Expr *>()) {
773+
auto weight = cs.getExprDepth(const_cast<Expr *>(anchor));
774774
if (weight)
775775
return *weight + 1;
776776
}

0 commit comments

Comments
 (0)