Skip to content

Commit 8d0612d

Browse files
committed
[Constraint application] Stop optimizing casts in the AST.
Constraint application was rewriting conditional casts (as?) and forced casts (as!) into coercions (as) at the AST level when it determined that there was a coercion. Stop doing that: it's the optimizer's job to remove such casts.
1 parent d379de8 commit 8d0612d

File tree

1 file changed

+7
-34
lines changed

1 file changed

+7
-34
lines changed

lib/Sema/CSApply.cpp

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3583,9 +3583,6 @@ namespace {
35833583
return nullptr;
35843584
case CheckedCastKind::Coercion:
35853585
case CheckedCastKind::BridgingCoercion: {
3586-
if (SuppressDiagnostics)
3587-
return nullptr;
3588-
35893586
if (cs.getType(sub)->isEqual(toType)) {
35903587
ctx.Diags.diagnose(expr->getLoc(), diag::forced_downcast_noop, toType)
35913588
.fixItRemove(SourceRange(
@@ -3599,14 +3596,9 @@ namespace {
35993596
"as");
36003597
}
36013598

3602-
// Transmute the checked cast into a coercion expression.
3603-
auto *result =
3604-
new (ctx) CoerceExpr(sub, expr->getLoc(), expr->getCastTypeLoc());
3605-
cs.setType(result, toType);
3606-
cs.setType(result->getCastTypeLoc(), toType);
3607-
unsigned disjunctionChoice =
3608-
(castKind == CheckedCastKind::Coercion ? 0 : 1);
3609-
return visitCoerceExpr(result, disjunctionChoice);
3599+
expr->setCastKind(castKind);
3600+
cs.setType(expr, toType);
3601+
return expr;
36103602
}
36113603

36123604
// Valid casts.
@@ -3659,37 +3651,18 @@ namespace {
36593651
fromType, toType, castContextKind, cs.DC, expr->getLoc(), sub,
36603652
expr->getCastTypeLoc().getSourceRange());
36613653
switch (castKind) {
3662-
/// Invalid cast.
3654+
// Invalid cast.
36633655
case CheckedCastKind::Unresolved:
36643656
expr->setCastKind(CheckedCastKind::ValueCast);
36653657
break;
36663658

36673659
case CheckedCastKind::Coercion:
36683660
case CheckedCastKind::BridgingCoercion: {
3669-
if (SuppressDiagnostics)
3670-
return nullptr;
3671-
36723661
ctx.Diags.diagnose(expr->getLoc(), diag::conditional_downcast_coercion,
36733662
cs.getType(sub), toType);
3674-
3675-
// Transmute the checked cast into a coercion expression.
3676-
auto *coerce =
3677-
new (ctx) CoerceExpr(sub, expr->getLoc(), expr->getCastTypeLoc());
3678-
cs.setType(coerce, toType);
3679-
cs.setType(coerce->getCastTypeLoc(), toType);
3680-
unsigned disjunctionChoice =
3681-
(castKind == CheckedCastKind::Coercion ? 0 : 1);
3682-
Expr *result = visitCoerceExpr(coerce, disjunctionChoice);
3683-
if (!result)
3684-
return nullptr;
3685-
3686-
// Wrap the result in an optional. Mark the optional injection as
3687-
// explicit, because the user did in fact write the '?' as part of
3688-
// 'as?', even though it wasn't necessary.
3689-
result =
3690-
new (ctx) InjectIntoOptionalExpr(result, OptionalType::get(toType));
3691-
result->setImplicit(false);
3692-
return cs.cacheType(result);
3663+
expr->setCastKind(castKind);
3664+
cs.setType(expr, OptionalType::get(toType));
3665+
return expr;
36933666
}
36943667

36953668
// Valid casts.

0 commit comments

Comments
 (0)