From c3ac2c93fb446329b09882ef452fd6820290bfc5 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sun, 12 Jan 2020 10:19:17 -0500 Subject: Allow attributes before function arguments This adds support for function calls of the form: ```rust ( #[attr(...)] 1.2, #[attr_one(...)] #[attr_two(...)] 1.5, ... etc ... ) ``` Closes https://github.com/rust-analyzer/rust-analyzer/issues/2801 --- crates/ra_parser/src/grammar/expressions.rs | 11 +++ .../test_data/parser/inline/ok/0152_arg_list.rs | 9 ++ .../test_data/parser/inline/ok/0152_arg_list.txt | 106 +++++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 crates/ra_syntax/test_data/parser/inline/ok/0152_arg_list.rs create mode 100644 crates/ra_syntax/test_data/parser/inline/ok/0152_arg_list.txt diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs index 81d4f75f9..7caed66a0 100644 --- a/crates/ra_parser/src/grammar/expressions.rs +++ b/crates/ra_parser/src/grammar/expressions.rs @@ -535,11 +535,22 @@ fn cast_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { m.complete(p, CAST_EXPR) } +// test arg_list +// fn assert_float(s: &str, n: f64) {} +// fn foo() { +// assert_float( +// "1.797693134862315708e+308L", +// #[allow(clippy::excessive_precision)] +// #[allow(dead_code)] +// 1.797_693_134_862_315_730_8e+308, +// ); +// } fn arg_list(p: &mut Parser) { assert!(p.at(T!['('])); let m = p.start(); p.bump(T!['(']); while !p.at(T![')']) && !p.at(EOF) { + attributes::outer_attributes(p); if !p.at_ts(EXPR_FIRST) { p.error("expected expression"); break; diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0152_arg_list.rs b/crates/ra_syntax/test_data/parser/inline/ok/0152_arg_list.rs new file mode 100644 index 000000000..a4009b392 --- /dev/null +++ b/crates/ra_syntax/test_data/parser/inline/ok/0152_arg_list.rs @@ -0,0 +1,9 @@ +fn assert_float(s: &str, n: f64) {} +fn foo() { + assert_float( + "1.797693134862315708e+308L", + #[allow(clippy::excessive_precision)] + #[allow(dead_code)] + 1.797_693_134_862_315_730_8e+308, + ); +} diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0152_arg_list.txt b/crates/ra_syntax/test_data/parser/inline/ok/0152_arg_list.txt new file mode 100644 index 000000000..6b1992dac --- /dev/null +++ b/crates/ra_syntax/test_data/parser/inline/ok/0152_arg_list.txt @@ -0,0 +1,106 @@ +SOURCE_FILE@[0; 228) + FN_DEF@[0; 35) + FN_KW@[0; 2) "fn" + WHITESPACE@[2; 3) " " + NAME@[3; 15) + IDENT@[3; 15) "assert_float" + PARAM_LIST@[15; 32) + L_PAREN@[15; 16) "(" + PARAM@[16; 23) + BIND_PAT@[16; 17) + NAME@[16; 17) + IDENT@[16; 17) "s" + COLON@[17; 18) ":" + WHITESPACE@[18; 19) " " + REFERENCE_TYPE@[19; 23) + AMP@[19; 20) "&" + PATH_TYPE@[20; 23) + PATH@[20; 23) + PATH_SEGMENT@[20; 23) + NAME_REF@[20; 23) + IDENT@[20; 23) "str" + COMMA@[23; 24) "," + WHITESPACE@[24; 25) " " + PARAM@[25; 31) + BIND_PAT@[25; 26) + NAME@[25; 26) + IDENT@[25; 26) "n" + COLON@[26; 27) ":" + WHITESPACE@[27; 28) " " + PATH_TYPE@[28; 31) + PATH@[28; 31) + PATH_SEGMENT@[28; 31) + NAME_REF@[28; 31) + IDENT@[28; 31) "f64" + R_PAREN@[31; 32) ")" + WHITESPACE@[32; 33) " " + BLOCK_EXPR@[33; 35) + BLOCK@[33; 35) + L_CURLY@[33; 34) "{" + R_CURLY@[34; 35) "}" + WHITESPACE@[35; 36) "\n" + FN_DEF@[36; 227) + FN_KW@[36; 38) "fn" + WHITESPACE@[38; 39) " " + NAME@[39; 42) + IDENT@[39; 42) "foo" + PARAM_LIST@[42; 44) + L_PAREN@[42; 43) "(" + R_PAREN@[43; 44) ")" + WHITESPACE@[44; 45) " " + BLOCK_EXPR@[45; 227) + BLOCK@[45; 227) + L_CURLY@[45; 46) "{" + WHITESPACE@[46; 51) "\n " + EXPR_STMT@[51; 225) + CALL_EXPR@[51; 224) + PATH_EXPR@[51; 63) + PATH@[51; 63) + PATH_SEGMENT@[51; 63) + NAME_REF@[51; 63) + IDENT@[51; 63) "assert_float" + ARG_LIST@[63; 224) + L_PAREN@[63; 64) "(" + WHITESPACE@[64; 73) "\n " + LITERAL@[73; 101) + STRING@[73; 101) "\"1.797693134862315708 ..." + COMMA@[101; 102) "," + WHITESPACE@[102; 111) "\n " + ATTR@[111; 148) + POUND@[111; 112) "#" + L_BRACK@[112; 113) "[" + PATH@[113; 118) + PATH_SEGMENT@[113; 118) + NAME_REF@[113; 118) + IDENT@[113; 118) "allow" + TOKEN_TREE@[118; 147) + L_PAREN@[118; 119) "(" + IDENT@[119; 125) "clippy" + COLON@[125; 126) ":" + COLON@[126; 127) ":" + IDENT@[127; 146) "excessive_precision" + R_PAREN@[146; 147) ")" + R_BRACK@[147; 148) "]" + WHITESPACE@[148; 157) "\n " + ATTR@[157; 176) + POUND@[157; 158) "#" + L_BRACK@[158; 159) "[" + PATH@[159; 164) + PATH_SEGMENT@[159; 164) + NAME_REF@[159; 164) + IDENT@[159; 164) "allow" + TOKEN_TREE@[164; 175) + L_PAREN@[164; 165) "(" + IDENT@[165; 174) "dead_code" + R_PAREN@[174; 175) ")" + R_BRACK@[175; 176) "]" + WHITESPACE@[176; 185) "\n " + LITERAL@[185; 217) + FLOAT_NUMBER@[185; 217) "1.797_693_134_862_315 ..." + COMMA@[217; 218) "," + WHITESPACE@[218; 223) "\n " + R_PAREN@[223; 224) ")" + SEMI@[224; 225) ";" + WHITESPACE@[225; 226) "\n" + R_CURLY@[226; 227) "}" + WHITESPACE@[227; 228) "\n" -- cgit v1.2.3 From c78e34968ff24bf8b195ea02be54835a09f53abd Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Wed, 15 Jan 2020 19:12:56 -0500 Subject: shrink inline tes --- crates/ra_parser/src/grammar/expressions.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs index 7caed66a0..d8105c382 100644 --- a/crates/ra_parser/src/grammar/expressions.rs +++ b/crates/ra_parser/src/grammar/expressions.rs @@ -535,15 +535,9 @@ fn cast_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { m.complete(p, CAST_EXPR) } -// test arg_list -// fn assert_float(s: &str, n: f64) {} -// fn foo() { -// assert_float( -// "1.797693134862315708e+308L", -// #[allow(clippy::excessive_precision)] -// #[allow(dead_code)] -// 1.797_693_134_862_315_730_8e+308, -// ); +// test arg_with_attr +// fn main() { +// foo(#[attr] 92) // } fn arg_list(p: &mut Parser) { assert!(p.at(T!['('])); -- cgit v1.2.3 From f077d5c303ecc4f38471b37be985bb0c5ab2f68c Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Thu, 16 Jan 2020 22:20:17 -0500 Subject: move inline function closer to relevant code also updates generated inline tests --- crates/ra_parser/src/grammar/expressions.rs | 8 +- .../test_data/parser/inline/ok/0152_arg_list.rs | 9 -- .../test_data/parser/inline/ok/0152_arg_list.txt | 106 --------------------- .../parser/inline/ok/0152_arg_with_attr.rs | 3 + .../parser/inline/ok/0152_arg_with_attr.txt | 37 +++++++ 5 files changed, 44 insertions(+), 119 deletions(-) delete mode 100644 crates/ra_syntax/test_data/parser/inline/ok/0152_arg_list.rs delete mode 100644 crates/ra_syntax/test_data/parser/inline/ok/0152_arg_list.txt create mode 100644 crates/ra_syntax/test_data/parser/inline/ok/0152_arg_with_attr.rs create mode 100644 crates/ra_syntax/test_data/parser/inline/ok/0152_arg_with_attr.txt diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs index d8105c382..95564b602 100644 --- a/crates/ra_parser/src/grammar/expressions.rs +++ b/crates/ra_parser/src/grammar/expressions.rs @@ -535,15 +535,15 @@ fn cast_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { m.complete(p, CAST_EXPR) } -// test arg_with_attr -// fn main() { -// foo(#[attr] 92) -// } fn arg_list(p: &mut Parser) { assert!(p.at(T!['('])); let m = p.start(); p.bump(T!['(']); while !p.at(T![')']) && !p.at(EOF) { + // test arg_with_attr + // fn main() { + // foo(#[attr] 92) + // } attributes::outer_attributes(p); if !p.at_ts(EXPR_FIRST) { p.error("expected expression"); diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0152_arg_list.rs b/crates/ra_syntax/test_data/parser/inline/ok/0152_arg_list.rs deleted file mode 100644 index a4009b392..000000000 --- a/crates/ra_syntax/test_data/parser/inline/ok/0152_arg_list.rs +++ /dev/null @@ -1,9 +0,0 @@ -fn assert_float(s: &str, n: f64) {} -fn foo() { - assert_float( - "1.797693134862315708e+308L", - #[allow(clippy::excessive_precision)] - #[allow(dead_code)] - 1.797_693_134_862_315_730_8e+308, - ); -} diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0152_arg_list.txt b/crates/ra_syntax/test_data/parser/inline/ok/0152_arg_list.txt deleted file mode 100644 index 6b1992dac..000000000 --- a/crates/ra_syntax/test_data/parser/inline/ok/0152_arg_list.txt +++ /dev/null @@ -1,106 +0,0 @@ -SOURCE_FILE@[0; 228) - FN_DEF@[0; 35) - FN_KW@[0; 2) "fn" - WHITESPACE@[2; 3) " " - NAME@[3; 15) - IDENT@[3; 15) "assert_float" - PARAM_LIST@[15; 32) - L_PAREN@[15; 16) "(" - PARAM@[16; 23) - BIND_PAT@[16; 17) - NAME@[16; 17) - IDENT@[16; 17) "s" - COLON@[17; 18) ":" - WHITESPACE@[18; 19) " " - REFERENCE_TYPE@[19; 23) - AMP@[19; 20) "&" - PATH_TYPE@[20; 23) - PATH@[20; 23) - PATH_SEGMENT@[20; 23) - NAME_REF@[20; 23) - IDENT@[20; 23) "str" - COMMA@[23; 24) "," - WHITESPACE@[24; 25) " " - PARAM@[25; 31) - BIND_PAT@[25; 26) - NAME@[25; 26) - IDENT@[25; 26) "n" - COLON@[26; 27) ":" - WHITESPACE@[27; 28) " " - PATH_TYPE@[28; 31) - PATH@[28; 31) - PATH_SEGMENT@[28; 31) - NAME_REF@[28; 31) - IDENT@[28; 31) "f64" - R_PAREN@[31; 32) ")" - WHITESPACE@[32; 33) " " - BLOCK_EXPR@[33; 35) - BLOCK@[33; 35) - L_CURLY@[33; 34) "{" - R_CURLY@[34; 35) "}" - WHITESPACE@[35; 36) "\n" - FN_DEF@[36; 227) - FN_KW@[36; 38) "fn" - WHITESPACE@[38; 39) " " - NAME@[39; 42) - IDENT@[39; 42) "foo" - PARAM_LIST@[42; 44) - L_PAREN@[42; 43) "(" - R_PAREN@[43; 44) ")" - WHITESPACE@[44; 45) " " - BLOCK_EXPR@[45; 227) - BLOCK@[45; 227) - L_CURLY@[45; 46) "{" - WHITESPACE@[46; 51) "\n " - EXPR_STMT@[51; 225) - CALL_EXPR@[51; 224) - PATH_EXPR@[51; 63) - PATH@[51; 63) - PATH_SEGMENT@[51; 63) - NAME_REF@[51; 63) - IDENT@[51; 63) "assert_float" - ARG_LIST@[63; 224) - L_PAREN@[63; 64) "(" - WHITESPACE@[64; 73) "\n " - LITERAL@[73; 101) - STRING@[73; 101) "\"1.797693134862315708 ..." - COMMA@[101; 102) "," - WHITESPACE@[102; 111) "\n " - ATTR@[111; 148) - POUND@[111; 112) "#" - L_BRACK@[112; 113) "[" - PATH@[113; 118) - PATH_SEGMENT@[113; 118) - NAME_REF@[113; 118) - IDENT@[113; 118) "allow" - TOKEN_TREE@[118; 147) - L_PAREN@[118; 119) "(" - IDENT@[119; 125) "clippy" - COLON@[125; 126) ":" - COLON@[126; 127) ":" - IDENT@[127; 146) "excessive_precision" - R_PAREN@[146; 147) ")" - R_BRACK@[147; 148) "]" - WHITESPACE@[148; 157) "\n " - ATTR@[157; 176) - POUND@[157; 158) "#" - L_BRACK@[158; 159) "[" - PATH@[159; 164) - PATH_SEGMENT@[159; 164) - NAME_REF@[159; 164) - IDENT@[159; 164) "allow" - TOKEN_TREE@[164; 175) - L_PAREN@[164; 165) "(" - IDENT@[165; 174) "dead_code" - R_PAREN@[174; 175) ")" - R_BRACK@[175; 176) "]" - WHITESPACE@[176; 185) "\n " - LITERAL@[185; 217) - FLOAT_NUMBER@[185; 217) "1.797_693_134_862_315 ..." - COMMA@[217; 218) "," - WHITESPACE@[218; 223) "\n " - R_PAREN@[223; 224) ")" - SEMI@[224; 225) ";" - WHITESPACE@[225; 226) "\n" - R_CURLY@[226; 227) "}" - WHITESPACE@[227; 228) "\n" diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0152_arg_with_attr.rs b/crates/ra_syntax/test_data/parser/inline/ok/0152_arg_with_attr.rs new file mode 100644 index 000000000..5daf1d7b0 --- /dev/null +++ b/crates/ra_syntax/test_data/parser/inline/ok/0152_arg_with_attr.rs @@ -0,0 +1,3 @@ +fn main() { + foo(#[attr] 92) +} diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0152_arg_with_attr.txt b/crates/ra_syntax/test_data/parser/inline/ok/0152_arg_with_attr.txt new file mode 100644 index 000000000..6b80ca8d0 --- /dev/null +++ b/crates/ra_syntax/test_data/parser/inline/ok/0152_arg_with_attr.txt @@ -0,0 +1,37 @@ +SOURCE_FILE@[0; 34) + FN_DEF@[0; 33) + FN_KW@[0; 2) "fn" + WHITESPACE@[2; 3) " " + NAME@[3; 7) + IDENT@[3; 7) "main" + PARAM_LIST@[7; 9) + L_PAREN@[7; 8) "(" + R_PAREN@[8; 9) ")" + WHITESPACE@[9; 10) " " + BLOCK_EXPR@[10; 33) + BLOCK@[10; 33) + L_CURLY@[10; 11) "{" + WHITESPACE@[11; 16) "\n " + CALL_EXPR@[16; 31) + PATH_EXPR@[16; 19) + PATH@[16; 19) + PATH_SEGMENT@[16; 19) + NAME_REF@[16; 19) + IDENT@[16; 19) "foo" + ARG_LIST@[19; 31) + L_PAREN@[19; 20) "(" + ATTR@[20; 27) + POUND@[20; 21) "#" + L_BRACK@[21; 22) "[" + PATH@[22; 26) + PATH_SEGMENT@[22; 26) + NAME_REF@[22; 26) + IDENT@[22; 26) "attr" + R_BRACK@[26; 27) "]" + WHITESPACE@[27; 28) " " + LITERAL@[28; 30) + INT_NUMBER@[28; 30) "92" + R_PAREN@[30; 31) ")" + WHITESPACE@[31; 32) "\n" + R_CURLY@[32; 33) "}" + WHITESPACE@[33; 34) "\n" -- cgit v1.2.3