Skip to content

Commit d02c140

Browse files
authored
Merge pull request swiftlang#30899 from nathawes/improve-enum-case-trailing-comma-recovery
2 parents 14fbb82 + 8edeab7 commit d02c140

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

lib/Parse/ParseDecl.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6609,8 +6609,7 @@ Parser::parseDeclEnumCase(ParseDeclOptions Flags,
66096609
// Handle the likely case someone typed 'case X, case Y'.
66106610
if (Tok.is(tok::kw_case) && CommaLoc.isValid()) {
66116611
diagnose(Tok, diag::expected_identifier_after_case_comma);
6612-
Status.setIsParseError();
6613-
return Status;
6612+
break;
66146613
}
66156614

66166615
if (Tok.is(tok::identifier)) {
@@ -6655,8 +6654,7 @@ Parser::parseDeclEnumCase(ParseDeclOptions Flags,
66556654
}
66566655
} else if (CommaLoc.isValid()) {
66576656
diagnose(Tok, diag::expected_identifier_after_case_comma);
6658-
Status.setIsParseError();
6659-
return Status;
6657+
break;
66606658
} else {
66616659
diagnose(CaseLoc, diag::expected_identifier_in_decl, "enum 'case'");
66626660
}

test/IDE/coloring.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,13 @@ func keywordInCaseAndLocalArgLabel(_ for: Int, for in: Int, class _: Int) {
454454
}
455455
}
456456

457+
enum CasesWithMissingElement {
458+
case a(Int, String),
459+
// CHECK: <kw>case</kw> a(<type>Int</type>, <type>String</type>)
460+
case b(Int, String),
461+
// CHECK: <kw>case</kw> b(<type>Int</type>, <type>String</type>)
462+
}
463+
457464
// CHECK: <kw>class</kw> Ownership {
458465
class Ownership {
459466
// CHECK: <attr-builtin>weak</attr-builtin> <kw>var</kw> w

test/Parse/enum.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,3 +591,13 @@ enum SR11261_Newline2 {
591591
enum SR11261_PatternMatching {
592592
case let .foo(x, y): // expected-error {{'case' label can only appear inside a 'switch' statement}}
593593
}
594+
595+
enum CasesWithMissingElement: Int {
596+
// expected-error@-1 {{'CasesWithMissingElement' declares raw type 'Int', but does not conform to RawRepresentable and conformance could not be synthesized}}
597+
598+
case a = "hello", // expected-error{{expected identifier after comma in enum 'case' declaration}}
599+
// expected-error@-1 {{cannot convert value of type 'String' to raw type 'Int'}}
600+
601+
case b = "hello", // expected-error{{expected identifier after comma in enum 'case' declaration}}
602+
// expected-error@-1 {{cannot convert value of type 'String' to raw type 'Int'}}
603+
}

test/stmt/statements.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,6 @@ outerLoop1: repeat { // expected-note {{did you mean 'outerLoop1'?}} {{14-23=out
717717

718718
// Errors in case syntax
719719
class
720-
case, // expected-error {{expected identifier in enum 'case' declaration}} expected-error {{expected identifier after comma in enum 'case' declaration}}
720+
case, // expected-error {{expected identifier in enum 'case' declaration}} expected-error {{expected identifier after comma in enum 'case' declaration}} expected-error {{enum 'case' is not allowed outside of an enum}}
721721
case // expected-error {{expected identifier in enum 'case' declaration}} expected-error {{enum 'case' is not allowed outside of an enum}}
722722
// NOTE: EOF is important here to properly test a code path that used to crash the parser

0 commit comments

Comments
 (0)