@@ -786,7 +786,8 @@ bool GenericArgumentsMismatchFailure::diagnoseAsError() {
786
786
//
787
787
// `value` has to get implicitly wrapped into 2 optionals
788
788
// before pointer types could be compared.
789
- auto path = getLocator ()->getPath ();
789
+ auto locator = getLocator ();
790
+ auto path = locator->getPath ();
790
791
unsigned toDrop = 0 ;
791
792
for (const auto &elt : llvm::reverse (path)) {
792
793
if (!elt.is <LocatorPathElt::OptionalPayload>())
@@ -802,7 +803,7 @@ bool GenericArgumentsMismatchFailure::diagnoseAsError() {
802
803
if (path.empty ()) {
803
804
if (isExpr<AssignExpr>(anchor)) {
804
805
diagnostic = getDiagnosticFor (CTP_AssignSource);
805
- } else if (isExpr<CoerceExpr>(anchor )) {
806
+ } else if (locator-> isForCoercion ( )) {
806
807
diagnostic = getDiagnosticFor (CTP_CoerceOperand);
807
808
} else {
808
809
return false ;
@@ -887,6 +888,11 @@ bool GenericArgumentsMismatchFailure::diagnoseAsError() {
887
888
break ;
888
889
}
889
890
891
+ case ConstraintLocator::CoercionOperand: {
892
+ diagnostic = getDiagnosticFor (CTP_CoerceOperand);
893
+ break ;
894
+ }
895
+
890
896
default :
891
897
break ;
892
898
}
@@ -1222,17 +1228,18 @@ ASTNode InvalidCoercionFailure::getAnchor() const {
1222
1228
bool InvalidCoercionFailure::diagnoseAsError () {
1223
1229
auto fromType = getFromType ();
1224
1230
auto toType = getToType ();
1231
+ auto *CE = getAsExpr<CoerceExpr>(getRawAnchor ());
1225
1232
1226
1233
emitDiagnostic (diag::cannot_coerce_to_type, fromType, toType);
1227
1234
1228
1235
if (UseConditionalCast) {
1229
1236
emitDiagnostic (diag::missing_optional_downcast)
1230
- .highlight (getSourceRange ())
1231
- .fixItReplace (getLoc (), " as?" );
1237
+ .highlight (CE-> getSourceRange ())
1238
+ .fixItReplace (CE-> getAsLoc (), " as?" );
1232
1239
} else {
1233
1240
emitDiagnostic (diag::missing_forced_downcast)
1234
- .highlight (getSourceRange ())
1235
- .fixItReplace (getLoc (), " as!" );
1241
+ .highlight (CE-> getSourceRange ())
1242
+ .fixItReplace (CE-> getAsLoc (), " as!" );
1236
1243
}
1237
1244
1238
1245
return true ;
@@ -2487,7 +2494,7 @@ bool ContextualFailure::diagnoseAsError() {
2487
2494
diagnostic = diag::cannot_convert_condition_value;
2488
2495
break ;
2489
2496
}
2490
-
2497
+ case ConstraintLocator::CoercionOperand:
2491
2498
case ConstraintLocator::InstanceType: {
2492
2499
if (diagnoseCoercionToUnrelatedType ())
2493
2500
return true ;
@@ -2655,7 +2662,7 @@ bool ContextualFailure::diagnoseAsError() {
2655
2662
}
2656
2663
2657
2664
bool ContextualFailure::diagnoseAsNote () {
2658
- auto *locator = getLocator ();
2665
+ auto *locator = getAmbiguityLocator ();
2659
2666
2660
2667
auto overload = getCalleeOverloadChoiceIfAvailable (locator);
2661
2668
if (!(overload && overload->choice .isDecl ()))
@@ -2809,7 +2816,7 @@ bool ContextualFailure::diagnoseConversionToNil() const {
2809
2816
emitDiagnostic (diag::unresolved_nil_literal);
2810
2817
return true ;
2811
2818
}
2812
- } else if (isa<CoerceExpr>(parentExpr )) {
2819
+ } else if (locator-> isForCoercion ( )) {
2813
2820
// `nil` is passed as a left-hand side of the coercion
2814
2821
// operator e.g. `nil as Foo`
2815
2822
CTP = CTP_CoerceOperand;
@@ -2891,23 +2898,23 @@ bool ContextualFailure::diagnoseExtraneousAssociatedValues() const {
2891
2898
}
2892
2899
2893
2900
bool ContextualFailure::diagnoseCoercionToUnrelatedType () const {
2894
- auto anchor = getAnchor ();
2901
+ auto anchor = getRawAnchor ();
2902
+ auto *coerceExpr = getAsExpr<CoerceExpr>(anchor);
2903
+ if (!coerceExpr) {
2904
+ return false ;
2905
+ }
2895
2906
2896
- if (auto *coerceExpr = getAsExpr<CoerceExpr>(anchor)) {
2897
- const auto fromType = getType (coerceExpr->getSubExpr ());
2898
- const auto toType = getType (coerceExpr->getCastTypeRepr ());
2907
+ const auto fromType = getType (coerceExpr->getSubExpr ());
2908
+ const auto toType = getType (coerceExpr->getCastTypeRepr ());
2899
2909
2900
- auto diagnostic = getDiagnosticFor (CTP_CoerceOperand, toType);
2910
+ auto diagnostic = getDiagnosticFor (CTP_CoerceOperand, toType);
2901
2911
2902
- auto diag = emitDiagnostic (*diagnostic, fromType, toType);
2903
- diag.highlight (getSourceRange ());
2912
+ auto diag = emitDiagnostic (*diagnostic, fromType, toType);
2913
+ diag.highlight (getSourceRange ());
2904
2914
2905
- (void )tryFixIts (diag);
2906
-
2907
- return true ;
2908
- }
2915
+ (void )tryFixIts (diag);
2909
2916
2910
- return false ;
2917
+ return true ;
2911
2918
}
2912
2919
2913
2920
bool ContextualFailure::diagnoseConversionToBool () const {
@@ -3178,9 +3185,6 @@ bool ContextualFailure::trySequenceSubsequenceFixIts(
3178
3185
if (getFromType ()->isSubstring ()) {
3179
3186
if (getToType ()->isString ()) {
3180
3187
auto *anchor = castToExpr (getAnchor ())->getSemanticsProvidingExpr ();
3181
- if (auto *CE = dyn_cast<CoerceExpr>(anchor)) {
3182
- anchor = CE->getSubExpr ();
3183
- }
3184
3188
3185
3189
if (auto *call = dyn_cast<CallExpr>(anchor)) {
3186
3190
auto *fnExpr = call->getFn ();
@@ -4927,7 +4931,7 @@ bool MissingArgumentsFailure::diagnoseAsError() {
4927
4931
}
4928
4932
4929
4933
bool MissingArgumentsFailure::diagnoseAsNote () {
4930
- auto *locator = getLocator ();
4934
+ auto *locator = getAmbiguityLocator ();
4931
4935
if (auto overload = getCalleeOverloadChoiceIfAvailable (locator)) {
4932
4936
auto *fn = resolveType (overload->adjustedOpenedType )->getAs <AnyFunctionType>();
4933
4937
auto loc = overload->choice .getDecl ()->getLoc ();
@@ -5729,7 +5733,7 @@ bool ExtraneousArgumentsFailure::diagnoseAsError() {
5729
5733
}
5730
5734
5731
5735
bool ExtraneousArgumentsFailure::diagnoseAsNote () {
5732
- auto overload = getCalleeOverloadChoiceIfAvailable (getLocator ());
5736
+ auto overload = getCalleeOverloadChoiceIfAvailable (getAmbiguityLocator ());
5733
5737
if (!(overload && overload->choice .isDecl ()))
5734
5738
return false ;
5735
5739
@@ -5741,7 +5745,7 @@ bool ExtraneousArgumentsFailure::diagnoseAsNote() {
5741
5745
(numArgs == 1 ));
5742
5746
} else {
5743
5747
emitDiagnosticAt (decl, diag::candidate_with_extraneous_args,
5744
- decl-> getInterfaceType () , numArgs, ContextualType,
5748
+ overload-> adjustedOpenedType , numArgs, ContextualType,
5745
5749
ContextualType->getNumParams ());
5746
5750
}
5747
5751
return true ;
@@ -8758,12 +8762,7 @@ GlobalActorFunctionMismatchFailure::getDiagnosticMessage() const {
8758
8762
auto path = locator->getPath ();
8759
8763
8760
8764
if (path.empty ()) {
8761
- auto anchor = getAnchor ();
8762
- if (isExpr<CoerceExpr>(anchor)) {
8763
- return diag::cannot_convert_global_actor_coercion;
8764
- } else {
8765
- return diag::cannot_convert_global_actor;
8766
- }
8765
+ return diag::cannot_convert_global_actor;
8767
8766
}
8768
8767
8769
8768
auto last = path.back ();
@@ -8781,6 +8780,9 @@ GlobalActorFunctionMismatchFailure::getDiagnosticMessage() const {
8781
8780
case ConstraintLocator::TernaryBranch: {
8782
8781
return diag::ternary_expr_cases_global_actor_mismatch;
8783
8782
}
8783
+ case ConstraintLocator::CoercionOperand: {
8784
+ return diag::cannot_convert_global_actor_coercion;
8785
+ }
8784
8786
default :
8785
8787
break ;
8786
8788
}
0 commit comments