From 636270f4a4f80f5d24571147bcadfbeaa29310a0 Mon Sep 17 00:00:00 2001 From: robojumper Date: Wed, 3 Apr 2019 17:11:56 +0200 Subject: Parse unsafe async / const unsafe fns properly --- crates/ra_parser/src/grammar/items.rs | 23 +++++++++---- .../data/parser/inline/err/0010_wrong_order_fns.rs | 2 ++ .../parser/inline/err/0010_wrong_order_fns.txt | 39 ++++++++++++++++++++++ .../data/parser/inline/ok/0128_combined_fns.rs | 2 ++ .../data/parser/inline/ok/0128_combined_fns.txt | 35 +++++++++++++++++++ 5 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 crates/ra_syntax/tests/data/parser/inline/err/0010_wrong_order_fns.rs create mode 100644 crates/ra_syntax/tests/data/parser/inline/err/0010_wrong_order_fns.txt create mode 100644 crates/ra_syntax/tests/data/parser/inline/ok/0128_combined_fns.rs create mode 100644 crates/ra_syntax/tests/data/parser/inline/ok/0128_combined_fns.txt (limited to 'crates') diff --git a/crates/ra_parser/src/grammar/items.rs b/crates/ra_parser/src/grammar/items.rs index c4b8ef3c7..318fd69a1 100644 --- a/crates/ra_parser/src/grammar/items.rs +++ b/crates/ra_parser/src/grammar/items.rs @@ -79,19 +79,22 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker, flavor: ItemFlavor) -> Resul let mut has_mods = false; // modifiers - // test_err async_without_semicolon - // fn foo() { let _ = async {} } has_mods |= p.eat(CONST_KW); - if p.at(ASYNC_KW) && p.nth(1) != L_CURLY && p.nth(1) != MOVE_KW && p.nth(1) != PIPE { - p.eat(ASYNC_KW); - has_mods = true; - } + // test_err unsafe_block_in_mod // fn foo(){} unsafe { } fn bar(){} if p.at(UNSAFE_KW) && p.nth(1) != L_CURLY { p.eat(UNSAFE_KW); has_mods = true; } + + // test_err async_without_semicolon + // fn foo() { let _ = async {} } + if p.at(ASYNC_KW) && p.nth(1) != L_CURLY && p.nth(1) != MOVE_KW && p.nth(1) != PIPE { + p.eat(ASYNC_KW); + has_mods = true; + } + if p.at(EXTERN_KW) { has_mods = true; abi(p); @@ -124,6 +127,14 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker, flavor: ItemFlavor) -> Resul // test unsafe_fn // unsafe fn foo() {} + + // test combined_fns + // unsafe async fn foo() {} + // const unsafe fn bar() {} + + // test_err wrong_order_fns + // async unsafe fn foo() {} + // unsafe const fn bar() {} FN_KW => { fn_def(p, flavor); m.complete(p, FN_DEF); diff --git a/crates/ra_syntax/tests/data/parser/inline/err/0010_wrong_order_fns.rs b/crates/ra_syntax/tests/data/parser/inline/err/0010_wrong_order_fns.rs new file mode 100644 index 000000000..16edee95d --- /dev/null +++ b/crates/ra_syntax/tests/data/parser/inline/err/0010_wrong_order_fns.rs @@ -0,0 +1,2 @@ +async unsafe fn foo() {} +unsafe const fn bar() {} diff --git a/crates/ra_syntax/tests/data/parser/inline/err/0010_wrong_order_fns.txt b/crates/ra_syntax/tests/data/parser/inline/err/0010_wrong_order_fns.txt new file mode 100644 index 000000000..220191ffa --- /dev/null +++ b/crates/ra_syntax/tests/data/parser/inline/err/0010_wrong_order_fns.txt @@ -0,0 +1,39 @@ +SOURCE_FILE@[0; 50) + ERROR@[0; 5) + ASYNC_KW@[0; 5) "async" + err: `expected fn, trait or impl` + WHITESPACE@[5; 6) " " + FN_DEF@[6; 24) + UNSAFE_KW@[6; 12) "unsafe" + WHITESPACE@[12; 13) " " + FN_KW@[13; 15) "fn" + WHITESPACE@[15; 16) " " + NAME@[16; 19) + IDENT@[16; 19) "foo" + PARAM_LIST@[19; 21) + L_PAREN@[19; 20) "(" + R_PAREN@[20; 21) ")" + WHITESPACE@[21; 22) " " + BLOCK@[22; 24) + L_CURLY@[22; 23) "{" + R_CURLY@[23; 24) "}" + WHITESPACE@[24; 25) "\n" + ERROR@[25; 31) + UNSAFE_KW@[25; 31) "unsafe" + err: `expected fn, trait or impl` + WHITESPACE@[31; 32) " " + FN_DEF@[32; 49) + CONST_KW@[32; 37) "const" + WHITESPACE@[37; 38) " " + FN_KW@[38; 40) "fn" + WHITESPACE@[40; 41) " " + NAME@[41; 44) + IDENT@[41; 44) "bar" + PARAM_LIST@[44; 46) + L_PAREN@[44; 45) "(" + R_PAREN@[45; 46) ")" + WHITESPACE@[46; 47) " " + BLOCK@[47; 49) + L_CURLY@[47; 48) "{" + R_CURLY@[48; 49) "}" + WHITESPACE@[49; 50) "\n" diff --git a/crates/ra_syntax/tests/data/parser/inline/ok/0128_combined_fns.rs b/crates/ra_syntax/tests/data/parser/inline/ok/0128_combined_fns.rs new file mode 100644 index 000000000..46af91b82 --- /dev/null +++ b/crates/ra_syntax/tests/data/parser/inline/ok/0128_combined_fns.rs @@ -0,0 +1,2 @@ +unsafe async fn foo() {} +const unsafe fn bar() {} diff --git a/crates/ra_syntax/tests/data/parser/inline/ok/0128_combined_fns.txt b/crates/ra_syntax/tests/data/parser/inline/ok/0128_combined_fns.txt new file mode 100644 index 000000000..2a16aeb61 --- /dev/null +++ b/crates/ra_syntax/tests/data/parser/inline/ok/0128_combined_fns.txt @@ -0,0 +1,35 @@ +SOURCE_FILE@[0; 50) + FN_DEF@[0; 24) + UNSAFE_KW@[0; 6) "unsafe" + WHITESPACE@[6; 7) " " + ASYNC_KW@[7; 12) "async" + WHITESPACE@[12; 13) " " + FN_KW@[13; 15) "fn" + WHITESPACE@[15; 16) " " + NAME@[16; 19) + IDENT@[16; 19) "foo" + PARAM_LIST@[19; 21) + L_PAREN@[19; 20) "(" + R_PAREN@[20; 21) ")" + WHITESPACE@[21; 22) " " + BLOCK@[22; 24) + L_CURLY@[22; 23) "{" + R_CURLY@[23; 24) "}" + WHITESPACE@[24; 25) "\n" + FN_DEF@[25; 49) + CONST_KW@[25; 30) "const" + WHITESPACE@[30; 31) " " + UNSAFE_KW@[31; 37) "unsafe" + WHITESPACE@[37; 38) " " + FN_KW@[38; 40) "fn" + WHITESPACE@[40; 41) " " + NAME@[41; 44) + IDENT@[41; 44) "bar" + PARAM_LIST@[44; 46) + L_PAREN@[44; 45) "(" + R_PAREN@[45; 46) ")" + WHITESPACE@[46; 47) " " + BLOCK@[47; 49) + L_CURLY@[47; 48) "{" + R_CURLY@[48; 49) "}" + WHITESPACE@[49; 50) "\n" -- cgit v1.2.3