Skip to content

Ban variadic enum cases #21419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -3442,6 +3442,8 @@ ERROR(tuple_single_element,none,
"cannot create a single-element tuple with an element label", ())
ERROR(tuple_ellipsis,none,
"cannot create a variadic tuple", ())
ERROR(enum_element_ellipsis,none,
"variadic enum cases are not supported", ())

WARNING(implicitly_unwrapped_optional_in_illegal_position_interpreted_as_optional,none,
"using '!' is not allowed here; treating this as '?' instead", ())
Expand Down
7 changes: 7 additions & 0 deletions lib/Sema/TypeCheckPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,7 @@ static bool validateParameterType(ParamDecl *decl, TypeResolution resolution,
if (auto ty = decl->getTypeLoc().getType())
return ty->hasError();

auto origContext = options.getContext();
options.setContext(None);

// If the element is a variadic parameter, resolve the parameter type as if
Expand Down Expand Up @@ -764,6 +765,12 @@ static bool validateParameterType(ParamDecl *decl, TypeResolution resolution,
hadError = true;
}
TL.setType(Ty);

// Disallow variadic parameters in enum elements.
if (!hadError && origContext == TypeResolverContext::EnumElementDecl) {
TC.diagnose(decl->getStartLoc(), diag::enum_element_ellipsis);
hadError = true;
}
}

if (hadError)
Expand Down
7 changes: 7 additions & 0 deletions test/decl/enum/enumtest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,10 @@ enum Lens<T> {
case baz((inout T) -> ()) // ok
case quux((inout T, inout T) -> ()) // ok
}

// In the long term, these should be legal, but we don't support them right
// now and we shouldn't pretend to.
// rdar://46684504
enum HasVariadic {
case variadic(x: Int...) // expected-error {{variadic enum cases are not supported}}
}