Skip to content

Commit 7b9e26e

Browse files
committed
Improve parsing error for static and const
Example --- ```rust static C<i32>: u32 = 0; ``` -> ```diff -error 8: missing type for `const` or `static` +error 8: `static` may not have generic parameters ``` --- ```rust const C = 0; ``` -> ```diff -error 7: missing type for `const` or `static` +error 7: missing type for `const` ``` --- ```rust static C = 0; ``` -> ```diff -error 8: missing type for `const` or `static` +error 8: missing type for `static` ```
1 parent d1ab4e6 commit 7b9e26e

File tree

8 files changed

+50
-4
lines changed

8 files changed

+50
-4
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,22 @@ fn const_or_static(p: &mut Parser<'_>, m: Marker, is_const: bool) {
3232
// const C<'a>: &'a () = &();
3333
// }
3434
generic_params::opt_generic_param_list(p);
35+
} else if p.at(T![<]) {
36+
p.error("`static` may not have generic parameters");
3537
}
3638
// test_err generic_static
3739
// static C<i32>: u32 = 0;
3840

3941
if p.at(T![:]) {
4042
types::ascription(p);
41-
} else {
42-
p.error("missing type for `const` or `static`");
43+
} else if is_const {
44+
// test_err missing_const_type
45+
// const C = 0;
46+
p.error("missing type for `const`");
47+
} else if !p.at(T![<]) {
48+
// test_err missing_static_type
49+
// static C = 0;
50+
p.error("missing type for `static`");
4351
}
4452
if p.eat(T![=]) {
4553
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ SOURCE_FILE
3030
ERROR
3131
SEMICOLON ";"
3232
WHITESPACE "\n"
33-
error 8: missing type for `const` or `static`
33+
error 8: `static` may not have generic parameters
3434
error 8: expected SEMICOLON
3535
error 8: expected an item
3636
error 12: expected an item
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)