Skip to content

Commit a9e5075

Browse files
Merge pull request #64039 from sophiapoirier/pack-type-syntax-each
[Variadic Generics] type parameter pack syntax change: T... -> each T
2 parents 4d3f1fa + 70cffb0 commit a9e5075

37 files changed

+410
-391
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ void *GenericParamList_create(void *ctx, void *lAngleLoc,
269269
BridgedArrayRef reqs, void *rAngleLoc);
270270
void *GenericTypeParamDecl_create(void *ctx, void *declContext,
271271
BridgedIdentifier name, void *nameLoc,
272-
void *_Nullable ellipsisLoc, long index,
272+
void *_Nullable eachLoc, long index,
273273
_Bool isParameterPack);
274274
void GenericTypeParamDecl_setInheritedType(void *ctx, void *Param, void *ty);
275275

include/swift/AST/Decl.h

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3331,17 +3331,18 @@ class GenericTypeParamDecl final
33313331
///
33323332
/// \param name The name of the generic parameter.
33333333
/// \param nameLoc The location of the name.
3334-
/// \param ellipsisLoc The location of the ellipsis for a type parameter pack.
3334+
/// \param eachLoc The location of the 'each' keyword for a type parameter
3335+
/// pack.
33353336
/// \param depth The generic signature depth.
33363337
/// \param index The index of the parameter in the generic signature.
33373338
/// \param isParameterPack Whether the generic parameter is for a type
3338-
/// parameter pack, denoted by \c <T...>.
3339+
/// parameter pack, denoted by \c <each T>.
33393340
/// \param isOpaqueType Whether the generic parameter is written as an opaque
33403341
/// parameter e.g 'some Collection'.
33413342
/// \param opaqueTypeRepr The TypeRepr of an opaque generic parameter.
33423343
///
33433344
GenericTypeParamDecl(DeclContext *dc, Identifier name, SourceLoc nameLoc,
3344-
SourceLoc ellipsisLoc, unsigned depth, unsigned index,
3345+
SourceLoc eachLoc, unsigned depth, unsigned index,
33453346
bool isParameterPack, bool isOpaqueType,
33463347
TypeRepr *opaqueTypeRepr);
33473348

