]> git.lizzy.rs Git - rust.git/blob - crates/parser/src/grammar/items/consts.rs
feat: improve parser error recovery for function parameters
[rust.git] / crates / parser / src / grammar / items / consts.rs
1 use super::*;
2
3 pub(super) fn static_(p: &mut Parser, m: Marker) {
4     const_or_static(p, m, T![static], STATIC)
5 }
6
7 pub(super) fn konst(p: &mut Parser, m: Marker) {
8     const_or_static(p, m, T![const], CONST)
9 }
10
11 fn const_or_static(p: &mut Parser, m: Marker, kw: SyntaxKind, def: SyntaxKind) {
12     assert!(p.at(kw));
13     p.bump(kw);
14     p.eat(T![mut]);
15
16     // Allow `_` in place of an identifier in a `const`.
17     let is_const_underscore = kw == T![const] && p.eat(T![_]);
18     if !is_const_underscore {
19         name(p);
20     }
21
22     // test_err static_underscore
23     // static _: i32 = 5;
24     if p.at(T![:]) {
25         types::ascription(p);
26     } else {
27         p.error("missing type for `const` or `static`")
28     }
29     if p.eat(T![=]) {
30         expressions::expr(p);
31     }
32     p.expect(T![;]);
33     m.complete(p, def);
34 }