@@ -11,6 +11,7 @@ use crate::overflow::OverflowableItem;
11
11
use crate :: rewrite:: { Rewrite , RewriteContext } ;
12
12
use crate :: shape:: Shape ;
13
13
use crate :: source_map:: SpanUtils ;
14
+ use crate :: types:: rewrite_lifetime_param;
14
15
use crate :: utils:: { last_line_width, left_most_sub_expr, stmt_expr, NodeIdExt } ;
15
16
16
17
// This module is pretty messy because of the rules around closures and blocks:
@@ -24,6 +25,7 @@ use crate::utils::{last_line_width, left_most_sub_expr, stmt_expr, NodeIdExt};
24
25
// can change whether it is treated as an expression or statement.
25
26
26
27
pub ( crate ) fn rewrite_closure (
28
+ binder : & ast:: ClosureBinder ,
27
29
capture : ast:: CaptureBy ,
28
30
is_async : & ast:: Async ,
29
31
movability : ast:: Movability ,
@@ -36,7 +38,7 @@ pub(crate) fn rewrite_closure(
36
38
debug ! ( "rewrite_closure {:?}" , body) ;
37
39
38
40
let ( prefix, extra_offset) = rewrite_closure_fn_decl (
39
- capture, is_async, movability, fn_decl, body, span, context, shape,
41
+ binder , capture, is_async, movability, fn_decl, body, span, context, shape,
40
42
) ?;
41
43
// 1 = space between `|...|` and body.
42
44
let body_shape = shape. offset_left ( extra_offset) ?;
@@ -227,6 +229,7 @@ fn rewrite_closure_block(
227
229
228
230
// Return type is (prefix, extra_offset)
229
231
fn rewrite_closure_fn_decl (
232
+ binder : & ast:: ClosureBinder ,
230
233
capture : ast:: CaptureBy ,
231
234
asyncness : & ast:: Async ,
232
235
movability : ast:: Movability ,
@@ -236,6 +239,17 @@ fn rewrite_closure_fn_decl(
236
239
context : & RewriteContext < ' _ > ,
237
240
shape : Shape ,
238
241
) -> Option < ( String , usize ) > {
242
+ let binder = match binder {
243
+ ast:: ClosureBinder :: For { generic_params, .. } if generic_params. is_empty ( ) => {
244
+ "for<> " . to_owned ( )
245
+ }
246
+ ast:: ClosureBinder :: For { generic_params, .. } => {
247
+ let lifetime_str = rewrite_lifetime_param ( context, shape, generic_params) ?;
248
+ format ! ( "for<{lifetime_str}> " )
249
+ }
250
+ ast:: ClosureBinder :: NotPresent => "" . to_owned ( ) ,
251
+ } ;
252
+
239
253
let immovable = if movability == ast:: Movability :: Static {
240
254
"static "
241
255
} else {
@@ -250,7 +264,7 @@ fn rewrite_closure_fn_decl(
250
264
// 4 = "|| {".len(), which is overconservative when the closure consists of
251
265
// a single expression.
252
266
let nested_shape = shape
253
- . shrink_left ( immovable. len ( ) + is_async. len ( ) + mover. len ( ) ) ?
267
+ . shrink_left ( binder . len ( ) + immovable. len ( ) + is_async. len ( ) + mover. len ( ) ) ?
254
268
. sub_width ( 4 ) ?;
255
269
256
270
// 1 = |
@@ -288,7 +302,7 @@ fn rewrite_closure_fn_decl(
288
302
. tactic ( tactic)
289
303
. preserve_newline ( true ) ;
290
304
let list_str = write_list ( & item_vec, & fmt) ?;
291
- let mut prefix = format ! ( "{}{}{}|{}|" , immovable, is_async, mover, list_str) ;
305
+ let mut prefix = format ! ( "{}{}{}{} |{}|" , binder , immovable, is_async, mover, list_str) ;
292
306
293
307
if !ret_str. is_empty ( ) {
294
308
if prefix. contains ( '\n' ) {
@@ -312,8 +326,15 @@ pub(crate) fn rewrite_last_closure(
312
326
expr : & ast:: Expr ,
313
327
shape : Shape ,
314
328
) -> Option < String > {
315
- if let ast:: ExprKind :: Closure ( capture, ref is_async, movability, ref fn_decl, ref body, _) =
316
- expr. kind
329
+ if let ast:: ExprKind :: Closure (
330
+ ref binder,
331
+ capture,
332
+ ref is_async,
333
+ movability,
334
+ ref fn_decl,
335
+ ref body,
336
+ _,
337
+ ) = expr. kind
317
338
{
318
339
let body = match body. kind {
319
340
ast:: ExprKind :: Block ( ref block, _)
@@ -326,7 +347,7 @@ pub(crate) fn rewrite_last_closure(
326
347
_ => body,
327
348
} ;
328
349
let ( prefix, extra_offset) = rewrite_closure_fn_decl (
329
- capture, is_async, movability, fn_decl, body, expr. span , context, shape,
350
+ binder , capture, is_async, movability, fn_decl, body, expr. span , context, shape,
330
351
) ?;
331
352
// If the closure goes multi line before its body, do not overflow the closure.
332
353
if prefix. contains ( '\n' ) {
0 commit comments