@@ -394,7 +394,7 @@ void RequirementFailure::emitRequirementNote(const Decl *anchor, Type lhs,
394
394
}
395
395
396
396
bool MissingConformanceFailure::diagnoseAsError () {
397
- auto *anchor = getAnchor (). get < const Expr *>( );
397
+ auto *anchor = castToExpr ( getAnchor ());
398
398
auto nonConformingType = getLHS ();
399
399
auto protocolType = getRHS ();
400
400
@@ -905,7 +905,7 @@ TypedNode MissingExplicitConversionFailure::getAnchor() const {
905
905
906
906
bool MissingExplicitConversionFailure::diagnoseAsError () {
907
907
auto *DC = getDC ();
908
- auto *anchor = getAnchor (). get < const Expr *>( );
908
+ auto *anchor = castToExpr ( getAnchor ());
909
909
910
910
auto fromType = getFromType ();
911
911
auto toType = getToType ();
@@ -1000,7 +1000,7 @@ void MissingOptionalUnwrapFailure::offerDefaultValueUnwrapFixIt(
1000
1000
DeclContext *DC, const Expr *expr) const {
1001
1001
assert (expr);
1002
1002
1003
- auto *anchor = getAnchor (). dyn_cast < const Expr *>( );
1003
+ auto *anchor = getAsExpr ( getAnchor ());
1004
1004
// If anchor is n explicit address-of, or expression which produces
1005
1005
// an l-value (e.g. first argument of `+=` operator), let's not
1006
1006
// suggest default value here because that would produce r-value type.
@@ -1089,7 +1089,7 @@ bool MissingOptionalUnwrapFailure::diagnoseAsError() {
1089
1089
return true ;
1090
1090
}
1091
1091
1092
- auto *anchor = getAnchor (). get < const Expr *>( );
1092
+ auto *anchor = castToExpr ( getAnchor ());
1093
1093
1094
1094
// If this is an unresolved member expr e.g. `.foo` its
1095
1095
// base type is going to be the same as result type minus
@@ -1197,7 +1197,7 @@ bool MissingOptionalUnwrapFailure::diagnoseAsError() {
1197
1197
bool RValueTreatedAsLValueFailure::diagnoseAsError () {
1198
1198
Diag<StringRef> subElementDiagID;
1199
1199
Diag<Type> rvalueDiagID = diag::assignment_lhs_not_lvalue;
1200
- auto diagExpr = getRawAnchor (). get < const Expr *>( );
1200
+ auto diagExpr = castToExpr ( getRawAnchor ());
1201
1201
SourceLoc loc = diagExpr->getLoc ();
1202
1202
1203
1203
// Assignment is not allowed inside of a condition,
@@ -1398,7 +1398,7 @@ bool TypeChecker::diagnoseSelfAssignment(const Expr *expr) {
1398
1398
}
1399
1399
1400
1400
bool TrailingClosureAmbiguityFailure::diagnoseAsNote () {
1401
- auto *anchor = getAnchor (). get < const Expr *>( );
1401
+ auto *anchor = castToExpr ( getAnchor ());
1402
1402
const auto *expr = findParentExpr (anchor);
1403
1403
auto *callExpr = dyn_cast_or_null<CallExpr>(expr);
1404
1404
if (!callExpr)
@@ -2161,7 +2161,7 @@ bool ContextualFailure::diagnoseConversionToNil() const {
2161
2161
// It could be e.g. an argument to a subscript/call, assignment
2162
2162
// source like `s[0] = nil` or an array element like `[nil]` or
2163
2163
// `[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 ));
2165
2165
2166
2166
// Looks like it's something similar to `let _ = nil`.
2167
2167
if (!parentExpr) {
@@ -2194,9 +2194,8 @@ bool ContextualFailure::diagnoseConversionToNil() const {
2194
2194
// out whether nil is a "key" or a "value".
2195
2195
if (isa<DictionaryExpr>(enclosingExpr)) {
2196
2196
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;
2200
2199
} else {
2201
2200
// Can't initialize one of the tuple elements with `nil`.
2202
2201
CTP = CTP_Initialization;
@@ -2342,7 +2341,7 @@ bool ContextualFailure::diagnoseConversionToBool() const {
2342
2341
if (!toType->isBool ())
2343
2342
return false ;
2344
2343
2345
- auto *anchor = getAnchor (). get < const Expr *>( );
2344
+ auto *anchor = castToExpr ( getAnchor ());
2346
2345
// Check for "=" converting to Bool. The user probably meant ==.
2347
2346
if (auto *AE = dyn_cast<AssignExpr>(anchor->getValueProvidingExpr ())) {
2348
2347
emitDiagnosticAt (AE->getEqualLoc (), diag::use_of_equal_instead_of_equality)
@@ -2587,7 +2586,7 @@ bool ContextualFailure::tryRawRepresentableFixIts(
2587
2586
convWrapAfter += " )" ;
2588
2587
}
2589
2588
2590
- if (auto *E = anchor. dyn_cast < const Expr *>( ))
2589
+ if (auto *E = getAsExpr (anchor ))
2591
2590
fixIt (convWrapBefore, convWrapAfter, E);
2592
2591
return true ;
2593
2592
}
@@ -2614,7 +2613,7 @@ bool ContextualFailure::tryRawRepresentableFixIts(
2614
2613
convWrapAfter += " )" ;
2615
2614
}
2616
2615
2617
- if (auto *E = anchor. dyn_cast < const Expr *>( ))
2616
+ if (auto *E = getAsExpr (anchor ))
2618
2617
fixIt (convWrapBefore, convWrapAfter, E);
2619
2618
return true ;
2620
2619
}
@@ -2646,7 +2645,7 @@ bool ContextualFailure::tryIntegerCastFixIts(
2646
2645
return parenE->getSubExpr ();
2647
2646
};
2648
2647
2649
- if (auto *anchor = getAnchor (). dyn_cast < const Expr *>( )) {
2648
+ if (auto *anchor = getAsExpr ( getAnchor ())) {
2650
2649
if (Expr *innerE = getInnerCastedExpr (anchor)) {
2651
2650
Type innerTy = getType (innerE);
2652
2651
if (TypeChecker::isConvertibleTo (innerTy, toType, getDC ())) {
@@ -2683,8 +2682,7 @@ bool ContextualFailure::trySequenceSubsequenceFixIts(
2683
2682
// Wrap in String.init
2684
2683
if (getFromType ()->isEqual (Substring)) {
2685
2684
if (getToType ()->isEqual (String)) {
2686
- auto anchor =
2687
- getAnchor ().get <const Expr *>()->getSemanticsProvidingExpr ();
2685
+ auto *anchor = castToExpr (getAnchor ())->getSemanticsProvidingExpr ();
2688
2686
if (auto *CE = dyn_cast<CoerceExpr>(anchor)) {
2689
2687
anchor = CE->getSubExpr ();
2690
2688
}
@@ -3237,7 +3235,7 @@ bool MissingMemberFailure::diagnoseAsError() {
3237
3235
diagnostic = diag::could_not_find_tuple_member;
3238
3236
3239
3237
bool hasUnresolvedPattern = false ;
3240
- if (auto *E = anchor. dyn_cast < const Expr *>( )) {
3238
+ if (auto *E = getAsExpr (anchor )) {
3241
3239
const_cast <Expr *>(E)->forEachChildExpr ([&](Expr *expr) {
3242
3240
hasUnresolvedPattern |= isa<UnresolvedPatternExpr>(expr);
3243
3241
return hasUnresolvedPattern ? nullptr : expr;
@@ -3440,7 +3438,7 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() {
3440
3438
if (!anchor.is <const Expr *>())
3441
3439
return false ;
3442
3440
3443
- Expr *expr = findParentExpr (anchor. get < const Expr *>( ));
3441
+ Expr *expr = findParentExpr (castToExpr (anchor ));
3444
3442
SourceRange baseRange = expr ? expr->getSourceRange () : SourceRange ();
3445
3443
3446
3444
// If the base is an implicit self type reference, and we're in a
@@ -3544,7 +3542,7 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() {
3544
3542
}
3545
3543
}
3546
3544
3547
- if (auto *maybeCallExpr = getRawAnchor (). dyn_cast < const Expr *>( )) {
3545
+ if (auto *maybeCallExpr = getAsExpr ( getRawAnchor ())) {
3548
3546
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(maybeCallExpr)) {
3549
3547
maybeCallExpr = UDE->getBase ();
3550
3548
}
@@ -3686,8 +3684,7 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() {
3686
3684
3687
3685
// Check if the expression is the matching operator ~=, most often used in
3688
3686
// 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 ()));
3691
3688
3692
3689
// The '~=' operator is an overloaded decl ref inside a binaryExpr
3693
3690
if (auto binaryExpr = dyn_cast<BinaryExpr>(contextualTypeNode)) {
@@ -4980,7 +4977,7 @@ bool MissingGenericArgumentsFailure::diagnoseAsError() {
4980
4977
});
4981
4978
4982
4979
if (!isScoped)
4983
- return diagnoseForAnchor (getAnchor (). get < const Expr *>( ), Parameters);
4980
+ return diagnoseForAnchor (castToExpr ( getAnchor ()), Parameters);
4984
4981
4985
4982
bool diagnosed = false ;
4986
4983
for (const auto &scope : scopedParameters)
@@ -5238,7 +5235,7 @@ bool SkipUnhandledConstructInFunctionBuilderFailure::diagnoseAsNote() {
5238
5235
}
5239
5236
5240
5237
bool MutatingMemberRefOnImmutableBase::diagnoseAsError () {
5241
- auto *anchor = getRawAnchor (). get < const Expr *>( );
5238
+ auto *anchor = castToExpr ( getRawAnchor ());
5242
5239
auto baseExpr = getBaseExprFor (anchor);
5243
5240
if (!baseExpr)
5244
5241
return false ;
@@ -5377,7 +5374,7 @@ bool InOutConversionFailure::diagnoseAsError() {
5377
5374
}
5378
5375
5379
5376
void InOutConversionFailure::fixItChangeArgumentType () const {
5380
- auto *argExpr = getAnchor (). get < const Expr *>( );
5377
+ auto *argExpr = castToExpr ( getAnchor ());
5381
5378
auto *DC = getDC ();
5382
5379
5383
5380
if (auto *IOE = dyn_cast<InOutExpr>(argExpr))
@@ -5528,8 +5525,7 @@ bool ArgumentMismatchFailure::diagnoseUseOfReferenceEqualityOperator() const {
5528
5525
// If both arguments where incorrect e.g. both are function types,
5529
5526
// let's avoid producing a diagnostic second time, because first
5530
5527
// 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>()) {
5533
5529
auto &cs = getConstraintSystem ();
5534
5530
if (cs.hasFixFor (getConstraintLocator (
5535
5531
binaryOp, {ConstraintLocator::ApplyArgument,
@@ -5711,7 +5707,7 @@ void ExpandArrayIntoVarargsFailure::tryDropArrayBracketsFixIt(
5711
5707
}
5712
5708
5713
5709
bool ExpandArrayIntoVarargsFailure::diagnoseAsError () {
5714
- if (auto *anchor = getAnchor (). dyn_cast < const Expr *>( )) {
5710
+ if (auto *anchor = getAsExpr ( getAnchor ())) {
5715
5711
emitDiagnostic (diag::cannot_convert_array_to_variadic, getFromType (),
5716
5712
getToType ());
5717
5713
tryDropArrayBracketsFixIt (anchor);
@@ -5723,7 +5719,7 @@ bool ExpandArrayIntoVarargsFailure::diagnoseAsError() {
5723
5719
5724
5720
bool ExpandArrayIntoVarargsFailure::diagnoseAsNote () {
5725
5721
auto overload = getCalleeOverloadChoiceIfAvailable (getLocator ());
5726
- auto *anchor = getAnchor (). dyn_cast < const Expr *>( );
5722
+ auto *anchor = getAsExpr ( getAnchor ());
5727
5723
if (!overload || !anchor)
5728
5724
return false ;
5729
5725
@@ -6054,7 +6050,7 @@ bool AssignmentTypeMismatchFailure::diagnoseAsNote() {
6054
6050
}
6055
6051
6056
6052
bool MissingContextualBaseInMemberRefFailure::diagnoseAsError () {
6057
- auto *anchor = getAnchor (). get < const Expr *>( );
6053
+ auto *anchor = castToExpr ( getAnchor ());
6058
6054
// Member reference could be wrapped into a number of parens
6059
6055
// e.g. `((.foo))`.
6060
6056
auto *parentExpr = findParentExpr (anchor);
0 commit comments