@@ -786,7 +786,8 @@ bool GenericArgumentsMismatchFailure::diagnoseAsError() {
786786 //
787787 // `value` has to get implicitly wrapped into 2 optionals
788788 // before pointer types could be compared.
789- auto path = getLocator ()->getPath ();
789+ auto locator = getLocator ();
790+ auto path = locator->getPath ();
790791 unsigned toDrop = 0 ;
791792 for (const auto &elt : llvm::reverse (path)) {
792793 if (!elt.is <LocatorPathElt::OptionalPayload>())
@@ -802,7 +803,7 @@ bool GenericArgumentsMismatchFailure::diagnoseAsError() {
802803 if (path.empty ()) {
803804 if (isExpr<AssignExpr>(anchor)) {
804805 diagnostic = getDiagnosticFor (CTP_AssignSource);
805- } else if (isExpr<CoerceExpr>(anchor )) {
806+ } else if (locator-> isForCoercion ( )) {
806807 diagnostic = getDiagnosticFor (CTP_CoerceOperand);
807808 } else {
808809 return false ;
@@ -887,6 +888,11 @@ bool GenericArgumentsMismatchFailure::diagnoseAsError() {
887888 break ;
888889 }
889890
891+ case ConstraintLocator::CoercionOperand: {
892+ diagnostic = getDiagnosticFor (CTP_CoerceOperand);
893+ break ;
894+ }
895+
890896 default :
891897 break ;
892898 }
@@ -1222,17 +1228,18 @@ ASTNode InvalidCoercionFailure::getAnchor() const {
12221228bool InvalidCoercionFailure::diagnoseAsError () {
12231229 auto fromType = getFromType ();
12241230 auto toType = getToType ();
1231+ auto *CE = getAsExpr<CoerceExpr>(getRawAnchor ());
12251232
12261233 emitDiagnostic (diag::cannot_coerce_to_type, fromType, toType);
12271234
12281235 if (UseConditionalCast) {
12291236 emitDiagnostic (diag::missing_optional_downcast)
1230- .highlight (getSourceRange ())
1231- .fixItReplace (getLoc (), " as?" );
1237+ .highlight (CE-> getSourceRange ())
1238+ .fixItReplace (CE-> getAsLoc (), " as?" );
12321239 } else {
12331240 emitDiagnostic (diag::missing_forced_downcast)
1234- .highlight (getSourceRange ())
1235- .fixItReplace (getLoc (), " as!" );
1241+ .highlight (CE-> getSourceRange ())
1242+ .fixItReplace (CE-> getAsLoc (), " as!" );
12361243 }
12371244
12381245 return true ;
@@ -2487,7 +2494,7 @@ bool ContextualFailure::diagnoseAsError() {
24872494 diagnostic = diag::cannot_convert_condition_value;
24882495 break ;
24892496 }
2490-
2497+ case ConstraintLocator::CoercionOperand:
24912498 case ConstraintLocator::InstanceType: {
24922499 if (diagnoseCoercionToUnrelatedType ())
24932500 return true ;
@@ -2655,7 +2662,7 @@ bool ContextualFailure::diagnoseAsError() {
26552662}
26562663
26572664bool ContextualFailure::diagnoseAsNote () {
2658- auto *locator = getLocator ();
2665+ auto *locator = getAmbiguityLocator ();
26592666
26602667 auto overload = getCalleeOverloadChoiceIfAvailable (locator);
26612668 if (!(overload && overload->choice .isDecl ()))
@@ -2809,7 +2816,7 @@ bool ContextualFailure::diagnoseConversionToNil() const {
28092816 emitDiagnostic (diag::unresolved_nil_literal);
28102817 return true ;
28112818 }
2812- } else if (isa<CoerceExpr>(parentExpr )) {
2819+ } else if (locator-> isForCoercion ( )) {
28132820 // `nil` is passed as a left-hand side of the coercion
28142821 // operator e.g. `nil as Foo`
28152822 CTP = CTP_CoerceOperand;
@@ -2891,23 +2898,23 @@ bool ContextualFailure::diagnoseExtraneousAssociatedValues() const {
28912898}
28922899
28932900bool ContextualFailure::diagnoseCoercionToUnrelatedType () const {
2894- auto anchor = getAnchor ();
2901+ auto anchor = getRawAnchor ();
2902+ auto *coerceExpr = getAsExpr<CoerceExpr>(anchor);
2903+ if (!coerceExpr) {
2904+ return false ;
2905+ }
28952906
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 ());
28992909
2900- auto diagnostic = getDiagnosticFor (CTP_CoerceOperand, toType);
2910+ auto diagnostic = getDiagnosticFor (CTP_CoerceOperand, toType);
29012911
2902- auto diag = emitDiagnostic (*diagnostic, fromType, toType);
2903- diag.highlight (getSourceRange ());
2912+ auto diag = emitDiagnostic (*diagnostic, fromType, toType);
2913+ diag.highlight (getSourceRange ());
29042914
2905- (void )tryFixIts (diag);
2906-
2907- return true ;
2908- }
2915+ (void )tryFixIts (diag);
29092916
2910- return false ;
2917+ return true ;
29112918}
29122919
29132920bool ContextualFailure::diagnoseConversionToBool () const {
@@ -3178,9 +3185,6 @@ bool ContextualFailure::trySequenceSubsequenceFixIts(
31783185 if (getFromType ()->isSubstring ()) {
31793186 if (getToType ()->isString ()) {
31803187 auto *anchor = castToExpr (getAnchor ())->getSemanticsProvidingExpr ();
3181- if (auto *CE = dyn_cast<CoerceExpr>(anchor)) {
3182- anchor = CE->getSubExpr ();
3183- }
31843188
31853189 if (auto *call = dyn_cast<CallExpr>(anchor)) {
31863190 auto *fnExpr = call->getFn ();
@@ -4927,7 +4931,7 @@ bool MissingArgumentsFailure::diagnoseAsError() {
49274931}
49284932
49294933bool MissingArgumentsFailure::diagnoseAsNote () {
4930- auto *locator = getLocator ();
4934+ auto *locator = getAmbiguityLocator ();
49314935 if (auto overload = getCalleeOverloadChoiceIfAvailable (locator)) {
49324936 auto *fn = resolveType (overload->adjustedOpenedType )->getAs <AnyFunctionType>();
49334937 auto loc = overload->choice .getDecl ()->getLoc ();
@@ -5729,7 +5733,7 @@ bool ExtraneousArgumentsFailure::diagnoseAsError() {
57295733}
57305734
57315735bool ExtraneousArgumentsFailure::diagnoseAsNote () {
5732- auto overload = getCalleeOverloadChoiceIfAvailable (getLocator ());
5736+ auto overload = getCalleeOverloadChoiceIfAvailable (getAmbiguityLocator ());
57335737 if (!(overload && overload->choice .isDecl ()))
57345738 return false ;
57355739
@@ -5741,7 +5745,7 @@ bool ExtraneousArgumentsFailure::diagnoseAsNote() {
57415745 (numArgs == 1 ));
57425746 } else {
57435747 emitDiagnosticAt (decl, diag::candidate_with_extraneous_args,
5744- decl-> getInterfaceType () , numArgs, ContextualType,
5748+ overload-> adjustedOpenedType , numArgs, ContextualType,
57455749 ContextualType->getNumParams ());
57465750 }
57475751 return true ;
@@ -8758,12 +8762,7 @@ GlobalActorFunctionMismatchFailure::getDiagnosticMessage() const {
87588762 auto path = locator->getPath ();
87598763
87608764 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;
87678766 }
87688767
87698768 auto last = path.back ();
@@ -8781,6 +8780,9 @@ GlobalActorFunctionMismatchFailure::getDiagnosticMessage() const {
87818780 case ConstraintLocator::TernaryBranch: {
87828781 return diag::ternary_expr_cases_global_actor_mismatch;
87838782 }
8783+ case ConstraintLocator::CoercionOperand: {
8784+ return diag::cannot_convert_global_actor_coercion;
8785+ }
87848786 default :
87858787 break ;
87868788 }
0 commit comments