@@ -3353,17 +3354,18 @@ class GenericTypeParamDecl final
33533354
///
33543355
/// \param name The name of the generic parameter.
33553356
/// \param nameLoc The location of the name.
3356-
/// \param ellipsisLoc The location of the ellipsis for a type parameter pack.
3357+
/// \param eachLoc The location of the 'each' keyword for a type parameter
3358+
/// pack.
33573359
/// \param depth The generic signature depth.
33583360
/// \param index The index of the parameter in the generic signature.
33593361
/// \param isParameterPack Whether the generic parameter is for a type
3360-
/// parameter pack, denoted by \c <T...>.
3362+
/// parameter pack, denoted by \c <each T>.
33613363
/// \param isOpaqueType Whether the generic parameter is written as an opaque
33623364
/// parameter e.g 'some Collection'.
33633365
/// \param opaqueTypeRepr The TypeRepr of an opaque generic parameter.
33643366
///
33653367
static GenericTypeParamDecl *create(DeclContext *dc, Identifier name,
3366-
SourceLoc nameLoc, SourceLoc ellipsisLoc,
3368+
SourceLoc nameLoc, SourceLoc eachLoc,
33673369
unsigned depth, unsigned index,
33683370
bool isParameterPack, bool isOpaqueType,
33693371
TypeRepr *opaqueTypeRepr);
@@ -3374,9 +3376,9 @@ class GenericTypeParamDecl final
33743376
/// Construct a new generic type parameter. This should only be used by the
33753377
/// ClangImporter, use \c GenericTypeParamDecl::create[...] instead.
33763378
GenericTypeParamDecl(DeclContext *dc, Identifier name, SourceLoc nameLoc,
3377-
SourceLoc ellipsisLoc, unsigned depth, unsigned index,
3379+
SourceLoc eachLoc, unsigned depth, unsigned index,
33783380
bool isParameterPack)
3379-
: GenericTypeParamDecl(dc, name, nameLoc, ellipsisLoc, depth, index,
3381+
: GenericTypeParamDecl(dc, name, nameLoc, eachLoc, depth, index,
33803382
isParameterPack, /*isOpaqueType*/ false, nullptr) {
33813383
}
33823384

@@ -3390,7 +3392,7 @@ class GenericTypeParamDecl final
33903392
/// \param depth The generic signature depth.
33913393
/// \param index The index of the parameter in the generic signature.
33923394
/// \param isParameterPack Whether the generic parameter is for a type
3393-
/// parameter pack, denoted by \c <T...>.
3395+
/// parameter pack, denoted by \c <each T>.
33943396
/// \param isOpaqueType Whether the generic parameter is written as an opaque
33953397
/// parameter e.g 'some Collection'.
33963398
///
@@ -3406,14 +3408,16 @@ class GenericTypeParamDecl final
34063408
///
34073409
/// \param name The name of the generic parameter.
34083410
/// \param nameLoc The location of the name.
3409-
/// \param ellipsisLoc The location of the ellipsis for a type parameter pack.
3411+
/// \param eachLoc The location of the 'each' keyword for a type parameter
3412+
/// pack.
34103413
/// \param index The index of the parameter in the generic signature.
34113414
/// \param isParameterPack Whether the generic parameter is for a type
3412-
/// parameter pack, denoted by \c <T...>.
3415+
/// parameter pack, denoted by \c <each T>.
34133416
///
3414-
static GenericTypeParamDecl *
3415-
createParsed(DeclContext *dc, Identifier name, SourceLoc nameLoc,
3416-
SourceLoc ellipsisLoc, unsigned index, bool isParameterPack);
3417+
static GenericTypeParamDecl *createParsed(DeclContext *dc, Identifier name,
3418+
SourceLoc nameLoc,
3419+
SourceLoc eachLoc, unsigned index,
3420+
bool isParameterPack);
34173421

34183422
/// Construct a new implicit generic type parameter.
34193423
///
@@ -3425,19 +3429,19 @@ class GenericTypeParamDecl final
34253429
/// \param depth The generic signature depth.
34263430
/// \param index The index of the parameter in the generic signature.
34273431
/// \param isParameterPack Whether the generic parameter is for a type
3428-
/// parameter pack, denoted by \c <T...>.
3432+
/// parameter pack, denoted by \c <each T>.
34293433
/// \param isOpaqueType Whether the generic parameter is written as an opaque
34303434
/// parameter e.g 'some Collection'.
34313435
/// \param opaqueTypeRepr The TypeRepr of an opaque generic parameter.
34323436
/// \param nameLoc The location of the name.
3433-
/// \param ellipsisLoc The location of the ellipsis for a type parameter pack.
3437+
/// \param eachLoc The location of the 'each' keyword for a type parameter
3438+
/// pack.
34343439
///
34353440
static GenericTypeParamDecl *
34363441
createImplicit(DeclContext *dc, Identifier name, unsigned depth,
34373442
unsigned index, bool isParameterPack = false,
34383443
bool isOpaqueType = false, TypeRepr *opaqueTypeRepr = nullptr,
3439-
SourceLoc nameLoc = SourceLoc(),
3440-
SourceLoc ellipsisLoc = SourceLoc());
3444+
SourceLoc nameLoc = {}, SourceLoc eachLoc = {});
34413445

34423446
/// The depth of this generic type parameter, i.e., the number of outer
34433447
/// levels of generic parameter lists that enclose this type parameter.
@@ -3463,8 +3467,8 @@ class GenericTypeParamDecl final
34633467
/// parameter pack.
34643468
///
34653469
/// \code
3466-
/// func foo<T...>(_ : T...) { }
3467-
/// struct Foo<T...> { }
3470+
/// func foo<each T>(_ : for each T) { }
3471+
/// struct Foo<each T> { }
34683472
/// \endcode
34693473
bool isParameterPack() const { return Bits.GenericTypeParamDecl.ParameterPack; }
34703474

