Skip to content

Commit 2f2962d

Browse files
authored
Merge pull request #21419 from rjmccall/ban-variadic-cases
Ban variadic enum cases
2 parents 0aba29c + 6e5ce64 commit 2f2962d

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
@@ -3442,6 +3442,8 @@ ERROR(tuple_single_element,none,
34423442
"cannot create a single-element tuple with an element label", ())
34433443
ERROR(tuple_ellipsis,none,
34443444
"cannot create a variadic tuple", ())
3445+
ERROR(enum_element_ellipsis,none,
3446+
"variadic enum cases are not supported", ())
34453447

34463448
WARNING(implicitly_unwrapped_optional_in_illegal_position_interpreted_as_optional,none,
34473449
"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)