Skip to content

Commit b7de0be

Browse files
authored
Merge pull request #37807 from hamishknight/repeat-customer
2 parents dddbf35 + c1b1feb commit b7de0be

16 files changed

+60
-82
lines changed

include/swift/AST/Expr.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4180,13 +4180,11 @@ class AutoClosureExpr : public AbstractClosureExpr {
41804180
/// Instances of this structure represent elements of the capture list that can
41814181
/// optionally occur in a capture expression.
41824182
struct CaptureListEntry {
4183-
VarDecl *Var;
4184-
PatternBindingDecl *Init;
4183+
PatternBindingDecl *PBD;
41854184

4186-
CaptureListEntry(VarDecl *Var, PatternBindingDecl *Init)
4187-
: Var(Var), Init(Init) {
4188-
}
4185+
explicit CaptureListEntry(PatternBindingDecl *PBD);
41894186

4187+
VarDecl *getVar() const;
41904188
bool isSimpleSelfCapture() const;
41914189
};
41924190

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2496,8 +2496,7 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
24962496
for (auto capture : E->getCaptureList()) {
24972497
OS << '\n';
24982498
Indent += 2;
2499-
printRec(capture.Var);
2500-
printRec(capture.Init);
2499+
printRec(capture.PBD);
25012500
Indent -= 2;
25022501
}
25032502
printRec(E->getClosureBody());

lib/AST/ASTScopeLookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ bool PatternEntryInitializerScope::lookupLocalsOrMembers(
408408

409409
bool CaptureListScope::lookupLocalsOrMembers(DeclConsumer consumer) const {
410410
for (auto &e : expr->getCaptureList()) {
411-
if (consumer.consume({e.Var}))
411+
if (consumer.consume({e.getVar()}))
412412
return true;
413413
}
414414
return false;

lib/AST/ASTWalker.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -796,14 +796,15 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
796796
Expr *visitCaptureListExpr(CaptureListExpr *expr) {
797797
for (auto c : expr->getCaptureList()) {
798798
if (Walker.shouldWalkCaptureInitializerExpressions()) {
799-
for (auto entryIdx : range(c.Init->getNumPatternEntries())) {
800-
if (auto newInit = doIt(c.Init->getInit(entryIdx)))
801-
c.Init->setInit(entryIdx, newInit);
799+
for (auto entryIdx : range(c.PBD->getNumPatternEntries())) {
800+
if (auto newInit = doIt(c.PBD->getInit(entryIdx)))
801+
c.PBD->setInit(entryIdx, newInit);
802802
else
803803
return nullptr;
804804
}
805-
} else if (doIt(c.Var) || doIt(c.Init)) {
806-
return nullptr;
805+
} else {
806+
if (doIt(c.PBD))
807+
return nullptr;
807808
}
808809
}
809810

lib/AST/Expr.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,18 @@ UnresolvedSpecializeExpr *UnresolvedSpecializeExpr::create(ASTContext &ctx,
12291229
UnresolvedParams, RAngleLoc);
12301230
}
12311231

1232+
CaptureListEntry::CaptureListEntry(PatternBindingDecl *PBD) : PBD(PBD) {
1233+
assert(PBD);
1234+
assert(PBD->getSingleVar() &&
1235+
"Capture lists only support single-var patterns");
1236+
}
1237+
1238+
VarDecl *CaptureListEntry::getVar() const {
1239+
return PBD->getSingleVar();
1240+
}
1241+
12321242
bool CaptureListEntry::isSimpleSelfCapture() const {
1243+
auto *Var = getVar();
12331244
auto &ctx = Var->getASTContext();
12341245

12351246
if (Var->getName() != ctx.Id_self)
@@ -1239,10 +1250,10 @@ bool CaptureListEntry::isSimpleSelfCapture() const {
12391250
if (attr->get() == ReferenceOwnership::Weak)
12401251
return false;
12411252

1242-
if (Init->getPatternList().size() != 1)
1253+
if (PBD->getPatternList().size() != 1)
12431254
return false;
12441255

1245-
auto *expr = Init->getInit(0);
1256+
auto *expr = PBD->getInit(0);
12461257

12471258
if (auto *DRE = dyn_cast<DeclRefExpr>(expr)) {
12481259
if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
@@ -1265,7 +1276,7 @@ CaptureListExpr *CaptureListExpr::create(ASTContext &ctx,
12651276
auto *expr = ::new(mem) CaptureListExpr(captureList, closureBody);
12661277

12671278
for (auto capture : captureList)
1268-
capture.Var->setParentCaptureList(expr);
1279+
capture.getVar()->setParentCaptureList(expr);
12691280

12701281
return expr;
12711282
}

lib/IDE/Formatting.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2586,18 +2586,17 @@ class FormatWalker : public ASTWalker {
25862586

25872587
ListAligner Aligner(SM, TargetLocation, ContextLoc, L, R);
25882588
for (auto &Entry: ParentCapture->getCaptureList()) {
2589-
if (auto *PBD = Entry.Init) {
2590-
NodesToSkip.insert(PBD);
2591-
SourceRange Range = PBD->getSourceRangeIncludingAttrs();
2592-
Aligner.updateAlignment(Range, PBD);
2589+
auto *PBD = Entry.PBD;
2590+
NodesToSkip.insert(PBD);
2591+
SourceRange Range = PBD->getSourceRangeIncludingAttrs();
2592+
Aligner.updateAlignment(Range, PBD);
25932593

2594-
if (isTargetContext(Range)) {
2595-
Aligner.setAlignmentIfNeeded(CtxOverride);
2596-
return IndentContext {
2597-
Range.Start,
2598-
!OutdentChecker::hasOutdent(SM, Range, PBD)
2599-
};
2600-
}
2594+
if (isTargetContext(Range)) {
2595+
Aligner.setAlignmentIfNeeded(CtxOverride);
2596+
return IndentContext {
2597+
Range.Start,
2598+
!OutdentChecker::hasOutdent(SM, Range, PBD)
2599+
};
26012600
}
26022601
}
26032602
return Aligner.getContextAndSetAlignment(CtxOverride);

lib/IDE/SyntaxModel.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -668,23 +668,6 @@ std::pair<bool, Expr *> ModelASTWalker::walkToExprPre(Expr *E) {
668668

669669
pushStructureNode(SN, Closure);
670670

671-
} else if (auto *CLE = dyn_cast<CaptureListExpr>(E)) {
672-
// The ASTWalker visits captured variables twice, from a `CaptureListEntry` they are visited
673-
// from the `VarDecl` and the `PatternBindingDecl` entries.
674-
// We take over visitation here to avoid walking the `PatternBindingDecl` ones.
675-
for (auto c : CLE->getCaptureList()) {
676-
if (auto *VD = c.Var) {
677-
// We're skipping over the PatternBindingDecl so we need to handle the
678-
// the VarDecl's attributes that we'd normally process visiting the PBD.
679-
if (!handleAttrs(VD->getAttrs()))
680-
return { false, nullptr };
681-
VD->walk(*this);
682-
}
683-
}
684-
if (auto *CE = CLE->getClosureBody())
685-
CE->walk(*this);
686-
return { false, walkToExprPost(E) };
687-
688671
} else if (auto SE = dyn_cast<SequenceExpr>(E)) {
689672
// In SequenceExpr, explicit cast expressions (e.g. 'as', 'is') appear
690673
// twice. Skip pointers we've already seen.

lib/Parse/ParseExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2651,7 +2651,7 @@ ParserStatus Parser::parseClosureSignatureIfPresent(
26512651
/*VarLoc*/ nameLoc, pattern, /*EqualLoc*/ equalLoc, initializer,
26522652
CurDeclContext);
26532653

2654-
auto CLE = CaptureListEntry(VD, PBD);
2654+
auto CLE = CaptureListEntry(PBD);
26552655
if (CLE.isSimpleSelfCapture())
26562656
VD->setIsSelfParamCapture();
26572657

lib/SILGen/SILGenExpr.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,10 +1568,8 @@ ManagedValue emitCFunctionPointer(SILGenFunction &SGF,
15681568
// Ensure that weak captures are in a separate scope.
15691569
DebugScope scope(SGF, CleanupLocation(captureList));
15701570
// CaptureListExprs evaluate their bound variables.
1571-
for (auto capture : captureList->getCaptureList()) {
1572-
SGF.visit(capture.Var);
1573-
SGF.visit(capture.Init);
1574-
}
1571+
for (auto capture : captureList->getCaptureList())
1572+
SGF.visit(capture.PBD);
15751573

15761574
// Emit the closure body.
15771575
auto *closure = captureList->getClosureBody();
@@ -2418,10 +2416,8 @@ RValue RValueEmitter::visitCaptureListExpr(CaptureListExpr *E, SGFContext C) {
24182416
// Ensure that weak captures are in a separate scope.
24192417
DebugScope scope(SGF, CleanupLocation(E));
24202418
// CaptureListExprs evaluate their bound variables.
2421-
for (auto capture : E->getCaptureList()) {
2422-
SGF.visit(capture.Var);
2423-
SGF.visit(capture.Init);
2424-
}
2419+
for (auto capture : E->getCaptureList())
2420+
SGF.visit(capture.PBD);
24252421

24262422
// Then they evaluate to their body.
24272423
return visit(E->getClosureBody(), C);

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8108,7 +8108,7 @@ namespace {
81088108
if (auto captureList = dyn_cast<CaptureListExpr>(expr)) {
81098109
// Rewrite captures.
81108110
for (const auto &capture : captureList->getCaptureList()) {
8111-
(void)rewriteTarget(SolutionApplicationTarget(capture.Init));
8111+
(void)rewriteTarget(SolutionApplicationTarget(capture.PBD));
81128112
}
81138113
}
81148114

0 commit comments

Comments
 (0)