Skip to content

Commit a54865f

Browse files
committed
Sema: Diagnose variadic enums, and inheritance from variadic classes
These are just temporary limitations.
1 parent f25e2d2 commit a54865f

File tree

4 files changed

+54
-7
lines changed

4 files changed

+54
-7
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5398,9 +5398,12 @@ ERROR(vararg_not_allowed,none,
53985398
ERROR(experimental_type_with_parameter_pack,none,
53995399
"generic types with parameter packs are experimental",
54005400
())
5401-
ERROR(more_than_one_parameter_pack_in_type,none,
5402-
"generic type cannot have more than one pack", ())
5403-
5401+
ERROR(more_than_one_pack_in_type,none,
5402+
"generic type cannot declare more than one type pack", ())
5403+
ERROR(enum_with_pack,none,
5404+
"enums cannot declare a type pack", ())
5405+
ERROR(superclass_with_pack,none,
5406+
"cannot inherit from a generic class that declares a type pack", ())
54045407
ERROR(expansion_not_allowed,none,
54055408
"pack expansion %0 can only appear in a function parameter list, "
54065409
"tuple element, or generic argument list", (Type))

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ static void checkGenericParams(GenericContext *ownerCtx) {
485485
}
486486

487487
if (hasPack) {
488-
gp->diagnose(diag::more_than_one_parameter_pack_in_type);
488+
gp->diagnose(diag::more_than_one_pack_in_type);
489489
}
490490

491491
hasPack = true;
@@ -2555,6 +2555,17 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
25552555
void visitEnumDecl(EnumDecl *ED) {
25562556
checkUnsupportedNestedType(ED);
25572557

2558+
// Temporary restriction until we figure out pattern matching and
2559+
// enum case construction with packs.
2560+
if (auto genericSig = ED->getGenericSignature()) {
2561+
for (auto paramTy : genericSig.getGenericParams()) {
2562+
if (paramTy->isParameterPack()) {
2563+
ED->diagnose(diag::enum_with_pack);
2564+
break;
2565+
}
2566+
}
2567+
}
2568+
25582569
// FIXME: Remove this once we clean up the mess involving raw values.
25592570
(void) ED->getInterfaceType();
25602571

@@ -2834,6 +2845,18 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
28342845
else if (superclass->isActor())
28352846
CD->diagnose(diag::actor_inheritance,
28362847
/*distributed=*/CD->isDistributedActor());
2848+
2849+
// Enforce a temporary restriction on inheriting from a superclass
2850+
// type with a pack, until we figure out the semantics of method
2851+
// overrides in these situations.
2852+
if (auto genericSig = superclass->getGenericSignature()) {
2853+
for (auto paramTy : genericSig.getGenericParams()) {
2854+
if (paramTy->isParameterPack()) {
2855+
CD->diagnose(diag::superclass_with_pack);
2856+
break;
2857+
}
2858+
}
2859+
}
28372860
}
28382861

28392862
if (CD->isDistributedActor()) {

test/Generics/variadic_generic_types.swift

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
// RUN: %target-typecheck-verify-swift -enable-experimental-feature VariadicGenerics
22

3+
// Because of -enable-experimental-feature VariadicGenerics
34
// REQUIRES: asserts
45

5-
struct MultiplePack<each T, each U> {} // expected-error {{generic type cannot have more than one pack}}
6-
typealias MultiplePackAlias<each T, each U> = (repeat each T, repeat each U) // expected-error {{generic type cannot have more than one pack}}
6+
// Disallowed cases
7+
struct MultiplePack<each T, each U> {} // expected-error {{generic type cannot declare more than one type pack}}
8+
typealias MultiplePackAlias<each T, each U> = (repeat each T, repeat each U) // expected-error {{generic type cannot declare more than one type pack}}
79

10+
// Temporary limitations
11+
enum EnumWithPack<each T> { // expected-error {{enums cannot declare a type pack}}
12+
case cheddar
13+
}
14+
15+
class ClassWithPack<each T> {}
16+
17+
struct OuterStruct<each T> {
18+
enum NestedEnum { // expected-error {{enums cannot declare a type pack}}
19+
case smokedGouda
20+
}
21+
22+
class NestedClass {}
23+
}
24+
25+
class BadInheritance1: ClassWithPack<Int> {} // expected-error {{cannot inherit from a generic class that declares a type pack}}
26+
class BadInheritance2: OuterStruct<Int>.NestedClass {} // expected-error {{cannot inherit from a generic class that declares a type pack}}
27+
28+
// Type resolution of variadic type aliases
829
func bindAll() {
930
struct Bind<each U> {}
1031

test/type/pack_expansion.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func f5<each T>() -> () -> (repeat each T) {}
3030

3131
func f6<each T>() -> (repeat each T) -> () {}
3232

33-
enum E<each T> {
33+
enum E<each T> { // expected-error {{enums cannot declare a type pack}}
3434
case f1(_: repeat each T)
3535

3636
case f2(_: G<repeat each T>)

0 commit comments

Comments
 (0)