Skip to content

Commit e47fad1

Browse files
committed
[Variadic Generics] type parameter pack syntax change: T... -> each T
1 parent 6c19201 commit e47fad1

37 files changed

+409
-390
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
@@ -1982,6 +1982,8 @@ ERROR(attr_requires_concurrency, none,
19821982

19831983
ERROR(associatedtype_cannot_be_variadic,none,
19841984
"associated types cannot be variadic", ())
1985+
ERROR(type_parameter_pack_ellipsis,none,
1986+
"ellipsis operator cannot be used with a type parameter pack", ())
19851987

19861988
//------------------------------------------------------------------------------
19871989
// 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
@@ -1711,6 +1711,8 @@ void PrintAST::printSingleDepthOfGenericSignature(
17111711
llvm::interleave(
17121712
genericParams,
17131713
[&](GenericTypeParamType *param) {
1714+
if (param->isParameterPack())
1715+
Printer << "each ";
17141716
if (!subMap.empty()) {
17151717
printType(substParam(param));
17161718
} else if (auto *GP = param->getDecl()) {
@@ -1723,8 +1725,6 @@ void PrintAST::printSingleDepthOfGenericSignature(
17231725
} else {
17241726
printType(param);
17251727
}
1726-
if (param->isParameterPack())
1727-
Printer << "...";
17281728
},
17291729
[&] { Printer << ", "; });
17301730
}
@@ -1814,11 +1814,11 @@ void PrintAST::printSingleDepthOfGenericSignature(
18141814
void PrintAST::printRequirement(const Requirement &req) {
18151815
switch (req.getKind()) {
18161816
case RequirementKind::SameShape:
1817-
Printer << "((";
1817+
Printer << "(repeat (each ";
18181818
printTransformedType(req.getFirstType());
1819-
Printer << ", ";
1819+
Printer << ", each ";
18201820
printTransformedType(req.getSecondType());
1821-
Printer << ")...) : Any";
1821+
Printer << ")) : Any";
18221822
return;
18231823
case RequirementKind::Layout:
18241824
printTransformedType(req.getFirstType());
@@ -3589,9 +3589,9 @@ void PrintAST::visitTypeAliasDecl(TypeAliasDecl *decl) {
35893589

35903590
void PrintAST::visitGenericTypeParamDecl(GenericTypeParamDecl *decl) {
35913591
recordDeclLoc(decl, [&] {
3592-
Printer.printName(decl->getName(), PrintNameContext::GenericParameter);
35933592
if (decl->isParameterPack())
3594-
Printer << "...";
3593+
Printer << "each ";
3594+
Printer.printName(decl->getName(), PrintNameContext::GenericParameter);
35953595
});
35963596

35973597
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
@@ -4863,13 +4863,15 @@ Type TypeAliasDecl::getStructuralType() const {
48634863
return ErrorType::get(ctx);
48644864
}
48654865

4866-
GenericTypeParamDecl::GenericTypeParamDecl(
4867-
DeclContext *dc, Identifier name, SourceLoc nameLoc, SourceLoc ellipsisLoc,
4868-
unsigned depth, unsigned index, bool isParameterPack, bool isOpaqueType,
4869-
TypeRepr *opaqueTypeRepr)
4870-
: TypeDecl(DeclKind::GenericTypeParam, dc, name, nameLoc, { }) {
4871-
assert(!(ellipsisLoc && !isParameterPack) &&
4872-
"Ellipsis always means type parameter pack");
4866+
GenericTypeParamDecl::GenericTypeParamDecl(DeclContext *dc, Identifier name,
4867+
SourceLoc nameLoc, SourceLoc eachLoc,
4868+
unsigned depth, unsigned index,
4869+
bool isParameterPack,
4870+
bool isOpaqueType,
4871+
TypeRepr *opaqueTypeRepr)
4872+
: TypeDecl(DeclKind::GenericTypeParam, dc, name, nameLoc, {}) {
4873+
assert(!(eachLoc && !isParameterPack) &&
4874+
"'each' keyword always means type parameter pack");
48734875

48744876
Bits.GenericTypeParamDecl.Depth = depth;
48754877
assert(Bits.GenericTypeParamDecl.Depth == depth && "Truncation");
@@ -4881,7 +4883,7 @@ GenericTypeParamDecl::GenericTypeParamDecl(
48814883
if (isOpaqueType)
48824884
*getTrailingObjects<TypeRepr *>() = opaqueTypeRepr;
48834885
if (isParameterPack)
4884-
*getTrailingObjects<SourceLoc>() = ellipsisLoc;
4886+
*getTrailingObjects<SourceLoc>() = eachLoc;
48854887

48864888
auto &ctx = dc->getASTContext();
48874889
RecursiveTypeProperties props = RecursiveTypeProperties::HasTypeParameter;
@@ -4892,15 +4894,15 @@ GenericTypeParamDecl::GenericTypeParamDecl(
48924894
}
48934895

48944896
GenericTypeParamDecl *GenericTypeParamDecl::create(
4895-
DeclContext *dc, Identifier name, SourceLoc nameLoc, SourceLoc ellipsisLoc,
4897+
DeclContext *dc, Identifier name, SourceLoc nameLoc, SourceLoc eachLoc,
48964898
unsigned depth, unsigned index, bool isParameterPack, bool isOpaqueType,
48974899
TypeRepr *opaqueTypeRepr) {
48984900
auto &ctx = dc->getASTContext();
48994901
auto allocSize = totalSizeToAlloc<TypeRepr *, SourceLoc>(
49004902
isOpaqueType ? 1 : 0, isParameterPack ? 1 : 0);
49014903
auto mem = ctx.Allocate(allocSize, alignof(GenericTypeParamDecl));
49024904
return new (mem)
4903-
GenericTypeParamDecl(dc, name, nameLoc, ellipsisLoc, depth, index,
4905+
GenericTypeParamDecl(dc, name, nameLoc, eachLoc, depth, index,
49044906
isParameterPack, isOpaqueType, opaqueTypeRepr);
49054907
}
49064908

@@ -4914,37 +4916,38 @@ GenericTypeParamDecl *GenericTypeParamDecl::createDeserialized(
49144916

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

49274929
GenericTypeParamDecl *GenericTypeParamDecl::createImplicit(
49284930
DeclContext *dc, Identifier name, unsigned depth, unsigned index,
49294931
bool isParameterPack, bool isOpaqueType, TypeRepr *opaqueTypeRepr,
4930-
SourceLoc nameLoc, SourceLoc ellipsisLoc) {
4931-
auto *param = GenericTypeParamDecl::create(dc, name, nameLoc, ellipsisLoc,
4932-
depth, index, isParameterPack,
4932+
SourceLoc nameLoc, SourceLoc eachLoc) {
4933+
auto *param = GenericTypeParamDecl::create(dc, name, nameLoc, eachLoc, depth,
4934+
index, isParameterPack,
49334935
isOpaqueType, opaqueTypeRepr);
49344936
param->setImplicit();
49354937
return param;
49364938
}
49374939

49384940
SourceRange GenericTypeParamDecl::getSourceRange() const {
4939-
SourceLoc endLoc = getNameLoc();
4941+
auto startLoc = getNameLoc();
4942+
auto endLoc = getNameLoc();
49404943

4941-
if (auto ellipsisLoc = getEllipsisLoc())
4942-
endLoc = ellipsisLoc;
4944+
if (const auto eachLoc = getEachLoc())
4945+
startLoc = eachLoc;
49434946

49444947
if (!getInherited().empty())
49454948
endLoc = getInherited().back().getSourceRange().End;
49464949

4947-
return SourceRange(getNameLoc(), endLoc);
4950+
return {startLoc, endLoc};
49484951
}
49494952

49504953
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)