Skip to content

Commit 9f02f78

Browse files
committed
[Diagnostics] NFC: Switch dyn_cast/get of anchor to either castToExpr or getAsExpr
1 parent 4b1aa29 commit 9f02f78

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ void RequirementFailure::emitRequirementNote(const Decl *anchor, Type lhs,
394394
}
395395

396396
bool MissingConformanceFailure::diagnoseAsError() {
397-
auto *anchor = getAnchor().get<const Expr *>();
397+
auto *anchor = castToExpr(getAnchor());
398398
auto nonConformingType = getLHS();
399399
auto protocolType = getRHS();
400400

@@ -905,7 +905,7 @@ TypedNode MissingExplicitConversionFailure::getAnchor() const {
905905

906906
bool MissingExplicitConversionFailure::diagnoseAsError() {
907907
auto *DC = getDC();
908-
auto *anchor = getAnchor().get<const Expr *>();
908+
auto *anchor = castToExpr(getAnchor());
909909

910910
auto fromType = getFromType();
911911
auto toType = getToType();
@@ -1000,7 +1000,7 @@ void MissingOptionalUnwrapFailure::offerDefaultValueUnwrapFixIt(
10001000
DeclContext *DC, const Expr *expr) const {
10011001
assert(expr);
10021002

1003-
auto *anchor = getAnchor().dyn_cast<const Expr *>();
1003+
auto *anchor = getAsExpr(getAnchor());
10041004
// If anchor is n explicit address-of, or expression which produces
10051005
// an l-value (e.g. first argument of `+=` operator), let's not
10061006
// suggest default value here because that would produce r-value type.
@@ -1089,7 +1089,7 @@ bool MissingOptionalUnwrapFailure::diagnoseAsError() {
10891089
return true;
10901090
}
10911091

1092-
auto *anchor = getAnchor().get<const Expr *>();
1092+
auto *anchor = castToExpr(getAnchor());
10931093

10941094
// If this is an unresolved member expr e.g. `.foo` its
10951095
// base type is going to be the same as result type minus
@@ -1197,7 +1197,7 @@ bool MissingOptionalUnwrapFailure::diagnoseAsError() {
11971197
bool RValueTreatedAsLValueFailure::diagnoseAsError() {
11981198
Diag<StringRef> subElementDiagID;
11991199
Diag<Type> rvalueDiagID = diag::assignment_lhs_not_lvalue;
1200-
auto diagExpr = getRawAnchor().get<const Expr *>();
1200+
auto diagExpr = castToExpr(getRawAnchor());
12011201
SourceLoc loc = diagExpr->getLoc();
12021202

12031203
// Assignment is not allowed inside of a condition,
@@ -1398,7 +1398,7 @@ bool TypeChecker::diagnoseSelfAssignment(const Expr *expr) {
13981398
}
13991399

14001400
bool TrailingClosureAmbiguityFailure::diagnoseAsNote() {
1401-
auto *anchor = getAnchor().get<const Expr *>();
1401+
auto *anchor = castToExpr(getAnchor());
14021402
const auto *expr = findParentExpr(anchor);
14031403
auto *callExpr = dyn_cast_or_null<CallExpr>(expr);
14041404
if (!callExpr)
@@ -2161,7 +2161,7 @@ bool ContextualFailure::diagnoseConversionToNil() const {
21612161
// It could be e.g. an argument to a subscript/call, assignment
21622162
// source like `s[0] = nil` or an array element like `[nil]` or
21632163
// `[nil: 42]` as a sub-expression to a larger one.
2164-
auto *parentExpr = findParentExpr(anchor.dyn_cast<const Expr *>());
2164+
auto *parentExpr = findParentExpr(getAsExpr(anchor));
21652165

21662166
// Looks like it's something similar to `let _ = nil`.
21672167
if (!parentExpr) {
@@ -2194,9 +2194,8 @@ bool ContextualFailure::diagnoseConversionToNil() const {
21942194
// out whether nil is a "key" or a "value".
21952195
if (isa<DictionaryExpr>(enclosingExpr)) {
21962196
assert(TE->getNumElements() == 2);
2197-
CTP = TE->getElement(0) == anchor.get<const Expr *>()
2198-
? CTP_DictionaryKey
2199-
: CTP_DictionaryValue;
2197+
CTP = TE->getElement(0) == castToExpr(anchor) ? CTP_DictionaryKey
2198+
: CTP_DictionaryValue;
22002199
} else {
22012200
// Can't initialize one of the tuple elements with `nil`.
22022201
CTP = CTP_Initialization;
@@ -2342,7 +2341,7 @@ bool ContextualFailure::diagnoseConversionToBool() const {
23422341
if (!toType->isBool())
23432342
return false;
23442343

2345-
auto *anchor = getAnchor().get<const Expr *>();
2344+
auto *anchor = castToExpr(getAnchor());
23462345
// Check for "=" converting to Bool. The user probably meant ==.
23472346
if (auto *AE = dyn_cast<AssignExpr>(anchor->getValueProvidingExpr())) {
23482347
emitDiagnosticAt(AE->getEqualLoc(), diag::use_of_equal_instead_of_equality)
@@ -2587,7 +2586,7 @@ bool ContextualFailure::tryRawRepresentableFixIts(
25872586
convWrapAfter += ")";
25882587
}
25892588

2590-
if (auto *E = anchor.dyn_cast<const Expr *>())
2589+
if (auto *E = getAsExpr(anchor))
25912590
fixIt(convWrapBefore, convWrapAfter, E);
25922591
return true;
25932592
}
@@ -2614,7 +2613,7 @@ bool ContextualFailure::tryRawRepresentableFixIts(
26142613
convWrapAfter += ")";
26152614
}
26162615

2617-
if (auto *E = anchor.dyn_cast<const Expr *>())
2616+
if (auto *E = getAsExpr(anchor))
26182617
fixIt(convWrapBefore, convWrapAfter, E);
26192618
return true;
26202619
}
@@ -2646,7 +2645,7 @@ bool ContextualFailure::tryIntegerCastFixIts(
26462645
return parenE->getSubExpr();
26472646
};
26482647

2649-
if (auto *anchor = getAnchor().dyn_cast<const Expr *>()) {
2648+
if (auto *anchor = getAsExpr(getAnchor())) {
26502649
if (Expr *innerE = getInnerCastedExpr(anchor)) {
26512650
Type innerTy = getType(innerE);
26522651
if (TypeChecker::isConvertibleTo(innerTy, toType, getDC())) {
@@ -2683,8 +2682,7 @@ bool ContextualFailure::trySequenceSubsequenceFixIts(
26832682
// Wrap in String.init
26842683
if (getFromType()->isEqual(Substring)) {
26852684
if (getToType()->isEqual(String)) {
2686-
auto anchor =
2687-
getAnchor().get<const Expr *>()->getSemanticsProvidingExpr();
2685+
auto *anchor = castToExpr(getAnchor())->getSemanticsProvidingExpr();
26882686
if (auto *CE = dyn_cast<CoerceExpr>(anchor)) {
26892687
anchor = CE->getSubExpr();
26902688
}
@@ -3237,7 +3235,7 @@ bool MissingMemberFailure::diagnoseAsError() {
32373235
diagnostic = diag::could_not_find_tuple_member;
32383236

32393237
bool hasUnresolvedPattern = false;
3240-
if (auto *E = anchor.dyn_cast<const Expr *>()) {
3238+
if (auto *E = getAsExpr(anchor)) {
32413239
const_cast<Expr *>(E)->forEachChildExpr([&](Expr *expr) {
32423240
hasUnresolvedPattern |= isa<UnresolvedPatternExpr>(expr);
32433241
return hasUnresolvedPattern ? nullptr : expr;
@@ -3440,7 +3438,7 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() {
34403438
if (!anchor.is<const Expr *>())
34413439
return false;
34423440

3443-
Expr *expr = findParentExpr(anchor.get<const Expr *>());
3441+
Expr *expr = findParentExpr(castToExpr(anchor));
34443442
SourceRange baseRange = expr ? expr->getSourceRange() : SourceRange();
34453443

34463444
// If the base is an implicit self type reference, and we're in a
@@ -3544,7 +3542,7 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() {
35443542
}
35453543
}
35463544

3547-
if (auto *maybeCallExpr = getRawAnchor().dyn_cast<const Expr *>()) {
3545+
if (auto *maybeCallExpr = getAsExpr(getRawAnchor())) {
35483546
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(maybeCallExpr)) {
35493547
maybeCallExpr = UDE->getBase();
35503548
}
@@ -3686,8 +3684,7 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() {
36863684

36873685
// Check if the expression is the matching operator ~=, most often used in
36883686
// case statements. If so, try to provide a single dot fix-it
3689-
const Expr *contextualTypeNode =
3690-
getRootExpr(getAnchor().dyn_cast<const Expr *>());
3687+
const Expr *contextualTypeNode = getRootExpr(getAsExpr(getAnchor()));
36913688

36923689
// The '~=' operator is an overloaded decl ref inside a binaryExpr
36933690
if (auto binaryExpr = dyn_cast<BinaryExpr>(contextualTypeNode)) {
@@ -4980,7 +4977,7 @@ bool MissingGenericArgumentsFailure::diagnoseAsError() {
49804977
});
49814978

49824979
if (!isScoped)
4983-
return diagnoseForAnchor(getAnchor().get<const Expr *>(), Parameters);
4980+
return diagnoseForAnchor(castToExpr(getAnchor()), Parameters);
49844981

49854982
bool diagnosed = false;
49864983
for (const auto &scope : scopedParameters)
@@ -5238,7 +5235,7 @@ bool SkipUnhandledConstructInFunctionBuilderFailure::diagnoseAsNote() {
52385235
}
52395236

52405237
bool MutatingMemberRefOnImmutableBase::diagnoseAsError() {
5241-
auto *anchor = getRawAnchor().get<const Expr *>();
5238+
auto *anchor = castToExpr(getRawAnchor());
52425239
auto baseExpr = getBaseExprFor(anchor);
52435240
if (!baseExpr)
52445241
return false;
@@ -5377,7 +5374,7 @@ bool InOutConversionFailure::diagnoseAsError() {
53775374
}
53785375

53795376
void InOutConversionFailure::fixItChangeArgumentType() const {
5380-
auto *argExpr = getAnchor().get<const Expr *>();
5377+
auto *argExpr = castToExpr(getAnchor());
53815378
auto *DC = getDC();
53825379

53835380
if (auto *IOE = dyn_cast<InOutExpr>(argExpr))
@@ -5528,8 +5525,7 @@ bool ArgumentMismatchFailure::diagnoseUseOfReferenceEqualityOperator() const {
55285525
// If both arguments where incorrect e.g. both are function types,
55295526
// let's avoid producing a diagnostic second time, because first
55305527
// one would cover both arguments.
5531-
if (getAnchor().dyn_cast<const Expr *>() == rhs &&
5532-
rhsType->is<FunctionType>()) {
5528+
if (getAsExpr(getAnchor()) == rhs && rhsType->is<FunctionType>()) {
55335529
auto &cs = getConstraintSystem();
55345530
if (cs.hasFixFor(getConstraintLocator(
55355531
binaryOp, {ConstraintLocator::ApplyArgument,
@@ -5711,7 +5707,7 @@ void ExpandArrayIntoVarargsFailure::tryDropArrayBracketsFixIt(
57115707
}
57125708

57135709
bool ExpandArrayIntoVarargsFailure::diagnoseAsError() {
5714-
if (auto *anchor = getAnchor().dyn_cast<const Expr *>()) {
5710+
if (auto *anchor = getAsExpr(getAnchor())) {
57155711
emitDiagnostic(diag::cannot_convert_array_to_variadic, getFromType(),
57165712
getToType());
57175713
tryDropArrayBracketsFixIt(anchor);
@@ -5723,7 +5719,7 @@ bool ExpandArrayIntoVarargsFailure::diagnoseAsError() {
57235719

57245720
bool ExpandArrayIntoVarargsFailure::diagnoseAsNote() {
57255721
auto overload = getCalleeOverloadChoiceIfAvailable(getLocator());
5726-
auto *anchor = getAnchor().dyn_cast<const Expr *>();
5722+
auto *anchor = getAsExpr(getAnchor());
57275723
if (!overload || !anchor)
57285724
return false;
57295725

@@ -6054,7 +6050,7 @@ bool AssignmentTypeMismatchFailure::diagnoseAsNote() {
60546050
}
60556051

60566052
bool MissingContextualBaseInMemberRefFailure::diagnoseAsError() {
6057-
auto *anchor = getAnchor().get<const Expr *>();
6053+
auto *anchor = castToExpr(getAnchor());
60586054
// Member reference could be wrapped into a number of parens
60596055
// e.g. `((.foo))`.
60606056
auto *parentExpr = findParentExpr(anchor);

0 commit comments

Comments
 (0)