Skip to content

Commit 79ed990

Browse files
committed
AST: Replace TupleTypeRepr's ellipsis with PackExpansionTypeRepr
1 parent db4b4c9 commit 79ed990

27 files changed

+281
-304
lines changed

include/swift/AST/Decl.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5904,10 +5904,8 @@ class ParamDecl : public VarDecl {
59045904

59055905
void setDefaultValueStringRepresentation(StringRef stringRepresentation);
59065906

5907-
/// Whether or not this parameter is varargs.
5908-
bool isVariadic() const {
5909-
return DefaultValueAndFlags.getInt().contains(Flags::IsVariadic);
5910-
}
5907+
/// Whether or not this parameter is old-style variadic.
5908+
bool isVariadic() const;
59115909
void setVariadic(bool value = true) {
59125910
auto flags = DefaultValueAndFlags.getInt();
59135911
DefaultValueAndFlags.setInt(value ? flags | Flags::IsVariadic

include/swift/AST/DiagnosticsParse.def

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -852,8 +852,6 @@ NOTE(subscript_array_element_fix_it_remove_space, none,
852852
// Tuple Types
853853
ERROR(expected_rparen_tuple_type_list,none,
854854
"expected ')' at end of tuple list", ())
855-
ERROR(multiple_ellipsis_in_tuple,none,
856-
"only a single element can be variadic", ())
857855
ERROR(tuple_type_init,none,
858856
"default argument not permitted in a tuple type", ())
859857
ERROR(protocol_method_argument_init,none,
@@ -959,8 +957,6 @@ ERROR(expected_parameter_colon,PointsToFirstBadToken,
959957
"expected ':' following argument label and parameter name", ())
960958
ERROR(expected_assignment_instead_of_comparison_operator,none,
961959
"expected '=' instead of '==' to assign default value for parameter", ())
962-
ERROR(parameter_vararg_default,none,
963-
"variadic parameter cannot have a default value", ())
964960
ERROR(parameter_specifier_as_attr_disallowed,none,
965961
"'%0' before a parameter name is not allowed, place it before the parameter type instead",
966962
(StringRef))
@@ -995,11 +991,6 @@ ERROR(false_available_is_called_unavailable,none,
995991
ERROR(initializer_as_typed_pattern,none,
996992
"unexpected initializer in pattern; did you mean to use '='?", ())
997993

998-
ERROR(unlabeled_parameter_following_variadic_parameter,none,
999-
"a parameter following a variadic parameter requires a label", ())
1000-
ERROR(closure_unlabeled_parameter_following_variadic_parameter,none,
1001-
"no parameters may follow a variadic parameter in a closure", ())
1002-
1003994
ERROR(enum_element_empty_arglist,none,
1004995
"enum element with associated values must have at least one "
1005996
"associated value", ())

include/swift/AST/DiagnosticsSema.def

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5014,14 +5014,22 @@ ERROR(dot_protocol_on_non_existential,none,
50145014
"cannot use 'Protocol' with non-protocol type %0", (Type))
50155015
ERROR(tuple_single_element,none,
50165016
"cannot create a single-element tuple with an element label", ())
5017-
ERROR(tuple_ellipsis,none,
5018-
"cannot create a variadic tuple", ())
50195017
ERROR(expansion_not_variadic,none,
50205018
"cannot create expansion with non-variadic type %0", (Type))
50215019
ERROR(tuple_duplicate_label,none,
50225020
"cannot create a tuple with a duplicate element label", ())
5021+
ERROR(multiple_ellipsis_in_tuple,none,
5022+
"only a single element can be variadic", ())
5023+
50235024
ERROR(enum_element_ellipsis,none,
50245025
"variadic enum cases are not supported", ())
5026+
ERROR(unlabeled_parameter_following_variadic_parameter,none,
5027+
"a parameter following a variadic parameter requires a label", ())
5028+
ERROR(closure_unlabeled_parameter_following_variadic_parameter,none,
5029+
"no parameters may follow a variadic parameter in a closure", ())
5030+
5031+
ERROR(parameter_vararg_default,none,
5032+
"variadic parameter cannot have a default value", ())
50255033

50265034
WARNING(implicitly_unwrapped_optional_in_illegal_position_interpreted_as_optional,none,
50275035
"using '!' is not allowed here; treating this as '?' instead", ())
@@ -5142,8 +5150,6 @@ ERROR(opened_bad_constraint_type,none,
51425150
"@opened constraint type %0 is not a protocol or protocol composition", (Type))
51435151
ERROR(opened_bad_interface_type,none,
51445152
"@opened interface type %0 is not a type parameter", (Type))
5145-
ERROR(sil_function_ellipsis,PointsToFirstBadToken,
5146-
"SIL function types cannot be variadic", ())
51475153
ERROR(sil_function_input_label,PointsToFirstBadToken,
51485154
"SIL function types cannot have labeled inputs", ())
51495155
ERROR(sil_function_output_label,PointsToFirstBadToken,

include/swift/AST/TypeRepr.h

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,7 @@ class alignas(1 << TypeReprAlignInBits) TypeRepr
7373
Warned : 1
7474
);
7575

76-
SWIFT_INLINE_BITFIELD_FULL(TupleTypeRepr, TypeRepr, 1+32,
77-
/// Whether this tuple has '...' and its position.
78-
HasEllipsis : 1,
79-
: NumPadBits,
76+
SWIFT_INLINE_BITFIELD_FULL(TupleTypeRepr, TypeRepr, 32,
8077
/// The number of elements contained.
8178
NumElements : 32
8279
);
@@ -754,19 +751,16 @@ class PackExpansionTypeRepr final : public TypeRepr {
754751
/// (_ x: Foo)
755752
/// \endcode
756753
class TupleTypeRepr final : public TypeRepr,
757-
private llvm::TrailingObjects<TupleTypeRepr, TupleTypeReprElement,
758-
Located<unsigned>> {
754+
private llvm::TrailingObjects<TupleTypeRepr, TupleTypeReprElement> {
759755
friend TrailingObjects;
760-
typedef Located<unsigned> SourceLocAndIdx;
761756

762757
SourceRange Parens;
763758

764759
size_t numTrailingObjects(OverloadToken<TupleTypeReprElement>) const {
765760
return Bits.TupleTypeRepr.NumElements;
766761
}
767762

768-
TupleTypeRepr(ArrayRef<TupleTypeReprElement> Elements,
769-
SourceRange Parens, SourceLoc Ellipsis, unsigned EllipsisIdx);
763+
TupleTypeRepr(ArrayRef<TupleTypeReprElement> Elements, SourceRange Parens);
770764

771765
public:
772766
unsigned getNumElements() const { return Bits.TupleTypeRepr.NumElements; }
@@ -822,47 +816,15 @@ class TupleTypeRepr final : public TypeRepr,
822816

823817
SourceRange getParens() const { return Parens; }
824818

825-
bool hasEllipsis() const {
826-
return Bits.TupleTypeRepr.HasEllipsis;
827-
}
828-
829-
SourceLoc getEllipsisLoc() const {
830-
return hasEllipsis() ?
831-
getTrailingObjects<SourceLocAndIdx>()[0].Loc : SourceLoc();
832-
}
833-
834-
unsigned getEllipsisIndex() const {
835-
return hasEllipsis() ?
836-
getTrailingObjects<SourceLocAndIdx>()[0].Item :
837-
Bits.TupleTypeRepr.NumElements;
838-
}
839-
840-
void removeEllipsis() {
841-
if (hasEllipsis()) {
842-
Bits.TupleTypeRepr.HasEllipsis = false;
843-
getTrailingObjects<SourceLocAndIdx>()[0] = {
844-
getNumElements(),
845-
SourceLoc()
846-
};
847-
}
848-
}
849-
850819
bool isParenType() const {
851820
return Bits.TupleTypeRepr.NumElements == 1 &&
852821
getElementNameLoc(0).isInvalid() &&
853-
!hasEllipsis();
822+
!isa<PackExpansionTypeRepr>(getElementType(0));
854823
}
855824

856825
static TupleTypeRepr *create(const ASTContext &C,
857826
ArrayRef<TupleTypeReprElement> Elements,
858-
SourceRange Parens,
859-
SourceLoc Ellipsis, unsigned EllipsisIdx);
860-
static TupleTypeRepr *create(const ASTContext &C,
861-
ArrayRef<TupleTypeReprElement> Elements,
862-
SourceRange Parens) {
863-
return create(C, Elements, Parens,
864-
SourceLoc(), Elements.size());
865-
}
827+
SourceRange Parens);
866828
static TupleTypeRepr *createEmpty(const ASTContext &C, SourceRange Parens);
867829

868830
static bool classof(const TypeRepr *T) {

include/swift/Parse/Parser.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,10 @@ class Parser {
13161316
CustomAttribute,
13171317
};
13181318

1319+
ParserResult<TypeRepr> parseTypeScalar(
1320+
Diag<> MessageID,
1321+
ParseTypeReason reason);
1322+
13191323
ParserResult<TypeRepr> parseType();
13201324
ParserResult<TypeRepr> parseType(
13211325
Diag<> MessageID,
@@ -1433,9 +1437,6 @@ class Parser {
14331437
/// \p SecondName is the name.
14341438
SourceLoc SecondNameLoc;
14351439

1436-
/// The location of the '...', if present.
1437-
SourceLoc EllipsisLoc;
1438-
14391440
/// The first name.
14401441
Identifier FirstName;
14411442

lib/AST/ASTDumper.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -960,8 +960,9 @@ namespace {
960960
}
961961
}
962962

963-
if (P->isVariadic())
964-
OS << " variadic";
963+
if (P->hasInterfaceType())
964+
if (P->isVariadic())
965+
OS << " variadic";
965966

966967
if (P->isAutoClosure())
967968
OS << " autoclosure";

lib/AST/ASTPrinter.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3832,13 +3832,6 @@ void PrintAST::printOneParameter(const ParamDecl *param,
38323832
TheTypeLoc = TypeLoc::withoutLoc(param->getInterfaceType());
38333833
}
38343834

3835-
// If the parameter is variadic, we will print the "..." after it, but we have
3836-
// to strip off the added array type.
3837-
if (param->isVariadic() && TheTypeLoc.getType()) {
3838-
if (auto *BGT = TheTypeLoc.getType()->getAs<BoundGenericType>())
3839-
TheTypeLoc.setType(BGT->getGenericArgs()[0]);
3840-
}
3841-
38423835
{
38433836
Printer.printStructurePre(PrintStructureKind::FunctionParameterType);
38443837
SWIFT_DEFER {
@@ -3853,9 +3846,6 @@ void PrintAST::printOneParameter(const ParamDecl *param,
38533846

38543847
printTypeLocForImplicitlyUnwrappedOptional(
38553848
TheTypeLoc, param->isImplicitlyUnwrappedOptional());
3856-
3857-
if (param->isVariadic())
3858-
Printer << "...";
38593849
}
38603850

38613851
if (param->isDefaultArgument() && Options.PrintDefaultArgumentValue) {

lib/AST/Decl.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,10 +1873,6 @@ static bool isDefaultInitializable(const TypeRepr *typeRepr, ASTContext &ctx) {
18731873
// Tuple types are default-initializable if all of their element
18741874
// types are.
18751875
if (const auto tuple = dyn_cast<TupleTypeRepr>(typeRepr)) {
1876-
// ... but not variadic ones.
1877-
if (tuple->hasEllipsis())
1878-
return false;
1879-
18801876
for (const auto &elt : tuple->getElements()) {
18811877
if (!isDefaultInitializable(elt.Type, ctx))
18821878
return false;
@@ -6530,6 +6526,12 @@ bool ParamDecl::isAnonClosureParam() const {
65306526
return nameStr[0] == '$';
65316527
}
65326528

6529+
bool ParamDecl::isVariadic() const {
6530+
(void) getInterfaceType();
6531+
6532+
return DefaultValueAndFlags.getInt().contains(Flags::IsVariadic);
6533+
}
6534+
65336535
ParamDecl::Specifier ParamDecl::getSpecifier() const {
65346536
auto &ctx = getASTContext();
65356537

lib/AST/NameLookup.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2811,10 +2811,6 @@ createOpaqueParameterGenericParams(
28112811
SmallVector<GenericTypeParamDecl *, 2> implicitGenericParams;
28122812
auto dc = value->getInnermostDeclContext();
28132813
for (auto param : *params) {
2814-
// Don't permit variadic parameters.
2815-
if (param->isVariadic())
2816-
continue;
2817-
28182814
auto typeRepr = param->getTypeRepr();
28192815
if (!typeRepr)
28202816
continue;

lib/AST/Type.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,8 +1315,7 @@ ParameterListInfo::ParameterListInfo(
13151315
inheritActorContext.set(i);
13161316
}
13171317

1318-
if (param->isVariadic() &&
1319-
param->getVarargBaseTy()->hasTypeSequence()) {
1318+
if (param->getInterfaceType()->is<PackExpansionType>()) {
13201319
variadicGenerics.set(i);
13211320
}
13221321
}

lib/AST/TypeRepr.cpp

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ CollectedOpaqueReprs TypeRepr::collectOpaqueReturnTypeReprs() {
120120
explicit Walker(CollectedOpaqueReprs &reprs) : Reprs(reprs) {}
121121

122122
bool walkToTypeReprPre(TypeRepr *repr) override {
123+
// Don't allow variadic opaque parameter or return types.
124+
if (isa<PackExpansionTypeRepr>(repr))
125+
return false;
126+
123127
if (auto opaqueRepr = dyn_cast<OpaqueReturnTypeRepr>(repr))
124128
Reprs.push_back(opaqueRepr);
125129
return true;
@@ -354,41 +358,26 @@ void ImplicitlyUnwrappedOptionalTypeRepr::printImpl(ASTPrinter &Printer,
354358
}
355359

356360
TupleTypeRepr::TupleTypeRepr(ArrayRef<TupleTypeReprElement> Elements,
357-
SourceRange Parens,
358-
SourceLoc Ellipsis, unsigned EllipsisIdx)
361+
SourceRange Parens)
359362
: TypeRepr(TypeReprKind::Tuple), Parens(Parens) {
360-
Bits.TupleTypeRepr.HasEllipsis = Ellipsis.isValid();
361363
Bits.TupleTypeRepr.NumElements = Elements.size();
362364

363-
// Copy elements.
364365
std::uninitialized_copy(Elements.begin(), Elements.end(),
365366
getTrailingObjects<TupleTypeReprElement>());
366-
367-
// Set ellipsis location and index.
368-
if (Ellipsis.isValid()) {
369-
getTrailingObjects<SourceLocAndIdx>()[0] = {EllipsisIdx, Ellipsis};
370-
}
371367
}
372368

373369
TupleTypeRepr *TupleTypeRepr::create(const ASTContext &C,
374370
ArrayRef<TupleTypeReprElement> Elements,
375-
SourceRange Parens,
376-
SourceLoc Ellipsis, unsigned EllipsisIdx) {
377-
assert(Ellipsis.isValid() ? EllipsisIdx < Elements.size()
378-
: EllipsisIdx == Elements.size());
379-
371+
SourceRange Parens) {
380372
size_t size =
381-
totalSizeToAlloc<TupleTypeReprElement, SourceLocAndIdx>(
382-
Elements.size(), Ellipsis.isValid() ? 1 : 0);
373+
totalSizeToAlloc<TupleTypeReprElement>(Elements.size());
383374
void *mem = C.Allocate(size, alignof(TupleTypeRepr));
384-
return new (mem) TupleTypeRepr(Elements, Parens,
385-
Ellipsis, EllipsisIdx);
375+
return new (mem) TupleTypeRepr(Elements, Parens);
386376
}
387377

388378
TupleTypeRepr *TupleTypeRepr::createEmpty(const ASTContext &C,
389379
SourceRange Parens) {
390-
return create(C, {}, Parens,
391-
/*Ellipsis=*/SourceLoc(), /*EllipsisIdx=*/0);
380+
return create(C, {}, Parens);
392381
}
393382

394383
GenericIdentTypeRepr *GenericIdentTypeRepr::create(const ASTContext &C,
@@ -473,9 +462,6 @@ void TupleTypeRepr::printImpl(ASTPrinter &Printer,
473462
}
474463
printTypeRepr(getElementType(i), Printer, Opts);
475464
Printer.printStructurePost(PrintStructureKind::TupleElement);
476-
477-
if (hasEllipsis() && getEllipsisIndex() == i)
478-
Printer << "...";
479465
}
480466

481467
Printer << ")";

lib/Parse/ParseDecl.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6255,7 +6255,6 @@ static AccessorDecl *createAccessorFunc(SourceLoc DeclLoc,
62556255
storageParam->getNameLoc(),
62566256
storageParam->getName(),
62576257
P->CurDeclContext);
6258-
accessorParam->setVariadic(storageParam->isVariadic());
62596258
accessorParam->setAutoClosure(storageParam->isAutoClosure());
62606259

62616260
// The cloned parameter is implicit.

0 commit comments

Comments
 (0)