Skip to content

[Parser] Improve diagnostics for generic parameter list in enum cases #69052

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

Closed
Closed
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/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ ERROR(let_cannot_be_addressed_property,none,
ERROR(disallowed_var_multiple_getset,none,
"'var' declarations with multiple variables cannot have explicit"
" getters/setters", ())
ERROR(unexpected_generic_in_enum_case,none,
"enum cases cannot have generic parameters. did you mean to attach it to enum declaration?", ())

ERROR(disallowed_init,none,
"initial value is not allowed here", ())
Expand Down
8 changes: 7 additions & 1 deletion lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8902,7 +8902,7 @@ Parser::parseDeclEnumCase(ParseDeclOptions Flags,

// Parse comma-separated enum elements.
SmallVector<EnumElementDecl*, 4> Elements;

SourceLoc CommaLoc;
for (;;) {
Identifier Name;
Expand Down Expand Up @@ -8970,6 +8970,12 @@ Parser::parseDeclEnumCase(ParseDeclOptions Flags,
}
}

// See if there's an illegal generic parameter list.
if (startsWithLess(Tok)) {
diagnose(Tok, diag::unexpected_generic_in_enum_case);
skipUntilGreaterInTypeList();
}

// See if there's a following argument type.
ParserResult<ParameterList> ArgParams;
SmallVector<Identifier, 4> argumentNames;
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 @@ -636,3 +636,10 @@ if case nil = foo1 {} // Okay
if case .none? = foo1 {} // Okay
if case nil = foo2 {} // Okay
if case .none?? = foo2 {} // Okay

enum UnsupportedGenericEnumCase {
case one<Type>(param: Type) // expected-error {{enum cases cannot have generic parameters. did you mean to attach it to enum declaration?}}
// expected-error@-1 {{cannot find type 'Type' in scope}}
case two<Partial(param: Int) // expected-error {{enum cases cannot have generic parameters. did you mean to attach it to enum declaration?}}
case three<(param: Int) // expected-error {{enum cases cannot have generic parameters. did you mean to attach it to enum declaration?}}
}