From 95d239da99cb1b7ba60f5f20f6df54e1397a1bca Mon Sep 17 00:00:00 2001 From: Arnaud Date: Mon, 15 Feb 2021 18:11:10 +0100 Subject: Specialization for async traits --- crates/parser/src/grammar/items.rs | 28 +++++++++++++++ .../inline/ok/0162_default_async_unsafe_fn.rast | 42 ++++++++++++++++++++++ .../inline/ok/0162_default_async_unsafe_fn.rs | 3 ++ .../parser/inline/ok/0163_default_async_fn.rast | 40 +++++++++++++++++++++ .../parser/inline/ok/0163_default_async_fn.rs | 3 ++ 5 files changed, 116 insertions(+) create mode 100644 crates/syntax/test_data/parser/inline/ok/0162_default_async_unsafe_fn.rast create mode 100644 crates/syntax/test_data/parser/inline/ok/0162_default_async_unsafe_fn.rs create mode 100644 crates/syntax/test_data/parser/inline/ok/0163_default_async_fn.rast create mode 100644 crates/syntax/test_data/parser/inline/ok/0163_default_async_fn.rs (limited to 'crates') diff --git a/crates/parser/src/grammar/items.rs b/crates/parser/src/grammar/items.rs index 1d894e907..adec74ef3 100644 --- a/crates/parser/src/grammar/items.rs +++ b/crates/parser/src/grammar/items.rs @@ -83,6 +83,7 @@ pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool) { } } +/// Try to parse an item, completing `m` in case of success. pub(super) fn maybe_item(p: &mut Parser, m: Marker) -> Result<(), Marker> { // test_err pub_expr // fn foo() { pub 92; } @@ -143,6 +144,33 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker) -> Result<(), Marker> { has_mods = true; } } + 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); + true + } else { + false + }; + + if matches!(maybe_fn, T![fn]) { + p.bump_remap(T![default]); + p.bump(T![async]); + if is_unsafe { + p.bump(T![unsafe]) + } + has_mods = true; + } + } _ => (), } } diff --git a/crates/syntax/test_data/parser/inline/ok/0162_default_async_unsafe_fn.rast b/crates/syntax/test_data/parser/inline/ok/0162_default_async_unsafe_fn.rast new file mode 100644 index 000000000..61c17333a --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0162_default_async_unsafe_fn.rast @@ -0,0 +1,42 @@ +SOURCE_FILE@0..56 + IMPL@0..55 + IMPL_KW@0..4 "impl" + WHITESPACE@4..5 " " + PATH_TYPE@5..6 + PATH@5..6 + PATH_SEGMENT@5..6 + NAME_REF@5..6 + IDENT@5..6 "T" + WHITESPACE@6..7 " " + FOR_KW@7..10 "for" + WHITESPACE@10..11 " " + PATH_TYPE@11..14 + PATH@11..14 + PATH_SEGMENT@11..14 + NAME_REF@11..14 + IDENT@11..14 "Foo" + WHITESPACE@14..15 " " + ASSOC_ITEM_LIST@15..55 + L_CURLY@15..16 "{" + WHITESPACE@16..21 "\n " + FN@21..53 + DEFAULT_KW@21..28 "default" + WHITESPACE@28..29 " " + ASYNC_KW@29..34 "async" + WHITESPACE@34..35 " " + UNSAFE_KW@35..41 "unsafe" + WHITESPACE@41..42 " " + FN_KW@42..44 "fn" + WHITESPACE@44..45 " " + NAME@45..48 + IDENT@45..48 "foo" + PARAM_LIST@48..50 + L_PAREN@48..49 "(" + R_PAREN@49..50 ")" + WHITESPACE@50..51 " " + BLOCK_EXPR@51..53 + L_CURLY@51..52 "{" + R_CURLY@52..53 "}" + WHITESPACE@53..54 "\n" + R_CURLY@54..55 "}" + WHITESPACE@55..56 "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0162_default_async_unsafe_fn.rs b/crates/syntax/test_data/parser/inline/ok/0162_default_async_unsafe_fn.rs new file mode 100644 index 000000000..05c20a68f --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0162_default_async_unsafe_fn.rs @@ -0,0 +1,3 @@ +impl T for Foo { + default async unsafe fn foo() {} +} diff --git a/crates/syntax/test_data/parser/inline/ok/0163_default_async_fn.rast b/crates/syntax/test_data/parser/inline/ok/0163_default_async_fn.rast new file mode 100644 index 000000000..9c43ccea5 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0163_default_async_fn.rast @@ -0,0 +1,40 @@ +SOURCE_FILE@0..49 + IMPL@0..48 + IMPL_KW@0..4 "impl" + WHITESPACE@4..5 " " + PATH_TYPE@5..6 + PATH@5..6 + PATH_SEGMENT@5..6 + NAME_REF@5..6 + IDENT@5..6 "T" + WHITESPACE@6..7 " " + FOR_KW@7..10 "for" + WHITESPACE@10..11 " " + PATH_TYPE@11..14 + PATH@11..14 + PATH_SEGMENT@11..14 + NAME_REF@11..14 + IDENT@11..14 "Foo" + WHITESPACE@14..15 " " + ASSOC_ITEM_LIST@15..48 + L_CURLY@15..16 "{" + WHITESPACE@16..21 "\n " + FN@21..46 + DEFAULT_KW@21..28 "default" + WHITESPACE@28..29 " " + ASYNC_KW@29..34 "async" + WHITESPACE@34..35 " " + FN_KW@35..37 "fn" + WHITESPACE@37..38 " " + NAME@38..41 + IDENT@38..41 "foo" + PARAM_LIST@41..43 + L_PAREN@41..42 "(" + R_PAREN@42..43 ")" + WHITESPACE@43..44 " " + BLOCK_EXPR@44..46 + L_CURLY@44..45 "{" + R_CURLY@45..46 "}" + WHITESPACE@46..47 "\n" + R_CURLY@47..48 "}" + WHITESPACE@48..49 "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0163_default_async_fn.rs b/crates/syntax/test_data/parser/inline/ok/0163_default_async_fn.rs new file mode 100644 index 000000000..78c3b4d85 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0163_default_async_fn.rs @@ -0,0 +1,3 @@ +impl T for Foo { + default async fn foo() {} +} -- cgit v1.2.3