Revert "Revert "[Diagnostic] Improve diagnostic for trailing closures in statement conditions (#25165)""

This reverts commit e3a6b67c63.
This commit is contained in:
Owen Voorhees
2019-11-19 13:24:20 -08:00
parent cd3ada5abb
commit a3946cdb50
3 changed files with 127 additions and 21 deletions

View File

@@ -965,23 +965,40 @@ static bool isValidTrailingClosure(bool isExprBasic, Parser &P){
// the token after the { is on the same line as the {.
if (P.peekToken().isAtStartOfLine())
return false;
// Determine if the {} goes with the expression by eating it, and looking
// to see if it is immediately followed by '{', 'where', or comma. If so,
// we consider it to be part of the proceeding expression.
// to see if it is immediately followed by a token which indicates we should
// consider it part of the preceding expression
Parser::BacktrackingScope backtrack(P);
P.consumeToken(tok::l_brace);
P.skipUntil(tok::r_brace);
SourceLoc endLoc;
if (!P.consumeIf(tok::r_brace, endLoc) ||
P.Tok.isNot(tok::l_brace, tok::kw_where, tok::comma)) {
if (!P.consumeIf(tok::r_brace, endLoc))
return false;
switch (P.Tok.getKind()) {
case tok::l_brace:
case tok::kw_where:
case tok::comma:
return true;
case tok::l_square:
case tok::l_paren:
case tok::period:
case tok::period_prefix:
case tok::kw_is:
case tok::kw_as:
case tok::question_postfix:
case tok::question_infix:
case tok::exclaim_postfix:
case tok::colon:
case tok::equal:
case tok::oper_postfix:
case tok::oper_binary_spaced:
case tok::oper_binary_unspaced:
return !P.Tok.isAtStartOfLine();
default:
return false;
}
// Recoverable case. Just return true here and Sema will emit a diagnostic
// later. see: Sema/MiscDiagnostics.cpp#checkStmtConditionTrailingClosure
return true;
}