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`
```
This commit is contained in:
A4-Tacks 2025-10-06 12:48:38 +08:00
parent a2e6043bfa
commit 216db6d5b4
No known key found for this signature in database
GPG key ID: DBD861323040663B
8 changed files with 50 additions and 4 deletions

View file

@ -32,14 +32,22 @@ fn const_or_static(p: &mut Parser<'_>, m: Marker, is_const: bool) {
// const C<'a>: &'a () = &();
// }
generic_params::opt_generic_param_list(p);
} else if p.at(T![<]) {
p.error("`static` may not have generic parameters");
}
// test_err generic_static
// static C<i32>: u32 = 0;
if p.at(T![:]) {
types::ascription(p);
} else {
p.error("missing type for `const` or `static`");
} else if is_const {
// test_err missing_const_type
// const C = 0;
p.error("missing type for `const`");
} else if !p.at(T![<]) {
// test_err missing_static_type
// static C = 0;
p.error("missing type for `static`");
}
if p.eat(T![=]) {
expressions::expr(p);

View file

@ -824,10 +824,18 @@ mod err {
run_and_expect_errors("test_data/parser/inline/err/misplaced_label_err.rs");
}
#[test]
fn missing_const_type() {
run_and_expect_errors("test_data/parser/inline/err/missing_const_type.rs");
}
#[test]
fn missing_fn_param_type() {
run_and_expect_errors("test_data/parser/inline/err/missing_fn_param_type.rs");
}
#[test]
fn missing_static_type() {
run_and_expect_errors("test_data/parser/inline/err/missing_static_type.rs");
}
#[test]
fn path_item_without_excl() {
run_and_expect_errors("test_data/parser/inline/err/path_item_without_excl.rs");
}

View file

@ -42,7 +42,7 @@ SOURCE_FILE
WHITESPACE "\n"
error 6: expected fn, trait or impl
error 38: expected a name
error 40: missing type for `const` or `static`
error 40: missing type for `const`
error 40: expected SEMICOLON
error 44: expected an item
error 44: expected an item

View file

@ -30,7 +30,7 @@ SOURCE_FILE
ERROR
SEMICOLON ";"
WHITESPACE "\n"
error 8: missing type for `const` or `static`
error 8: `static` may not have generic parameters
error 8: expected SEMICOLON
error 8: expected an item
error 12: expected an item

View file

@ -0,0 +1,14 @@
SOURCE_FILE
CONST
CONST_KW "const"
WHITESPACE " "
NAME
IDENT "C"
WHITESPACE " "
EQ "="
WHITESPACE " "
LITERAL
INT_NUMBER "0"
SEMICOLON ";"
WHITESPACE "\n"
error 7: missing type for `const`

View file

@ -0,0 +1 @@
const C = 0;

View file

@ -0,0 +1,14 @@
SOURCE_FILE
STATIC
STATIC_KW "static"
WHITESPACE " "
NAME
IDENT "C"
WHITESPACE " "
EQ "="
WHITESPACE " "
LITERAL
INT_NUMBER "0"
SEMICOLON ";"
WHITESPACE "\n"
error 8: missing type for `static`

View file

@ -0,0 +1 @@
static C = 0;