Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions crates/parser/src/grammar/items/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's preferable to eat generic params here, or even better, call opt_generic_params() unconditionally, and just err before for statics. This will also allow us to simplify the next if.

Copy link
Contributor Author

@A4-Tacks A4-Tacks Oct 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the AST of a static item contain GENERIC_PARAM_LIST?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It causes no harm.

}
// 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);
Expand Down
8 changes: 8 additions & 0 deletions crates/parser/test_data/generated/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 16 additions & 31 deletions crates/parser/test_data/parser/inline/err/generic_static.rast
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions crates/parser/test_data/parser/inline/err/missing_const_type.rast
Original file line number Diff line number Diff line change
@@ -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`
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const C = 0;
14 changes: 14 additions & 0 deletions crates/parser/test_data/parser/inline/err/missing_static_type.rast
Original file line number Diff line number Diff line change
@@ -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`
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
static C = 0;