Skip to content

Commit db058a4

Browse files
authored
Merge pull request #64753 from slavapestov/variadic-conformances
Fixes for conformances of variadic generic types
2 parents d02c8f6 + 268fff7 commit db058a4

File tree

12 files changed

+320
-171
lines changed

12 files changed

+320
-171
lines changed

include/swift/AST/Types.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,7 +2081,7 @@ class TypeAliasType final
20812081
/// this type references.
20822082
ArrayRef<Type> getDirectGenericArgs() const;
20832083

2084-
PackType *getExpandedGenericArgsPack();
2084+
SmallVector<Type, 2> getExpandedGenericArgs();
20852085

20862086
// Support for FoldingSet.
20872087
void Profile(llvm::FoldingSetNodeID &id) const;
@@ -2562,7 +2562,7 @@ class BoundGenericType : public NominalOrBoundGenericNominalType,
25622562
return {getTrailingObjectsPointer(), Bits.BoundGenericType.GenericArgCount};
25632563
}
25642564

2565-
PackType *getExpandedGenericArgsPack();
2565+
SmallVector<Type, 2> getExpandedGenericArgs();
25662566

25672567
void Profile(llvm::FoldingSetNodeID &ID) {
25682568
Profile(ID, getDecl(), getParent(), getGenericArgs());
@@ -6820,15 +6820,15 @@ class PackType final : public TypeBase, public llvm::FoldingSetNode,
68206820
/// Creates a pack from the types in \p elements.
68216821
static PackType *get(const ASTContext &C, ArrayRef<Type> elements);
68226822

6823-
static PackType *get(const ASTContext &C,
6824-
TypeArrayView<GenericTypeParamType> params,
6825-
ArrayRef<Type> args);
6826-
68276823
/// Given a type T, which must be a pack parameter, a member type
68286824
/// of a pack parameter, or a pack archetype, construct the type
68296825
/// Pack{repeat each T}.
68306826
static PackType *getSingletonPackExpansion(Type packParameter);
68316827

6828+
static SmallVector<Type, 2> getExpandedGenericArgs(
6829+
TypeArrayView<GenericTypeParamType> params,
6830+
ArrayRef<Type> args);
6831+
68326832
public:
68336833
/// Retrieves the number of elements in this pack.
68346834
unsigned getNumElements() const { return Bits.PackType.Count; }

lib/AST/ASTPrinter.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5637,9 +5637,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
56375637
Optional<llvm::DenseMap<const clang::Module *, ModuleDecl *>>
56385638
VisibleClangModules;
56395639

5640-
void printGenericArgs(PackType *flatArgs) {
5640+
void printGenericArgs(ArrayRef<Type> flatArgs) {
56415641
Printer << "<";
5642-
interleave(flatArgs->getElementTypes(),
5642+
interleave(flatArgs,
56435643
[&](Type arg) { visit(arg); },
56445644
[&] { Printer << ", "; });
56455645
Printer << ">";
@@ -5648,7 +5648,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
56485648
void printGenericArgs(ASTContext &ctx,
56495649
TypeArrayView<GenericTypeParamType> params,
56505650
ArrayRef<Type> args) {
5651-
printGenericArgs(PackType::get(ctx, params, args));
5651+
printGenericArgs(PackType::getExpandedGenericArgs(params, args));
56525652
}
56535653

56545654
/// Helper function for printing a type that is embedded within a larger type.
@@ -5999,7 +5999,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
59995999

60006000
auto *typeAliasDecl = T->getDecl();
60016001
if (typeAliasDecl->isGeneric()) {
6002-
printGenericArgs(T->getExpandedGenericArgsPack());
6002+
printGenericArgs(T->getExpandedGenericArgs());
60036003
}
60046004
}
60056005

@@ -6104,7 +6104,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
61046104
}
61056105
printQualifiedType(T);
61066106

6107-
printGenericArgs(T->getExpandedGenericArgsPack());
6107+
printGenericArgs(T->getExpandedGenericArgs());
61086108
}
61096109

61106110
void visitParentType(Type T) {

lib/AST/ParameterPack.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ struct PackTypeParameterCollector: TypeWalker {
4040
if (auto parentType = boundGenericType->getParent())
4141
parentType.walk(*this);
4242

43-
Type(boundGenericType->getExpandedGenericArgsPack()).walk(*this);
43+
for (auto type : boundGenericType->getExpandedGenericArgs())
44+
type.walk(*this);
45+
4446
return Action::SkipChildren;
4547
}
4648

@@ -49,7 +51,9 @@ struct PackTypeParameterCollector: TypeWalker {
4951
if (auto parentType = typeAliasType->getParent())
5052
parentType.walk(*this);
5153

52-
Type(typeAliasType->getExpandedGenericArgsPack()).walk(*this);
54+
for (auto type : typeAliasType->getExpandedGenericArgs())
55+
type.walk(*this);
56+
5357
return Action::SkipChildren;
5458
}
5559
}
@@ -244,7 +248,7 @@ unsigned ParameterList::getOrigParamIndex(SubstitutionMap subMap,
244248
}
245249

246250
/// <T...> Foo<T, Pack{Int, String}> => Pack{T..., Int, String}
247-
PackType *BoundGenericType::getExpandedGenericArgsPack() {
251+
SmallVector<Type, 2> BoundGenericType::getExpandedGenericArgs() {
248252
// It would be nicer to use genericSig.getInnermostGenericParams() here,
249253
// but that triggers a request cycle if we're in the middle of computing
250254
// the generic signature already.
@@ -253,26 +257,26 @@ PackType *BoundGenericType::getExpandedGenericArgsPack() {
253257
params.push_back(paramDecl->getDeclaredInterfaceType());
254258
}
255259

256-
return PackType::get(getASTContext(),
260+
return PackType::getExpandedGenericArgs(
257261
TypeArrayView<GenericTypeParamType>(params),
258262
getGenericArgs());
259263
}
260264

261265
/// <T...> Foo<T, Pack{Int, String}> => Pack{T..., Int, String}
262-
PackType *TypeAliasType::getExpandedGenericArgsPack() {
266+
SmallVector<Type, 2> TypeAliasType::getExpandedGenericArgs() {
263267
if (!getDecl()->isGeneric())
264-
return nullptr;
268+
return SmallVector<Type, 2>();
265269

266270
auto genericSig = getGenericSignature();
267-
return PackType::get(getASTContext(),
271+
return PackType::getExpandedGenericArgs(
268272
genericSig.getInnermostGenericParams(),
269273
getDirectGenericArgs());
270274
}
271275

272-
/// <T...> Pack{T, Pack{Int, String}} => Pack{T..., Int, String}
273-
PackType *PackType::get(const ASTContext &C,
274-
TypeArrayView<GenericTypeParamType> params,
275-
ArrayRef<Type> args) {
276+
/// <T...> Pack{T, Pack{Int, String}} => {T..., Int, String}
277+
SmallVector<Type, 2>
278+
PackType::getExpandedGenericArgs(TypeArrayView<GenericTypeParamType> params,
279+
ArrayRef<Type> args) {
276280
SmallVector<Type, 2> wrappedArgs;
277281

278282
assert(params.size() == args.size());
@@ -288,7 +292,7 @@ PackType *PackType::get(const ASTContext &C,
288292
wrappedArgs.push_back(arg);
289293
}
290294

291-
return get(C, wrappedArgs);
295+
return wrappedArgs;
292296
}
293297

294298
PackType *PackType::getSingletonPackExpansion(Type param) {
@@ -347,7 +351,7 @@ static CanPackType getApproximateFormalPackType(const ASTContext &ctx,
347351
Collection loweredEltTypes) {
348352
// Build an array of formal element types, but be lazy about it:
349353
// use the original array unless we see an element type that doesn't
350-
// work as a legal format type.
354+
// work as a legal formal type.
351355
Optional<SmallVector<CanType, 4>> formalEltTypes;
352356
for (auto i : indices(loweredEltTypes)) {
353357
auto loweredEltType = loweredEltTypes[i];

lib/AST/RequirementEnvironment.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,12 @@ RequirementEnvironment::RequirementEnvironment(
108108
// invalid code.
109109
if (genericParam->getDepth() != 1)
110110
return Type();
111-
auto substGenericParam = GenericTypeParamType::get(
111+
Type substGenericParam = GenericTypeParamType::get(
112112
genericParam->isParameterPack(), depth, genericParam->getIndex(), ctx);
113+
if (genericParam->isParameterPack()) {
114+
substGenericParam = PackType::getSingletonPackExpansion(
115+
substGenericParam);
116+
}
113117
return substGenericParam;
114118
},
115119
[selfType, substConcreteType, conformance, conformanceDC, &ctx](

0 commit comments

Comments
 (0)