Skip to content

Commit 758791f

Browse files
committed
Improve error recovery for && in stmt-condition when followed by
let/var/case. Thanks to Radek Pietruszewski for noticing this!
1 parent b728b89 commit 758791f

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

lib/Parse/ParseExpr.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,16 @@ ParserResult<Expr> Parser::parseExprSequence(Diag<> Message,
230230
if (!GreaterThanIsOperator && startsWithGreater(Tok))
231231
goto done;
232232

233-
// If this is an "&& #available()" expression, then don't eat it.
234-
// #available() is not a general expression, and && is an infix operator,
233+
// If this is an "&& #available()" expression (or related things that
234+
// show up in a stmt-condition production), then don't eat it.
235+
//
236+
// These are not general expressions, and && is an infix operator,
235237
// so the code is invalid. We get better recovery if we bail out from
236238
// this, because then we can produce a fixit to rewrite the && into a ,
237239
// if we're in a stmt-condition.
238240
if (Tok.getText() == "&&" &&
239-
peekToken().is(tok::pound_available))
241+
peekToken().isAny(tok::pound_available,
242+
tok::kw_let, tok::kw_var, tok::kw_case))
240243
goto done;
241244

242245
// Parse the operator.

test/stmt/statements.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,9 @@ func f21080671() {
464464
func f(x : Int, y : Int) {
465465
if x == y && #available(iOS 9, *) {} // expected-error {{expected ',' joining parts of a multi-clause condition}} {{13-15=,}}
466466
if #available(iOS 9, *) && x == y {} // expected-error {{expected ',' joining parts of a multi-clause condition}} {{27-29=,}}
467+
468+
// https://twitter.com/radexp/status/694790631881883648
469+
if x == y && let _ = Optional(y) {} // expected-error {{expected ',' joining parts of a multi-clause condition}} {{13-15=,}}
467470
}
468471

469472

0 commit comments

Comments
 (0)