Unnamed repository; edit this file 'description' to name the repository.
Merge #10269
10269: internal: cleanup item parsing r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
bors[bot] 2021-09-18
parent c72a30a · parent d890c76 · commit 894abd1
-rw-r--r--crates/parser/src/grammar.rs5
-rw-r--r--crates/parser/src/grammar/items.rs132
-rw-r--r--crates/parser/src/grammar/items/traits.rs7
-rw-r--r--crates/syntax/test_data/parser/inline/ok/0152_impl.rast22
-rw-r--r--crates/syntax/test_data/parser/inline/ok/0152_impl.rs1
-rw-r--r--crates/syntax/test_data/parser/inline/ok/0153_trait.rast11
-rw-r--r--crates/syntax/test_data/parser/inline/ok/0153_trait.rs1
7 files changed, 64 insertions, 115 deletions
diff --git a/crates/parser/src/grammar.rs b/crates/parser/src/grammar.rs
index 23f0cc0153..58e182d68c 100644
--- a/crates/parser/src/grammar.rs
+++ b/crates/parser/src/grammar.rs
@@ -183,6 +183,7 @@ fn opt_visibility(p: &mut Parser) -> bool {
}
}
m.complete(p, VISIBILITY);
+ true
}
// test crate_keyword_vis
// crate fn main() { }
@@ -197,10 +198,10 @@ fn opt_visibility(p: &mut Parser) -> bool {
let m = p.start();
p.bump(T![crate]);
m.complete(p, VISIBILITY);
+ true
}
- _ => return false,
+ _ => false,
}
- true
}
fn opt_rename(p: &mut Parser) {
diff --git a/crates/parser/src/grammar/items.rs b/crates/parser/src/grammar/items.rs
index e0b740a815..78d99f284a 100644
--- a/crates/parser/src/grammar/items.rs
+++ b/crates/parser/src/grammar/items.rs
@@ -135,27 +135,25 @@ pub(super) fn opt_item(p: &mut Parser, m: Marker) -> Result<(), Marker> {
p.bump_remap(T![default]);
has_mods = true;
}
- T![unsafe] => {
- // test default_unsafe_item
- // default unsafe impl T for Foo {
- // default unsafe fn foo() {}
- // }
- if matches!(p.nth(2), T![impl] | T![fn]) {
- p.bump_remap(T![default]);
- p.bump(T![unsafe]);
- has_mods = true;
- }
+ // test default_unsafe_item
+ // default unsafe impl T for Foo {
+ // default unsafe fn foo() {}
+ // }
+ T![unsafe] if matches!(p.nth(2), T![impl] | T![fn]) => {
+ p.bump_remap(T![default]);
+ p.bump(T![unsafe]);
+ has_mods = true;
}
+ // test default_async_fn
+ // impl T for Foo {
+ // default async fn foo() {}
+ // }
+
+ // test default_async_unsafe_fn
+ // impl T for Foo {
+ // default async unsafe fn foo() {}
+ // }
T![async] => {
- // test default_async_fn
- // impl T for Foo {
- // default async fn foo() {}
- // }
-
- // test default_async_unsafe_fn
- // impl T for Foo {
- // default async unsafe fn foo() {}
- // }
let mut maybe_fn = p.nth(2);
let is_unsafe = if matches!(maybe_fn, T![unsafe]) {
maybe_fn = p.nth(3);
@@ -186,34 +184,14 @@ pub(super) fn opt_item(p: &mut Parser, m: Marker) -> Result<(), Marker> {
// items
match p.current() {
- // test fn
- // fn foo() {}
- T![fn] => {
- fn_(p);
- m.complete(p, FN);
- }
+ T![fn] => fn_(p, m),
- // test trait
- // trait T {}
- T![trait] => {
- traits::trait_(p);
- m.complete(p, TRAIT);
- }
+ T![const] if p.nth(1) != T!['{'] => consts::konst(p, m),
- T![const] if p.nth(1) != T!['{'] => {
- consts::konst(p, m);
- }
-
- // test impl
- // impl T for S {}
- T![impl] => {
- traits::impl_(p);
- m.complete(p, IMPL);
- }
+ T![trait] => traits::trait_(p, m),
+ T![impl] => traits::impl_(p, m),
- T![type] => {
- type_alias(p, m);
- }
+ T![type] => type_alias(p, m),
// test extern_block
// unsafe extern "C" {}
@@ -341,38 +319,6 @@ pub(crate) fn extern_item_list(p: &mut Parser) {
m.complete(p, EXTERN_ITEM_LIST);
}
-fn fn_(p: &mut Parser) {
- assert!(p.at(T![fn]));
- p.bump(T![fn]);
-
- name_r(p, ITEM_RECOVERY_SET);
- // test function_type_params
- // fn foo<T: Clone + Copy>(){}
- type_params::opt_generic_param_list(p);
-
- if p.at(T!['(']) {
- params::param_list_fn_def(p);
- } else {
- p.error("expected function arguments");
- }
- // test function_ret_type
- // fn foo() {}
- // fn bar() -> () {}
- opt_ret_type(p);
-
- // test function_where_clause
- // fn foo<T>() where T: Copy {}
- type_params::opt_where_clause(p);
-
- // test fn_decl
- // trait T { fn foo(); }
- if p.at(T![;]) {
- p.bump(T![;]);
- } else {
- expressions::block_expr(p)
- }
-}
-
fn macro_rules(p: &mut Parser, m: Marker) {
assert!(p.at_contextual_kw("macro_rules"));
p.bump_remap(T![macro_rules]);
@@ -430,6 +376,40 @@ fn macro_def(p: &mut Parser, m: Marker) {
m.complete(p, MACRO_DEF);
}
+// test fn
+// fn foo() {}
+fn fn_(p: &mut Parser, m: Marker) {
+ p.bump(T![fn]);
+
+ name_r(p, ITEM_RECOVERY_SET);
+ // test function_type_params
+ // fn foo<T: Clone + Copy>(){}
+ type_params::opt_generic_param_list(p);
+
+ if p.at(T!['(']) {
+ params::param_list_fn_def(p);
+ } else {
+ p.error("expected function arguments");
+ }
+ // test function_ret_type
+ // fn foo() {}
+ // fn bar() -> () {}
+ opt_ret_type(p);
+
+ // test function_where_clause
+ // fn foo<T>() where T: Copy {}
+ type_params::opt_where_clause(p);
+
+ // test fn_decl
+ // trait T { fn foo(); }
+ if p.at(T![;]) {
+ p.bump(T![;]);
+ } else {
+ expressions::block_expr(p)
+ }
+ m.complete(p, FN);
+}
+
fn macro_call(p: &mut Parser) -> BlockLike {
assert!(paths::is_use_path_start(p));
paths::use_path(p);
diff --git a/crates/parser/src/grammar/items/traits.rs b/crates/parser/src/grammar/items/traits.rs
index 74f11b45a1..569fc58a1c 100644
--- a/crates/parser/src/grammar/items/traits.rs
+++ b/crates/parser/src/grammar/items/traits.rs
@@ -3,7 +3,7 @@ use super::*;
// test trait_item
// trait T<U>: Hash + Clone where U: Copy {}
// trait X<U: Debug + Display>: Hash + Clone where U: Copy {}
-pub(super) fn trait_(p: &mut Parser) {
+pub(super) fn trait_(p: &mut Parser, m: Marker) {
assert!(p.at(T![trait]));
p.bump(T![trait]);
name_r(p, ITEM_RECOVERY_SET);
@@ -16,6 +16,7 @@ pub(super) fn trait_(p: &mut Parser) {
type_params::bounds_without_colon(p);
type_params::opt_where_clause(p);
p.expect(T![;]);
+ m.complete(p, TRAIT);
return;
}
if p.at(T![:]) {
@@ -27,11 +28,12 @@ pub(super) fn trait_(p: &mut Parser) {
} else {
p.error("expected `{`");
}
+ m.complete(p, TRAIT);
}
// test impl_def
// impl Foo {}
-pub(super) fn impl_(p: &mut Parser) {
+pub(super) fn impl_(p: &mut Parser, m: Marker) {
assert!(p.at(T![impl]));
p.bump(T![impl]);
if choose_type_params_over_qpath(p) {
@@ -58,6 +60,7 @@ pub(super) fn impl_(p: &mut Parser) {
} else {
p.error("expected `{`");
}
+ m.complete(p, IMPL);
}
// test impl_item_list
diff --git a/crates/syntax/test_data/parser/inline/ok/0152_impl.rast b/crates/syntax/test_data/parser/inline/ok/0152_impl.rast
deleted file mode 100644
index 7968cf9ffa..0000000000
--- a/crates/syntax/test_data/parser/inline/ok/0152_impl.rast
+++ /dev/null
@@ -1,22 +0,0 @@
diff --git a/crates/syntax/test_data/parser/inline/ok/0152_impl.rs b/crates/syntax/test_data/parser/inline/ok/0152_impl.rs
deleted file mode 100644
index a1a550d8a6..0000000000
--- a/crates/syntax/test_data/parser/inline/ok/0152_impl.rs
+++ /dev/null
@@ -1 +0,0 @@
-impl T for S {}
diff --git a/crates/syntax/test_data/parser/inline/ok/0153_trait.rast b/crates/syntax/test_data/parser/inline/ok/0153_trait.rast
deleted file mode 100644
index 9881e5048c..0000000000
--- a/crates/syntax/test_data/parser/inline/ok/0153_trait.rast
+++ /dev/null
@@ -1,11 +0,0 @@
diff --git a/crates/syntax/test_data/parser/inline/ok/0153_trait.rs b/crates/syntax/test_data/parser/inline/ok/0153_trait.rs
deleted file mode 100644
index 8d183dbb5d..0000000000
--- a/crates/syntax/test_data/parser/inline/ok/0153_trait.rs
+++ /dev/null
@@ -1 +0,0 @@
-trait T {}