Skip to content

Commit 395ac06

Browse files
committed
Resolve an ambiguity with the multiple-trailing-closure syntax in favor of parsing default: labels.
1 parent 7eae1fe commit 395ac06

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

lib/Parse/ParseExpr.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3149,8 +3149,12 @@ ParserStatus Parser::parseExprList(tok leftTok, tok rightTok,
31493149
}
31503150

31513151
static bool isStartOfLabelledTrailingClosure(Parser &P) {
3152-
// Fast path: the next two tokens are a label and a colon.
3153-
if (!P.Tok.canBeArgumentLabel() || !P.peekToken().is(tok::colon))
3152+
// Fast path: the next two tokens must be a label and a colon.
3153+
// But 'default:' is ambiguous with switch cases and we disallow it
3154+
// (unless escaped) even outside of switches.
3155+
if (!P.Tok.canBeArgumentLabel() ||
3156+
P.Tok.is(tok::kw_default) ||
3157+
!P.peekToken().is(tok::colon))
31543158
return false;
31553159

31563160
// Do some tentative parsing to distinguish `label: { ... }` and

test/Parse/multiple_trailing_closures.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,10 @@ func test_multiple_trailing_syntax_without_labels() {
116116
_: {}
117117
_: {}
118118
}
119+
120+
func produce(fn: () -> Int?, default d: () -> Int) -> Int { // expected-note {{declared here}}
121+
return fn() ?? d()
122+
}
123+
// TODO: The diagnostics here are perhaps a little overboard.
124+
_ = produce { 0 } default: { 1 } // expected-error {{missing argument for parameter 'fn' in call}} expected-error {{consecutive statements}} expected-error {{'default' label can only appear inside a 'switch' statement}} expected-error {{top-level statement cannot begin with a closure expression}} expected-error {{closure expression is unused}} expected-note {{did you mean to use a 'do' statement?}}
125+
_ = produce { 2 } `default`: { 3 }

0 commit comments

Comments
 (0)