Skip to content

Commit cb69e00

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 f9eb34a commit cb69e00

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
@@ -3535,9 +3535,6 @@ namespace {
35353535
return nullptr;
35363536
case CheckedCastKind::Coercion:
35373537
case CheckedCastKind::BridgingCoercion: {
3538-
if (SuppressDiagnostics)
3539-
return nullptr;
3540-
35413538
if (cs.getType(sub)->isEqual(toType)) {
35423539
ctx.Diags.diagnose(expr->getLoc(), diag::forced_downcast_noop, toType)
35433540
.fixItRemove(SourceRange(
@@ -3551,14 +3548,9 @@ namespace {
35513548
"as");
35523549
}
35533550

3554-
// Transmute the checked cast into a coercion expression.
3555-
auto *result =
3556-
new (ctx) CoerceExpr(sub, expr->getLoc(), expr->getCastTypeLoc());
3557-
cs.setType(result, toType);
3558-
cs.setType(result->getCastTypeLoc(), toType);
3559-
unsigned disjunctionChoice =
3560-
(castKind == CheckedCastKind::Coercion ? 0 : 1);
3561-
return visitCoerceExpr(result, disjunctionChoice);
3551+
expr->setCastKind(castKind);
3552+
cs.setType(expr, toType);
3553+
return expr;
35623554
}
35633555

35643556
// Valid casts.
@@ -3611,37 +3603,18 @@ namespace {
36113603
fromType, toType, castContextKind, cs.DC, expr->getLoc(), sub,
36123604
expr->getCastTypeLoc().getSourceRange());
36133605
switch (castKind) {
3614-
/// Invalid cast.
3606+
// Invalid cast.
36153607
case CheckedCastKind::Unresolved:
36163608
expr->setCastKind(CheckedCastKind::ValueCast);
36173609
break;
36183610

36193611
case CheckedCastKind::Coercion:
36203612
case CheckedCastKind::BridgingCoercion: {
3621-
if (SuppressDiagnostics)
3622-
return nullptr;
3623-
36243613
ctx.Diags.diagnose(expr->getLoc(), diag::conditional_downcast_coercion,
36253614
cs.getType(sub), toType);
3626-
3627-
// Transmute the checked cast into a coercion expression.
3628-
auto *coerce =
3629-
new (ctx) CoerceExpr(sub, expr->getLoc(), expr->getCastTypeLoc());
3630-
cs.setType(coerce, toType);
3631-
cs.setType(coerce->getCastTypeLoc(), toType);
3632-
unsigned disjunctionChoice =
3633-
(castKind == CheckedCastKind::Coercion ? 0 : 1);
3634-
Expr *result = visitCoerceExpr(coerce, disjunctionChoice);
3635-
if (!result)
3636-
return nullptr;
3637-
3638-
// Wrap the result in an optional. Mark the optional injection as
3639-
// explicit, because the user did in fact write the '?' as part of
3640-
// 'as?', even though it wasn't necessary.
3641-
result =
3642-
new (ctx) InjectIntoOptionalExpr(result, OptionalType::get(toType));
3643-
result->setImplicit(false);
3644-
return cs.cacheType(result);
3615+
expr->setCastKind(castKind);
3616+
cs.setType(expr, OptionalType::get(toType));
3617+
return expr;
36453618
}
36463619

36473620
// Valid casts.

0 commit comments

Comments
 (0)