Skip to content

Commit f170d1d

Browse files
committed
Ban variadic enum cases.
These should be supported in the long term, but in the short term, crashing is not accepable behavior. rdar://46821582
1 parent fdd904b commit f170d1d

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3498,6 +3498,8 @@ ERROR(tuple_single_element,none,
34983498
"cannot create a single-element tuple with an element label", ())
34993499
ERROR(tuple_ellipsis,none,
35003500
"cannot create a variadic tuple", ())
3501+
ERROR(enum_element_ellipsis,none,
3502+
"variadic enum cases are not supported", ())
35013503

35023504
WARNING(implicitly_unwrapped_optional_in_illegal_position_interpreted_as_optional,none,
35033505
"using '!' is not allowed here; treating this as '?' instead", ())

lib/Sema/TypeCheckPattern.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ static bool validateParameterType(ParamDecl *decl, TypeResolution resolution,
723723
if (auto ty = decl->getTypeLoc().getType())
724724
return ty->hasError();
725725

726+
auto origContext = options.getContext();
726727
options.setContext(None);
727728

728729
// If the element is a variadic parameter, resolve the parameter type as if
@@ -764,6 +765,12 @@ static bool validateParameterType(ParamDecl *decl, TypeResolution resolution,
764765
hadError = true;
765766
}
766767
TL.setType(Ty);
768+
769+
// Disallow variadic parameters in enum elements.
770+
if (!hadError && origContext == TypeResolverContext::EnumElementDecl) {
771+
TC.diagnose(decl->getStartLoc(), diag::enum_element_ellipsis);
772+
hadError = true;
773+
}
767774
}
768775

769776
if (hadError)

test/decl/enum/enumtest.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,10 @@ enum Lens<T> {
326326
case baz((inout T) -> ()) // ok
327327
case quux((inout T, inout T) -> ()) // ok
328328
}
329+
330+
// In the long term, these should be legal, but we don't support them right
331+
// now and we shouldn't pretend to.
332+
// rdar://46684504
333+
enum HasVariadic {
334+
case variadic(x: Int...) // expected-error {{variadic enum cases are not supported}}
335+
}

0 commit comments

Comments
 (0)