Skip to content

Commit 8e61245

Browse files
bors[bot]matklad
andauthored
Merge #10270
10270: intenral: more item parsing cleanup r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 894abd1 + 3474e3b commit 8e61245

15 files changed

+133
-219
lines changed

crates/parser/src/grammar/items.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,13 @@ pub(super) fn opt_item(p: &mut Parser, m: Marker) -> Result<(), Marker> {
148148
// impl T for Foo {
149149
// default async fn foo() {}
150150
// }
151-
152-
// test default_async_unsafe_fn
153-
// impl T for Foo {
154-
// default async unsafe fn foo() {}
155-
// }
156151
T![async] => {
157152
let mut maybe_fn = p.nth(2);
158153
let is_unsafe = if matches!(maybe_fn, T![unsafe]) {
154+
// test default_async_unsafe_fn
155+
// impl T for Foo {
156+
// default async unsafe fn foo() {}
157+
// }
159158
maybe_fn = p.nth(3);
160159
true
161160
} else {
@@ -400,9 +399,9 @@ fn fn_(p: &mut Parser, m: Marker) {
400399
// fn foo<T>() where T: Copy {}
401400
type_params::opt_where_clause(p);
402401

403-
// test fn_decl
404-
// trait T { fn foo(); }
405402
if p.at(T![;]) {
403+
// test fn_decl
404+
// trait T { fn foo(); }
406405
p.bump(T![;]);
407406
} else {
408407
expressions::block_expr(p)

crates/parser/src/grammar/items/adt.rs

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ fn struct_or_union(p: &mut Parser, m: Marker, is_struct: bool) {
2222
T![where] => {
2323
type_params::opt_where_clause(p);
2424
match p.current() {
25-
T![;] => {
26-
p.bump(T![;]);
27-
}
25+
T![;] => p.bump(T![;]),
2826
T!['{'] => record_field_list(p),
2927
_ => {
3028
//FIXME: special case `(` error message
@@ -53,7 +51,6 @@ fn struct_or_union(p: &mut Parser, m: Marker, is_struct: bool) {
5351
}
5452

5553
pub(super) fn enum_(p: &mut Parser, m: Marker) {
56-
assert!(p.at(T![enum]));
5754
p.bump(T![enum]);
5855
name_r(p, ITEM_RECOVERY_SET);
5956
type_params::opt_generic_param_list(p);
@@ -75,7 +72,16 @@ pub(crate) fn variant_list(p: &mut Parser) {
7572
error_block(p, "expected enum variant");
7673
continue;
7774
}
78-
let var = p.start();
75+
variant(p);
76+
if !p.at(T!['}']) {
77+
p.expect(T![,]);
78+
}
79+
}
80+
p.expect(T!['}']);
81+
m.complete(p, VARIANT_LIST);
82+
83+
fn variant(p: &mut Parser) {
84+
let m = p.start();
7985
attributes::outer_attrs(p);
8086
if p.at(IDENT) {
8187
name(p);
@@ -90,17 +96,12 @@ pub(crate) fn variant_list(p: &mut Parser) {
9096
if p.eat(T![=]) {
9197
expressions::expr(p);
9298
}
93-
var.complete(p, VARIANT);
99+
m.complete(p, VARIANT);
94100
} else {
95-
var.abandon(p);
101+
m.abandon(p);
96102
p.err_and_bump("expected enum variant");
97103
}
98-
if !p.at(T!['}']) {
99-
p.expect(T![,]);
100-
}
101104
}
102-
p.expect(T!['}']);
103-
m.complete(p, VARIANT_LIST);
104105
}
105106

106107
// test record_field_list
@@ -114,21 +115,18 @@ pub(crate) fn record_field_list(p: &mut Parser) {
114115
error_block(p, "expected field");
115116
continue;
116117
}
117-
record_field_def(p);
118+
record_field(p);
118119
if !p.at(T!['}']) {
119120
p.expect(T![,]);
120121
}
121122
}
122123
p.expect(T!['}']);
123124
m.complete(p, RECORD_FIELD_LIST);
124125

125-
fn record_field_def(p: &mut Parser) {
126+
fn record_field(p: &mut Parser) {
126127
let m = p.start();
127128
// test record_field_attrs
128-
// struct S {
129-
// #[serde(with = "url_serde")]
130-
// pub uri: Uri,
131-
// }
129+
// struct S { #[attr] f: f32 }
132130
attributes::outer_attrs(p);
133131
opt_visibility(p);
134132
if p.at(IDENT) {
@@ -146,20 +144,11 @@ pub(crate) fn record_field_list(p: &mut Parser) {
146144
fn tuple_field_list(p: &mut Parser) {
147145
assert!(p.at(T!['(']));
148146
let m = p.start();
149-
if !p.expect(T!['(']) {
150-
return;
151-
}
147+
p.bump(T!['(']);
152148
while !p.at(T![')']) && !p.at(EOF) {
153149
let m = p.start();
154150
// test tuple_field_attrs
155-
// struct S (
156-
// #[serde(with = "url_serde")]
157-
// pub Uri,
158-
// );
159-
//
160-
// enum S {
161-
// Uri(#[serde(with = "url_serde")] Uri),
162-
// }
151+
// struct S (#[attr] f32);
163152
attributes::outer_attrs(p);
164153
opt_visibility(p);
165154
if !p.at_ts(types::TYPE_FIRST) {
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
use super::*;
22

3-
pub(super) fn static_(p: &mut Parser, m: Marker) {
4-
const_or_static(p, m, T![static], STATIC)
3+
// test const_item
4+
// const C: u32 = 92;
5+
pub(super) fn konst(p: &mut Parser, m: Marker) {
6+
p.bump(T![const]);
7+
const_or_static(p, m, true)
58
}
69

7-
pub(super) fn konst(p: &mut Parser, m: Marker) {
8-
const_or_static(p, m, T![const], CONST)
10+
pub(super) fn static_(p: &mut Parser, m: Marker) {
11+
p.bump(T![static]);
12+
const_or_static(p, m, false)
913
}
1014

11-
fn const_or_static(p: &mut Parser, m: Marker, kw: SyntaxKind, def: SyntaxKind) {
12-
assert!(p.at(kw));
13-
p.bump(kw);
15+
fn const_or_static(p: &mut Parser, m: Marker, is_const: bool) {
1416
p.eat(T![mut]);
1517

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 {
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;
1924
name(p);
2025
}
2126

22-
// test_err static_underscore
23-
// static _: i32 = 5;
2427
if p.at(T![:]) {
2528
types::ascription(p);
2629
} else {
@@ -30,5 +33,5 @@ fn const_or_static(p: &mut Parser, m: Marker, kw: SyntaxKind, def: SyntaxKind) {
3033
expressions::expr(p);
3134
}
3235
p.expect(T![;]);
33-
m.complete(p, def);
36+
m.complete(p, if is_const { CONST } else { STATIC });
3437
}
Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,33 @@
1-
SOURCE_FILE@0..64
2-
STRUCT@0..63
1+
SOURCE_FILE@0..28
2+
STRUCT@0..27
33
44
55
66
77
8-
RECORD_FIELD_LIST@9..63
8+
RECORD_FIELD_LIST@9..27
99
10-
11-
12-
13-
14-
15-
16-
17-
18-
19-
20-
21-
22-
23-
24-
25-
26-
[email protected] "\"url_serde\""
27-
28-
29-
30-
31-
32-
33-
34-
35-
36-
37-
38-
39-
40-
41-
42-
43-
44-
45-
10+
11+
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
struct S {
2-
#[serde(with = "url_serde")]
3-
pub uri: Uri,
4-
}
1+
struct S { #[attr] f: f32 }
Lines changed: 22 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,28 @@
1-
SOURCE_FILE@0..115
2-
STRUCT@0..59
1+
SOURCE_FILE@0..24
2+
STRUCT@0..23
33
44
55
66
77
8-
TUPLE_FIELD_LIST@9..58
8+
TUPLE_FIELD_LIST@9..22
99
10-
11-
12-
13-
14-
15-
16-
17-
18-
19-
20-
21-
22-
23-
24-
25-
26-
[email protected] "\"url_serde\""
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-
[email protected] "\"url_serde\""
73-
74-
75-
76-
77-
78-
79-
80-
81-
82-
83-
84-
85-
10+
11+
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1 @@
1-
struct S (
2-
#[serde(with = "url_serde")]
3-
pub Uri,
4-
);
5-
6-
enum S {
7-
Uri(#[serde(with = "url_serde")] Uri),
8-
}
1+
struct S (#[attr] f32);

0 commit comments

Comments
 (0)