@@ -2120,7 +2120,9 @@ bool ContextualFailure::diagnoseAsError() {
2120
2120
}
2121
2121
2122
2122
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);
2124
2126
return true ;
2125
2127
}
2126
2128
@@ -2729,6 +2731,15 @@ bool ContextualFailure::tryIntegerCastFixIts(
2729
2731
auto fromType = getFromType ();
2730
2732
auto toType = getToType ();
2731
2733
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
+
2732
2743
if (!isIntegerType (fromType) || !isIntegerType (toType))
2733
2744
return false ;
2734
2745
@@ -2747,8 +2758,8 @@ bool ContextualFailure::tryIntegerCastFixIts(
2747
2758
return parenE->getSubExpr ();
2748
2759
};
2749
2760
2750
- if (auto *anchor = getAsExpr (getAnchor () )) {
2751
- if (Expr *innerE = getInnerCastedExpr (anchor )) {
2761
+ if (auto *expr = getAsExpr (anchor )) {
2762
+ if (Expr *innerE = getInnerCastedExpr (expr )) {
2752
2763
Type innerTy = getType (innerE);
2753
2764
if (TypeChecker::isConvertibleTo (innerTy, toType, getDC ())) {
2754
2765
// Remove the unnecessary cast.
@@ -2763,7 +2774,6 @@ bool ContextualFailure::tryIntegerCastFixIts(
2763
2774
std::string convWrapBefore = toType.getString ();
2764
2775
convWrapBefore += " (" ;
2765
2776
std::string convWrapAfter = " )" ;
2766
- SourceRange exprRange = getSourceRange ();
2767
2777
diagnostic.fixItInsert (exprRange.Start , convWrapBefore);
2768
2778
diagnostic.fixItInsertAfter (exprRange.End , convWrapAfter);
2769
2779
return true ;
0 commit comments