Skip to content

Commit

Permalink
Merge pull request swiftlang#30899 from nathawes/improve-enum-case-tr…
Browse files Browse the repository at this point in the history
…ailing-comma-recovery
  • Loading branch information
swift-ci authored Apr 11, 2020
2 parents 14fbb82 + 8edeab7 commit d02c140
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
6 changes: 2 additions & 4 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6609,8 +6609,7 @@ Parser::parseDeclEnumCase(ParseDeclOptions Flags,
// Handle the likely case someone typed 'case X, case Y'.
if (Tok.is(tok::kw_case) && CommaLoc.isValid()) {
diagnose(Tok, diag::expected_identifier_after_case_comma);
Status.setIsParseError();
return Status;
break;
}

if (Tok.is(tok::identifier)) {
Expand Down Expand Up @@ -6655,8 +6654,7 @@ Parser::parseDeclEnumCase(ParseDeclOptions Flags,
}
} else if (CommaLoc.isValid()) {
diagnose(Tok, diag::expected_identifier_after_case_comma);
Status.setIsParseError();
return Status;
break;
} else {
diagnose(CaseLoc, diag::expected_identifier_in_decl, "enum 'case'");
}
Expand Down
7 changes: 7 additions & 0 deletions test/IDE/coloring.swift
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,13 @@ func keywordInCaseAndLocalArgLabel(_ for: Int, for in: Int, class _: Int) {
}
}

enum CasesWithMissingElement {
case a(Int, String),
// CHECK: <kw>case</kw> a(<type>Int</type>, <type>String</type>)
case b(Int, String),
// CHECK: <kw>case</kw> b(<type>Int</type>, <type>String</type>)
}

// CHECK: <kw>class</kw> Ownership {
class Ownership {
// CHECK: <attr-builtin>weak</attr-builtin> <kw>var</kw> w
Expand Down
10 changes: 10 additions & 0 deletions test/Parse/enum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -591,3 +591,13 @@ enum SR11261_Newline2 {
enum SR11261_PatternMatching {
case let .foo(x, y): // expected-error {{'case' label can only appear inside a 'switch' statement}}
}

enum CasesWithMissingElement: Int {
// expected-error@-1 {{'CasesWithMissingElement' declares raw type 'Int', but does not conform to RawRepresentable and conformance could not be synthesized}}

case a = "hello", // expected-error{{expected identifier after comma in enum 'case' declaration}}
// expected-error@-1 {{cannot convert value of type 'String' to raw type 'Int'}}

case b = "hello", // expected-error{{expected identifier after comma in enum 'case' declaration}}
// expected-error@-1 {{cannot convert value of type 'String' to raw type 'Int'}}
}
2 changes: 1 addition & 1 deletion test/stmt/statements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,6 @@ outerLoop1: repeat { // expected-note {{did you mean 'outerLoop1'?}} {{14-23=out

// Errors in case syntax
class
case, // expected-error {{expected identifier in enum 'case' declaration}} expected-error {{expected identifier after comma in enum 'case' declaration}}
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}}
case // expected-error {{expected identifier in enum 'case' declaration}} expected-error {{enum 'case' is not allowed outside of an enum}}
// NOTE: EOF is important here to properly test a code path that used to crash the parser

0 comments on commit d02c140

Please sign in to comment.