@@ -3574,12 +3574,14 @@ namespace {
3574
3574
castKind == CheckedCastKind::ArrayDowncast ||
3575
3575
castKind == CheckedCastKind::DictionaryDowncast ||
3576
3576
castKind == CheckedCastKind::SetDowncast) {
3577
- auto *const cast = ConditionalCheckedCastExpr::createImplicit (
3578
- ctx, sub, castTypeRepr, toType);
3577
+ auto *const cast =
3578
+ ConditionalCheckedCastExpr::createImplicit (ctx, sub, toType);
3579
+ cast->setType (OptionalType::get (toType));
3580
+ cast->setCastType (toType);
3579
3581
cs.setType (cast, cast->getType ());
3580
3582
3581
3583
// Type-check this conditional case.
3582
- Expr *result = handleConditionalCheckedCastExpr (cast, true );
3584
+ Expr *result = handleConditionalCheckedCastExpr (cast, castTypeRepr );
3583
3585
if (!result)
3584
3586
return nullptr ;
3585
3587
@@ -3588,7 +3590,8 @@ namespace {
3588
3590
3589
3591
// Match the optional value against its `Some` case.
3590
3592
auto *someDecl = ctx.getOptionalSomeDecl ();
3591
- auto isSomeExpr = new (ctx) EnumIsCaseExpr (result, someDecl);
3593
+ auto isSomeExpr =
3594
+ new (ctx) EnumIsCaseExpr (result, castTypeRepr, someDecl);
3592
3595
auto boolDecl = ctx.getBoolDecl ();
3593
3596
3594
3597
if (!boolDecl) {
@@ -4010,40 +4013,45 @@ namespace {
4010
4013
}
4011
4014
4012
4015
Expr *visitConditionalCheckedCastExpr (ConditionalCheckedCastExpr *expr) {
4016
+ // Simplify and update the type we're casting to.
4017
+ auto *const castTypeRepr = expr->getCastTypeRepr ();
4018
+ const auto toType = simplifyType (cs.getType (castTypeRepr));
4019
+ expr->setCastType (toType);
4020
+ cs.setType (castTypeRepr, toType);
4021
+
4013
4022
// If we need to insert a force-unwrap for coercions of the form
4014
4023
// 'as! T!', do so now.
4015
4024
if (hasForcedOptionalResult (expr)) {
4016
- auto *coerced = handleConditionalCheckedCastExpr (expr);
4025
+ auto *coerced = handleConditionalCheckedCastExpr (expr, castTypeRepr );
4017
4026
if (!coerced)
4018
4027
return nullptr ;
4019
4028
4020
4029
return coerceImplicitlyUnwrappedOptionalToValue (
4021
4030
coerced, cs.getType (coerced)->getOptionalObjectType ());
4022
4031
}
4023
4032
4024
- return handleConditionalCheckedCastExpr (expr);
4033
+ return handleConditionalCheckedCastExpr (expr, castTypeRepr );
4025
4034
}
4026
4035
4027
4036
Expr *handleConditionalCheckedCastExpr (ConditionalCheckedCastExpr *expr,
4028
- bool isInsideIsExpr = false ) {
4037
+ TypeRepr *castTypeRepr) {
4038
+ assert (castTypeRepr &&
4039
+ " cast requires TypeRepr; implicit casts are superfluous" );
4040
+
4029
4041
// The subexpression is always an rvalue.
4030
4042
auto &ctx = cs.getASTContext ();
4031
4043
auto sub = cs.coerceToRValue (expr->getSubExpr ());
4032
4044
expr->setSubExpr (sub);
4033
4045
4034
4046
// Simplify and update the type we're casting to.
4035
- auto *const castTypeRepr = expr->getCastTypeRepr ();
4036
-
4037
4047
const auto fromType = cs.getType (sub);
4038
- const auto toType = simplifyType (cs.getType (castTypeRepr));
4039
- expr->setCastType (toType);
4040
- cs.setType (castTypeRepr, toType);
4048
+ const auto toType = expr->getCastType ();
4041
4049
4042
4050
bool isSubExprLiteral = isa<LiteralExpr>(sub);
4043
4051
auto castContextKind =
4044
- (SuppressDiagnostics || isInsideIsExpr || isSubExprLiteral)
4045
- ? CheckedCastContextKind::None
4046
- : CheckedCastContextKind::ConditionalCast;
4052
+ (SuppressDiagnostics || expr-> isImplicit () || isSubExprLiteral)
4053
+ ? CheckedCastContextKind::None
4054
+ : CheckedCastContextKind::ConditionalCast;
4047
4055
4048
4056
auto castKind = TypeChecker::typeCheckCheckedCast (
4049
4057
fromType, toType, castContextKind, cs.DC , expr->getLoc (), sub,
0 commit comments