Skip to content

Commit 81b1358

Browse files
committed
[AST] Improve OptionalSomePattern a bit
Rather than having Sema set the same Optional some decl, let's just store a pointer to the ASTContext and retrieve it on demand. Additionally, add `create` and `createImplicit` factory methods.
1 parent 7018c55 commit 81b1358

File tree

5 files changed

+40
-26
lines changed

5 files changed

+40
-26
lines changed

include/swift/AST/Pattern.h

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -603,17 +603,24 @@ class BoolPattern : public Pattern {
603603
}
604604
};
605605

606-
/// A pattern "x?" which matches ".Some(x)".
606+
/// A pattern "x?" which matches ".some(x)".
607607
class OptionalSomePattern : public Pattern {
608+
const ASTContext &Ctx;
608609
Pattern *SubPattern;
609610
SourceLoc QuestionLoc;
610-
EnumElementDecl *ElementDecl = nullptr;
611+
612+
OptionalSomePattern(const ASTContext &ctx, Pattern *subPattern,
613+
SourceLoc questionLoc)
614+
: Pattern(PatternKind::OptionalSome), Ctx(ctx), SubPattern(subPattern),
615+
QuestionLoc(questionLoc) {}
611616

612617
public:
613-
explicit OptionalSomePattern(Pattern *SubPattern,
614-
SourceLoc QuestionLoc)
615-
: Pattern(PatternKind::OptionalSome), SubPattern(SubPattern),
616-
QuestionLoc(QuestionLoc) { }
618+
static OptionalSomePattern *create(ASTContext &ctx, Pattern *subPattern,
619+
SourceLoc questionLoc);
620+
621+
static OptionalSomePattern *
622+
createImplicit(ASTContext &ctx, Pattern *subPattern,
623+
SourceLoc questionLoc = SourceLoc());
617624

618625
SourceLoc getQuestionLoc() const { return QuestionLoc; }
619626
SourceRange getSourceRange() const {
@@ -624,8 +631,8 @@ class OptionalSomePattern : public Pattern {
624631
Pattern *getSubPattern() { return SubPattern; }
625632
void setSubPattern(Pattern *p) { SubPattern = p; }
626633

627-
EnumElementDecl *getElementDecl() const { return ElementDecl; }
628-
void setElementDecl(EnumElementDecl *d) { ElementDecl = d; }
634+
/// Retrieve the Optional.some enum element decl.
635+
EnumElementDecl *getElementDecl() const;
629636

630637
static bool classof(const Pattern *P) {
631638
return P->getKind() == PatternKind::OptionalSome;

lib/AST/Pattern.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,24 @@ bool Pattern::hasAnyMutableBindings() const {
306306
return HasMutable;
307307
}
308308

309+
OptionalSomePattern *OptionalSomePattern::create(ASTContext &ctx,
310+
Pattern *subPattern,
311+
SourceLoc questionLoc) {
312+
return new (ctx) OptionalSomePattern(ctx, subPattern, questionLoc);
313+
}
314+
315+
OptionalSomePattern *
316+
OptionalSomePattern::createImplicit(ASTContext &ctx, Pattern *subPattern,
317+
SourceLoc questionLoc) {
318+
auto *P = OptionalSomePattern::create(ctx, subPattern, questionLoc);
319+
P->setImplicit();
320+
return P;
321+
}
322+
323+
EnumElementDecl *OptionalSomePattern::getElementDecl() const {
324+
return Ctx.getOptionalSomeDecl();
325+
}
326+
309327
/// Return true if this is a non-resolved ExprPattern which is syntactically
310328
/// irrefutable.
311329
static bool isIrrefutableExprPattern(const ExprPattern *EP) {

lib/Sema/TypeCheckPattern.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,8 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
444444
}
445445

446446
auto *subExpr = convertBindingsToOptionalSome(bindExpr->getSubExpr());
447-
return new (Context)
448-
OptionalSomePattern(subExpr, bindExpr->getQuestionLoc());
447+
return OptionalSomePattern::create(Context, subExpr,
448+
bindExpr->getQuestionLoc());
449449
}
450450

451451
// Convert a x? to OptionalSome pattern. In the AST form, this will look like
@@ -708,8 +708,7 @@ Pattern *TypeChecker::resolvePattern(Pattern *P, DeclContext *DC,
708708

709709
// "if let" implicitly looks inside of an optional, so wrap it in an
710710
// OptionalSome pattern.
711-
P = new (Context) OptionalSomePattern(P, P->getEndLoc());
712-
P->setImplicit();
711+
P = OptionalSomePattern::createImplicit(Context, P, P->getEndLoc());
713712
}
714713

715714
return P;
@@ -1442,9 +1441,8 @@ Pattern *TypeChecker::coercePatternToType(ContextualPattern pattern,
14421441
if (lookupEnumMemberElement(dc,
14431442
baseType->lookThroughAllOptionalTypes(),
14441443
EEP->getName(), EEP->getLoc())) {
1445-
P = new (Context)
1446-
OptionalSomePattern(EEP, EEP->getEndLoc());
1447-
P->setImplicit();
1444+
P = OptionalSomePattern::createImplicit(Context, EEP,
1445+
EEP->getEndLoc());
14481446
return coercePatternToType(
14491447
pattern.forSubPattern(P, /*retainTopLevel=*/true), type,
14501448
options);
@@ -1658,12 +1656,6 @@ Pattern *TypeChecker::coercePatternToType(ContextualPattern pattern,
16581656
return nullptr;
16591657
}
16601658

1661-
EnumElementDecl *elementDecl = Context.getOptionalSomeDecl();
1662-
if (!elementDecl)
1663-
return nullptr;
1664-
1665-
OP->setElementDecl(elementDecl);
1666-
16671659
Pattern *sub = OP->getSubPattern();
16681660
auto newSubOptions = subOptions;
16691661
newSubOptions.setContext(TypeResolverContext::EnumPatternPayload);

lib/Sema/TypeCheckStorage.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,9 +1434,7 @@ synthesizeLazyGetterBody(AccessorDecl *Get, VarDecl *VD, VarDecl *Storage,
14341434
Named->setType(Tmp1VD->getType());
14351435
auto *Let = BindingPattern::createImplicit(Ctx, /*let*/ true, Named);
14361436
Let->setType(Named->getType());
1437-
auto *Some = new (Ctx) OptionalSomePattern(Let, SourceLoc());
1438-
Some->setImplicit();
1439-
Some->setElementDecl(Ctx.getOptionalSomeDecl());
1437+
auto *Some = OptionalSomePattern::createImplicit(Ctx, Let);
14401438
Some->setType(OptionalType::get(Let->getType()));
14411439

14421440
auto *StoredValueExpr =

lib/Sema/TypeCheckSwitchStmt.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,8 +1480,7 @@ namespace {
14801480
}
14811481
case PatternKind::OptionalSome: {
14821482
auto *OSP = cast<OptionalSomePattern>(item);
1483-
auto &Ctx = OSP->getElementDecl()->getASTContext();
1484-
const Identifier name = Ctx.getOptionalSomeDecl()->getBaseIdentifier();
1483+
const Identifier name = OSP->getElementDecl()->getBaseIdentifier();
14851484

14861485
auto subSpace = projectPattern(OSP->getSubPattern());
14871486
// To match patterns like (_, _, ...)?, we must rewrite the underlying

0 commit comments

Comments
 (0)