Skip to content

Commit 28bb16a

Browse files
committed
add a support for immovable generators
1 parent 7d63490 commit 28bb16a

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

src/closures.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use utils::{last_line_width, left_most_sub_expr, stmt_expr};
3333

3434
pub fn rewrite_closure(
3535
capture: ast::CaptureBy,
36+
movability: ast::Movability,
3637
fn_decl: &ast::FnDecl,
3738
body: &ast::Expr,
3839
span: Span,
@@ -42,7 +43,7 @@ pub fn rewrite_closure(
4243
debug!("rewrite_closure {:?}", body);
4344

4445
let (prefix, extra_offset) =
45-
rewrite_closure_fn_decl(capture, fn_decl, body, span, context, shape)?;
46+
rewrite_closure_fn_decl(capture, movability, fn_decl, body, span, context, shape)?;
4647
// 1 = space between `|...|` and body.
4748
let body_shape = shape.offset_left(extra_offset)?;
4849

@@ -194,6 +195,7 @@ fn rewrite_closure_block(
194195
// Return type is (prefix, extra_offset)
195196
fn rewrite_closure_fn_decl(
196197
capture: ast::CaptureBy,
198+
movability: ast::Movability,
197199
fn_decl: &ast::FnDecl,
198200
body: &ast::Expr,
199201
span: Span,
@@ -205,9 +207,17 @@ fn rewrite_closure_fn_decl(
205207
} else {
206208
""
207209
};
210+
211+
let immovable = if movability == ast::Movability::Static {
212+
"static "
213+
} else {
214+
""
215+
};
208216
// 4 = "|| {".len(), which is overconservative when the closure consists of
209217
// a single expression.
210-
let nested_shape = shape.shrink_left(mover.len())?.sub_width(4)?;
218+
let nested_shape = shape
219+
.shrink_left(mover.len() + immovable.len())?
220+
.sub_width(4)?;
211221

212222
// 1 = |
213223
let argument_offset = nested_shape.indent + 1;
@@ -254,7 +264,7 @@ fn rewrite_closure_fn_decl(
254264
config: context.config,
255265
};
256266
let list_str = write_list(&item_vec, &fmt)?;
257-
let mut prefix = format!("{}|{}|", mover, list_str);
267+
let mut prefix = format!("{}{}|{}|", immovable, mover, list_str);
258268

259269
if !ret_str.is_empty() {
260270
if prefix.contains('\n') {
@@ -278,7 +288,7 @@ pub fn rewrite_last_closure(
278288
expr: &ast::Expr,
279289
shape: Shape,
280290
) -> Option<String> {
281-
if let ast::ExprKind::Closure(capture, _, ref fn_decl, ref body, _) = expr.node {
291+
if let ast::ExprKind::Closure(capture, movability, ref fn_decl, ref body, _) = expr.node {
282292
let body = match body.node {
283293
ast::ExprKind::Block(ref block)
284294
if !is_unsafe_block(block) && is_simple_block(block, context.codemap) =>
@@ -287,8 +297,15 @@ pub fn rewrite_last_closure(
287297
}
288298
_ => body,
289299
};
290-
let (prefix, extra_offset) =
291-
rewrite_closure_fn_decl(capture, fn_decl, body, expr.span, context, shape)?;
300+
let (prefix, extra_offset) = rewrite_closure_fn_decl(
301+
capture,
302+
movability,
303+
fn_decl,
304+
body,
305+
expr.span,
306+
context,
307+
shape,
308+
)?;
292309
// If the closure goes multi line before its body, do not overflow the closure.
293310
if prefix.contains('\n') {
294311
return None;

src/expr.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,16 @@ pub fn format_expr(
159159
} else {
160160
Some("yield".to_string())
161161
},
162-
ast::ExprKind::Closure(capture, _, ref fn_decl, ref body, _) => {
163-
closures::rewrite_closure(capture, fn_decl, body, expr.span, context, shape)
162+
ast::ExprKind::Closure(capture, movability, ref fn_decl, ref body, _) => {
163+
closures::rewrite_closure(
164+
capture,
165+
movability,
166+
fn_decl,
167+
body,
168+
expr.span,
169+
context,
170+
shape,
171+
)
164172
}
165173
ast::ExprKind::Try(..)
166174
| ast::ExprKind::Field(..)

tests/source/immovable_generators.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![feature(generators)]
2+
3+
unsafe fn foo() {
4+
let mut ga = static || {
5+
yield 1;
6+
};
7+
}

tests/target/immovable_generators.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![feature(generators)]
2+
3+
unsafe fn foo() {
4+
let mut ga = static || {
5+
yield 1;
6+
};
7+
}

0 commit comments

Comments
 (0)