Skip to content

Commit 894abd1

Browse files
bors[bot]matklad
andauthored
Merge #10269
10269: internal: cleanup item parsing r=matklad a=matklad bors r+ πŸ€– Co-authored-by: Aleksey Kladov <[email protected]>
2 parents c72a30a + d890c76 commit 894abd1

File tree

7 files changed

+64
-115
lines changed

7 files changed

+64
-115
lines changed

β€Žcrates/parser/src/grammar.rsβ€Ž

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ fn opt_visibility(p: &mut Parser) -> bool {
183183
}
184184
}
185185
m.complete(p, VISIBILITY);
186+
true
186187
}
187188
// test crate_keyword_vis
188189
// crate fn main() { }
@@ -197,10 +198,10 @@ fn opt_visibility(p: &mut Parser) -> bool {
197198
let m = p.start();
198199
p.bump(T![crate]);
199200
m.complete(p, VISIBILITY);
201+
true
200202
}
201-
_ => return false,
203+
_ => false,
202204
}
203-
true
204205
}
205206

206207
fn opt_rename(p: &mut Parser) {

β€Žcrates/parser/src/grammar/items.rsβ€Ž

Lines changed: 56 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -135,27 +135,25 @@ pub(super) fn opt_item(p: &mut Parser, m: Marker) -> Result<(), Marker> {
135135
p.bump_remap(T![default]);
136136
has_mods = true;
137137
}
138-
T![unsafe] => {
139-
// test default_unsafe_item
140-
// default unsafe impl T for Foo {
141-
// default unsafe fn foo() {}
142-
// }
143-
if matches!(p.nth(2), T![impl] | T![fn]) {
144-
p.bump_remap(T![default]);
145-
p.bump(T![unsafe]);
146-
has_mods = true;
147-
}
138+
// test default_unsafe_item
139+
// default unsafe impl T for Foo {
140+
// default unsafe fn foo() {}
141+
// }
142+
T![unsafe] if matches!(p.nth(2), T![impl] | T![fn]) => {
143+
p.bump_remap(T![default]);
144+
p.bump(T![unsafe]);
145+
has_mods = true;
148146
}
147+
// test default_async_fn
148+
// impl T for Foo {
149+
// default async fn foo() {}
150+
// }
151+
152+
// test default_async_unsafe_fn
153+
// impl T for Foo {
154+
// default async unsafe fn foo() {}
155+
// }
149156
T![async] => {
150-
// test default_async_fn
151-
// impl T for Foo {
152-
// default async fn foo() {}
153-
// }
154-
155-
// test default_async_unsafe_fn
156-
// impl T for Foo {
157-
// default async unsafe fn foo() {}
158-
// }
159157
let mut maybe_fn = p.nth(2);
160158
let is_unsafe = if matches!(maybe_fn, T![unsafe]) {
161159
maybe_fn = p.nth(3);
@@ -186,34 +184,14 @@ pub(super) fn opt_item(p: &mut Parser, m: Marker) -> Result<(), Marker> {
186184

187185
// items
188186
match p.current() {
189-
// test fn
190-
// fn foo() {}
191-
T![fn] => {
192-
fn_(p);
193-
m.complete(p, FN);
194-
}
187+
T![fn] => fn_(p, m),
195188

196-
// test trait
197-
// trait T {}
198-
T![trait] => {
199-
traits::trait_(p);
200-
m.complete(p, TRAIT);
201-
}
189+
T![const] if p.nth(1) != T!['{'] => consts::konst(p, m),
202190

203-
T![const] if p.nth(1) != T!['{'] => {
204-
consts::konst(p, m);
205-
}
206-
207-
// test impl
208-
// impl T for S {}
209-
T![impl] => {
210-
traits::impl_(p);
211-
m.complete(p, IMPL);
212-
}
191+
T![trait] => traits::trait_(p, m),
192+
T![impl] => traits::impl_(p, m),
213193

214-
T![type] => {
215-
type_alias(p, m);
216-
}
194+
T![type] => type_alias(p, m),
217195

218196
// test extern_block
219197
// unsafe extern "C" {}
@@ -341,38 +319,6 @@ pub(crate) fn extern_item_list(p: &mut Parser) {
341319
m.complete(p, EXTERN_ITEM_LIST);
342320
}
343321

344-
fn fn_(p: &mut Parser) {
345-
assert!(p.at(T![fn]));
346-
p.bump(T![fn]);
347-
348-
name_r(p, ITEM_RECOVERY_SET);
349-
// test function_type_params
350-
// fn foo<T: Clone + Copy>(){}
351-
type_params::opt_generic_param_list(p);
352-
353-
if p.at(T!['(']) {
354-
params::param_list_fn_def(p);
355-
} else {
356-
p.error("expected function arguments");
357-
}
358-
// test function_ret_type
359-
// fn foo() {}
360-
// fn bar() -> () {}
361-
opt_ret_type(p);
362-
363-
// test function_where_clause
364-
// fn foo<T>() where T: Copy {}
365-
type_params::opt_where_clause(p);
366-
367-
// test fn_decl
368-
// trait T { fn foo(); }
369-
if p.at(T![;]) {
370-
p.bump(T![;]);
371-
} else {
372-
expressions::block_expr(p)
373-
}
374-
}
375-
376322
fn macro_rules(p: &mut Parser, m: Marker) {
377323
assert!(p.at_contextual_kw("macro_rules"));
378324
p.bump_remap(T![macro_rules]);
@@ -430,6 +376,40 @@ fn macro_def(p: &mut Parser, m: Marker) {
430376
m.complete(p, MACRO_DEF);
431377
}
432378

379+
// test fn
380+
// fn foo() {}
381+
fn fn_(p: &mut Parser, m: Marker) {
382+
p.bump(T![fn]);
383+
384+
name_r(p, ITEM_RECOVERY_SET);
385+
// test function_type_params
386+
// fn foo<T: Clone + Copy>(){}
387+
type_params::opt_generic_param_list(p);
388+
389+
if p.at(T!['(']) {
390+
params::param_list_fn_def(p);
391+
} else {
392+
p.error("expected function arguments");
393+
}
394+
// test function_ret_type
395+
// fn foo() {}
396+
// fn bar() -> () {}
397+
opt_ret_type(p);
398+
399+
// test function_where_clause
400+
// fn foo<T>() where T: Copy {}
401+
type_params::opt_where_clause(p);
402+
403+
// test fn_decl
404+
// trait T { fn foo(); }
405+
if p.at(T![;]) {
406+
p.bump(T![;]);
407+
} else {
408+
expressions::block_expr(p)
409+
}
410+
m.complete(p, FN);
411+
}
412+
433413
fn macro_call(p: &mut Parser) -> BlockLike {
434414
assert!(paths::is_use_path_start(p));
435415
paths::use_path(p);

β€Žcrates/parser/src/grammar/items/traits.rsβ€Ž

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use super::*;
33
// test trait_item
44
// trait T<U>: Hash + Clone where U: Copy {}
55
// trait X<U: Debug + Display>: Hash + Clone where U: Copy {}
6-
pub(super) fn trait_(p: &mut Parser) {
6+
pub(super) fn trait_(p: &mut Parser, m: Marker) {
77
assert!(p.at(T![trait]));
88
p.bump(T![trait]);
99
name_r(p, ITEM_RECOVERY_SET);
@@ -16,6 +16,7 @@ pub(super) fn trait_(p: &mut Parser) {
1616
type_params::bounds_without_colon(p);
1717
type_params::opt_where_clause(p);
1818
p.expect(T![;]);
19+
m.complete(p, TRAIT);
1920
return;
2021
}
2122
if p.at(T![:]) {
@@ -27,11 +28,12 @@ pub(super) fn trait_(p: &mut Parser) {
2728
} else {
2829
p.error("expected `{`");
2930
}
31+
m.complete(p, TRAIT);
3032
}
3133

3234
// test impl_def
3335
// impl Foo {}
34-
pub(super) fn impl_(p: &mut Parser) {
36+
pub(super) fn impl_(p: &mut Parser, m: Marker) {
3537
assert!(p.at(T![impl]));
3638
p.bump(T![impl]);
3739
if choose_type_params_over_qpath(p) {
@@ -58,6 +60,7 @@ pub(super) fn impl_(p: &mut Parser) {
5860
} else {
5961
p.error("expected `{`");
6062
}
63+
m.complete(p, IMPL);
6164
}
6265

6366
// test impl_item_list

β€Žcrates/syntax/test_data/parser/inline/ok/0152_impl.rastβ€Ž

Lines changed: 0 additions & 22 deletions
This file was deleted.

β€Žcrates/syntax/test_data/parser/inline/ok/0152_impl.rsβ€Ž

Lines changed: 0 additions & 1 deletion
This file was deleted.

β€Žcrates/syntax/test_data/parser/inline/ok/0153_trait.rastβ€Ž

Lines changed: 0 additions & 11 deletions
This file was deleted.

β€Žcrates/syntax/test_data/parser/inline/ok/0153_trait.rsβ€Ž

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
Β (0)