parser/grammar/items/
consts.rs

1use super::*;
2
3// test const_item
4// const C: u32 = 92;
5pub(super) fn konst(p: &mut Parser<'_>, m: Marker) {
6    p.bump(T![const]);
7    const_or_static(p, m, true);
8}
9
10pub(super) fn static_(p: &mut Parser<'_>, m: Marker) {
11    p.bump(T![static]);
12    const_or_static(p, m, false);
13}
14
15fn const_or_static(p: &mut Parser<'_>, m: Marker, is_const: bool) {
16    p.eat(T![mut]);
17
18    if is_const && p.eat(T![_]) {
19        // test anonymous_const
20        // const _: u32 = 0;
21    } else {
22        // test_err anonymous_static
23        // static _: i32 = 5;
24        name(p);
25    }
26
27    // FIXME: Recover on statics with generic params/where clause.
28    if !is_const && p.at(T![<]) {
29        // test_err generic_static
30        // static C<i32>: u32 = 0;
31        p.error("`static` may not have generic parameters");
32    }
33    // test generic_const
34    // const C<i32>: u32 = 0;
35    // impl Foo {
36    //     const C<'a>: &'a () = &();
37    // }
38    generic_params::opt_generic_param_list(p);
39
40    if p.at(T![:]) {
41        types::ascription(p);
42    } else if is_const {
43        // test_err missing_const_type
44        // const C = 0;
45        p.error("missing type for `const`");
46    } else {
47        // test_err missing_static_type
48        // static C = 0;
49        p.error("missing type for `static`");
50    }
51    if p.eat(T![=]) {
52        expressions::expr(p);
53    }
54
55    if is_const {
56        // test const_where_clause
57        // const C<i32>: u32 = 0
58        // where i32: Copy;
59        // trait Foo {
60        //     const C: i32 where i32: Copy;
61        // }
62        generic_params::opt_where_clause(p);
63    }
64    // test_err static_where_clause
65    // static C: u32 = 0
66    // where i32: Copy;
67
68    p.expect(T![;]);
69    m.complete(p, if is_const { CONST } else { STATIC });
70}