@@ -3924,15 +3924,22 @@ namespace {
39243924 }
39253925
39263926 Expr *visitCoerceExprImpl (CoerceExpr *expr) {
3927- // Simplify and update the type we're coercing to.
3928- assert (expr->getCastTypeRepr ());
3929- const auto toType = simplifyType (cs.getType (expr->getCastTypeRepr ()));
3930- expr->setCastType (toType);
3931- cs.setType (expr->getCastTypeRepr (), toType);
3927+ if (auto *castTypeRepr = expr->getCastTypeRepr ()) {
3928+ // Simplify and update the type we're coercing to.
3929+ auto toType = simplifyType (cs.getType (castTypeRepr));
3930+ expr->setCastType (toType);
3931+ cs.setType (castTypeRepr, toType);
3932+ } else {
3933+ assert (expr->isImplicit ());
3934+ assert (expr->getCastType ());
3935+ }
3936+
3937+ cs.setType (expr, expr->getCastType ());
39323938
39333939 // If this is a literal that got converted into constructor call
39343940 // lets put proper source information in place.
39353941 if (expr->isLiteralInit ()) {
3942+ auto toType = expr->getCastType ();
39363943 auto *literalInit = expr->getSubExpr ();
39373944 if (auto *call = dyn_cast<CallExpr>(literalInit)) {
39383945 cs.forEachExpr (call->getFn (), [&](Expr *subExpr) -> Expr * {
@@ -3958,7 +3965,6 @@ namespace {
39583965 literalInit->setImplicit (false );
39593966 }
39603967
3961- cs.setType (expr, toType);
39623968 // Keep the coercion around, because it contains the source range
39633969 // for the original constructor call.
39643970 return expr;
@@ -3980,28 +3986,30 @@ namespace {
39803986 // Convert the subexpression.
39813987 Expr *sub = expr->getSubExpr ();
39823988
3983- sub = solution.coerceToType (sub, toType, cs.getConstraintLocator (sub));
3989+ sub = solution.coerceToType (sub, expr->getCastType (),
3990+ cs.getConstraintLocator (sub));
39843991 if (!sub)
39853992 return nullptr ;
39863993
39873994 expr->setSubExpr (sub);
3988- cs.setType (expr, toType);
39893995 return expr;
39903996 }
39913997
39923998 // Bridging conversion.
39933999 assert (choice == 1 && " should be bridging" );
39944000
39954001 // Handle optional bindings.
3996- Expr *sub = handleOptionalBindings (expr->getSubExpr (), toType,
3997- OptionalBindingsCastKind::Bridged,
3998- [&](Expr *sub, Type toInstanceType) {
3999- return buildObjCBridgeExpr (sub, toInstanceType, locator);
4000- });
4002+ Expr *sub = handleOptionalBindings (
4003+ expr->getSubExpr (), expr->getCastType (),
4004+ OptionalBindingsCastKind::Bridged,
4005+ [&](Expr *sub, Type toInstanceType) {
4006+ return buildObjCBridgeExpr (sub, toInstanceType, locator);
4007+ });
4008+
4009+ if (!sub)
4010+ return nullptr ;
40014011
4002- if (!sub) return nullptr ;
40034012 expr->setSubExpr (sub);
4004- cs.setType (expr, toType);
40054013 return expr;
40064014 }
40074015
@@ -4011,24 +4019,35 @@ namespace {
40114019 auto sub = cs.coerceToRValue (expr->getSubExpr ());
40124020 expr->setSubExpr (sub);
40134021
4022+ const auto fromType = cs.getType (sub);
4023+
4024+ Type toType;
4025+ SourceRange castTypeRange;
4026+
40144027 // Simplify and update the type we're casting to.
4015- auto *const castTypeRepr = expr->getCastTypeRepr ();
4028+ if (auto *const castTypeRepr = expr->getCastTypeRepr ()) {
4029+ toType = simplifyType (cs.getType (castTypeRepr));
4030+ castTypeRange = castTypeRepr->getSourceRange ();
4031+
4032+ cs.setType (castTypeRepr, toType);
4033+ expr->setCastType (toType);
4034+ } else {
4035+ assert (expr->isImplicit ());
4036+ assert (expr->getCastType ());
4037+
4038+ toType = expr->getCastType ();
4039+ }
40164040
4017- const auto fromType = cs.getType (sub);
4018- auto toType = simplifyType (cs.getType (castTypeRepr));
40194041 if (hasForcedOptionalResult (expr))
40204042 toType = toType->getOptionalObjectType ();
40214043
4022- expr->setCastType (toType);
4023- cs.setType (castTypeRepr, toType);
4024-
4025- auto castContextKind =
4026- SuppressDiagnostics ? CheckedCastContextKind::None
4027- : CheckedCastContextKind::ForcedCast;
4044+ auto castContextKind = SuppressDiagnostics || expr->isImplicit ()
4045+ ? CheckedCastContextKind::None
4046+ : CheckedCastContextKind::ForcedCast;
40284047
40294048 const auto castKind = TypeChecker::typeCheckCheckedCast (
4030- fromType, toType, castContextKind, cs. DC , expr->getLoc (), sub,
4031- castTypeRepr-> getSourceRange () );
4049+ fromType, toType, castContextKind, dc , expr->getLoc (), sub,
4050+ castTypeRange );
40324051 switch (castKind) {
40334052 // / Invalid cast.
40344053 case CheckedCastKind::Unresolved:
@@ -4048,16 +4067,30 @@ namespace {
40484067 expr->setCastKind (castKind);
40494068 break ;
40504069 }
4051-
4070+
40524071 return handleOptionalBindingsForCast (expr, simplifyType (cs.getType (expr)),
40534072 OptionalBindingsCastKind::Forced);
40544073 }
40554074
40564075 Expr *visitConditionalCheckedCastExpr (ConditionalCheckedCastExpr *expr) {
4057- // Simplify and update the type we're casting to.
40584076 auto *const castTypeRepr = expr->getCastTypeRepr ();
4077+
4078+ // If there is no type repr, it means this is implicit cast which
4079+ // should have a type set.
4080+ if (!castTypeRepr) {
4081+ assert (expr->isImplicit ());
4082+ assert (expr->getCastType ());
4083+
4084+ auto sub = cs.coerceToRValue (expr->getSubExpr ());
4085+ expr->setSubExpr (sub);
4086+
4087+ return expr;
4088+ }
4089+
4090+ // Simplify and update the type we're casting to.
40594091 const auto toType = simplifyType (cs.getType (castTypeRepr));
40604092 expr->setCastType (toType);
4093+
40614094 cs.setType (castTypeRepr, toType);
40624095
40634096 // If we need to insert a force-unwrap for coercions of the form
@@ -4094,7 +4127,7 @@ namespace {
40944127 : CheckedCastContextKind::ConditionalCast;
40954128
40964129 auto castKind = TypeChecker::typeCheckCheckedCast (
4097- fromType, toType, castContextKind, cs. DC , expr->getLoc (), sub,
4130+ fromType, toType, castContextKind, dc , expr->getLoc (), sub,
40984131 castTypeRepr->getSourceRange ());
40994132 switch (castKind) {
41004133 // Invalid cast.
@@ -4107,7 +4140,7 @@ namespace {
41074140 // Special handle for literals conditional checked cast when they can
41084141 // be statically coerced to the cast type.
41094142 if (protocol && TypeChecker::conformsToProtocol (
4110- toType, protocol, cs. DC ->getParentModule ())) {
4143+ toType, protocol, dc ->getParentModule ())) {
41114144 ctx.Diags
41124145 .diagnose (expr->getLoc (),
41134146 diag::literal_conditional_downcast_to_coercion,
0 commit comments