Skip to content

Commit 048f5a4

Browse files
committed
[Parse] Treat '@unknown' as the start of a statement
...allowing a plain 'return' to work in a switch in a Void-returning function. https://bugs.swift.org/browse/SR-9920
1 parent fa148d2 commit 048f5a4

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

lib/Parse/ParseStmt.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ bool Parser::isStartOfStmt() {
9292
// putting a label on something inappropriate in parseStmt().
9393
return isStartOfStmt();
9494
}
95+
96+
case tok::at_sign: {
97+
// Might be a statement or case attribute. The only one of these we have
98+
// right now is `@unknown default`, so hardcode a check for an attribute
99+
// without any parens.
100+
if (!peekToken().is(tok::identifier))
101+
return false;
102+
Parser::BacktrackingScope backtrack(*this);
103+
consumeToken(tok::at_sign);
104+
consumeToken(tok::identifier);
105+
return isStartOfStmt();
106+
}
95107
}
96108
}
97109

test/Parse/switch.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,3 +609,38 @@ switch x {
609609
@unknown default: // expected-error {{additional 'case' blocks cannot appear after the 'default' block of a 'switch'}}
610610
break
611611
}
612+
613+
func testReturnBeforeUnknownDefault() {
614+
switch x { // expected-error {{switch must be exhaustive}}
615+
case 1:
616+
return
617+
@unknown default: // expected-note {{remove '@unknown' to handle remaining values}}
618+
break
619+
}
620+
}
621+
622+
func testReturnBeforeIncompleteUnknownDefault() {
623+
switch x { // expected-error {{switch must be exhaustive}}
624+
case 1:
625+
return
626+
@unknown default // expected-error {{expected ':' after 'default'}}
627+
// expected-note@-1 {{remove '@unknown' to handle remaining values}}
628+
}
629+
}
630+
631+
func testReturnBeforeIncompleteUnknownDefault2() {
632+
switch x { // expected-error {{switch must be exhaustive}} expected-note {{do you want to add a default clause?}}
633+
case 1:
634+
return
635+
@unknown // expected-error {{unknown attribute 'unknown'}}
636+
} // expected-error {{expected declaration}}
637+
}
638+
639+
func testIncompleteArrayLiteral() {
640+
switch x { // expected-error {{switch must be exhaustive}}
641+
case 1:
642+
_ = [1 // expected-error {{expected ']' in container literal expression}} expected-note {{to match this opening '['}}
643+
@unknown default: // expected-note {{remove '@unknown' to handle remaining values}}
644+
()
645+
}
646+
}

0 commit comments

Comments
 (0)