@@ -3407,8 +3407,10 @@ static bool isIntegerToStringIndexConversion(Type fromType, Type toType,
3407
3407
// / expected, by wrapping the expression in a call to the rawValue
3408
3408
// / accessor
3409
3409
// /
3410
+ // / - Return true on the fixit is added, false otherwise.
3411
+ // /
3410
3412
// / This helps migration with SDK changes.
3411
- static void tryRawRepresentableFixIts (InFlightDiagnostic &diag,
3413
+ static bool tryRawRepresentableFixIts (InFlightDiagnostic &diag,
3412
3414
ConstraintSystem *CS,
3413
3415
Type fromType,
3414
3416
Type toType,
@@ -3459,7 +3461,7 @@ static void tryRawRepresentableFixIts(InFlightDiagnostic &diag,
3459
3461
convWrapAfter += " )" ;
3460
3462
}
3461
3463
fixIt (convWrapBefore, convWrapAfter);
3462
- return ;
3464
+ return true ;
3463
3465
}
3464
3466
}
3465
3467
@@ -3473,9 +3475,10 @@ static void tryRawRepresentableFixIts(InFlightDiagnostic &diag,
3473
3475
convWrapAfter += " )" ;
3474
3476
}
3475
3477
fixIt (convWrapBefore, convWrapAfter);
3476
- return ;
3478
+ return true ;
3477
3479
}
3478
3480
}
3481
+ return false ;
3479
3482
}
3480
3483
3481
3484
// / Attempts to add fix-its for these two mistakes:
@@ -3486,14 +3489,16 @@ static void tryRawRepresentableFixIts(InFlightDiagnostic &diag,
3486
3489
// / - Passing an integer but expecting different integer type. The fixit adds
3487
3490
// / a wrapping cast.
3488
3491
// /
3492
+ // / - Return true on the fixit is added, false otherwise.
3493
+ // /
3489
3494
// / This helps migration with SDK changes.
3490
- static void tryIntegerCastFixIts (InFlightDiagnostic &diag,
3495
+ static bool tryIntegerCastFixIts (InFlightDiagnostic &diag,
3491
3496
ConstraintSystem *CS,
3492
3497
Type fromType,
3493
3498
Type toType,
3494
3499
Expr *expr) {
3495
3500
if (!isIntegerType (fromType, CS) || !isIntegerType (toType, CS))
3496
- return ;
3501
+ return false ;
3497
3502
3498
3503
auto getInnerCastedExpr = [&]() -> Expr* {
3499
3504
CallExpr *CE = dyn_cast<CallExpr>(expr);
@@ -3513,7 +3518,7 @@ static void tryIntegerCastFixIts(InFlightDiagnostic &diag,
3513
3518
// Remove the unnecessary cast.
3514
3519
diag.fixItRemoveChars (expr->getLoc (), innerE->getStartLoc ())
3515
3520
.fixItRemove (expr->getEndLoc ());
3516
- return ;
3521
+ return true ;
3517
3522
}
3518
3523
}
3519
3524
@@ -3524,6 +3529,24 @@ static void tryIntegerCastFixIts(InFlightDiagnostic &diag,
3524
3529
SourceRange exprRange = expr->getSourceRange ();
3525
3530
diag.fixItInsert (exprRange.Start , convWrapBefore);
3526
3531
diag.fixItInsertAfter (exprRange.End , convWrapAfter);
3532
+ return true ;
3533
+ }
3534
+
3535
+ static bool
3536
+ addTypeCoerceFixit (InFlightDiagnostic &diag, ConstraintSystem *CS,
3537
+ Type fromType, Type toType, Expr *expr) {
3538
+ if (CS->getTypeChecker ().typeCheckCheckedCast (fromType, toType, CS->DC ,
3539
+ SourceLoc (), SourceRange (), SourceRange (), [](Type T) { return false ; },
3540
+ true ) != CheckedCastKind::Unresolved) {
3541
+ SmallString<32 > buffer;
3542
+ llvm::raw_svector_ostream OS (buffer);
3543
+ toType->print (OS);
3544
+ diag.fixItInsert (Lexer::getLocForEndOfToken (CS->DC ->getASTContext ().SourceMgr ,
3545
+ expr->getEndLoc ()),
3546
+ (llvm::Twine (" as! " ) + OS.str ()).str ());
3547
+ return true ;
3548
+ }
3549
+ return false ;
3527
3550
}
3528
3551
3529
3552
bool FailureDiagnosis::diagnoseContextualConversionError () {
@@ -3800,11 +3823,12 @@ bool FailureDiagnosis::diagnoseContextualConversionError() {
3800
3823
case CTP_DictionaryValue:
3801
3824
tryRawRepresentableFixIts (diag, CS, exprType, contextualType,
3802
3825
KnownProtocolKind::ExpressibleByIntegerLiteral,
3803
- expr);
3826
+ expr) ||
3804
3827
tryRawRepresentableFixIts (diag, CS, exprType, contextualType,
3805
3828
KnownProtocolKind::ExpressibleByStringLiteral,
3806
- expr);
3807
- tryIntegerCastFixIts (diag, CS, exprType, contextualType, expr);
3829
+ expr) ||
3830
+ tryIntegerCastFixIts (diag, CS, exprType, contextualType, expr) ||
3831
+ addTypeCoerceFixit (diag, CS, exprType, contextualType, expr);
3808
3832
break ;
3809
3833
3810
3834
default :
0 commit comments