@@ -3530,12 +3530,9 @@ namespace {
3530
3530
// Convert the subexpression.
3531
3531
Expr *sub = expr->getSubExpr ();
3532
3532
3533
- solution.setExprTypes (sub);
3534
-
3535
- if (TypeChecker::convertToType (sub, toType, cs.DC ))
3533
+ sub = solution.coerceToType (sub, toType, cs.getConstraintLocator (sub));
3534
+ if (!sub)
3536
3535
return nullptr ;
3537
-
3538
- cs.cacheExprTypes (sub);
3539
3536
3540
3537
expr->setSubExpr (sub);
3541
3538
cs.setType (expr, toType);
@@ -3586,9 +3583,6 @@ namespace {
3586
3583
return nullptr ;
3587
3584
case CheckedCastKind::Coercion:
3588
3585
case CheckedCastKind::BridgingCoercion: {
3589
- if (SuppressDiagnostics)
3590
- return nullptr ;
3591
-
3592
3586
if (cs.getType (sub)->isEqual (toType)) {
3593
3587
ctx.Diags .diagnose (expr->getLoc (), diag::forced_downcast_noop, toType)
3594
3588
.fixItRemove (SourceRange (
@@ -3602,14 +3596,9 @@ namespace {
3602
3596
" as" );
3603
3597
}
3604
3598
3605
- // Transmute the checked cast into a coercion expression.
3606
- auto *result =
3607
- new (ctx) CoerceExpr (sub, expr->getLoc (), expr->getCastTypeLoc ());
3608
- cs.setType (result, toType);
3609
- cs.setType (result->getCastTypeLoc (), toType);
3610
- unsigned disjunctionChoice =
3611
- (castKind == CheckedCastKind::Coercion ? 0 : 1 );
3612
- return visitCoerceExpr (result, disjunctionChoice);
3599
+ expr->setCastKind (castKind);
3600
+ cs.setType (expr, toType);
3601
+ return expr;
3613
3602
}
3614
3603
3615
3604
// Valid casts.
@@ -3662,37 +3651,18 @@ namespace {
3662
3651
fromType, toType, castContextKind, cs.DC , expr->getLoc (), sub,
3663
3652
expr->getCastTypeLoc ().getSourceRange ());
3664
3653
switch (castKind) {
3665
- / / / Invalid cast.
3654
+ // Invalid cast.
3666
3655
case CheckedCastKind::Unresolved:
3667
3656
expr->setCastKind (CheckedCastKind::ValueCast);
3668
3657
break ;
3669
3658
3670
3659
case CheckedCastKind::Coercion:
3671
3660
case CheckedCastKind::BridgingCoercion: {
3672
- if (SuppressDiagnostics)
3673
- return nullptr ;
3674
-
3675
3661
ctx.Diags .diagnose (expr->getLoc (), diag::conditional_downcast_coercion,
3676
3662
cs.getType (sub), toType);
3677
-
3678
- // Transmute the checked cast into a coercion expression.
3679
- auto *coerce =
3680
- new (ctx) CoerceExpr (sub, expr->getLoc (), expr->getCastTypeLoc ());
3681
- cs.setType (coerce, toType);
3682
- cs.setType (coerce->getCastTypeLoc (), toType);
3683
- unsigned disjunctionChoice =
3684
- (castKind == CheckedCastKind::Coercion ? 0 : 1 );
3685
- Expr *result = visitCoerceExpr (coerce, disjunctionChoice);
3686
- if (!result)
3687
- return nullptr ;
3688
-
3689
- // Wrap the result in an optional. Mark the optional injection as
3690
- // explicit, because the user did in fact write the '?' as part of
3691
- // 'as?', even though it wasn't necessary.
3692
- result =
3693
- new (ctx) InjectIntoOptionalExpr (result, OptionalType::get (toType));
3694
- result->setImplicit (false );
3695
- return cs.cacheType (result);
3663
+ expr->setCastKind (castKind);
3664
+ cs.setType (expr, OptionalType::get (toType));
3665
+ return expr;
3696
3666
}
3697
3667
3698
3668
// Valid casts.
0 commit comments