Merge pull request #20805 from A4-Tacks/improve-static-const-parse-error
Some checks are pending
metrics / build_metrics (push) Waiting to run
metrics / other_metrics (diesel-1.4.8) (push) Blocked by required conditions
metrics / other_metrics (hyper-0.14.18) (push) Blocked by required conditions
metrics / other_metrics (ripgrep-13.0.0) (push) Blocked by required conditions
metrics / other_metrics (self) (push) Blocked by required conditions
metrics / other_metrics (webrender-2022) (push) Blocked by required conditions
metrics / generate_final_metrics (push) Blocked by required conditions
rustdoc / rustdoc (push) Waiting to run

Improve parsing error for `static` and `const`
This commit is contained in:
Chayim Refael Friedman 2025-10-09 03:09:29 +00:00 committed by GitHub
commit f8fdf544e4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 72 additions and 42 deletions

View file

@ -25,21 +25,28 @@ fn const_or_static(p: &mut Parser<'_>, m: Marker, is_const: bool) {
}
// FIXME: Recover on statics with generic params/where clause.
if is_const {
// test generic_const
// const C<i32>: u32 = 0;
// impl Foo {
// const C<'a>: &'a () = &();
// }
generic_params::opt_generic_param_list(p);
if !is_const && p.at(T![<]) {
// test_err generic_static
// static C<i32>: u32 = 0;
p.error("`static` may not have generic parameters");
}
// test_err generic_static
// static C<i32>: u32 = 0;
// test generic_const
// const C<i32>: u32 = 0;
// impl Foo {
// const C<'a>: &'a () = &();
// }
generic_params::opt_generic_param_list(p);
if p.at(T![:]) {
types::ascription(p);
} else if is_const {
// test_err missing_const_type
// const C = 0;
p.error("missing type for `const`");
} else {
p.error("missing type for `const` or `static`");
// 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

@ -4,39 +4,24 @@ SOURCE_FILE
WHITESPACE " "
NAME
IDENT "C"
ERROR
L_ANGLE "<"
ERROR
PATH
PATH_SEGMENT
NAME_REF
GENERIC_PARAM_LIST
L_ANGLE "<"
TYPE_PARAM
NAME
IDENT "i32"
ERROR
R_ANGLE ">"
ERROR
R_ANGLE ">"
COLON ":"
WHITESPACE " "
ERROR
PATH
PATH_SEGMENT
NAME_REF
IDENT "u32"
WHITESPACE " "
ERROR
WHITESPACE " "
PATH_TYPE
PATH
PATH_SEGMENT
NAME_REF
IDENT "u32"
WHITESPACE " "
EQ "="
WHITESPACE " "
ERROR
INT_NUMBER "0"
ERROR
WHITESPACE " "
LITERAL
INT_NUMBER "0"
SEMICOLON ";"
WHITESPACE "\n"
error 8: missing type for `const` or `static`
error 8: expected SEMICOLON
error 8: expected an item
error 12: expected an item
error 12: expected an item
error 13: expected an item
error 18: expected an item
error 19: expected an item
error 21: expected an item
error 22: expected an item
error 8: `static` may not have generic parameters

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;