Skip to content

Commit 5836490

Browse files
committed
[AST/CSGen] Move expanded pack collection into constraint generator
This is going to help us identify and fix positions were 'each' keyword is mising.
1 parent 31e0a52 commit 5836490

File tree

3 files changed

+45
-43
lines changed

3 files changed

+45
-43
lines changed

include/swift/AST/Expr.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3627,8 +3627,6 @@ class PackExpansionExpr final : public Expr {
36273627
PatternExpr = patternExpr;
36283628
}
36293629

3630-
void getExpandedPacks(SmallVectorImpl<ASTNode> &packs);
3631-
36323630
GenericEnvironment *getGenericEnvironment() {
36333631
return Environment;
36343632
}

lib/AST/Expr.cpp

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,46 +1279,6 @@ PackExpansionExpr::create(ASTContext &ctx, SourceLoc repeatLoc,
12791279
implicit, type);
12801280
}
12811281

1282-
void PackExpansionExpr::getExpandedPacks(SmallVectorImpl<ASTNode> &packs) {
1283-
struct PackCollector : public ASTWalker {
1284-
llvm::SmallVector<ASTNode, 2> packs;
1285-
1286-
/// Walk everything that's available.
1287-
MacroWalking getMacroWalkingBehavior() const override {
1288-
return MacroWalking::ArgumentsAndExpansion;
1289-
}
1290-
1291-
virtual PreWalkResult<Expr *> walkToExprPre(Expr *E) override {
1292-
// Don't walk into nested pack expansions
1293-
if (isa<PackExpansionExpr>(E)) {
1294-
return Action::SkipChildren(E);
1295-
}
1296-
1297-
if (isa<PackElementExpr>(E)) {
1298-
packs.push_back(E);
1299-
}
1300-
1301-
return Action::Continue(E);
1302-
}
1303-
1304-
virtual PreWalkAction walkToTypeReprPre(TypeRepr *T) override {
1305-
// Don't walk into nested pack expansions
1306-
if (isa<PackExpansionTypeRepr>(T)) {
1307-
return Action::SkipChildren();
1308-
}
1309-
1310-
if (isa<PackElementTypeRepr>(T)) {
1311-
packs.push_back(T);
1312-
}
1313-
1314-
return Action::Continue();
1315-
}
1316-
} packCollector;
1317-
1318-
getPatternExpr()->walk(packCollector);
1319-
packs.append(packCollector.packs.begin(), packCollector.packs.end());
1320-
}
1321-
13221282
PackElementExpr *
13231283
PackElementExpr::create(ASTContext &ctx, SourceLoc eachLoc, Expr *packRefExpr,
13241284
bool implicit, Type type) {

lib/Sema/CSGen.cpp

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3063,6 +3063,50 @@ namespace {
30633063
return variadicSeq;
30643064
}
30653065

3066+
void collectExpandedPacks(PackExpansionExpr *expansion,
3067+
SmallVectorImpl<ASTNode> &packs) {
3068+
struct PackCollector : public ASTWalker {
3069+
private:
3070+
SmallVectorImpl<ASTNode> &Packs;
3071+
3072+
public:
3073+
PackCollector(SmallVectorImpl<ASTNode> &packs) : Packs(packs) {}
3074+
3075+
/// Walk everything that's available.
3076+
MacroWalking getMacroWalkingBehavior() const override {
3077+
return MacroWalking::ArgumentsAndExpansion;
3078+
}
3079+
3080+
virtual PreWalkResult<Expr *> walkToExprPre(Expr *E) override {
3081+
// Don't walk into nested pack expansions
3082+
if (isa<PackExpansionExpr>(E)) {
3083+
return Action::SkipChildren(E);
3084+
}
3085+
3086+
if (isa<PackElementExpr>(E)) {
3087+
Packs.push_back(E);
3088+
}
3089+
3090+
return Action::Continue(E);
3091+
}
3092+
3093+
virtual PreWalkAction walkToTypeReprPre(TypeRepr *T) override {
3094+
// Don't walk into nested pack expansions
3095+
if (isa<PackExpansionTypeRepr>(T)) {
3096+
return Action::SkipChildren();
3097+
}
3098+
3099+
if (isa<PackElementTypeRepr>(T)) {
3100+
Packs.push_back(T);
3101+
}
3102+
3103+
return Action::Continue();
3104+
}
3105+
} packCollector(packs);
3106+
3107+
expansion->getPatternExpr()->walk(packCollector);
3108+
}
3109+
30663110
Type visitPackExpansionExpr(PackExpansionExpr *expr) {
30673111
assert(PackElementEnvironments.back() == expr);
30683112
PackElementEnvironments.pop_back();
@@ -3086,7 +3130,7 @@ namespace {
30863130
// Generate ShapeOf constraints between all packs expanded by this
30873131
// pack expansion expression through the shape type variable.
30883132
SmallVector<ASTNode, 2> expandedPacks;
3089-
expr->getExpandedPacks(expandedPacks);
3133+
collectExpandedPacks(expr, expandedPacks);
30903134

30913135
if (expandedPacks.empty()) {
30923136
(void)CS.recordFix(AllowValueExpansionWithoutPackReferences::create(

0 commit comments

Comments
 (0)