@@ -2969,18 +2969,27 @@ namespace {
29692969 return typeVar;
29702970 }
29712971
2972+ Type getTypeForCast (ExplicitCastExpr *E) {
2973+ if (auto *const repr = E->getCastTypeRepr ()) {
2974+ // Validate the resulting type.
2975+ return resolveTypeReferenceInExpression (
2976+ repr, TypeResolverContext::ExplicitCastExpr,
2977+ CS.getConstraintLocator (E));
2978+ }
2979+ assert (E->isImplicit ());
2980+ return E->getCastType ();
2981+ }
2982+
29722983 Type visitForcedCheckedCastExpr (ForcedCheckedCastExpr *expr) {
29732984 auto fromExpr = expr->getSubExpr ();
29742985 if (!fromExpr) // Either wasn't constructed correctly or wasn't folded.
29752986 return nullptr ;
29762987
2977- auto *const repr = expr->getCastTypeRepr ();
2978- // Validate the resulting type.
2979- const auto toType = resolveTypeReferenceInExpression (
2980- repr, TypeResolverContext::ExplicitCastExpr,
2981- CS.getConstraintLocator (expr));
2988+ auto toType = getTypeForCast (expr);
29822989 if (!toType)
2983- return nullptr ;
2990+ return Type ();
2991+
2992+ auto *const repr = expr->getCastTypeRepr ();
29842993
29852994 // Cache the type we're casting to.
29862995 if (repr) CS.setType (repr, toType);
@@ -3001,13 +3010,12 @@ namespace {
30013010
30023011 Type visitCoerceExpr (CoerceExpr *expr) {
30033012 // Validate the resulting type.
3004- auto *const repr = expr->getCastTypeRepr ();
3005- const auto toType = resolveTypeReferenceInExpression (
3006- repr, TypeResolverContext::ExplicitCastExpr,
3007- CS.getConstraintLocator (expr));
3013+ auto toType = getTypeForCast (expr);
30083014 if (!toType)
30093015 return nullptr ;
30103016
3017+ auto *const repr = expr->getCastTypeRepr ();
3018+
30113019 // Cache the type we're casting to.
30123020 if (repr) CS.setType (repr, toType);
30133021
@@ -3033,13 +3041,12 @@ namespace {
30333041 return nullptr ;
30343042
30353043 // Validate the resulting type.
3036- auto *const repr = expr->getCastTypeRepr ();
3037- const auto toType = resolveTypeReferenceInExpression (
3038- repr, TypeResolverContext::ExplicitCastExpr,
3039- CS.getConstraintLocator (expr));
3044+ const auto toType = getTypeForCast (expr);
30403045 if (!toType)
30413046 return nullptr ;
30423047
3048+ auto *const repr = expr->getCastTypeRepr ();
3049+
30433050 // Cache the type we're casting to.
30443051 if (repr) CS.setType (repr, toType);
30453052
@@ -3058,24 +3065,22 @@ namespace {
30583065 }
30593066
30603067 Type visitIsExpr (IsExpr *expr) {
3061- // Validate the type.
3062- // FIXME: Locator for the cast type?
3063- auto &ctx = CS.getASTContext ();
3064- const auto toType = resolveTypeReferenceInExpression (
3065- expr->getCastTypeRepr (), TypeResolverContext::ExplicitCastExpr,
3066- CS.getConstraintLocator (expr));
3068+ auto toType = getTypeForCast (expr);
30673069 if (!toType)
30683070 return nullptr ;
30693071
3072+ auto *const repr = expr->getCastTypeRepr ();
30703073 // Cache the type we're checking.
3071- CS.setType (expr->getCastTypeRepr (), toType);
3074+ if (repr)
3075+ CS.setType (repr, toType);
30723076
30733077 // Add a checked cast constraint.
30743078 auto fromType = CS.getType (expr->getSubExpr ());
30753079
30763080 CS.addConstraint (ConstraintKind::CheckedCast, fromType, toType,
30773081 CS.getConstraintLocator (expr));
30783082
3083+ auto &ctx = CS.getASTContext ();
30793084 // The result is Bool.
30803085 auto boolDecl = ctx.getBoolDecl ();
30813086
0 commit comments