From fa80fc3bd59f7c8f32030e392c183dae1a62f9d6 Mon Sep 17 00:00:00 2001 From: Minhyuk Kim Date: Sun, 8 Oct 2023 23:46:56 +0900 Subject: [PATCH] Improve diagnostics for generic parameter list in enum cases --- include/swift/AST/DiagnosticsParse.def | 2 ++ lib/Parse/ParseDecl.cpp | 8 +++++++- test/decl/enum/enumtest.swift | 7 +++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/include/swift/AST/DiagnosticsParse.def b/include/swift/AST/DiagnosticsParse.def index 031eb93099241..5d2d99eaab177 100644 --- a/include/swift/AST/DiagnosticsParse.def +++ b/include/swift/AST/DiagnosticsParse.def @@ -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", ()) diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index ce594a999a4c6..711e8fcd58c71 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -8902,7 +8902,7 @@ Parser::parseDeclEnumCase(ParseDeclOptions Flags, // Parse comma-separated enum elements. SmallVector Elements; - + SourceLoc CommaLoc; for (;;) { Identifier Name; @@ -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 ArgParams; SmallVector argumentNames; diff --git a/test/decl/enum/enumtest.swift b/test/decl/enum/enumtest.swift index 9cd20c410568a..b31a4ecdcd667 100644 --- a/test/decl/enum/enumtest.swift +++ b/test/decl/enum/enumtest.swift @@ -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(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