Skip to content

Commit 398f378

Browse files
committed
[ConstraintSystem] Elevate TypedNode access helpers up to the namespace
Originally such accessors were only useful for `FailureDiagnostic` but now since `ConstraintLocator` is anchored with `TypedNode` it makes sense to make them universally accessible.
1 parent 099379a commit 398f378

File tree

4 files changed

+64
-62
lines changed

4 files changed

+64
-62
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,6 @@ TypedNode FailureDiagnostic::getAnchor() const {
7272
return anchor;
7373
}
7474

75-
SourceLoc FailureDiagnostic::getLoc(TypedNode anchor) {
76-
if (auto *E = anchor.dyn_cast<const Expr *>()) {
77-
return E->getLoc();
78-
} else if (auto *T = anchor.dyn_cast<const TypeLoc *>()) {
79-
return T->getLoc();
80-
} else if (auto *V = anchor.dyn_cast<const VarDecl *>()) {
81-
return V->getNameLoc();
82-
} else {
83-
return anchor.get<const Pattern *>()->getLoc();
84-
}
85-
}
86-
87-
SourceRange FailureDiagnostic::getSourceRange(TypedNode anchor) {
88-
if (auto *E = anchor.dyn_cast<const Expr *>()) {
89-
return E->getSourceRange();
90-
} else if (auto *T = anchor.dyn_cast<const TypeLoc *>()) {
91-
return T->getSourceRange();
92-
} else if (auto *V = anchor.dyn_cast<const VarDecl *>()) {
93-
return V->getSourceRange();
94-
} else {
95-
return anchor.get<const Pattern *>()->getSourceRange();
96-
}
97-
}
98-
9975
Type FailureDiagnostic::getType(TypedNode node, bool wantRValue) const {
10076
return resolveType(S.getType(node), /*reconstituteSugar=*/false, wantRValue);
10177
}
@@ -734,7 +710,7 @@ bool GenericArgumentsMismatchFailure::diagnoseAsError() {
734710
if (!diagnostic)
735711
return false;
736712

737-
emitDiagnosticAt(getLoc(anchor), *diagnostic, fromType, toType);
713+
emitDiagnosticAt(::getLoc(anchor), *diagnostic, fromType, toType);
738714
emitNotesForMismatches();
739715
return true;
740716
}
@@ -2580,7 +2556,7 @@ bool ContextualFailure::tryRawRepresentableFixIts(
25802556
if (conformsToKnownProtocol(CS, fromType, KnownProtocolKind::OptionSet) &&
25812557
isExpr<IntegerLiteralExpr>(anchor) &&
25822558
castToExpr<IntegerLiteralExpr>(anchor)->getDigitsText() == "0") {
2583-
diagnostic.fixItReplace(getSourceRange(anchor), "[]");
2559+
diagnostic.fixItReplace(::getSourceRange(anchor), "[]");
25842560
return true;
25852561
}
25862562
if (auto rawTy = isRawRepresentable(CS, toType, rawRepresentableProtocol)) {
@@ -3045,7 +3021,7 @@ TypedNode MissingCallFailure::getAnchor() const {
30453021

30463022
bool MissingCallFailure::diagnoseAsError() {
30473023
auto anchor = getAnchor();
3048-
SourceLoc insertLoc = getSourceRange(anchor).End;
3024+
SourceLoc insertLoc = getSourceRange().End;
30493025

30503026
// Calls are not yet supported by key path, but it
30513027
// is useful to record this fix to diagnose chaining
@@ -3063,7 +3039,7 @@ bool MissingCallFailure::diagnoseAsError() {
30633039
case ConstraintLocator::ApplyArgToParam: {
30643040
auto fnType = getType(anchor)->castTo<FunctionType>();
30653041
emitDiagnostic(diag::missing_nullary_call, fnType->getResult())
3066-
.fixItInsertAfter(getSourceRange().End, "()");
3042+
.fixItInsertAfter(insertLoc, "()");
30673043
return true;
30683044
}
30693045

@@ -3142,7 +3118,7 @@ bool ExtraneousPropertyWrapperUnwrapFailure::diagnoseAsError() {
31423118
}
31433119

31443120
bool MissingPropertyWrapperUnwrapFailure::diagnoseAsError() {
3145-
auto endLoc = getLoc(getAnchor()).getAdvancedLoc(1);
3121+
auto endLoc = getLoc().getAdvancedLoc(1);
31463122

31473123
if (auto *member = getReferencedMember()) {
31483124
emitDiagnostic(diag::incorrect_property_wrapper_reference_member,
@@ -3238,7 +3214,7 @@ bool MissingMemberFailure::diagnoseAsError() {
32383214

32393215
auto baseType = resolveType(getBaseType())->getWithoutSpecifierType();
32403216

3241-
DeclNameLoc nameLoc(FailureDiagnostic::getLoc(anchor));
3217+
DeclNameLoc nameLoc(::getLoc(anchor));
32423218
if (auto *UDE = getAsExpr<UnresolvedDotExpr>(anchor)) {
32433219
nameLoc = UDE->getNameLoc();
32443220
} else if (auto *UME = getAsExpr<UnresolvedMemberExpr>(anchor)) {
@@ -3336,8 +3312,7 @@ bool MissingMemberFailure::diagnoseAsError() {
33363312
emitBasicError(baseType);
33373313
}
33383314
} else if (auto moduleTy = baseType->getAs<ModuleType>()) {
3339-
emitDiagnosticAt(FailureDiagnostic::getLoc(memberBase),
3340-
diag::no_member_of_module,
3315+
emitDiagnosticAt(::getLoc(memberBase), diag::no_member_of_module,
33413316
moduleTy->getModule()->getName(), getName())
33423317
.highlight(getSourceRange())
33433318
.highlight(nameLoc.getSourceRange());
@@ -4740,7 +4715,7 @@ bool InaccessibleMemberFailure::diagnoseAsError() {
47404715
return false;
47414716
}
47424717

4743-
auto loc = nameLoc.isValid() ? nameLoc.getStartLoc() : getLoc(anchor);
4718+
auto loc = nameLoc.isValid() ? nameLoc.getStartLoc() : ::getLoc(anchor);
47444719
auto accessLevel = Member->getFormalAccessScope().accessLevelForDiagnostics();
47454720
if (auto *CD = dyn_cast<ConstructorDecl>(Member)) {
47464721
emitDiagnosticAt(loc, diag::init_candidate_inaccessible,
@@ -4764,7 +4739,7 @@ SourceLoc AnyObjectKeyPathRootFailure::getLoc() const {
47644739
return rootTyRepr->getLoc();
47654740
}
47664741

4767-
return FailureDiagnostic::getLoc(anchor);
4742+
return ::getLoc(anchor);
47684743
}
47694744

47704745
SourceRange AnyObjectKeyPathRootFailure::getSourceRange() const {
@@ -4775,7 +4750,7 @@ SourceRange AnyObjectKeyPathRootFailure::getSourceRange() const {
47754750
return rootTyRepr->getSourceRange();
47764751
}
47774752

4778-
return FailureDiagnostic::getSourceRange(anchor);
4753+
return ::getSourceRange(anchor);
47794754
}
47804755

47814756
bool AnyObjectKeyPathRootFailure::diagnoseAsError() {
@@ -4813,7 +4788,7 @@ SourceLoc InvalidMemberRefInKeyPath::getLoc() const {
48134788
return KPE->getComponents()[component->getIndex()].getLoc();
48144789
}
48154790

4816-
return FailureDiagnostic::getLoc(anchor);
4791+
return ::getLoc(anchor);
48174792
}
48184793

48194794
bool InvalidStaticMemberRefInKeyPath::diagnoseAsError() {
@@ -4840,7 +4815,7 @@ SourceLoc InvalidUseOfAddressOf::getLoc() const {
48404815
if (auto *assign = getAsExpr<AssignExpr>(anchor))
48414816
return assign->getSrc()->getLoc();
48424817

4843-
return FailureDiagnostic::getLoc(anchor);
4818+
return ::getLoc(anchor);
48444819
}
48454820

48464821
bool InvalidUseOfAddressOf::diagnoseAsError() {
@@ -5509,7 +5484,7 @@ bool ArgumentMismatchFailure::diagnoseAsError() {
55095484
if (getType(argument, /*wantRValue=*/false)->is<LValueType>()) {
55105485
auto elementTy = paramType->getAnyPointerElementType();
55115486
if (elementTy && argType->isEqual(elementTy)) {
5512-
diag.fixItInsert(getSourceRange(argument).Start, "&");
5487+
diag.fixItInsert(::getSourceRange(argument).Start, "&");
55135488
return true;
55145489
}
55155490
}
@@ -5965,8 +5940,8 @@ bool NonEphemeralConversionFailure::diagnosePointerInit() const {
59655940
: diag::cannot_construct_dangling_pointer;
59665941

59675942
auto anchor = getRawAnchor();
5968-
emitDiagnosticAt(getLoc(anchor), diagID, constructedTy, constructorKind)
5969-
.highlight(getSourceRange(anchor));
5943+
emitDiagnosticAt(::getLoc(anchor), diagID, constructedTy, constructorKind)
5944+
.highlight(::getSourceRange(anchor));
59705945

59715946
emitSuggestionNotes();
59725947
return true;
@@ -6155,7 +6130,7 @@ getImportModuleAndDefaultType(const ASTContext &ctx,
61556130
}
61566131

61576132
SourceLoc UnableToInferProtocolLiteralType::getLoc() const {
6158-
return FailureDiagnostic::getLoc(getRawAnchor());
6133+
return ::getLoc(getRawAnchor());
61596134
}
61606135

61616136
bool UnableToInferProtocolLiteralType::diagnoseAsError() {

lib/Sema/CSDiagnostics.h

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ class FailureDiagnostic {
5050

5151
virtual ~FailureDiagnostic();
5252

53-
virtual SourceLoc getLoc() const { return getLoc(getAnchor()); }
53+
virtual SourceLoc getLoc() const { return constraints::getLoc(getAnchor()); }
5454

5555
virtual SourceRange getSourceRange() const {
56-
return getSourceRange(getAnchor());
56+
return constraints::getSourceRange(getAnchor());
5757
}
5858

5959
/// Try to diagnose a problem given affected expression,
@@ -201,24 +201,6 @@ class FailureDiagnostic {
201201
Type type,
202202
llvm::function_ref<void(GenericTypeParamType *, Type)> substitution =
203203
[](GenericTypeParamType *, Type) {});
204-
205-
static SourceLoc getLoc(TypedNode node);
206-
static SourceRange getSourceRange(TypedNode node);
207-
208-
template <typename T> static const T *castToExpr(TypedNode node) {
209-
return cast<T>(node.get<const Expr *>());
210-
}
211-
212-
template <typename T> static T *getAsExpr(TypedNode node) {
213-
if (const auto *E = node.dyn_cast<const Expr *>())
214-
return dyn_cast<T>(const_cast<Expr *>(E));
215-
return nullptr;
216-
}
217-
218-
template <typename T> static bool isExpr(TypedNode node) {
219-
auto *E = node.get<const Expr *>();
220-
return isa<T>(E);
221-
}
222204
};
223205

224206
/// Base class for all of the diagnostics related to generic requirement
@@ -1065,7 +1047,7 @@ class MissingMemberFailure final : public InvalidMemberRefFailure {
10651047

10661048
SourceLoc getLoc() const override {
10671049
// Diagnostic should point to the member instead of its base expression.
1068-
return FailureDiagnostic::getLoc(getRawAnchor());
1050+
return constraints::getLoc(getRawAnchor());
10691051
}
10701052

10711053
bool diagnoseAsError() override;

lib/Sema/ConstraintSystem.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4447,3 +4447,27 @@ void ConstraintSystem::maybeProduceFallbackDiagnostic(
44474447

44484448
ctx.Diags.diagnose(target.getLoc(), diag::failed_to_produce_diagnostic);
44494449
}
4450+
4451+
SourceLoc constraints::getLoc(TypedNode anchor) {
4452+
if (auto *E = anchor.dyn_cast<const Expr *>()) {
4453+
return E->getLoc();
4454+
} else if (auto *T = anchor.dyn_cast<const TypeLoc *>()) {
4455+
return T->getLoc();
4456+
} else if (auto *V = anchor.dyn_cast<const VarDecl *>()) {
4457+
return V->getNameLoc();
4458+
} else {
4459+
return anchor.get<const Pattern *>()->getLoc();
4460+
}
4461+
}
4462+
4463+
SourceRange constraints::getSourceRange(TypedNode anchor) {
4464+
if (auto *E = anchor.dyn_cast<const Expr *>()) {
4465+
return E->getSourceRange();
4466+
} else if (auto *T = anchor.dyn_cast<const TypeLoc *>()) {
4467+
return T->getSourceRange();
4468+
} else if (auto *V = anchor.dyn_cast<const VarDecl *>()) {
4469+
return V->getSourceRange();
4470+
} else {
4471+
return anchor.get<const Pattern *>()->getSourceRange();
4472+
}
4473+
}

lib/Sema/ConstraintSystem.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,27 @@ class TypeVariableType::Implementation {
455455

456456
namespace constraints {
457457

458+
template <typename T = Expr> T *castToExpr(TypedNode node) {
459+
return cast<T>(const_cast<Expr *>(node.get<const Expr *>()));
460+
}
461+
462+
template <typename T = Expr> T *getAsExpr(TypedNode node) {
463+
if (const auto *E = node.dyn_cast<const Expr *>())
464+
return dyn_cast_or_null<T>(const_cast<Expr *>(E));
465+
return nullptr;
466+
}
467+
468+
template <typename T> bool isExpr(TypedNode node) {
469+
if (node.isNull() || !node.is<const Expr *>())
470+
return false;
471+
472+
auto *E = node.get<const Expr *>();
473+
return isa<T>(E);
474+
}
475+
476+
SourceLoc getLoc(TypedNode node);
477+
SourceRange getSourceRange(TypedNode node);
478+
458479
/// The result of comparing two constraint systems that are a solutions
459480
/// to the given set of constraints.
460481
enum class SolutionCompareResult {

0 commit comments

Comments
 (0)