Skip to content

Commit 914e0c4

Browse files
committed
AST: Small cleanup for PackExpansionMatcher
1 parent d602465 commit 914e0c4

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

include/swift/AST/PackExpansionMatcher.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ namespace swift {
2727

2828
class ASTContext;
2929

30-
/// The result of a match. If one of lhs or rhs is a pack expansion type,
31-
/// the other one is a pack type.
30+
/// The result of a match. An important invariant is that either both types
31+
/// are PackExpansionTypes, or both types are scalars. In particular, any
32+
/// PackTypes are always wrapped in a PackExpansionType.
3233
struct MatchedPair {
3334
Type lhs;
3435
Type rhs;

lib/AST/PackExpansionMatcher.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,24 @@
2424

2525
using namespace swift;
2626

27-
static Type createPackBinding(ASTContext &ctx, ArrayRef<Type> types) {
28-
// If there is only one element and it's a pack expansion type,
29-
// return the pattern type directly. Because PackType can only appear
30-
// inside a PackExpansion, PackType(PackExpansionType()) will always
31-
// simplify to the pattern type.
27+
static PackExpansionType *createPackBinding(ASTContext &ctx,
28+
ArrayRef<Type> types) {
29+
// If there is only one element and it's a PackExpansionType,
30+
// return it directly.
3231
if (types.size() == 1) {
33-
if (types.front()->is<PackExpansionType>()) {
34-
return types.front();
32+
if (auto *expansionType = types.front()->getAs<PackExpansionType>()) {
33+
return expansionType;
3534
}
3635
}
3736

37+
// Otherwise, wrap the elements in PackExpansionType(PackType(...)).
3838
auto *packType = PackType::get(ctx, types);
3939
return PackExpansionType::get(packType, packType);
4040
}
4141

42-
static Type gatherTupleElements(ArrayRef<TupleTypeElt> &elts,
43-
Identifier name,
44-
ASTContext &ctx) {
42+
static PackExpansionType *gatherTupleElements(ArrayRef<TupleTypeElt> &elts,
43+
Identifier name,
44+
ASTContext &ctx) {
4545
SmallVector<Type, 2> types;
4646

4747
if (!elts.empty() && elts.front().getName() == name) {
@@ -190,7 +190,7 @@ bool ParamPackMatcher::match() {
190190
// to what remains of the right hand side.
191191
if (lhsParams.size() == 1) {
192192
auto lhsType = lhsParams[0].getPlainType();
193-
if (lhsType->is<PackExpansionType>()) {
193+
if (auto *lhsExpansion = lhsType->getAs<PackExpansionType>()) {
194194
SmallVector<Type, 2> rhsTypes;
195195
for (auto rhsParam : rhsParams) {
196196
// FIXME: Check rhs flags
@@ -199,7 +199,7 @@ bool ParamPackMatcher::match() {
199199
auto rhs = createPackBinding(ctx, rhsTypes);
200200

201201
// FIXME: Check lhs flags
202-
pairs.emplace_back(lhsType, rhs, prefixLength);
202+
pairs.emplace_back(lhsExpansion, rhs, prefixLength);
203203
return false;
204204
}
205205
}
@@ -208,7 +208,7 @@ bool ParamPackMatcher::match() {
208208
// to what remains of the left hand side.
209209
if (rhsParams.size() == 1) {
210210
auto rhsType = rhsParams[0].getPlainType();
211-
if (rhsType->is<PackExpansionType>()) {
211+
if (auto *rhsExpansion = rhsType->getAs<PackExpansionType>()) {
212212
SmallVector<Type, 2> lhsTypes;
213213
for (auto lhsParam : lhsParams) {
214214
// FIXME: Check lhs flags
@@ -217,7 +217,7 @@ bool ParamPackMatcher::match() {
217217
auto lhs = createPackBinding(ctx, lhsTypes);
218218

219219
// FIXME: Check rhs flags
220-
pairs.emplace_back(lhs, rhsType, prefixLength);
220+
pairs.emplace_back(lhs, rhsExpansion, prefixLength);
221221
return false;
222222
}
223223
}
@@ -287,10 +287,10 @@ bool PackMatcher::match() {
287287
// to what remains of the right hand side.
288288
if (lhsTypes.size() == 1) {
289289
auto lhsType = lhsTypes[0];
290-
if (lhsType->is<PackExpansionType>()) {
290+
if (auto *lhsExpansion = lhsType->getAs<PackExpansionType>()) {
291291
auto rhs = createPackBinding(ctx, rhsTypes);
292292

293-
pairs.emplace_back(lhsType, rhs, prefixLength);
293+
pairs.emplace_back(lhsExpansion, rhs, prefixLength);
294294
return false;
295295
}
296296
}
@@ -299,10 +299,10 @@ bool PackMatcher::match() {
299299
// to what remains of the left hand side.
300300
if (rhsTypes.size() == 1) {
301301
auto rhsType = rhsTypes[0];
302-
if (rhsType->is<PackExpansionType>()) {
302+
if (auto *rhsExpansion = rhsType->getAs<PackExpansionType>()) {
303303
auto lhs = createPackBinding(ctx, lhsTypes);
304304

305-
pairs.emplace_back(lhs, rhsType, prefixLength);
305+
pairs.emplace_back(lhs, rhsExpansion, prefixLength);
306306
return false;
307307
}
308308
}

0 commit comments

Comments
 (0)