Skip to content

Commit dce0d71

Browse files
Merge #8685
8685: feat: parse const param defaults r=jonas-schievink a=jonas-schievink These will probably be stabilized in a few releases, let's make sure we can at least parse them bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents 9d19948 + cb8632d commit dce0d71

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed

crates/parser/src/grammar/type_args.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,38 @@ pub(super) fn opt_generic_arg_list(p: &mut Parser, colon_colon_required: bool) {
2525
m.complete(p, GENERIC_ARG_LIST);
2626
}
2727

28+
pub(super) fn const_arg(p: &mut Parser) {
29+
let m = p.start();
30+
// FIXME: duplicates the code below
31+
match p.current() {
32+
T!['{'] => {
33+
expressions::block_expr(p);
34+
m.complete(p, CONST_ARG);
35+
}
36+
k if k.is_literal() => {
37+
expressions::literal(p);
38+
m.complete(p, CONST_ARG);
39+
}
40+
T![true] | T![false] => {
41+
expressions::literal(p);
42+
m.complete(p, CONST_ARG);
43+
}
44+
T![-] => {
45+
let lm = p.start();
46+
p.bump(T![-]);
47+
expressions::literal(p);
48+
lm.complete(p, PREFIX_EXPR);
49+
m.complete(p, CONST_ARG);
50+
}
51+
_ => {
52+
let lm = p.start();
53+
paths::use_path(p);
54+
lm.complete(p, PATH_EXPR);
55+
m.complete(p, CONST_ARG);
56+
}
57+
}
58+
}
59+
2860
// test type_arg
2961
// type A = B<'static, i32, 1, { 2 }, Item=u64, true, false>;
3062
fn generic_arg(p: &mut Parser) {

crates/parser/src/grammar/type_params.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ fn const_param(p: &mut Parser, m: Marker) {
7070
p.bump(T![const]);
7171
name(p);
7272
types::ascription(p);
73+
74+
// test const_param_defaults
75+
// struct A<const N: i32 = -1>;
76+
// struct B<const N: i32 = {}>;
77+
// struct C<const N: i32 = some::CONST>;
78+
if p.at(T![=]) {
79+
p.bump(T![=]);
80+
type_args::const_arg(p);
81+
}
82+
7383
m.complete(p, CONST_PARAM);
7484
}
7585

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
2+
3+
4+
5+
6+
7+
8+
9+
10+
11+
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
70+
71+
72+
73+
74+
75+
76+
77+
78+
79+
80+
81+
82+
83+
84+
85+
86+
87+
88+
89+
90+
91+
92+
93+
94+
95+
96+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
struct A<const N: i32 = -1>;
2+
struct B<const N: i32 = {}>;
3+
struct C<const N: i32 = some::CONST>;

0 commit comments

Comments
 (0)