@@ -3648,9 +3648,9 @@ namespace {
3648
3648
auto sub = cs.coerceToRValue (expr->getSubExpr ());
3649
3649
expr->setSubExpr (sub);
3650
3650
3651
-
3651
+ bool isSubExprLiteral = isa<LiteralExpr>(sub);
3652
3652
auto castContextKind =
3653
- (SuppressDiagnostics || isInsideIsExpr)
3653
+ (SuppressDiagnostics || isInsideIsExpr || isSubExprLiteral )
3654
3654
? CheckedCastContextKind::None
3655
3655
: CheckedCastContextKind::ConditionalCast;
3656
3656
@@ -3661,13 +3661,35 @@ namespace {
3661
3661
switch (castKind) {
3662
3662
// Invalid cast.
3663
3663
case CheckedCastKind::Unresolved:
3664
+ // FIXME: This literal diagnostics needs to be revisited by a proposal
3665
+ // to unify casting semantics for literals.
3666
+ // https://bugs.swift.org/browse/SR-12093
3667
+ if (isSubExprLiteral) {
3668
+ auto protocol = TypeChecker::getLiteralProtocol (ctx, sub);
3669
+ // Special handle for literals conditional checked cast when they can
3670
+ // be statically coerced to the cast type.
3671
+ if (protocol && TypeChecker::conformsToProtocol (
3672
+ toType, protocol, cs.DC ,
3673
+ ConformanceCheckFlags::InExpression)) {
3674
+ ctx.Diags
3675
+ .diagnose (expr->getLoc (),
3676
+ diag::literal_conditional_downcast_to_coercion,
3677
+ toType);
3678
+ } else {
3679
+ ctx.Diags
3680
+ .diagnose (expr->getLoc (), diag::downcast_to_unrelated, fromType,
3681
+ toType)
3682
+ .highlight (sub->getSourceRange ())
3683
+ .highlight (expr->getCastTypeLoc ().getSourceRange ());
3684
+ }
3685
+ }
3664
3686
expr->setCastKind (CheckedCastKind::ValueCast);
3665
3687
break ;
3666
3688
3667
3689
case CheckedCastKind::Coercion:
3668
3690
case CheckedCastKind::BridgingCoercion: {
3669
3691
ctx.Diags .diagnose (expr->getLoc (), diag::conditional_downcast_coercion,
3670
- cs. getType (sub) , toType);
3692
+ fromType , toType);
3671
3693
expr->setCastKind (castKind);
3672
3694
cs.setType (expr, OptionalType::get (toType));
3673
3695
return expr;
@@ -3681,7 +3703,7 @@ namespace {
3681
3703
expr->setCastKind (castKind);
3682
3704
break ;
3683
3705
}
3684
-
3706
+
3685
3707
return handleOptionalBindingsForCast (expr, simplifyType (cs.getType (expr)),
3686
3708
OptionalBindingsCastKind::Conditional);
3687
3709
}
0 commit comments