@@ -2120,7 +2120,9 @@ bool ContextualFailure::diagnoseAsError() {
21202120 }
21212121
21222122 if (isExpr<AssignExpr>(anchor)) {
2123- emitDiagnostic (diag::cannot_convert_assign, getFromType (), getToType ());
2123+ auto diagnostic = emitDiagnostic (diag::cannot_convert_assign,
2124+ getFromType (), getToType ());
2125+ tryIntegerCastFixIts (diagnostic);
21242126 return true ;
21252127 }
21262128
@@ -2729,6 +2731,15 @@ bool ContextualFailure::tryIntegerCastFixIts(
27292731 auto fromType = getFromType ();
27302732 auto toType = getToType ();
27312733
2734+ auto anchor = getAnchor ();
2735+ auto exprRange = getSourceRange ();
2736+
2737+ if (auto *assignment = getAsExpr<AssignExpr>(anchor)) {
2738+ toType = toType->lookThroughAllOptionalTypes ();
2739+ anchor = assignment->getSrc ();
2740+ exprRange = assignment->getSrc ()->getSourceRange ();
2741+ }
2742+
27322743 if (!isIntegerType (fromType) || !isIntegerType (toType))
27332744 return false ;
27342745
@@ -2747,8 +2758,8 @@ bool ContextualFailure::tryIntegerCastFixIts(
27472758 return parenE->getSubExpr ();
27482759 };
27492760
2750- if (auto *anchor = getAsExpr (getAnchor () )) {
2751- if (Expr *innerE = getInnerCastedExpr (anchor )) {
2761+ if (auto *expr = getAsExpr (anchor )) {
2762+ if (Expr *innerE = getInnerCastedExpr (expr )) {
27522763 Type innerTy = getType (innerE);
27532764 if (TypeChecker::isConvertibleTo (innerTy, toType, getDC ())) {
27542765 // Remove the unnecessary cast.
@@ -2763,7 +2774,6 @@ bool ContextualFailure::tryIntegerCastFixIts(
27632774 std::string convWrapBefore = toType.getString ();
27642775 convWrapBefore += " (" ;
27652776 std::string convWrapAfter = " )" ;
2766- SourceRange exprRange = getSourceRange ();
27672777 diagnostic.fixItInsert (exprRange.Start , convWrapBefore);
27682778 diagnostic.fixItInsertAfter (exprRange.End , convWrapAfter);
27692779 return true ;
0 commit comments