@@ -361,10 +361,16 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
361361 if (!cast)
362362 return nullptr ;
363363
364- const auto tyLoc = TypeLoc (cast->getCastTypeRepr (), cast->getCastType ());
365364 Pattern *subPattern = getSubExprPattern (E->getElement (0 ));
366- return new (Context) IsPattern (cast->getLoc (), tyLoc, subPattern,
367- CheckedCastKind::Unresolved);
365+ if (cast->isImplicit ()) {
366+ return IsPattern::createImplicit (Context, cast->getCastType (), subPattern,
367+ CheckedCastKind::Unresolved);
368+ }
369+ auto *TE = new (Context) TypeExpr (cast->getCastTypeRepr ());
370+ if (auto castTy = cast->getType ())
371+ TE->setType (MetatypeType::get (castTy));
372+ return new (Context)
373+ IsPattern (cast->getLoc (), TE, subPattern, CheckedCastKind::Unresolved);
368374 }
369375
370376 // Convert a paren expr to a pattern if it contains a pattern.
@@ -614,12 +620,10 @@ Pattern *TypeChecker::resolvePattern(Pattern *P, DeclContext *DC,
614620 if (auto *TE = dyn_cast<TypeExpr>(EP->getSubExpr ())) {
615621 Context.Diags .diagnose (TE->getStartLoc (), diag::type_pattern_missing_is)
616622 .fixItInsert (TE->getStartLoc (), " is " );
617-
618- P = new (Context) IsPattern (TE->getStartLoc (),
619- TypeLoc (TE->getTypeRepr (),
620- TE->getInstanceType ()),
621- /* subpattern*/ nullptr ,
622- CheckedCastKind::Unresolved);
623+
624+ P = new (Context)
625+ IsPattern (TE->getStartLoc (), TE,
626+ /* subpattern*/ nullptr , CheckedCastKind::Unresolved);
623627 }
624628
625629 // Look through a TypedPattern if present.
@@ -1222,11 +1226,11 @@ Pattern *TypeChecker::coercePatternToType(ContextualPattern pattern,
12221226
12231227 // Type-check the type parameter.
12241228 TypeResolutionOptions paramOptions (TypeResolverContext::InExpression);
1225- TypeResolution resolution = TypeResolution::forContextual (dc, paramOptions);
1226- if (validateType (IP->getCastTypeLoc (), resolution))
1229+ auto castType = TypeResolution::forContextual (dc, paramOptions)
1230+ .resolveType (IP->getCastTypeRepr ());
1231+ if (!castType || castType->hasError ())
12271232 return nullptr ;
1228-
1229- auto castType = IP->getCastTypeLoc ().getType ();
1233+ IP->setCastType (castType);
12301234
12311235 // Determine whether we have an imbalance in the number of optionals.
12321236 SmallVector<Type, 2 > inputTypeOptionals;
@@ -1254,15 +1258,11 @@ Pattern *TypeChecker::coercePatternToType(ContextualPattern pattern,
12541258 pattern.forSubPattern (P, /* retainTopLevle=*/ true ), type, options);
12551259 }
12561260
1257- CheckedCastKind castKind
1258- = TypeChecker::typeCheckCheckedCast (type, IP->getCastTypeLoc ().getType (),
1259- type->hasError ()
1260- ? CheckedCastContextKind::None
1261- : CheckedCastContextKind::IsPattern,
1262- dc,
1263- IP->getLoc (),
1264- nullptr ,
1265- IP->getCastTypeLoc ().getSourceRange ());
1261+ CheckedCastKind castKind = TypeChecker::typeCheckCheckedCast (
1262+ type, IP->getCastType (),
1263+ type->hasError () ? CheckedCastContextKind::None
1264+ : CheckedCastContextKind::IsPattern,
1265+ dc, IP->getLoc (), nullptr , IP->getCastTypeRepr ()->getSourceRange ());
12661266 switch (castKind) {
12671267 case CheckedCastKind::Unresolved:
12681268 return nullptr ;
@@ -1272,8 +1272,7 @@ Pattern *TypeChecker::coercePatternToType(ContextualPattern pattern,
12721272 // it is "useful" because it is providing a different type to the
12731273 // sub-pattern. If this is an 'is' pattern or an 'as' pattern where the
12741274 // types are the same, then produce a warning.
1275- if (!IP->getSubPattern () ||
1276- type->isEqual (IP->getCastTypeLoc ().getType ())) {
1275+ if (!IP->getSubPattern () || type->isEqual (IP->getCastType ())) {
12771276 diags.diagnose (IP->getLoc (), diag::isa_is_always_true,
12781277 IP->getSubPattern () ? " as" : " is" );
12791278 }
@@ -1286,7 +1285,7 @@ Pattern *TypeChecker::coercePatternToType(ContextualPattern pattern,
12861285 case CheckedCastKind::SetDowncast: {
12871286 diags.diagnose (IP->getLoc (),
12881287 diag::isa_collection_downcast_pattern_value_unimplemented,
1289- IP->getCastTypeLoc (). getType ());
1288+ IP->getCastType ());
12901289 return P;
12911290 }
12921291
@@ -1300,8 +1299,8 @@ Pattern *TypeChecker::coercePatternToType(ContextualPattern pattern,
13001299 if (Pattern *sub = IP->getSubPattern ()) {
13011300 sub = coercePatternToType (
13021301 pattern.forSubPattern (sub, /* retainTopLevel=*/ false ),
1303- IP->getCastTypeLoc (). getType (),
1304- subOptions| TypeResolutionFlags::FromNonInferredPattern);
1302+ IP->getCastType (),
1303+ subOptions | TypeResolutionFlags::FromNonInferredPattern);
13051304 if (!sub)
13061305 return nullptr ;
13071306
@@ -1528,10 +1527,8 @@ Pattern *TypeChecker::coercePatternToType(ContextualPattern pattern,
15281527
15291528 // If we needed a cast, wrap the pattern in a cast pattern.
15301529 if (castKind) {
1531- auto isPattern = new (Context) IsPattern (SourceLoc (),
1532- TypeLoc::withoutLoc (enumTy),
1533- EEP, *castKind);
1534- isPattern->setImplicit ();
1530+ auto isPattern =
1531+ IsPattern::createImplicit (Context, enumTy, EEP, *castKind);
15351532 isPattern->setType (type);
15361533 P = isPattern;
15371534 }
0 commit comments