Skip to content

[Parse] Treat '@unknown' as the start of a statement #22974

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
Mar 1, 2019
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
12 changes: 12 additions & 0 deletions lib/Parse/ParseStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ bool Parser::isStartOfStmt() {
// putting a label on something inappropriate in parseStmt().
return isStartOfStmt();
}

case tok::at_sign: {
// Might be a statement or case attribute. The only one of these we have
// right now is `@unknown default`, so hardcode a check for an attribute
// without any parens.
if (!peekToken().is(tok::identifier))
return false;
Parser::BacktrackingScope backtrack(*this);
consumeToken(tok::at_sign);
consumeToken(tok::identifier);
return isStartOfStmt();
}
}
}

Expand Down
35 changes: 35 additions & 0 deletions test/Parse/switch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -609,3 +609,38 @@ switch x {
@unknown default: // expected-error {{additional 'case' blocks cannot appear after the 'default' block of a 'switch'}}
break
}

func testReturnBeforeUnknownDefault() {
switch x { // expected-error {{switch must be exhaustive}}
case 1:
return
@unknown default: // expected-note {{remove '@unknown' to handle remaining values}}
break
}
}

func testReturnBeforeIncompleteUnknownDefault() {
switch x { // expected-error {{switch must be exhaustive}}
case 1:
return
@unknown default // expected-error {{expected ':' after 'default'}}
// expected-note@-1 {{remove '@unknown' to handle remaining values}}
}
}

func testReturnBeforeIncompleteUnknownDefault2() {
switch x { // expected-error {{switch must be exhaustive}} expected-note {{do you want to add a default clause?}}
case 1:
return
@unknown // expected-error {{unknown attribute 'unknown'}}
} // expected-error {{expected declaration}}
}

func testIncompleteArrayLiteral() {
switch x { // expected-error {{switch must be exhaustive}}
case 1:
_ = [1 // expected-error {{expected ']' in container literal expression}} expected-note {{to match this opening '['}}
@unknown default: // expected-note {{remove '@unknown' to handle remaining values}}
()
}
}