@@ -282,6 +282,9 @@ ValueDecl *RequirementFailure::getDeclRef() const {
282282 return getAffectedDeclFromType (contextualTy);
283283 }
284284
285+ if (getLocator ()->isFirstElement <LocatorPathElt::CoercionOperand>())
286+ return getAffectedDeclFromType (getOwnerType ());
287+
285288 if (auto overload = getCalleeOverloadChoiceIfAvailable (getLocator ())) {
286289 // If there is a declaration associated with this
287290 // failure e.g. an overload choice of the call
@@ -786,7 +789,8 @@ bool GenericArgumentsMismatchFailure::diagnoseAsError() {
786789 //
787790 // `value` has to get implicitly wrapped into 2 optionals
788791 // before pointer types could be compared.
789- auto path = getLocator ()->getPath ();
792+ auto locator = getLocator ();
793+ auto path = locator->getPath ();
790794 unsigned toDrop = 0 ;
791795 for (const auto &elt : llvm::reverse (path)) {
792796 if (!elt.is <LocatorPathElt::OptionalPayload>())
@@ -802,7 +806,7 @@ bool GenericArgumentsMismatchFailure::diagnoseAsError() {
802806 if (path.empty ()) {
803807 if (isExpr<AssignExpr>(anchor)) {
804808 diagnostic = getDiagnosticFor (CTP_AssignSource);
805- } else if (isExpr<CoerceExpr>(anchor )) {
809+ } else if (locator-> isForCoercion ( )) {
806810 diagnostic = getDiagnosticFor (CTP_CoerceOperand);
807811 } else {
808812 return false ;
@@ -887,6 +891,11 @@ bool GenericArgumentsMismatchFailure::diagnoseAsError() {
887891 break ;
888892 }
889893
894+ case ConstraintLocator::CoercionOperand: {
895+ diagnostic = getDiagnosticFor (CTP_CoerceOperand);
896+ break ;
897+ }
898+
890899 default :
891900 break ;
892901 }
@@ -1219,6 +1228,14 @@ ASTNode InvalidCoercionFailure::getAnchor() const {
12191228 return anchor;
12201229}
12211230
1231+ SourceLoc InvalidCoercionFailure::getLoc () const {
1232+ if (getLocator ()->isForCoercion ()) {
1233+ auto *CE = castToExpr<CoerceExpr>(getRawAnchor ());
1234+ return CE->getAsLoc ();
1235+ }
1236+ return FailureDiagnostic::getLoc ();
1237+ }
1238+
12221239bool InvalidCoercionFailure::diagnoseAsError () {
12231240 auto fromType = getFromType ();
12241241 auto toType = getToType ();
@@ -2487,7 +2504,7 @@ bool ContextualFailure::diagnoseAsError() {
24872504 diagnostic = diag::cannot_convert_condition_value;
24882505 break ;
24892506 }
2490-
2507+ case ConstraintLocator::CoercionOperand:
24912508 case ConstraintLocator::InstanceType: {
24922509 if (diagnoseCoercionToUnrelatedType ())
24932510 return true ;
@@ -2809,7 +2826,7 @@ bool ContextualFailure::diagnoseConversionToNil() const {
28092826 emitDiagnostic (diag::unresolved_nil_literal);
28102827 return true ;
28112828 }
2812- } else if (isa<CoerceExpr>(parentExpr )) {
2829+ } else if (locator-> isForCoercion ( )) {
28132830 // `nil` is passed as a left-hand side of the coercion
28142831 // operator e.g. `nil as Foo`
28152832 CTP = CTP_CoerceOperand;
@@ -2891,23 +2908,23 @@ bool ContextualFailure::diagnoseExtraneousAssociatedValues() const {
28912908}
28922909
28932910bool ContextualFailure::diagnoseCoercionToUnrelatedType () const {
2894- auto anchor = getAnchor ();
2911+ auto anchor = getRawAnchor ();
2912+ auto *coerceExpr = getAsExpr<CoerceExpr>(anchor);
2913+ if (!coerceExpr) {
2914+ return false ;
2915+ }
28952916
2896- if (auto *coerceExpr = getAsExpr<CoerceExpr>(anchor)) {
2897- const auto fromType = getType (coerceExpr->getSubExpr ());
2898- const auto toType = getType (coerceExpr->getCastTypeRepr ());
2917+ const auto fromType = getType (coerceExpr->getSubExpr ());
2918+ const auto toType = getType (coerceExpr->getCastTypeRepr ());
28992919
2900- auto diagnostic = getDiagnosticFor (CTP_CoerceOperand, toType);
2920+ auto diagnostic = getDiagnosticFor (CTP_CoerceOperand, toType);
29012921
2902- auto diag = emitDiagnostic (*diagnostic, fromType, toType);
2903- diag.highlight (getSourceRange ());
2922+ auto diag = emitDiagnostic (*diagnostic, fromType, toType);
2923+ diag.highlight (getSourceRange ());
29042924
2905- (void )tryFixIts (diag);
2906-
2907- return true ;
2908- }
2925+ (void )tryFixIts (diag);
29092926
2910- return false ;
2927+ return true ;
29112928}
29122929
29132930bool ContextualFailure::diagnoseConversionToBool () const {
@@ -3178,9 +3195,6 @@ bool ContextualFailure::trySequenceSubsequenceFixIts(
31783195 if (getFromType ()->isSubstring ()) {
31793196 if (getToType ()->isString ()) {
31803197 auto *anchor = castToExpr (getAnchor ())->getSemanticsProvidingExpr ();
3181- if (auto *CE = dyn_cast<CoerceExpr>(anchor)) {
3182- anchor = CE->getSubExpr ();
3183- }
31843198
31853199 if (auto *call = dyn_cast<CallExpr>(anchor)) {
31863200 auto *fnExpr = call->getFn ();
@@ -5860,9 +5874,15 @@ bool ExtraneousArgumentsFailure::diagnoseAsNote() {
58605874
58615875 auto *decl = overload->choice .getDecl ();
58625876 auto numArgs = getTotalNumArguments ();
5863- emitDiagnosticAt (decl, diag::candidate_with_extraneous_args, ContextualType,
5864- ContextualType->getNumParams (), numArgs, (numArgs == 1 ),
5865- isExpr<ClosureExpr>(getAnchor ()));
5877+ if (isExpr<ClosureExpr>(getAnchor ())) {
5878+ emitDiagnosticAt (decl, diag::candidate_with_extraneous_args_closure,
5879+ ContextualType, ContextualType->getNumParams (), numArgs,
5880+ (numArgs == 1 ));
5881+ } else {
5882+ emitDiagnosticAt (decl, diag::candidate_with_extraneous_args,
5883+ overload->adjustedOpenedType , numArgs, ContextualType,
5884+ ContextualType->getNumParams ());
5885+ }
58665886 return true ;
58675887}
58685888
@@ -8878,12 +8898,7 @@ GlobalActorFunctionMismatchFailure::getDiagnosticMessage() const {
88788898 auto path = locator->getPath ();
88798899
88808900 if (path.empty ()) {
8881- auto anchor = getAnchor ();
8882- if (isExpr<CoerceExpr>(anchor)) {
8883- return diag::cannot_convert_global_actor_coercion;
8884- } else {
8885- return diag::cannot_convert_global_actor;
8886- }
8901+ return diag::cannot_convert_global_actor;
88878902 }
88888903
88898904 auto last = path.back ();
@@ -8901,6 +8916,9 @@ GlobalActorFunctionMismatchFailure::getDiagnosticMessage() const {
89018916 case ConstraintLocator::TernaryBranch: {
89028917 return diag::ternary_expr_cases_global_actor_mismatch;
89038918 }
8919+ case ConstraintLocator::CoercionOperand: {
8920+ return diag::cannot_convert_global_actor_coercion;
8921+ }
89048922 default :
89058923 break ;
89068924 }
0 commit comments