Skip to content

Commit bb05a0e

Browse files
committed
Add a convenience API to build canonical pack types from slices
of canonical tuple type elements. This has come up more than you might think.
1 parent 6c06650 commit bb05a0e

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

include/swift/AST/Types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6829,6 +6829,7 @@ class PackType final : public TypeBase, public llvm::FoldingSetNode,
68296829
};
68306830
BEGIN_CAN_TYPE_WRAPPER(PackType, Type)
68316831
static CanPackType get(const ASTContext &ctx, ArrayRef<CanType> elements);
6832+
static CanPackType get(const ASTContext &ctx, CanTupleEltTypeArrayRef elts);
68326833

68336834
CanType getElementType(unsigned elementNo) const {
68346835
return CanType(getPointer()->getElementType(elementNo));

lib/AST/ASTContext.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3303,6 +3303,12 @@ CanPackType CanPackType::get(const ASTContext &C, ArrayRef<CanType> elements) {
33033303
return CanPackType(PackType::get(C, ncElements));
33043304
}
33053305

3306+
CanPackType CanPackType::get(const ASTContext &C,
3307+
CanTupleEltTypeArrayRef elements) {
3308+
SmallVector<Type, 8> ncElements(elements.begin(), elements.end());
3309+
return CanPackType(PackType::get(C, ncElements));
3310+
}
3311+
33063312
PackType *PackType::get(const ASTContext &C, ArrayRef<Type> elements) {
33073313
RecursiveTypeProperties properties;
33083314
bool isCanonical = true;

lib/AST/ParameterPack.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -478,13 +478,6 @@ bool SILPackType::containsPackExpansionType() const {
478478
CanPackType
479479
CanTupleType::getInducedPackTypeImpl(CanTupleType tuple, unsigned start, unsigned count) {
480480
assert(start + count <= tuple->getNumElements() && "range out of range");
481-
482481
auto &ctx = tuple->getASTContext();
483-
if (count == 0) return CanPackType::get(ctx, {});
484-
485-
SmallVector<CanType, 4> eltTypes;
486-
eltTypes.reserve(count);
487-
for (unsigned i = start, e = start + count; i != e; ++i)
488-
eltTypes.push_back(tuple.getElementType(i));
489-
return CanPackType::get(ctx, eltTypes);
482+
return CanPackType::get(ctx, tuple.getElementTypes().slice(start, count));
490483
}

0 commit comments

Comments
 (0)