Skip to content

Commit 8c4ef65

Browse files
committed
[AST] Plumb reduced shape types through local requirements into pack archetypes.
1 parent 5584752 commit 8c4ef65

File tree

6 files changed

+30
-8
lines changed

6 files changed

+30
-8
lines changed

include/swift/AST/GenericSignature.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ class GenericSignature {
179179

180180
RequiredProtocols protos;
181181
LayoutConstraint layout;
182+
183+
Type packShape;
182184
};
183185

184186
private:

include/swift/AST/Types.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6009,11 +6009,17 @@ BEGIN_CAN_TYPE_WRAPPER(OpenedArchetypeType, ArchetypeType)
60096009
}
60106010
END_CAN_TYPE_WRAPPER(OpenedArchetypeType, ArchetypeType)
60116011

6012+
/// A wrapper around a shape type to use in ArchetypeTrailingObjects
6013+
/// for PackArchetypeType.
6014+
struct PackShape {
6015+
Type shapeType;
6016+
};
6017+
60126018
/// An archetype that represents an opaque element of a type
60136019
/// parameter pack in context.
60146020
class PackArchetypeType final
60156021
: public ArchetypeType,
6016-
private ArchetypeTrailingObjects<PackArchetypeType> {
6022+
private ArchetypeTrailingObjects<PackArchetypeType, PackShape> {
60176023
friend TrailingObjects;
60186024
friend ArchetypeType;
60196025

@@ -6024,18 +6030,23 @@ class PackArchetypeType final
60246030
/// by this routine.
60256031
static CanTypeWrapper<PackArchetypeType>
60266032
get(const ASTContext &Ctx, GenericEnvironment *GenericEnv,
6027-
Type InterfaceType,
6033+
Type InterfaceType, Type ShapeType,
60286034
SmallVectorImpl<ProtocolDecl *> &ConformsTo, Type Superclass,
60296035
LayoutConstraint Layout);
60306036

6037+
// Returns the reduced shape type for this pack archetype.
6038+
Type getShape() const {
6039+
return getTrailingObjects<PackShape>()->shapeType;
6040+
}
6041+
60316042
static bool classof(const TypeBase *T) {
60326043
return T->getKind() == TypeKind::PackArchetype;
60336044
}
60346045

60356046
private:
60366047
PackArchetypeType(const ASTContext &Ctx, GenericEnvironment *GenericEnv,
60376048
Type InterfaceType, ArrayRef<ProtocolDecl *> ConformsTo,
6038-
Type Superclass, LayoutConstraint Layout);
6049+
Type Superclass, LayoutConstraint Layout, PackShape Shape);
60396050
};
60406051
BEGIN_CAN_TYPE_WRAPPER(PackArchetypeType, ArchetypeType)
60416052
END_CAN_TYPE_WRAPPER(PackArchetypeType, ArchetypeType)

lib/AST/GenericEnvironment.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ GenericEnvironment::getOrCreateArchetypeFromInterfaceType(Type depType) {
365365
if (rootGP->isParameterPack()) {
366366
assert(getKind() == Kind::Primary);
367367
result = PackArchetypeType::get(ctx, this, requirements.anchor,
368+
requirements.packShape,
368369
requirements.protos, superclass,
369370
requirements.layout);
370371
} else {

lib/AST/RequirementMachine/GenericSignatureQueries.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ RequirementMachine::getLocalRequirements(
7676

7777
result.layout = props->getLayoutConstraint();
7878

79+
result.packShape = getReducedShape(depType);
80+
7981
return result;
8082
}
8183

lib/AST/Type.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3959,17 +3959,18 @@ UUID OpenedArchetypeType::getOpenedExistentialID() const {
39593959
PackArchetypeType::PackArchetypeType(
39603960
const ASTContext &Ctx, GenericEnvironment *GenericEnv, Type InterfaceType,
39613961
ArrayRef<ProtocolDecl *> ConformsTo, Type Superclass,
3962-
LayoutConstraint Layout)
3962+
LayoutConstraint Layout, PackShape Shape)
39633963
: ArchetypeType(TypeKind::PackArchetype, Ctx,
39643964
RecursiveTypeProperties::HasArchetype, InterfaceType,
39653965
ConformsTo, Superclass, Layout, GenericEnv) {
39663966
assert(InterfaceType->isParameterPack());
3967+
*getTrailingObjects<PackShape>() = Shape;
39673968
}
39683969

39693970
CanPackArchetypeType
39703971
PackArchetypeType::get(const ASTContext &Ctx,
39713972
GenericEnvironment *GenericEnv,
3972-
Type InterfaceType,
3973+
Type InterfaceType, Type ShapeType,
39733974
SmallVectorImpl<ProtocolDecl *> &ConformsTo,
39743975
Type Superclass, LayoutConstraint Layout) {
39753976
assert(!Superclass || Superclass->getClassOrBoundGenericClass());
@@ -3981,12 +3982,14 @@ PackArchetypeType::get(const ASTContext &Ctx,
39813982
auto arena = AllocationArena::Permanent;
39823983
void *mem =
39833984
Ctx.Allocate(PackArchetypeType::totalSizeToAlloc<ProtocolDecl *, Type,
3984-
LayoutConstraint>(
3985-
ConformsTo.size(), Superclass ? 1 : 0, Layout ? 1 : 0),
3985+
LayoutConstraint, PackShape>(
3986+
ConformsTo.size(), Superclass ? 1 : 0, Layout ? 1 : 0,
3987+
/*shapeSize*/1),
39863988
alignof(PackArchetypeType), arena);
39873989

39883990
return CanPackArchetypeType(::new (mem) PackArchetypeType(
3989-
Ctx, GenericEnv, InterfaceType, ConformsTo, Superclass, Layout));
3991+
Ctx, GenericEnv, InterfaceType, ConformsTo, Superclass, Layout,
3992+
{ShapeType}));
39903993
}
39913994

39923995
ElementArchetypeType::ElementArchetypeType(

test/Constraints/variadic_generic_functions.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ func debugPrint<T...>(_ items: T...)
88
}*/
99
}
1010

11+
// FIXME: Add diagnostics for 'T' appearing outside of a pack expansion.
12+
/*
1113
func max<T...>(_ values: T...) -> T?
1214
where T: Comparable
1315
{
@@ -17,6 +19,7 @@ func max<T...>(_ values: T...) -> T?
1719
func min<T...: Comparable>(_ values: T...) -> T? {
1820
return nil
1921
}
22+
*/
2023

2124
func invalidPacks() {
2225
func monovariadic1() -> (String...) {} // expected-error {{variadic expansion 'String' must contain at least one variadic generic parameter}}

0 commit comments

Comments
 (0)