Skip to content

Commit 99ef865

Browse files
committed
[Diagnostics] Switch FailureDiagnostic to use ASTNode instead of TypedNode
1 parent 955f0f3 commit 99ef865

File tree

2 files changed

+38
-40
lines changed

2 files changed

+38
-40
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ bool FailureDiagnostic::diagnoseAsNote() {
5151
return false;
5252
}
5353

54-
TypedNode FailureDiagnostic::getAnchor() const {
54+
ASTNode FailureDiagnostic::getAnchor() const {
5555
auto &cs = getConstraintSystem();
5656

5757
auto *locator = getLocator();
@@ -72,7 +72,7 @@ TypedNode FailureDiagnostic::getAnchor() const {
7272
return anchor;
7373
}
7474

75-
Type FailureDiagnostic::getType(TypedNode node, bool wantRValue) const {
75+
Type FailureDiagnostic::getType(ASTNode node, bool wantRValue) const {
7676
return resolveType(S.getType(node), /*reconstituteSugar=*/false, wantRValue);
7777
}
7878

@@ -411,7 +411,7 @@ bool MissingConformanceFailure::diagnoseAsError() {
411411
llvm::SmallPtrSet<Expr *, 4> anchors;
412412
for (const auto *fix : cs.getFixes()) {
413413
if (auto anchor = fix->getAnchor()) {
414-
if (anchor.is<const Expr *>())
414+
if (anchor.is<Expr *>())
415415
anchors.insert(getAsExpr(anchor));
416416
}
417417
}
@@ -860,7 +860,7 @@ bool NoEscapeFuncToTypeConversionFailure::diagnoseParameterUse() const {
860860
return true;
861861
}
862862

863-
TypedNode MissingForcedDowncastFailure::getAnchor() const {
863+
ASTNode MissingForcedDowncastFailure::getAnchor() const {
864864
auto anchor = FailureDiagnostic::getAnchor();
865865
if (auto *assignExpr = getAsExpr<AssignExpr>(anchor))
866866
return assignExpr->getSrc();
@@ -891,7 +891,7 @@ bool MissingAddressOfFailure::diagnoseAsError() {
891891
return true;
892892
}
893893

894-
TypedNode MissingExplicitConversionFailure::getAnchor() const {
894+
ASTNode MissingExplicitConversionFailure::getAnchor() const {
895895
auto anchor = FailureDiagnostic::getAnchor();
896896

897897
if (auto *assign = getAsExpr<AssignExpr>(anchor))
@@ -1451,8 +1451,7 @@ bool TrailingClosureAmbiguityFailure::diagnoseAsNote() {
14511451
return true;
14521452
}
14531453

1454-
AssignmentFailure::AssignmentFailure(const Expr *destExpr,
1455-
const Solution &solution,
1454+
AssignmentFailure::AssignmentFailure(Expr *destExpr, const Solution &solution,
14561455
SourceLoc diagnosticLoc)
14571456
: FailureDiagnostic(solution, destExpr), DestExpr(destExpr),
14581457
Loc(diagnosticLoc),
@@ -3011,7 +3010,7 @@ bool NonOptionalUnwrapFailure::diagnoseAsError() {
30113010
return true;
30123011
}
30133012

3014-
TypedNode MissingCallFailure::getAnchor() const {
3013+
ASTNode MissingCallFailure::getAnchor() const {
30153014
auto anchor = FailureDiagnostic::getAnchor();
30163015

30173016
if (auto *FVE = getAsExpr<ForceValueExpr>(anchor))
@@ -3468,7 +3467,7 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() {
34683467

34693468
auto anchor = getAnchor();
34703469

3471-
if (!anchor.is<const Expr *>())
3470+
if (!anchor.is<Expr *>())
34723471
return false;
34733472

34743473
Expr *expr = findParentExpr(castToExpr(anchor));
@@ -3743,7 +3742,7 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() {
37433742
}
37443743

37453744
// Fall back to a fix-it with a full type qualifier
3746-
const Expr *baseExpr = nullptr;
3745+
Expr *baseExpr = nullptr;
37473746
if (const auto *SE = getAsExpr<SubscriptExpr>(getRawAnchor()))
37483747
baseExpr = SE->getBase();
37493748
else if (const auto UDE = getAsExpr<UnresolvedDotExpr>(getRawAnchor()))
@@ -3821,7 +3820,7 @@ bool ImplicitInitOnNonConstMetatypeFailure::diagnoseAsError() {
38213820
return true;
38223821
}
38233822

3824-
TypedNode MissingArgumentsFailure::getAnchor() const {
3823+
ASTNode MissingArgumentsFailure::getAnchor() const {
38253824
auto anchor = FailureDiagnostic::getAnchor();
38263825

38273826
if (auto *captureList = getAsExpr<CaptureListExpr>(anchor))
@@ -4300,7 +4299,7 @@ bool MissingArgumentsFailure::isMisplacedMissingArgument(
43004299
}
43014300

43024301
std::tuple<Expr *, Expr *, unsigned, bool>
4303-
MissingArgumentsFailure::getCallInfo(TypedNode anchor) const {
4302+
MissingArgumentsFailure::getCallInfo(ASTNode anchor) const {
43044303
if (auto *call = getAsExpr<CallExpr>(anchor)) {
43054304
return std::make_tuple(call->getFn(), call->getArg(),
43064305
call->getNumArguments(), call->hasTrailingClosure());
@@ -5056,8 +5055,8 @@ bool MissingGenericArgumentsFailure::diagnoseParameter(
50565055
Anchor anchor, GenericTypeParamType *GP) const {
50575056
auto &cs = getConstraintSystem();
50585057

5059-
auto loc = anchor.is<const Expr *>() ? anchor.get<const Expr *>()->getLoc()
5060-
: anchor.get<TypeRepr *>()->getLoc();
5058+
auto loc = anchor.is<Expr *>() ? anchor.get<Expr *>()->getLoc()
5059+
: anchor.get<TypeRepr *>()->getLoc();
50615060

50625061
auto *locator = getLocator();
50635062
// Type variables associated with missing generic parameters are
@@ -5111,7 +5110,7 @@ void MissingGenericArgumentsFailure::emitGenericSignatureNote(
51115110
return;
51125111

51135112
auto *GTD = dyn_cast<GenericTypeDecl>(paramDC);
5114-
if (!GTD || anchor.is<const Expr *>())
5113+
if (!GTD || anchor.is<Expr *>())
51155114
return;
51165115

51175116
auto getParamDecl =

lib/Sema/CSDiagnostics.h

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "ConstraintSystem.h"
2121
#include "OverloadChoice.h"
2222
#include "swift/AST/ASTContext.h"
23+
#include "swift/AST/ASTNode.h"
2324
#include "swift/AST/Decl.h"
2425
#include "swift/AST/DiagnosticEngine.h"
2526
#include "swift/AST/Expr.h"
@@ -45,7 +46,7 @@ class FailureDiagnostic {
4546
FailureDiagnostic(const Solution &solution, ConstraintLocator *locator)
4647
: S(solution), Locator(locator) {}
4748

48-
FailureDiagnostic(const Solution &solution, TypedNode anchor)
49+
FailureDiagnostic(const Solution &solution, ASTNode anchor)
4950
: FailureDiagnostic(solution, solution.getConstraintLocator(anchor)) {}
5051

5152
virtual ~FailureDiagnostic();
@@ -77,13 +78,13 @@ class FailureDiagnostic {
7778
/// e.g. ambiguity error.
7879
virtual bool diagnoseAsNote();
7980

80-
TypedNode getRawAnchor() const { return Locator->getAnchor(); }
81+
ASTNode getRawAnchor() const { return Locator->getAnchor(); }
8182

82-
virtual TypedNode getAnchor() const;
83+
virtual ASTNode getAnchor() const;
8384

8485
ConstraintLocator *getLocator() const { return Locator; }
8586

86-
Type getType(TypedNode node, bool wantRValue = true) const;
87+
Type getType(ASTNode node, bool wantRValue = true) const;
8788

8889
/// Resolve type variables present in the raw type, if any.
8990
Type resolveType(Type rawType, bool reconstituteSugar = false,
@@ -117,17 +118,17 @@ class FailureDiagnostic {
117118
return S.getConstraintSystem();
118119
}
119120

120-
Type getContextualType(TypedNode anchor) const {
121+
Type getContextualType(ASTNode anchor) const {
121122
auto &cs = getConstraintSystem();
122123
return cs.getContextualType(anchor);
123124
}
124125

125-
TypeLoc getContextualTypeLoc(TypedNode anchor) const {
126+
TypeLoc getContextualTypeLoc(ASTNode anchor) const {
126127
auto &cs = getConstraintSystem();
127128
return cs.getContextualTypeLoc(anchor);
128129
}
129130

130-
ContextualTypePurpose getContextualTypePurpose(TypedNode anchor) const {
131+
ContextualTypePurpose getContextualTypePurpose(ASTNode anchor) const {
131132
auto &cs = getConstraintSystem();
132133
return cs.getContextualTypePurpose(anchor);
133134
}
@@ -157,17 +158,17 @@ class FailureDiagnostic {
157158
}
158159

159160
ConstraintLocator *
160-
getConstraintLocator(TypedNode anchor,
161+
getConstraintLocator(ASTNode anchor,
161162
ConstraintLocator::PathElement element) const {
162-
return S.getConstraintLocator(anchor.get<const Expr *>(), {element});
163+
return S.getConstraintLocator(anchor, {element});
163164
}
164165

165166
/// Retrive the constraint locator for the given anchor and
166167
/// path, uniqued and automatically calculate the summary flags
167168
ConstraintLocator *getConstraintLocator(
168-
TypedNode anchor,
169+
ASTNode anchor,
169170
ArrayRef<ConstraintLocator::PathElement> path = {}) const {
170-
return S.getConstraintLocator(anchor.get<const Expr *>(), path);
171+
return S.getConstraintLocator(anchor, path);
171172
}
172173

173174
ConstraintLocator *
@@ -247,7 +248,7 @@ class RequirementFailure : public FailureDiagnostic {
247248
assert(getGenericContext() &&
248249
"Affected decl not within a generic context?");
249250

250-
if (auto *parentExpr = findParentExpr(getRawAnchor().get<const Expr *>()))
251+
if (auto *parentExpr = findParentExpr(getRawAnchor().get<Expr *>()))
251252
Apply = dyn_cast<ApplyExpr>(parentExpr);
252253
}
253254

@@ -490,8 +491,7 @@ class TrailingClosureAmbiguityFailure final : public FailureDiagnostic {
490491
ArrayRef<OverloadChoice> Choices;
491492

492493
public:
493-
TrailingClosureAmbiguityFailure(ArrayRef<Solution> solutions,
494-
TypedNode anchor,
494+
TrailingClosureAmbiguityFailure(ArrayRef<Solution> solutions, ASTNode anchor,
495495
ArrayRef<OverloadChoice> choices)
496496
: FailureDiagnostic(solutions.front(), anchor), Choices(choices) {}
497497

@@ -504,16 +504,16 @@ class TrailingClosureAmbiguityFailure final : public FailureDiagnostic {
504504
/// trying to assign something to immutable value, or trying
505505
/// to access mutating member on immutable base.
506506
class AssignmentFailure final : public FailureDiagnostic {
507-
const Expr *DestExpr;
507+
Expr *DestExpr;
508508
SourceLoc Loc;
509509
Diag<StringRef> DeclDiagnostic;
510510
Diag<Type> TypeDiagnostic;
511511

512512
public:
513-
AssignmentFailure(const Expr *destExpr, const Solution &solution,
513+
AssignmentFailure(Expr *destExpr, const Solution &solution,
514514
SourceLoc diagnosticLoc);
515515

516-
AssignmentFailure(const Expr *destExpr, const Solution &solution,
516+
AssignmentFailure(Expr *destExpr, const Solution &solution,
517517
SourceLoc diagnosticLoc, Diag<StringRef> declDiag,
518518
Diag<Type> typeDiag)
519519
: FailureDiagnostic(solution, destExpr), DestExpr(destExpr),
@@ -789,7 +789,7 @@ class MissingExplicitConversionFailure final : public ContextualFailure {
789789
Type toType, ConstraintLocator *locator)
790790
: ContextualFailure(solution, fromType, toType, locator) {}
791791

792-
TypedNode getAnchor() const override;
792+
ASTNode getAnchor() const override;
793793

794794
bool diagnoseAsError() override;
795795

@@ -950,7 +950,7 @@ class MissingCallFailure final : public FailureDiagnostic {
950950
MissingCallFailure(const Solution &solution, ConstraintLocator *locator)
951951
: FailureDiagnostic(solution, locator) {}
952952

953-
TypedNode getAnchor() const override;
953+
ASTNode getAnchor() const override;
954954

955955
bool diagnoseAsError() override;
956956
};
@@ -1149,7 +1149,7 @@ class InvalidInitRefFailure : public FailureDiagnostic {
11491149
const ConstructorDecl *Init;
11501150
SourceRange BaseRange;
11511151

1152-
TypedNode getAnchor() const override { return getRawAnchor(); }
1152+
ASTNode getAnchor() const override { return getRawAnchor(); }
11531153

11541154
InvalidInitRefFailure(const Solution &solution, Type baseTy,
11551155
const ConstructorDecl *init, SourceRange baseRange,
@@ -1243,7 +1243,7 @@ class MissingArgumentsFailure final : public FailureDiagnostic {
12431243
assert(!SynthesizedArgs.empty() && "No missing arguments?!");
12441244
}
12451245

1246-
TypedNode getAnchor() const override;
1246+
ASTNode getAnchor() const override;
12471247

12481248
bool diagnoseAsError() override;
12491249

@@ -1268,8 +1268,7 @@ class MissingArgumentsFailure final : public FailureDiagnostic {
12681268
/// Gather information associated with expression that represents
12691269
/// a call - function, arguments, # of arguments and whether it has
12701270
/// a trailing closure.
1271-
std::tuple<Expr *, Expr *, unsigned, bool>
1272-
getCallInfo(TypedNode anchor) const;
1271+
std::tuple<Expr *, Expr *, unsigned, bool> getCallInfo(ASTNode anchor) const;
12731272

12741273
/// Transform given argument into format suitable for a fix-it
12751274
/// text e.g. `[<label>:]? <#<type#>`
@@ -1636,7 +1635,7 @@ class InOutConversionFailure final : public ContextualFailure {
16361635
/// _ = S()
16371636
/// ```
16381637
class MissingGenericArgumentsFailure final : public FailureDiagnostic {
1639-
using Anchor = llvm::PointerUnion<TypeRepr *, const Expr *>;
1638+
using Anchor = llvm::PointerUnion<TypeRepr *, Expr *>;
16401639

16411640
SmallVector<GenericTypeParamType *, 4> Parameters;
16421641

@@ -1872,7 +1871,7 @@ class MissingForcedDowncastFailure final : public ContextualFailure {
18721871
Type toType, ConstraintLocator *locator)
18731872
: ContextualFailure(solution, fromType, toType, locator) {}
18741873

1875-
TypedNode getAnchor() const override;
1874+
ASTNode getAnchor() const override;
18761875

18771876
bool diagnoseAsError() override;
18781877
};

0 commit comments

Comments
 (0)