Skip to content

Commit a2ae2bb

Browse files
bors[bot]matklad
andauthored
Merge #4261
4261: Fix parsing of blocks without `{` r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents fb8fb65 + 359d3be commit a2ae2bb

File tree

11 files changed

+107
-62
lines changed

11 files changed

+107
-62
lines changed

crates/ra_parser/src/grammar.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub(crate) mod fragments {
5454
use super::*;
5555

5656
pub(crate) use super::{
57-
expressions::block, paths::type_path as path, patterns::pattern, types::type_,
57+
expressions::block_expr, paths::type_path as path, patterns::pattern, types::type_,
5858
};
5959

6060
pub(crate) fn expr(p: &mut Parser) {
@@ -143,7 +143,7 @@ pub(crate) fn reparser(
143143
parent: Option<SyntaxKind>,
144144
) -> Option<fn(&mut Parser)> {
145145
let res = match node {
146-
BLOCK_EXPR => expressions::block,
146+
BLOCK_EXPR => expressions::block_expr,
147147
RECORD_FIELD_DEF_LIST => items::record_field_def_list,
148148
RECORD_FIELD_LIST => items::record_field_list,
149149
ENUM_VARIANT_LIST => items::enum_variant_list,

crates/ra_parser/src/grammar/expressions.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
mod atom;
44

5-
pub(crate) use self::atom::match_arm_list;
5+
pub(crate) use self::atom::{block_expr, match_arm_list};
66
pub(super) use self::atom::{literal, LITERAL_FIRST};
77
use super::*;
88

@@ -49,19 +49,6 @@ fn expr_no_struct(p: &mut Parser) {
4949
expr_bp(p, r, 1);
5050
}
5151

52-
// test block
53-
// fn a() {}
54-
// fn b() { let _ = 1; }
55-
// fn c() { 1; 2; }
56-
// fn d() { 1; 2 }
57-
pub(crate) fn block(p: &mut Parser) {
58-
if !p.at(T!['{']) {
59-
p.error("expected a block");
60-
return;
61-
}
62-
atom::block_expr(p);
63-
}
64-
6552
fn is_expr_stmt_attr_allowed(kind: SyntaxKind) -> bool {
6653
match kind {
6754
BIN_EXPR | RANGE_EXPR | IF_EXPR => false,

crates/ra_parser/src/grammar/expressions/atom.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar
132132
// break;
133133
// }
134134
// }
135-
block_expr(p)
135+
block_expr_unchecked(p)
136136
}
137137
T![return] => return_expr(p),
138138
T![continue] => continue_expr(p),
@@ -240,13 +240,9 @@ fn lambda_expr(p: &mut Parser) -> CompletedMarker {
240240
p.eat(T![move]);
241241
params::param_list_closure(p);
242242
if opt_fn_ret_type(p) {
243-
if p.at(T!['{']) {
244-
// test lambda_ret_block
245-
// fn main() { || -> i32 { 92 }(); }
246-
block_expr(p);
247-
} else {
248-
p.error("expected `{`");
249-
}
243+
// test lambda_ret_block
244+
// fn main() { || -> i32 { 92 }(); }
245+
block_expr(p);
250246
} else {
251247
if p.at_ts(EXPR_FIRST) {
252248
expr(p);
@@ -270,13 +266,13 @@ fn if_expr(p: &mut Parser) -> CompletedMarker {
270266
let m = p.start();
271267
p.bump(T![if]);
272268
cond(p);
273-
block(p);
269+
block_expr(p);
274270
if p.at(T![else]) {
275271
p.bump(T![else]);
276272
if p.at(T![if]) {
277273
if_expr(p);
278274
} else {
279-
block(p);
275+
block_expr(p);
280276
}
281277
}
282278
m.complete(p, IF_EXPR)
@@ -304,7 +300,7 @@ fn loop_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
304300
assert!(p.at(T![loop]));
305301
let m = m.unwrap_or_else(|| p.start());
306302
p.bump(T![loop]);
307-
block(p);
303+
block_expr(p);
308304
m.complete(p, LOOP_EXPR)
309305
}
310306

@@ -319,7 +315,7 @@ fn while_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
319315
let m = m.unwrap_or_else(|| p.start());
320316
p.bump(T![while]);
321317
cond(p);
322-
block(p);
318+
block_expr(p);
323319
m.complete(p, WHILE_EXPR)
324320
}
325321

@@ -334,7 +330,7 @@ fn for_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
334330
patterns::pattern(p);
335331
p.expect(T![in]);
336332
expr_no_struct(p);
337-
block(p);
333+
block_expr(p);
338334
m.complete(p, FOR_EXPR)
339335
}
340336

@@ -467,11 +463,20 @@ fn match_guard(p: &mut Parser) -> CompletedMarker {
467463
m.complete(p, MATCH_GUARD)
468464
}
469465

470-
// test block_expr
471-
// fn foo() {
472-
// {};
473-
// }
474-
pub(super) fn block_expr(p: &mut Parser) -> CompletedMarker {
466+
// test block
467+
// fn a() {}
468+
// fn b() { let _ = 1; }
469+
// fn c() { 1; 2; }
470+
// fn d() { 1; 2 }
471+
pub(crate) fn block_expr(p: &mut Parser) {
472+
if !p.at(T!['{']) {
473+
p.error("expected a block");
474+
return;
475+
}
476+
block_expr_unchecked(p);
477+
}
478+
479+
fn block_expr_unchecked(p: &mut Parser) -> CompletedMarker {
475480
assert!(p.at(T!['{']));
476481
let m = p.start();
477482
p.bump(T!['{']);

crates/ra_parser/src/grammar/items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ fn fn_def(p: &mut Parser) {
329329
if p.at(T![;]) {
330330
p.bump(T![;]);
331331
} else {
332-
expressions::block(p)
332+
expressions::block_expr(p)
333333
}
334334
}
335335

crates/ra_parser/src/grammar/type_args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn type_arg(p: &mut Parser) {
4848
m.complete(p, ASSOC_TYPE_ARG);
4949
}
5050
T!['{'] => {
51-
expressions::block(p);
51+
expressions::block_expr(p);
5252
m.complete(p, CONST_ARG);
5353
}
5454
k if k.is_literal() => {

crates/ra_parser/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub fn parse_fragment(
112112
FragmentKind::Type => grammar::fragments::type_,
113113
FragmentKind::Pattern => grammar::fragments::pattern,
114114
FragmentKind::Item => grammar::fragments::item,
115-
FragmentKind::Block => grammar::fragments::block,
115+
FragmentKind::Block => grammar::fragments::block_expr,
116116
FragmentKind::Visibility => grammar::fragments::opt_visibility,
117117
FragmentKind::MetaItem => grammar::fragments::meta_item,
118118
FragmentKind::Statement => grammar::fragments::stmt,

crates/ra_syntax/test_data/parser/err/0010_unsafe_lambda_block.rast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ SOURCE_FILE@0..42
4040
WHITESPACE@39..40 "\n"
4141
R_CURLY@40..41 "}"
4242
WHITESPACE@41..42 "\n"
43-
error 24..24: expected `{`
43+
error 24..24: expected a block
4444
error 24..24: expected SEMICOLON
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
SOURCE_FILE@0..83
2+
FN_DEF@0..82
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..82
12+
L_CURLY@10..11 "{"
13+
WHITESPACE@11..16 "\n "
14+
EXPR_STMT@16..29
15+
BLOCK_EXPR@16..29
16+
L_CURLY@16..17 "{"
17+
WHITESPACE@17..18 " "
18+
ERROR@18..24
19+
UNSAFE_KW@18..24 "unsafe"
20+
WHITESPACE@24..25 " "
21+
LITERAL@25..27
22+
INT_NUMBER@25..27 "92"
23+
WHITESPACE@27..28 " "
24+
R_CURLY@28..29 "}"
25+
WHITESPACE@29..34 "\n "
26+
EXPR_STMT@34..46
27+
BLOCK_EXPR@34..46
28+
L_CURLY@34..35 "{"
29+
WHITESPACE@35..36 " "
30+
ERROR@36..41
31+
ASYNC_KW@36..41 "async"
32+
WHITESPACE@41..42 " "
33+
LITERAL@42..44
34+
INT_NUMBER@42..44 "92"
35+
WHITESPACE@44..45 " "
36+
R_CURLY@45..46 "}"
37+
WHITESPACE@46..51 "\n "
38+
EXPR_STMT@51..61
39+
BLOCK_EXPR@51..61
40+
L_CURLY@51..52 "{"
41+
WHITESPACE@52..53 " "
42+
EXPR_STMT@53..56
43+
EFFECT_EXPR@53..56
44+
TRY_KW@53..56 "try"
45+
WHITESPACE@56..57 " "
46+
LITERAL@57..59
47+
INT_NUMBER@57..59 "92"
48+
WHITESPACE@59..60 " "
49+
R_CURLY@60..61 "}"
50+
WHITESPACE@61..66 "\n "
51+
BLOCK_EXPR@66..80
52+
L_CURLY@66..67 "{"
53+
WHITESPACE@67..68 " "
54+
EXPR_STMT@68..75
55+
ERROR@68..75
56+
LABEL@68..75
57+
LIFETIME@68..74 "\'label"
58+
COLON@74..75 ":"
59+
WHITESPACE@75..76 " "
60+
LITERAL@76..78
61+
INT_NUMBER@76..78 "92"
62+
WHITESPACE@78..79 " "
63+
R_CURLY@79..80 "}"
64+
WHITESPACE@80..81 "\n"
65+
R_CURLY@81..82 "}"
66+
WHITESPACE@82..83 "\n"
67+
error 24..24: expected existential, fn, trait or impl
68+
error 41..41: expected existential, fn, trait or impl
69+
error 56..56: expected a block
70+
error 75..75: expected a loop
71+
error 75..75: expected SEMICOLON
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
{ unsafe 92 }
3+
{ async 92 }
4+
{ try 92 }
5+
{ 'label: 92 }
6+
}

crates/ra_syntax/test_data/parser/inline/ok/0105_block_expr.rast

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)