1
- use syntax:: parse:: classify;
2
1
use syntax:: source_map:: Span ;
3
2
use syntax:: { ast, ptr} ;
4
3
@@ -25,7 +24,7 @@ use crate::utils::{last_line_width, left_most_sub_expr, stmt_expr, NodeIdExt};
25
24
26
25
pub ( crate ) fn rewrite_closure (
27
26
capture : ast:: CaptureBy ,
28
- asyncness : ast:: IsAsync ,
27
+ is_async : & ast:: IsAsync ,
29
28
movability : ast:: Movability ,
30
29
fn_decl : & ast:: FnDecl ,
31
30
body : & ast:: Expr ,
@@ -36,7 +35,7 @@ pub(crate) fn rewrite_closure(
36
35
debug ! ( "rewrite_closure {:?}" , body) ;
37
36
38
37
let ( prefix, extra_offset) = rewrite_closure_fn_decl (
39
- capture, asyncness , movability, fn_decl, body, span, context, shape,
38
+ capture, is_async , movability, fn_decl, body, span, context, shape,
40
39
) ?;
41
40
// 1 = space between `|...|` and body.
42
41
let body_shape = shape. offset_left ( extra_offset) ?;
@@ -134,7 +133,7 @@ fn rewrite_closure_with_block(
134
133
shape : Shape ,
135
134
) -> Option < String > {
136
135
let left_most = left_most_sub_expr ( body) ;
137
- let veto_block = veto_block ( body) && !classify :: expr_requires_semi_to_be_stmt ( left_most) ;
136
+ let veto_block = veto_block ( body) && !expr_requires_semi_to_be_stmt ( left_most) ;
138
137
if veto_block {
139
138
return None ;
140
139
}
@@ -207,7 +206,7 @@ fn rewrite_closure_block(
207
206
// Return type is (prefix, extra_offset)
208
207
fn rewrite_closure_fn_decl (
209
208
capture : ast:: CaptureBy ,
210
- asyncness : ast:: IsAsync ,
209
+ asyncness : & ast:: IsAsync ,
211
210
movability : ast:: Movability ,
212
211
fn_decl : & ast:: FnDecl ,
213
212
body : & ast:: Expr ,
@@ -291,7 +290,7 @@ pub(crate) fn rewrite_last_closure(
291
290
expr : & ast:: Expr ,
292
291
shape : Shape ,
293
292
) -> Option < String > {
294
- if let ast:: ExprKind :: Closure ( capture, asyncness , movability, ref fn_decl, ref body, _) =
293
+ if let ast:: ExprKind :: Closure ( capture, ref is_async , movability, ref fn_decl, ref body, _) =
295
294
expr. node
296
295
{
297
296
let body = match body. node {
@@ -305,7 +304,7 @@ pub(crate) fn rewrite_last_closure(
305
304
_ => body,
306
305
} ;
307
306
let ( prefix, extra_offset) = rewrite_closure_fn_decl (
308
- capture, asyncness , movability, fn_decl, body, expr. span , context, shape,
307
+ capture, is_async , movability, fn_decl, body, expr. span , context, shape,
309
308
) ?;
310
309
// If the closure goes multi line before its body, do not overflow the closure.
311
310
if prefix. contains ( '\n' ) {
@@ -387,3 +386,26 @@ fn is_block_closure_forced_inner(expr: &ast::Expr, version: Version) -> bool {
387
386
_ => false ,
388
387
}
389
388
}
389
+
390
+ /// Does this expression require a semicolon to be treated
391
+ /// as a statement? The negation of this: 'can this expression
392
+ /// be used as a statement without a semicolon' -- is used
393
+ /// as an early-bail-out in the parser so that, for instance,
394
+ /// if true {...} else {...}
395
+ /// |x| 5
396
+ /// isn't parsed as (if true {...} else {...} | x) | 5
397
+ // From https://github.com/rust-lang/rust/blob/master/src/libsyntax/parse/classify.rs.
398
+ fn expr_requires_semi_to_be_stmt ( e : & ast:: Expr ) -> bool {
399
+ match e. node {
400
+ ast:: ExprKind :: If ( ..)
401
+ | ast:: ExprKind :: IfLet ( ..)
402
+ | ast:: ExprKind :: Match ( ..)
403
+ | ast:: ExprKind :: Block ( ..)
404
+ | ast:: ExprKind :: While ( ..)
405
+ | ast:: ExprKind :: WhileLet ( ..)
406
+ | ast:: ExprKind :: Loop ( ..)
407
+ | ast:: ExprKind :: ForLoop ( ..)
408
+ | ast:: ExprKind :: TryBlock ( ..) => false ,
409
+ _ => true ,
410
+ }
411
+ }
0 commit comments