@@ -3574,12 +3574,14 @@ namespace {
35743574 castKind == CheckedCastKind::ArrayDowncast ||
35753575 castKind == CheckedCastKind::DictionaryDowncast ||
35763576 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);
35793581 cs.setType (cast, cast->getType ());
35803582
35813583 // Type-check this conditional case.
3582- Expr *result = handleConditionalCheckedCastExpr (cast, true );
3584+ Expr *result = handleConditionalCheckedCastExpr (cast, castTypeRepr );
35833585 if (!result)
35843586 return nullptr ;
35853587
@@ -3588,7 +3590,8 @@ namespace {
35883590
35893591 // Match the optional value against its `Some` case.
35903592 auto *someDecl = ctx.getOptionalSomeDecl ();
3591- auto isSomeExpr = new (ctx) EnumIsCaseExpr (result, someDecl);
3593+ auto isSomeExpr =
3594+ new (ctx) EnumIsCaseExpr (result, castTypeRepr, someDecl);
35923595 auto boolDecl = ctx.getBoolDecl ();
35933596
35943597 if (!boolDecl) {
@@ -4010,40 +4013,45 @@ namespace {
40104013 }
40114014
40124015 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+
40134022 // If we need to insert a force-unwrap for coercions of the form
40144023 // 'as! T!', do so now.
40154024 if (hasForcedOptionalResult (expr)) {
4016- auto *coerced = handleConditionalCheckedCastExpr (expr);
4025+ auto *coerced = handleConditionalCheckedCastExpr (expr, castTypeRepr );
40174026 if (!coerced)
40184027 return nullptr ;
40194028
40204029 return coerceImplicitlyUnwrappedOptionalToValue (
40214030 coerced, cs.getType (coerced)->getOptionalObjectType ());
40224031 }
40234032
4024- return handleConditionalCheckedCastExpr (expr);
4033+ return handleConditionalCheckedCastExpr (expr, castTypeRepr );
40254034 }
40264035
40274036 Expr *handleConditionalCheckedCastExpr (ConditionalCheckedCastExpr *expr,
4028- bool isInsideIsExpr = false ) {
4037+ TypeRepr *castTypeRepr) {
4038+ assert (castTypeRepr &&
4039+ " cast requires TypeRepr; implicit casts are superfluous" );
4040+
40294041 // The subexpression is always an rvalue.
40304042 auto &ctx = cs.getASTContext ();
40314043 auto sub = cs.coerceToRValue (expr->getSubExpr ());
40324044 expr->setSubExpr (sub);
40334045
40344046 // Simplify and update the type we're casting to.
4035- auto *const castTypeRepr = expr->getCastTypeRepr ();
4036-
40374047 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 ();
40414049
40424050 bool isSubExprLiteral = isa<LiteralExpr>(sub);
40434051 auto castContextKind =
4044- (SuppressDiagnostics || isInsideIsExpr || isSubExprLiteral)
4045- ? CheckedCastContextKind::None
4046- : CheckedCastContextKind::ConditionalCast;
4052+ (SuppressDiagnostics || expr-> isImplicit () || isSubExprLiteral)
4053+ ? CheckedCastContextKind::None
4054+ : CheckedCastContextKind::ConditionalCast;
40474055
40484056 auto castKind = TypeChecker::typeCheckCheckedCast (
40494057 fromType, toType, castContextKind, cs.DC , expr->getLoc (), sub,
0 commit comments