Skip to content

Commit c7921c6

Browse files
Merge pull request rust-lang#20805 from A4-Tacks/improve-static-const-parse-error
Improve parsing error for `static` and `const`
2 parents ad1b9cb + fae6cdf commit c7921c6

File tree

8 files changed

+72
-42
lines changed

8 files changed

+72
-42
lines changed

src/tools/rust-analyzer/crates/parser/src/grammar/items/consts.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,28 @@ fn const_or_static(p: &mut Parser<'_>, m: Marker, is_const: bool) {
2525
}
2626

2727
// FIXME: Recover on statics with generic params/where clause.
28-
if is_const {
29-
// test generic_const
30-
// const C<i32>: u32 = 0;
31-
// impl Foo {
32-
// const C<'a>: &'a () = &();
33-
// }
34-
generic_params::opt_generic_param_list(p);
28+
if !is_const && p.at(T![<]) {
29+
// test_err generic_static
30+
// static C<i32>: u32 = 0;
31+
p.error("`static` may not have generic parameters");
3532
}
36-
// test_err generic_static
37-
// static C<i32>: u32 = 0;
33+
// test generic_const
34+
// const C<i32>: u32 = 0;
35+
// impl Foo {
36+
// const C<'a>: &'a () = &();
37+
// }
38+
generic_params::opt_generic_param_list(p);
3839

3940
if p.at(T![:]) {
4041
types::ascription(p);
42+
} else if is_const {
43+
// test_err missing_const_type
44+
// const C = 0;
45+
p.error("missing type for `const`");
4146
} else {
42-
p.error("missing type for `const` or `static`");
47+
// test_err missing_static_type
48+
// static C = 0;
49+
p.error("missing type for `static`");
4350
}
4451
if p.eat(T![=]) {
4552
expressions::expr(p);

src/tools/rust-analyzer/crates/parser/test_data/generated/runner.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,10 +824,18 @@ mod err {
824824
run_and_expect_errors("test_data/parser/inline/err/misplaced_label_err.rs");
825825
}
826826
#[test]
827+
fn missing_const_type() {
828+
run_and_expect_errors("test_data/parser/inline/err/missing_const_type.rs");
829+
}
830+
#[test]
827831
fn missing_fn_param_type() {
828832
run_and_expect_errors("test_data/parser/inline/err/missing_fn_param_type.rs");
829833
}
830834
#[test]
835+
fn missing_static_type() {
836+
run_and_expect_errors("test_data/parser/inline/err/missing_static_type.rs");
837+
}
838+
#[test]
831839
fn path_item_without_excl() {
832840
run_and_expect_errors("test_data/parser/inline/err/path_item_without_excl.rs");
833841
}

src/tools/rust-analyzer/crates/parser/test_data/parser/err/0044_item_modifiers.rast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ SOURCE_FILE
4242
WHITESPACE "\n"
4343
error 6: expected fn, trait or impl
4444
error 38: expected a name
45-
error 40: missing type for `const` or `static`
45+
error 40: missing type for `const`
4646
error 40: expected SEMICOLON
4747
error 44: expected an item
4848
error 44: expected an item

src/tools/rust-analyzer/crates/parser/test_data/parser/inline/err/generic_static.rast

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,24 @@ SOURCE_FILE
44
WHITESPACE " "
55
NAME
66
IDENT "C"
7-
ERROR
8-
L_ANGLE "<"
9-
ERROR
10-
PATH
11-
PATH_SEGMENT
12-
NAME_REF
7+
GENERIC_PARAM_LIST
8+
L_ANGLE "<"
9+
TYPE_PARAM
10+
NAME
1311
IDENT "i32"
14-
ERROR
15-
R_ANGLE ">"
16-
ERROR
12+
R_ANGLE ">"
1713
COLON ":"
18-
WHITESPACE " "
19-
ERROR
20-
PATH
21-
PATH_SEGMENT
22-
NAME_REF
23-
IDENT "u32"
24-
WHITESPACE " "
25-
ERROR
14+
WHITESPACE " "
15+
PATH_TYPE
16+
PATH
17+
PATH_SEGMENT
18+
NAME_REF
19+
IDENT "u32"
20+
WHITESPACE " "
2621
EQ "="
27-
WHITESPACE " "
28-
ERROR
29-
INT_NUMBER "0"
30-
ERROR
22+
WHITESPACE " "
23+
LITERAL
24+
INT_NUMBER "0"
3125
SEMICOLON ";"
3226
WHITESPACE "\n"
33-
error 8: missing type for `const` or `static`
34-
error 8: expected SEMICOLON
35-
error 8: expected an item
36-
error 12: expected an item
37-
error 12: expected an item
38-
error 13: expected an item
39-
error 18: expected an item
40-
error 19: expected an item
41-
error 21: expected an item
42-
error 22: expected an item
27+
error 8: `static` may not have generic parameters
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
SOURCE_FILE
2+
CONST
3+
CONST_KW "const"
4+
WHITESPACE " "
5+
NAME
6+
IDENT "C"
7+
WHITESPACE " "
8+
EQ "="
9+
WHITESPACE " "
10+
LITERAL
11+
INT_NUMBER "0"
12+
SEMICOLON ";"
13+
WHITESPACE "\n"
14+
error 7: missing type for `const`
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const C = 0;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
SOURCE_FILE
2+
STATIC
3+
STATIC_KW "static"
4+
WHITESPACE " "
5+
NAME
6+
IDENT "C"
7+
WHITESPACE " "
8+
EQ "="
9+
WHITESPACE " "
10+
LITERAL
11+
INT_NUMBER "0"
12+
SEMICOLON ";"
13+
WHITESPACE "\n"
14+
error 8: missing type for `static`
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
static C = 0;

0 commit comments

Comments
 (0)