@@ -3504,8 +3508,8 @@ class GenericTypeParamDecl final
35043508
/// Here 'T' and 'U' have indexes 0 and 1, respectively. 'V' has index 0.
35053509
unsigned getIndex() const { return Bits.GenericTypeParamDecl.Index; }
35063510

3507-
/// Retrieve the ellipsis location for a type parameter pack \c T...
3508-
SourceLoc getEllipsisLoc() const {
3511+
/// Retrieve the 'each' keyword location for a type parameter pack \c each T
3512+
SourceLoc getEachLoc() const {
35093513
if (!isParameterPack())
35103514
return SourceLoc();
35113515

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,6 +1989,8 @@ ERROR(attr_requires_concurrency, none,
19891989

19901990
ERROR(associatedtype_cannot_be_variadic,none,
19911991
"associated types cannot be variadic", ())
1992+
ERROR(type_parameter_pack_ellipsis,none,
1993+
"ellipsis operator cannot be used with a type parameter pack", ())
19921994

19931995
//------------------------------------------------------------------------------
19941996
// MARK: syntax parsing diagnostics

include/swift/AST/Identifier.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,6 @@ class Identifier {
128128
return is("??");
129129
}
130130

131-
bool isExpansionOperator() const {
132-
return is("...");
133-
}
134-
135131
/// isOperatorStartCodePoint - Return true if the specified code point is a
136132
/// valid start of an operator.
137133
static bool isOperatorStartCodePoint(uint32_t C) {

include/swift/IRGen/IRABIDetailsProvider.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class LoweredFunctionSignature {
117117
friend class LoweredFunctionSignature;
118118
};
119119

120-
/// Represents a generic requirement paremeter that must be passed to the
120+
/// Represents a generic requirement parameter that must be passed to the
121121
/// function.
122122
class GenericRequirementParameter {
123123
public:

lib/AST/ASTPrinter.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,8 @@ void PrintAST::printSingleDepthOfGenericSignature(
17131713
llvm::interleave(
17141714
genericParams,
17151715
[&](GenericTypeParamType *param) {
1716+
if (param->isParameterPack())
1717+
Printer << "each ";
17161718
if (!subMap.empty()) {
17171719
printType(substParam(param));
17181720
} else if (auto *GP = param->getDecl()) {
@@ -1725,8 +1727,6 @@ void PrintAST::printSingleDepthOfGenericSignature(
17251727
} else {
17261728
printType(param);
17271729
}
1728-
if (param->isParameterPack())
1729-
Printer << "...";
17301730
},
17311731
[&] { Printer << ", "; });
17321732
}
@@ -1816,11 +1816,11 @@ void PrintAST::printSingleDepthOfGenericSignature(
18161816
void PrintAST::printRequirement(const Requirement &req) {
18171817
switch (req.getKind()) {
18181818
case RequirementKind::SameShape:
1819-
Printer << "((";
1819+
Printer << "(repeat (each ";
18201820
printTransformedType(req.getFirstType());
1821-
Printer << ", ";
1821+
Printer << ", each ";
18221822
printTransformedType(req.getSecondType());
1823-
Printer << ")...) : Any";
1823+
Printer << ")) : Any";
18241824
return;
18251825
case RequirementKind::Layout:
18261826
printTransformedType(req.getFirstType());
@@ -3615,9 +3615,9 @@ void PrintAST::visitTypeAliasDecl(TypeAliasDecl *decl) {
36153615

36163616
void PrintAST::visitGenericTypeParamDecl(GenericTypeParamDecl *decl) {
36173617
recordDeclLoc(decl, [&] {
3618-
Printer.printName(decl->getName(), PrintNameContext::GenericParameter);
36193618
if (decl->isParameterPack())
3620-
Printer << "...";
3619+
Printer << "each ";
3620+
Printer.printName(decl->getName(), PrintNameContext::GenericParameter);
36213621
});
36223622

36233623
printInherited(decl);

lib/AST/CASTBridging.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,12 +579,12 @@ void *GenericParamList_create(void *ctx, void *lAngleLoc,
579579

580580
void *GenericTypeParamDecl_create(void *ctx, void *declContext,
581581
BridgedIdentifier name, void *nameLoc,
582-
void *_Nullable ellipsisLoc, long index,
582+
void *_Nullable eachLoc, long index,
583583
bool isParameterPack) {
584584
return GenericTypeParamDecl::createParsed(
585585
static_cast<DeclContext *>(declContext),
586586
Identifier::getFromOpaquePointer(name), getSourceLocFromPointer(nameLoc),
587-
getSourceLocFromPointer(ellipsisLoc),
587+
getSourceLocFromPointer(eachLoc),
588588
/*index*/ index, isParameterPack);
589589
}
590590

lib/AST/Decl.cpp

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4869,13 +4869,15 @@ Type TypeAliasDecl::getStructuralType() const {
48694869
return ErrorType::get(ctx);
48704870
}
48714871

4872-
GenericTypeParamDecl::GenericTypeParamDecl(
4873-
DeclContext *dc, Identifier name, SourceLoc nameLoc, SourceLoc ellipsisLoc,
4874-
unsigned depth, unsigned index, bool isParameterPack, bool isOpaqueType,
4875-
TypeRepr *opaqueTypeRepr)
4876-
: TypeDecl(DeclKind::GenericTypeParam, dc, name, nameLoc, { }) {
4877-
assert(!(ellipsisLoc && !isParameterPack) &&
4878-
"Ellipsis always means type parameter pack");
4872+
GenericTypeParamDecl::GenericTypeParamDecl(DeclContext *dc, Identifier name,
4873+
SourceLoc nameLoc, SourceLoc eachLoc,
4874+
unsigned depth, unsigned index,
4875+
bool isParameterPack,
4876+
bool isOpaqueType,
4877+
TypeRepr *opaqueTypeRepr)
4878+
: TypeDecl(DeclKind::GenericTypeParam, dc, name, nameLoc, {}) {
4879+
assert(!(eachLoc && !isParameterPack) &&
4880+
"'each' keyword always means type parameter pack");
48794881

48804882
Bits.GenericTypeParamDecl.Depth = depth;
48814883
assert(Bits.GenericTypeParamDecl.Depth == depth && "Truncation");
@@ -4887,7 +4889,7 @@ GenericTypeParamDecl::GenericTypeParamDecl(
48874889
if (isOpaqueType)
48884890
*getTrailingObjects<TypeRepr *>() = opaqueTypeRepr;
48894891
if (isParameterPack)
4890-
*getTrailingObjects<SourceLoc>() = ellipsisLoc;
4892+
*getTrailingObjects<SourceLoc>() = eachLoc;
48914893

48924894
auto &ctx = dc->getASTContext();
48934895
RecursiveTypeProperties props = RecursiveTypeProperties::HasTypeParameter;
@@ -4898,15 +4900,15 @@ GenericTypeParamDecl::GenericTypeParamDecl(
48984900
}
48994901

49004902
GenericTypeParamDecl *GenericTypeParamDecl::create(
4901-
DeclContext *dc, Identifier name, SourceLoc nameLoc, SourceLoc ellipsisLoc,
4903+
DeclContext *dc, Identifier name, SourceLoc nameLoc, SourceLoc eachLoc,
49024904
unsigned depth, unsigned index, bool isParameterPack, bool isOpaqueType,
49034905
TypeRepr *opaqueTypeRepr) {
49044906
auto &ctx = dc->getASTContext();
49054907
auto allocSize = totalSizeToAlloc<TypeRepr *, SourceLoc>(
49064908
isOpaqueType ? 1 : 0, isParameterPack ? 1 : 0);
49074909
auto mem = ctx.Allocate(allocSize, alignof(GenericTypeParamDecl));
49084910
return new (mem)
4909-
GenericTypeParamDecl(dc, name, nameLoc, ellipsisLoc, depth, index,
4911+
GenericTypeParamDecl(dc, name, nameLoc, eachLoc, depth, index,
49104912
isParameterPack, isOpaqueType, opaqueTypeRepr);
49114913
}
49124914

@@ -4920,37 +4922,38 @@ GenericTypeParamDecl *GenericTypeParamDecl::createDeserialized(
49204922

49214923
GenericTypeParamDecl *
49224924
GenericTypeParamDecl::createParsed(DeclContext *dc, Identifier name,
4923-
SourceLoc nameLoc, SourceLoc ellipsisLoc,
4925+
SourceLoc nameLoc, SourceLoc eachLoc,
49244926
unsigned index, bool isParameterPack) {
49254927
// We always create generic type parameters with an invalid depth.
49264928
// Semantic analysis fills in the depth when it processes the generic
49274929
// parameter list.
49284930
return GenericTypeParamDecl::create(
4929-
dc, name, nameLoc, ellipsisLoc, GenericTypeParamDecl::InvalidDepth, index,
4931+
dc, name, nameLoc, eachLoc, GenericTypeParamDecl::InvalidDepth, index,
49304932
isParameterPack, /*isOpaqueType*/ false, /*opaqueTypeRepr*/ nullptr);
49314933
}
49324934

49334935
GenericTypeParamDecl *GenericTypeParamDecl::createImplicit(
49344936
DeclContext *dc, Identifier name, unsigned depth, unsigned index,
49354937
bool isParameterPack, bool isOpaqueType, TypeRepr *opaqueTypeRepr,
4936-
SourceLoc nameLoc, SourceLoc ellipsisLoc) {
4937-
auto *param = GenericTypeParamDecl::create(dc, name, nameLoc, ellipsisLoc,
4938-
depth, index, isParameterPack,
4938+
SourceLoc nameLoc, SourceLoc eachLoc) {
4939+
auto *param = GenericTypeParamDecl::create(dc, name, nameLoc, eachLoc, depth,
4940+
index, isParameterPack,
49394941
isOpaqueType, opaqueTypeRepr);
49404942
param->setImplicit();
49414943
return param;
49424944
}
49434945

49444946
SourceRange GenericTypeParamDecl::getSourceRange() const {
4945-
SourceLoc endLoc = getNameLoc();
4947+
auto startLoc = getNameLoc();
4948+
auto endLoc = getNameLoc();
49464949

4947-
if (auto ellipsisLoc = getEllipsisLoc())
4948-
endLoc = ellipsisLoc;
4950+
if (const auto eachLoc = getEachLoc())
4951+
startLoc = eachLoc;
49494952

49504953
if (!getInherited().empty())
49514954
endLoc = getInherited().back().getSourceRange().End;
49524955

4953-
return SourceRange(getNameLoc(), endLoc);
4956+
return {startLoc, endLoc};
49544957
}
49554958

49564959
AssociatedTypeDecl::AssociatedTypeDecl(DeclContext *dc, SourceLoc keywordLoc,

lib/ASTGen/Sources/ASTGen/Generics.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ extension ASTGenVisitor {
2121
return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count)
2222
}
2323
let nameLoc = self.base.advanced(by: node.name.position.utf8Offset).raw
24-
let ellipsisLoc = node.ellipsis.map { self.base.advanced(by: $0.position.utf8Offset).raw }
24+
let eachLoc = node.each.map { self.base.advanced(by: $0.position.utf8Offset).raw }
2525

2626
return .decl(
2727
GenericTypeParamDecl_create(
28-
self.ctx, self.declContext, name, nameLoc, ellipsisLoc, node.indexInParent / 2,
29-
ellipsisLoc != nil))
28+
self.ctx, self.declContext, name, nameLoc, eachLoc, node.indexInParent / 2,
29+
eachLoc != nil))
3030
}
3131
}
3232

0 commit comments

Comments
 (0)