@@ -3540,12 +3540,9 @@ namespace {
3540
3540
// Convert the subexpression.
3541
3541
Expr *sub = expr->getSubExpr ();
3542
3542
3543
- solution.setExprTypes (sub);
3544
-
3545
- if (TypeChecker::convertToType (sub, toType, cs.DC ))
3543
+ sub = solution.coerceToType (sub, toType, cs.getConstraintLocator (sub));
3544
+ if (!sub)
3546
3545
return nullptr ;
3547
-
3548
- cs.cacheExprTypes (sub);
3549
3546
3550
3547
expr->setSubExpr (sub);
3551
3548
cs.setType (expr, toType);
@@ -3596,9 +3593,6 @@ namespace {
3596
3593
return nullptr ;
3597
3594
case CheckedCastKind::Coercion:
3598
3595
case CheckedCastKind::BridgingCoercion: {
3599
- if (SuppressDiagnostics)
3600
- return nullptr ;
3601
-
3602
3596
if (cs.getType (sub)->isEqual (toType)) {
3603
3597
ctx.Diags .diagnose (expr->getLoc (), diag::forced_downcast_noop, toType)
3604
3598
.fixItRemove (SourceRange (
@@ -3612,14 +3606,9 @@ namespace {
3612
3606
" as" );
3613
3607
}
3614
3608
3615
- // Transmute the checked cast into a coercion expression.
3616
- auto *result =
3617
- new (ctx) CoerceExpr (sub, expr->getLoc (), expr->getCastTypeLoc ());
3618
- cs.setType (result, toType);
3619
- cs.setType (result->getCastTypeLoc (), toType);
3620
- unsigned disjunctionChoice =
3621
- (castKind == CheckedCastKind::Coercion ? 0 : 1 );
3622
- return visitCoerceExpr (result, disjunctionChoice);
3609
+ expr->setCastKind (castKind);
3610
+ cs.setType (expr, toType);
3611
+ return expr;
3623
3612
}
3624
3613
3625
3614
// Valid casts.
@@ -3672,37 +3661,18 @@ namespace {
3672
3661
fromType, toType, castContextKind, cs.DC , expr->getLoc (), sub,
3673
3662
expr->getCastTypeLoc ().getSourceRange ());
3674
3663
switch (castKind) {
3675
- / / / Invalid cast.
3664
+ // Invalid cast.
3676
3665
case CheckedCastKind::Unresolved:
3677
3666
expr->setCastKind (CheckedCastKind::ValueCast);
3678
3667
break ;
3679
3668
3680
3669
case CheckedCastKind::Coercion:
3681
3670
case CheckedCastKind::BridgingCoercion: {
3682
- if (SuppressDiagnostics)
3683
- return nullptr ;
3684
-
3685
3671
ctx.Diags .diagnose (expr->getLoc (), diag::conditional_downcast_coercion,
3686
3672
cs.getType (sub), toType);
3687
-
3688
- // Transmute the checked cast into a coercion expression.
3689
- auto *coerce =
3690
- new (ctx) CoerceExpr (sub, expr->getLoc (), expr->getCastTypeLoc ());
3691
- cs.setType (coerce, toType);
3692
- cs.setType (coerce->getCastTypeLoc (), toType);
3693
- unsigned disjunctionChoice =
3694
- (castKind == CheckedCastKind::Coercion ? 0 : 1 );
3695
- Expr *result = visitCoerceExpr (coerce, disjunctionChoice);
3696
- if (!result)
3697
- return nullptr ;
3698
-
3699
- // Wrap the result in an optional. Mark the optional injection as
3700
- // explicit, because the user did in fact write the '?' as part of
3701
- // 'as?', even though it wasn't necessary.
3702
- result =
3703
- new (ctx) InjectIntoOptionalExpr (result, OptionalType::get (toType));
3704
- result->setImplicit (false );
3705
- return cs.cacheType (result);
3673
+ expr->setCastKind (castKind);
3674
+ cs.setType (expr, OptionalType::get (toType));
3675
+ return expr;
3706
3676
}
3707
3677
3708
3678
// Valid casts.
0 commit comments