Skip to content

Commit fc9070c

Browse files
committed
Strip TypeLoc from IsPattern
1 parent ca72891 commit fc9070c

File tree

10 files changed

+85
-68
lines changed

10 files changed

+85
-68
lines changed

include/swift/AST/Pattern.h

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -470,19 +470,14 @@ class IsPattern : public Pattern {
470470
CheckedCastKind CastKind;
471471

472472
/// The type being checked for.
473-
TypeLoc CastType;
473+
TypeExpr *CastType;
474474

475475
public:
476-
IsPattern(SourceLoc IsLoc, TypeLoc CastTy,
477-
Pattern *SubPattern,
478-
CheckedCastKind Kind)
479-
: Pattern(PatternKind::Is),
480-
IsLoc(IsLoc),
481-
SubPattern(SubPattern),
482-
CastKind(Kind),
483-
CastType(CastTy) {
484-
assert(IsLoc.isValid() == CastTy.hasLocation());
485-
}
476+
IsPattern(SourceLoc IsLoc, TypeExpr *CastTy, Pattern *SubPattern,
477+
CheckedCastKind Kind);
478+
479+
static IsPattern *createImplicit(ASTContext &Ctx, Type castTy,
480+
Pattern *SubPattern, CheckedCastKind Kind);
486481

487482
CheckedCastKind getCastKind() const { return CastKind; }
488483
void setCastKind(CheckedCastKind kind) { CastKind = kind; }
@@ -493,16 +488,11 @@ class IsPattern : public Pattern {
493488
void setSubPattern(Pattern *p) { SubPattern = p; }
494489

495490
SourceLoc getLoc() const { return IsLoc; }
496-
SourceRange getSourceRange() const {
497-
SourceLoc beginLoc =
498-
SubPattern ? SubPattern->getSourceRange().Start : IsLoc;
499-
SourceLoc endLoc =
500-
(isImplicit() ? beginLoc : CastType.getSourceRange().End);
501-
return { beginLoc, endLoc };
502-
}
491+
SourceRange getSourceRange() const;
503492

504-
TypeLoc &getCastTypeLoc() { return CastType; }
505-
TypeLoc getCastTypeLoc() const { return CastType; }
493+
void setCastType(Type castTy);
494+
Type getCastType() const;
495+
TypeRepr *getCastTypeRepr() const;
506496

507497
static bool classof(const Pattern *P) {
508498
return P->getKind() == PatternKind::Is;

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ namespace {
489489
void visitIsPattern(IsPattern *P) {
490490
printCommon(P, "pattern_is")
491491
<< ' ' << getCheckedCastKindName(P->getCastKind()) << ' ';
492-
P->getCastTypeLoc().getType().print(OS);
492+
P->getCastType().print(OS);
493493
if (auto sub = P->getSubPattern()) {
494494
OS << '\n';
495495
printRec(sub);

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ void PrintAST::printPattern(const Pattern *pattern) {
11581158
case PatternKind::Is: {
11591159
auto isa = cast<IsPattern>(pattern);
11601160
Printer << tok::kw_is << " ";
1161-
isa->getCastTypeLoc().getType().print(Printer, Options);
1161+
isa->getCastType().print(Printer, Options);
11621162
break;
11631163
}
11641164

lib/AST/ASTWalker.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,8 +1691,9 @@ Pattern *Traversal::visitIsPattern(IsPattern *P) {
16911691
}
16921692
}
16931693
if (!P->isImplicit())
1694-
if (doIt(P->getCastTypeLoc()))
1695-
return nullptr;
1694+
if (auto *TR = P->getCastTypeRepr())
1695+
if (doIt(TR))
1696+
return nullptr;
16961697
return P;
16971698
}
16981699

lib/AST/Pattern.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,36 @@ SourceRange TypedPattern::getSourceRange() const {
441441
PatTypeRepr->getSourceRange().End };
442442
}
443443

444+
IsPattern::IsPattern(SourceLoc IsLoc, TypeExpr *CastTy, Pattern *SubPattern,
445+
CheckedCastKind Kind)
446+
: Pattern(PatternKind::Is), IsLoc(IsLoc), SubPattern(SubPattern),
447+
CastKind(Kind), CastType(CastTy) {
448+
assert(IsLoc.isValid() == CastTy->getLoc().isValid());
449+
}
450+
451+
IsPattern *IsPattern::createImplicit(ASTContext &Ctx, Type castTy,
452+
Pattern *SubPattern,
453+
CheckedCastKind Kind) {
454+
assert(castTy);
455+
auto *CastTE = TypeExpr::createImplicit(castTy, Ctx);
456+
auto *ip = new (Ctx) IsPattern(SourceLoc(), CastTE, SubPattern, Kind);
457+
ip->setImplicit();
458+
return ip;
459+
}
460+
461+
SourceRange IsPattern::getSourceRange() const {
462+
SourceLoc beginLoc = SubPattern ? SubPattern->getSourceRange().Start : IsLoc;
463+
SourceLoc endLoc = (isImplicit() ? beginLoc : CastType->getEndLoc());
464+
return {beginLoc, endLoc};
465+
}
466+
467+
Type IsPattern::getCastType() const { return CastType->getInstanceType(); }
468+
void IsPattern::setCastType(Type type) {
469+
CastType->setType(MetatypeType::get(type));
470+
}
471+
472+
TypeRepr *IsPattern::getCastTypeRepr() const { return CastType->getTypeRepr(); }
473+
444474
/// Construct an ExprPattern.
445475
ExprPattern::ExprPattern(Expr *e, bool isResolved, Expr *matchExpr,
446476
VarDecl *matchVar)

lib/Parse/ParsePattern.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,9 +1184,9 @@ ParserResult<Pattern> Parser::parseMatchingPattern(bool isExprBasic) {
11841184
ParserResult<TypeRepr> castType = parseType();
11851185
if (castType.isNull() || castType.hasCodeCompletion())
11861186
return nullptr;
1187-
return makeParserResult(new (Context) IsPattern(isLoc, castType.get(),
1188-
nullptr,
1189-
CheckedCastKind::Unresolved));
1187+
auto *CastTE = new (Context) TypeExpr(castType.get());
1188+
return makeParserResult(new (Context) IsPattern(
1189+
isLoc, CastTE, nullptr, CheckedCastKind::Unresolved));
11901190
}
11911191

11921192
// matching-pattern ::= expr

lib/SILGen/SILGenDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ copyOrInitValueInto(SILGenFunction &SGF, SILLocation loc,
966966

967967
// Try to perform the cast to the destination type, producing an optional that
968968
// indicates whether we succeeded.
969-
auto destType = OptionalType::get(pattern->getCastTypeLoc().getType());
969+
auto destType = OptionalType::get(pattern->getCastType());
970970

971971
value =
972972
emitConditionalCheckedCast(SGF, loc, value, pattern->getType(), destType,

lib/SILGen/SILGenPattern.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static void dumpPattern(const Pattern *p, llvm::raw_ostream &os) {
8181
}
8282
case PatternKind::Is:
8383
os << "is ";
84-
cast<IsPattern>(p)->getCastTypeLoc().getType()->print(os);
84+
cast<IsPattern>(p)->getCastType()->print(os);
8585
break;
8686
case PatternKind::EnumElement: {
8787
auto eep = cast<EnumElementPattern>(p);
@@ -286,8 +286,7 @@ static Pattern *getSimilarSpecializingPattern(Pattern *p, Pattern *first) {
286286
auto pIs = cast<IsPattern>(p);
287287
// 'is' patterns are only similar to matches to the same type.
288288
if (auto firstIs = dyn_cast<IsPattern>(first)) {
289-
if (firstIs->getCastTypeLoc().getType()
290-
->isEqual(pIs->getCastTypeLoc().getType()))
289+
if (firstIs->getCastType()->isEqual(pIs->getCastType()))
291290
return p;
292291
}
293292
return nullptr;
@@ -1581,7 +1580,7 @@ emitTupleDispatch(ArrayRef<RowToSpecialize> rows, ConsumableManagedValue src,
15811580
}
15821581

15831582
static CanType getTargetType(const RowToSpecialize &row) {
1584-
auto type = cast<IsPattern>(row.Pattern)->getCastTypeLoc().getType();
1583+
auto type = cast<IsPattern>(row.Pattern)->getCastType();
15851584
return type->getCanonicalType();
15861585
}
15871586

lib/Sema/CSGen.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2524,7 +2524,7 @@ namespace {
25242524
auto isPattern = cast<IsPattern>(pattern);
25252525

25262526
Type castType = resolveTypeReferenceInExpression(
2527-
isPattern->getCastTypeLoc(), TypeResolverContext::InExpression);
2527+
isPattern->getCastTypeRepr(), TypeResolverContext::InExpression);
25282528

25292529
if (!castType)
25302530
return Type();
@@ -2749,10 +2749,10 @@ namespace {
27492749
// of is-patterns applied to an irrefutable pattern.
27502750
pattern = pattern->getSemanticsProvidingPattern();
27512751
while (auto isp = dyn_cast<IsPattern>(pattern)) {
2752-
if (TypeChecker::validateType(
2753-
isp->getCastTypeLoc(),
2754-
TypeResolution::forContextual(
2755-
CS.DC, TypeResolverContext::InExpression))) {
2752+
Type castType = TypeResolution::forContextual(
2753+
CS.DC, TypeResolverContext::InExpression)
2754+
.resolveType(isp->getCastTypeRepr());
2755+
if (!castType) {
27562756
return false;
27572757
}
27582758

lib/Sema/TypeCheckPattern.cpp

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)