Skip to content

Commit 924f15e

Browse files
committed
[Parse] Simplify missing_func_keyword diagnostic
1 parent f25e50d commit 924f15e

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

lib/Parse/ParseDecl.cpp

+14-4
Original file line numberDiff line numberDiff line change
@@ -3002,15 +3002,25 @@ Parser::parseDecl(ParseDeclOptions Flags,
30023002
// Must not confuse it with trailing closure syntax, so we only
30033003
// recover in contexts where there can be no statements.
30043004

3005-
if ((Tok.isIdentifierOrUnderscore() &&
3006-
peekToken().isAny(tok::colon, tok::equal, tok::comma)) ||
3007-
Tok.is(tok::l_paren)) {
3005+
const bool IsProbablyVarDecl =
3006+
Tok.isIdentifierOrUnderscore() &&
3007+
peekToken().isAny(tok::colon, tok::equal, tok::comma);
3008+
3009+
const bool IsProbablyTupleDecl =
3010+
Tok.is(tok::l_paren) && peekToken().isIdentifierOrUnderscore();
3011+
3012+
if (IsProbablyVarDecl || IsProbablyTupleDecl) {
30083013
diagnose(Tok.getLoc(), diag::expected_keyword_in_decl, "var",
30093014
"property")
30103015
.fixItInsert(Tok.getLoc(), "var ");
30113016
parseLetOrVar(/*HasLetOrVarKeyword=*/false);
30123017
break;
3013-
} else if (Tok.isIdentifierOrUnderscore() || Tok.isAnyOperator()) {
3018+
}
3019+
3020+
const bool IsProbablyFuncDecl =
3021+
Tok.isIdentifierOrUnderscore() || Tok.isAnyOperator();
3022+
3023+
if (IsProbablyFuncDecl) {
30143024
diagnose(Tok.getLoc(), diag::expected_keyword_in_decl, "func",
30153025
"function")
30163026
.fixItInsert(Tok.getLoc(), "func ");

test/Parse/diagnostic_missing_func_keyword.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct Bar {
3636
_: Int = 42 // expected-error {{expected 'var' keyword in property declaration}} {{3-3=var }}
3737
// expected-error @-1 {{property declaration does not bind any variables}}
3838

39-
((x, y), z) = ((1, 2), 3) // expected-error {{expected 'var' keyword in property declaration}}
40-
39+
(light, dark) = (100, 200)// expected-error {{expected 'var' keyword in property declaration}} {{3-3=var }}
40+
4141
a, b: Int // expected-error {{expected 'var' keyword in property declaration}} {{3-3=var }}
4242
}

0 commit comments

Comments
 (0)