Skip to content

Commit 9f85ce3

Browse files
authored
Merge pull request swiftlang#28796 from DougGregor/leave-optimization-to-the-optimizers
[Constraint application] Stop optimizing casts in the AST.
2 parents 6cc1337 + cb69e00 commit 9f85ce3

File tree

1 file changed

+9
-39
lines changed

1 file changed

+9
-39
lines changed

lib/Sema/CSApply.cpp

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3540,12 +3540,9 @@ namespace {
35403540
// Convert the subexpression.
35413541
Expr *sub = expr->getSubExpr();
35423542

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)
35463545
return nullptr;
3547-
3548-
cs.cacheExprTypes(sub);
35493546

35503547
expr->setSubExpr(sub);
35513548
cs.setType(expr, toType);
@@ -3596,9 +3593,6 @@ namespace {
35963593
return nullptr;
35973594
case CheckedCastKind::Coercion:
35983595
case CheckedCastKind::BridgingCoercion: {
3599-
if (SuppressDiagnostics)
3600-
return nullptr;
3601-
36023596
if (cs.getType(sub)->isEqual(toType)) {
36033597
ctx.Diags.diagnose(expr->getLoc(), diag::forced_downcast_noop, toType)
36043598
.fixItRemove(SourceRange(
@@ -3612,14 +3606,9 @@ namespace {
36123606
"as");
36133607
}
36143608

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;
36233612
}
36243613

36253614
// Valid casts.
@@ -3672,37 +3661,18 @@ namespace {
36723661
fromType, toType, castContextKind, cs.DC, expr->getLoc(), sub,
36733662
expr->getCastTypeLoc().getSourceRange());
36743663
switch (castKind) {
3675-
/// Invalid cast.
3664+
// Invalid cast.
36763665
case CheckedCastKind::Unresolved:
36773666
expr->setCastKind(CheckedCastKind::ValueCast);
36783667
break;
36793668

36803669
case CheckedCastKind::Coercion:
36813670
case CheckedCastKind::BridgingCoercion: {
3682-
if (SuppressDiagnostics)
3683-
return nullptr;
3684-
36853671
ctx.Diags.diagnose(expr->getLoc(), diag::conditional_downcast_coercion,
36863672
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;
37063676
}
37073677

37083678
// Valid casts.

0 commit comments

Comments
 (0)