diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-03-25 16:01:54 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-03-25 16:01:54 +0000 |
commit | 0f6fb0ec3b5ac5cd4a5e74c74813abec8257868b (patch) | |
tree | 2e5bc7a56752adfe4297daf712ad45ff8430b9a7 | |
parent | a69fc239257bff1cda54bc2070cb197d477bb563 (diff) | |
parent | f6188caaa0d226bef88418c9ff3f13a63ae95358 (diff) |
Merge #3722
3722: Fix parsing lambdas with return type r=matklad a=matklad
We should eat only a single block, and not whatever larger expression
may start with a block.
closes #3721
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
4 files changed, 75 insertions, 21 deletions
diff --git a/crates/ra_parser/src/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs index 2335d99b3..0d277a586 100644 --- a/crates/ra_parser/src/grammar/expressions/atom.rs +++ b/crates/ra_parser/src/grammar/expressions/atom.rs | |||
@@ -230,14 +230,20 @@ fn lambda_expr(p: &mut Parser) -> CompletedMarker { | |||
230 | p.eat(T![async]); | 230 | p.eat(T![async]); |
231 | p.eat(T![move]); | 231 | p.eat(T![move]); |
232 | params::param_list_closure(p); | 232 | params::param_list_closure(p); |
233 | if opt_fn_ret_type(p) && !p.at(T!['{']) { | 233 | if opt_fn_ret_type(p) { |
234 | p.error("expected `{`"); | 234 | if p.at(T!['{']) { |
235 | } | 235 | // test lambda_ret_block |
236 | 236 | // fn main() { || -> i32 { 92 }(); } | |
237 | if p.at_ts(EXPR_FIRST) { | 237 | block_expr(p, None); |
238 | expr(p); | 238 | } else { |
239 | p.error("expected `{`"); | ||
240 | } | ||
239 | } else { | 241 | } else { |
240 | p.error("expected expression"); | 242 | if p.at_ts(EXPR_FIRST) { |
243 | expr(p); | ||
244 | } else { | ||
245 | p.error("expected expression"); | ||
246 | } | ||
241 | } | 247 | } |
242 | m.complete(p, LAMBDA_EXPR) | 248 | m.complete(p, LAMBDA_EXPR) |
243 | } | 249 | } |
diff --git a/crates/ra_syntax/test_data/parser/err/0010_unsafe_lambda_block.txt b/crates/ra_syntax/test_data/parser/err/0010_unsafe_lambda_block.txt index e0edf6a2d..0ffbd25aa 100644 --- a/crates/ra_syntax/test_data/parser/err/0010_unsafe_lambda_block.txt +++ b/crates/ra_syntax/test_data/parser/err/0010_unsafe_lambda_block.txt | |||
@@ -12,8 +12,8 @@ SOURCE_FILE@[0; 42) | |||
12 | BLOCK@[10; 41) | 12 | BLOCK@[10; 41) |
13 | L_CURLY@[10; 11) "{" | 13 | L_CURLY@[10; 11) "{" |
14 | WHITESPACE@[11; 16) "\n " | 14 | WHITESPACE@[11; 16) "\n " |
15 | EXPR_STMT@[16; 39) | 15 | EXPR_STMT@[16; 24) |
16 | LAMBDA_EXPR@[16; 38) | 16 | LAMBDA_EXPR@[16; 24) |
17 | PARAM_LIST@[16; 18) | 17 | PARAM_LIST@[16; 18) |
18 | PIPE@[16; 17) "|" | 18 | PIPE@[16; 17) "|" |
19 | PIPE@[17; 18) "|" | 19 | PIPE@[17; 18) "|" |
@@ -24,20 +24,22 @@ SOURCE_FILE@[0; 42) | |||
24 | TUPLE_TYPE@[22; 24) | 24 | TUPLE_TYPE@[22; 24) |
25 | L_PAREN@[22; 23) "(" | 25 | L_PAREN@[22; 23) "(" |
26 | R_PAREN@[23; 24) ")" | 26 | R_PAREN@[23; 24) ")" |
27 | WHITESPACE@[24; 25) " " | 27 | WHITESPACE@[24; 25) " " |
28 | BLOCK_EXPR@[25; 38) | 28 | EXPR_STMT@[25; 39) |
29 | UNSAFE_KW@[25; 31) "unsafe" | 29 | BLOCK_EXPR@[25; 38) |
30 | WHITESPACE@[31; 32) " " | 30 | UNSAFE_KW@[25; 31) "unsafe" |
31 | BLOCK@[32; 38) | 31 | WHITESPACE@[31; 32) " " |
32 | L_CURLY@[32; 33) "{" | 32 | BLOCK@[32; 38) |
33 | WHITESPACE@[33; 34) " " | 33 | L_CURLY@[32; 33) "{" |
34 | TUPLE_EXPR@[34; 36) | 34 | WHITESPACE@[33; 34) " " |
35 | L_PAREN@[34; 35) "(" | 35 | TUPLE_EXPR@[34; 36) |
36 | R_PAREN@[35; 36) ")" | 36 | L_PAREN@[34; 35) "(" |
37 | WHITESPACE@[36; 37) " " | 37 | R_PAREN@[35; 36) ")" |
38 | R_CURLY@[37; 38) "}" | 38 | WHITESPACE@[36; 37) " " |
39 | R_CURLY@[37; 38) "}" | ||
39 | SEMI@[38; 39) ";" | 40 | SEMI@[38; 39) ";" |
40 | WHITESPACE@[39; 40) "\n" | 41 | WHITESPACE@[39; 40) "\n" |
41 | R_CURLY@[40; 41) "}" | 42 | R_CURLY@[40; 41) "}" |
42 | WHITESPACE@[41; 42) "\n" | 43 | WHITESPACE@[41; 42) "\n" |
43 | error [24; 24): expected `{` | 44 | error [24; 24): expected `{` |
45 | error [24; 24): expected SEMI | ||
diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0158_lambda_ret_block.rs b/crates/ra_syntax/test_data/parser/inline/ok/0158_lambda_ret_block.rs new file mode 100644 index 000000000..061118d3a --- /dev/null +++ b/crates/ra_syntax/test_data/parser/inline/ok/0158_lambda_ret_block.rs | |||
@@ -0,0 +1 @@ | |||
fn main() { || -> i32 { 92 }(); } | |||
diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0158_lambda_ret_block.txt b/crates/ra_syntax/test_data/parser/inline/ok/0158_lambda_ret_block.txt new file mode 100644 index 000000000..ba8779094 --- /dev/null +++ b/crates/ra_syntax/test_data/parser/inline/ok/0158_lambda_ret_block.txt | |||
@@ -0,0 +1,45 @@ | |||
1 | SOURCE_FILE@[0; 34) | ||
2 | FN_DEF@[0; 33) | ||
3 | FN_KW@[0; 2) "fn" | ||
4 | WHITESPACE@[2; 3) " " | ||
5 | NAME@[3; 7) | ||
6 | IDENT@[3; 7) "main" | ||
7 | PARAM_LIST@[7; 9) | ||
8 | L_PAREN@[7; 8) "(" | ||
9 | R_PAREN@[8; 9) ")" | ||
10 | WHITESPACE@[9; 10) " " | ||
11 | BLOCK_EXPR@[10; 33) | ||
12 | BLOCK@[10; 33) | ||
13 | L_CURLY@[10; 11) "{" | ||
14 | WHITESPACE@[11; 12) " " | ||
15 | EXPR_STMT@[12; 31) | ||
16 | CALL_EXPR@[12; 30) | ||
17 | LAMBDA_EXPR@[12; 28) | ||
18 | PARAM_LIST@[12; 14) | ||
19 | PIPE@[12; 13) "|" | ||
20 | PIPE@[13; 14) "|" | ||
21 | WHITESPACE@[14; 15) " " | ||
22 | RET_TYPE@[15; 21) | ||
23 | THIN_ARROW@[15; 17) "->" | ||
24 | WHITESPACE@[17; 18) " " | ||
25 | PATH_TYPE@[18; 21) | ||
26 | PATH@[18; 21) | ||
27 | PATH_SEGMENT@[18; 21) | ||
28 | NAME_REF@[18; 21) | ||
29 | IDENT@[18; 21) "i32" | ||
30 | WHITESPACE@[21; 22) " " | ||
31 | BLOCK_EXPR@[22; 28) | ||
32 | BLOCK@[22; 28) | ||
33 | L_CURLY@[22; 23) "{" | ||
34 | WHITESPACE@[23; 24) " " | ||
35 | LITERAL@[24; 26) | ||
36 | INT_NUMBER@[24; 26) "92" | ||
37 | WHITESPACE@[26; 27) " " | ||
38 | R_CURLY@[27; 28) "}" | ||
39 | ARG_LIST@[28; 30) | ||
40 | L_PAREN@[28; 29) "(" | ||
41 | R_PAREN@[29; 30) ")" | ||
42 | SEMI@[30; 31) ";" | ||
43 | WHITESPACE@[31; 32) " " | ||
44 | R_CURLY@[32; 33) "}" | ||
45 | WHITESPACE@[33; 34) "\n" | ||