Skip to content

Commit 7669e0d

Browse files
committed
[CSFix] Switch ConstraintFix::getAnchor to return TypedNode
1 parent 5dfd51a commit 7669e0d

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

lib/Sema/CSFix.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ using namespace constraints;
3434

3535
ConstraintFix::~ConstraintFix() {}
3636

37-
Expr *ConstraintFix::getAnchor() const { return getLocator()->getAnchor(); }
37+
TypedNode ConstraintFix::getAnchor() const { return getLocator()->getAnchor(); }
3838

3939
void ConstraintFix::print(llvm::raw_ostream &Out) const {
4040
Out << "[fix: ";
@@ -135,10 +135,10 @@ CoerceToCheckedCast *CoerceToCheckedCast::attempt(ConstraintSystem &cs,
135135
if (fromType->hasTypeVariable() || toType->hasTypeVariable())
136136
return nullptr;
137137

138-
auto *expr = locator->getAnchor();
139-
if (auto *assignExpr = dyn_cast<AssignExpr>(expr))
140-
expr = assignExpr->getSrc();
141-
auto *coerceExpr = dyn_cast<CoerceExpr>(expr);
138+
auto anchor = locator->getAnchor();
139+
if (auto *assignExpr = getAsExpr<AssignExpr>(anchor))
140+
anchor = assignExpr->getSrc();
141+
auto *coerceExpr = getAsExpr<CoerceExpr>(anchor);
142142
if (!coerceExpr)
143143
return nullptr;
144144

@@ -189,7 +189,8 @@ bool MissingConformance::diagnose(const Solution &solution, bool asNote) const {
189189

190190
if (IsContextual) {
191191
auto &cs = solution.getConstraintSystem();
192-
auto context = cs.getContextualTypePurpose(locator->getAnchor());
192+
auto context =
193+
cs.getContextualTypePurpose(locator->getAnchor().get<const Expr *>());
193194
MissingContextualConformanceFailure failure(
194195
solution, context, NonConformingType, ProtocolType, locator);
195196
return failure.diagnose(asNote);
@@ -264,7 +265,7 @@ getStructuralTypeContext(const Solution &solution, ConstraintLocator *locator) {
264265
locator->isLastElement<LocatorPathElt::FunctionArgument>());
265266

266267
auto &cs = solution.getConstraintSystem();
267-
auto *anchor = locator->getAnchor();
268+
auto *anchor = locator->getAnchor().get<const Expr *>();
268269
auto contextualType = cs.getContextualType(anchor);
269270
auto exprType = cs.getType(anchor);
270271
return std::make_tuple(cs.getContextualTypePurpose(anchor), exprType,
@@ -273,15 +274,15 @@ getStructuralTypeContext(const Solution &solution, ConstraintLocator *locator) {
273274
return std::make_tuple(CTP_CallArgument,
274275
argApplyInfo->getArgType(),
275276
argApplyInfo->getParamType());
276-
} else if (auto *coerceExpr = dyn_cast<CoerceExpr>(locator->getAnchor())) {
277+
} else if (auto *coerceExpr = getAsExpr<CoerceExpr>(locator->getAnchor())) {
277278
return std::make_tuple(CTP_CoerceOperand,
278279
solution.getType(coerceExpr->getSubExpr()),
279280
solution.getType(coerceExpr));
280-
} else if (auto *assignExpr = dyn_cast<AssignExpr>(locator->getAnchor())) {
281+
} else if (auto *assignExpr = getAsExpr<AssignExpr>(locator->getAnchor())) {
281282
return std::make_tuple(CTP_AssignSource,
282283
solution.getType(assignExpr->getSrc()),
283284
solution.getType(assignExpr->getDest()));
284-
} else if (auto *call = dyn_cast<CallExpr>(locator->getAnchor())) {
285+
} else if (auto *call = getAsExpr<CallExpr>(locator->getAnchor())) {
285286
assert(isa<TypeExpr>(call->getFn()));
286287
return std::make_tuple(
287288
CTP_Initialization,
@@ -313,7 +314,8 @@ bool AllowTupleTypeMismatch::coalesceAndDiagnose(
313314
Type toType;
314315

315316
if (getFromType()->is<TupleType>() && getToType()->is<TupleType>()) {
316-
purpose = cs.getContextualTypePurpose(locator->getAnchor());
317+
purpose =
318+
cs.getContextualTypePurpose(locator->getAnchor().get<const Expr *>());
317319
fromType = getFromType();
318320
toType = getToType();
319321
} else if (auto contextualTypeInfo =
@@ -500,8 +502,9 @@ DefineMemberBasedOnUse::diagnoseForAmbiguity(CommonFixesArray commonFixes) const
500502
concreteBaseType = baseType;
501503

502504
if (concreteBaseType->getCanonicalType() != baseType->getCanonicalType()) {
503-
getConstraintSystem().getASTContext().Diags.diagnose(getAnchor()->getLoc(),
504-
diag::unresolved_member_no_inference, Name);
505+
auto &DE = getConstraintSystem().getASTContext().Diags;
506+
DE.diagnose(getLoc(getAnchor()), diag::unresolved_member_no_inference,
507+
Name);
505508
return true;
506509
}
507510
}
@@ -655,7 +658,7 @@ bool RemoveExtraneousArguments::diagnose(const Solution &solution,
655658

656659
bool RemoveExtraneousArguments::isMinMaxNameShadowing(
657660
ConstraintSystem &cs, ConstraintLocatorBuilder locator) {
658-
auto *anchor = dyn_cast_or_null<CallExpr>(locator.getAnchor());
661+
auto *anchor = getAsExpr<CallExpr>(locator.getAnchor());
659662
if (!anchor)
660663
return false;
661664

@@ -1001,7 +1004,9 @@ IgnoreContextualType *IgnoreContextualType::create(ConstraintSystem &cs,
10011004
bool IgnoreAssignmentDestinationType::diagnose(const Solution &solution,
10021005
bool asNote) const {
10031006
auto &cs = getConstraintSystem();
1004-
auto *AE = cast<AssignExpr>(getAnchor());
1007+
auto *AE = getAsExpr<AssignExpr>(getAnchor());
1008+
1009+
assert(AE);
10051010

10061011
// Let's check whether this is a situation of chained assignment where
10071012
// one of the steps in the chain is an assignment to self e.g.

lib/Sema/CSFix.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef SWIFT_SEMA_CSFIX_H
1818
#define SWIFT_SEMA_CSFIX_H
1919

20+
#include "ConstraintLocator.h"
2021
#include "swift/AST/Decl.h"
2122
#include "swift/AST/Expr.h"
2223
#include "swift/AST/Identifier.h"
@@ -312,7 +313,7 @@ class ConstraintFix {
312313
/// Retrieve anchor expression associated with this fix.
313314
/// NOTE: such anchor comes directly from locator without
314315
/// any simplication attempts.
315-
Expr *getAnchor() const;
316+
TypedNode getAnchor() const;
316317
ConstraintLocator *getLocator() const { return Locator; }
317318

318319
protected:

0 commit comments

Comments
 (0)