Skip to content

Commit 2b3ebf7

Browse files
committed
Use DescriptiveDeclKind for better expected_keyword_in_decl diagnostic
1 parent 924f15e commit 2b3ebf7

File tree

7 files changed

+58
-13
lines changed

7 files changed

+58
-13
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ ERROR(expected_decl,none,
217217
ERROR(expected_identifier_in_decl,none,
218218
"expected identifier in %0 declaration", (StringRef))
219219
ERROR(expected_keyword_in_decl,none,
220-
"expected '%0' keyword in %1 declaration", (StringRef, StringRef))
220+
"expected '%0' keyword in %1 declaration", (StringRef, DescriptiveDeclKind))
221221
ERROR(number_cant_start_decl_name,none,
222222
"%0 name can only start with a letter or underscore, not a number",
223223
(StringRef))

lib/Parse/ParseDecl.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3010,8 +3010,23 @@ Parser::parseDecl(ParseDeclOptions Flags,
30103010
Tok.is(tok::l_paren) && peekToken().isIdentifierOrUnderscore();
30113011

30123012
if (IsProbablyVarDecl || IsProbablyTupleDecl) {
3013+
3014+
DescriptiveDeclKind DescriptiveKind;
3015+
3016+
switch (StaticSpelling) {
3017+
case StaticSpellingKind::None:
3018+
DescriptiveKind = DescriptiveDeclKind::Property;
3019+
break;
3020+
case StaticSpellingKind::KeywordStatic:
3021+
DescriptiveKind = DescriptiveDeclKind::StaticProperty;
3022+
break;
3023+
case StaticSpellingKind::KeywordClass:
3024+
llvm_unreachable("kw_class is only parsed as a modifier if it's "
3025+
"followed by a keyword");
3026+
}
3027+
30133028
diagnose(Tok.getLoc(), diag::expected_keyword_in_decl, "var",
3014-
"property")
3029+
DescriptiveKind)
30153030
.fixItInsert(Tok.getLoc(), "var ");
30163031
parseLetOrVar(/*HasLetOrVarKeyword=*/false);
30173032
break;
@@ -3021,8 +3036,27 @@ Parser::parseDecl(ParseDeclOptions Flags,
30213036
Tok.isIdentifierOrUnderscore() || Tok.isAnyOperator();
30223037

30233038
if (IsProbablyFuncDecl) {
3039+
3040+
DescriptiveDeclKind DescriptiveKind;
3041+
3042+
if (Tok.isAnyOperator()) {
3043+
DescriptiveKind = DescriptiveDeclKind::OperatorFunction;
3044+
} else {
3045+
switch (StaticSpelling) {
3046+
case StaticSpellingKind::None:
3047+
DescriptiveKind = DescriptiveDeclKind::Method;
3048+
break;
3049+
case StaticSpellingKind::KeywordStatic:
3050+
DescriptiveKind = DescriptiveDeclKind::StaticMethod;
3051+
break;
3052+
case StaticSpellingKind::KeywordClass:
3053+
llvm_unreachable("kw_class is only parsed as a modifier if it's "
3054+
"followed by a keyword");
3055+
}
3056+
}
3057+
30243058
diagnose(Tok.getLoc(), diag::expected_keyword_in_decl, "func",
3025-
"function")
3059+
DescriptiveKind)
30263060
.fixItInsert(Tok.getLoc(), "func ");
30273061
parseFunc(/*HasFuncKeyword=*/false);
30283062
break;

test/Parse/ConditionalCompilation/stmt_in_type.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ struct S1 { // expected-note {{in declaration of 'S1'}}
88
#if FOO
99
return 1; // expected-error {{expected declaration}}
1010
#elseif BAR
11-
foo(); // expected-error {{expected 'func' keyword in function declaration}}
11+
foo(); // expected-error {{expected 'func' keyword in instance method declaration}}
1212
#endif
1313
}

test/Parse/diagnostic_missing_func_keyword.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
// https://bugs.swift.org/browse/SR-10477
44

55
protocol Brew { // expected-note {{in declaration of 'Brew'}}
6-
tripel() -> Int // expected-error {{expected 'func' keyword in function declaration}} {{3-3=func }}
6+
tripel() -> Int // expected-error {{expected 'func' keyword in instance method declaration}} {{3-3=func }}
77

88
quadrupel: Int { get } // expected-error {{expected 'var' keyword in property declaration}} {{3-3=var }}
99

10-
static + (lhs: Self, rhs: Self) -> Self // expected-error {{expected 'func' keyword in function declaration}} {{10-10=func }}
10+
static + (lhs: Self, rhs: Self) -> Self // expected-error {{expected 'func' keyword in operator function declaration}} {{10-10=func }}
1111

12-
* (lhs: Self, rhs: Self) -> Self // expected-error {{expected 'func' keyword in function declaration}} {{3-3=func }}
12+
* (lhs: Self, rhs: Self) -> Self // expected-error {{expected 'func' keyword in operator function declaration}} {{3-3=func }}
1313
// expected-error @-1 {{operator '*' declared in protocol must be 'static'}} {{3-3=static }}
1414

1515
ipa: Int { get }; apa: Float { get }
@@ -27,7 +27,7 @@ infix operator %%
2727
struct Bar {
2828
fisr = 0x5F3759DF // expected-error {{expected 'var' keyword in property declaration}} {{3-3=var }}
2929

30-
%%<T: Brew> (lhs: T, rhs: T) -> T { // expected-error {{expected 'func' keyword in function declaration}} {{3-3=func }}
30+
%%<T: Brew> (lhs: T, rhs: T) -> T { // expected-error {{expected 'func' keyword in operator function declaration}} {{3-3=func }}
3131
// expected-error @-1 {{operator '%%' declared in type 'Bar' must be 'static'}}
3232
// expected-error @-2 {{member operator '%%' must have at least one argument of type 'Bar'}}
3333
lhs + lhs + rhs + rhs
@@ -40,3 +40,14 @@ struct Bar {
4040

4141
a, b: Int // expected-error {{expected 'var' keyword in property declaration}} {{3-3=var }}
4242
}
43+
44+
class Baz {
45+
46+
instanceMethod() {} // expected-error {{expected 'func' keyword in instance method declaration}} {{3-3=func }}
47+
48+
static staticMethod() {} // expected-error {{expected 'func' keyword in static method declaration}} {{10-10=func }}
49+
50+
instanceProperty: Int { 0 } // expected-error {{expected 'var' keyword in property declaration}} {{3-3=var }}
51+
52+
static staticProperty: Int { 0 } // expected-error {{expected 'var' keyword in static property declaration}} {{10-10=var }}
53+
}

test/Parse/line-directive.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ x x // expected-error{{consecutive statements}} {{2-2=;}}
2424

2525
// rdar://19582475
2626
public struct S { // expected-note{{in declaration of 'S'}}
27-
// expected-error@+8{{expected 'func' keyword in function declaration}}
27+
// expected-error@+8{{expected 'func' keyword in operator function declaration}}
2828
// expected-error@+7{{operator '/' declared in type 'S' must be 'static'}}
2929
// expected-error@+6{{expected '(' in argument list of function declaration}}
3030
// expected-error@+5{{operators must have one or two arguments}}

test/Parse/recovery.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -542,14 +542,14 @@ func use_BracesInsideNominalDecl1() {
542542

543543
class SR771 {
544544
print("No one else was in the room where it happened") // expected-note {{'print()' previously declared here}}
545-
// expected-error @-1 {{expected 'func' keyword in function declaration}}
545+
// expected-error @-1 {{expected 'func' keyword in instance method declaration}}
546546
// expected-error @-2 {{expected '{' in body of function declaration}}
547547
// expected-error @-3 {{expected parameter name followed by ':'}}
548548
}
549549

550550
extension SR771 {
551551
print("The room where it happened, the room where it happened")
552-
// expected-error @-1 {{expected 'func' keyword in function declaration}}
552+
// expected-error @-1 {{expected 'func' keyword in instance method declaration}}
553553
// expected-error @-2 {{invalid redeclaration of 'print()'}}
554554
// expected-error @-3 {{expected parameter name followed by ':'}}
555555
}
@@ -558,7 +558,7 @@ extension SR771 {
558558
//===--- Recovery for wrong decl introducer keyword.
559559

560560
class WrongDeclIntroducerKeyword1 {
561-
notAKeyword() {} // expected-error {{expected 'func' keyword in function declaration}}
561+
notAKeyword() {} // expected-error {{expected 'func' keyword in instance method declaration}}
562562
func foo() {}
563563
class func bar() {}
564564
}

test/Sema/immutability.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ struct StructWithDelegatingInit {
511511
struct F {
512512
mutating mutating mutating f() { // expected-error 2 {{duplicate modifier}}
513513
// expected-note@-1 2 {{modifier already specified here}}
514-
// expected-error@-2 {{expected 'func' keyword in function declaration}}
514+
// expected-error@-2 {{expected 'func' keyword in instance method declaration}}
515515
}
516516

517517
mutating nonmutating func g() {} // expected-error {{method must not be declared both 'mutating' and 'nonmutating'}}

0 commit comments

Comments
 (0)