@@ -11,6 +11,7 @@ use crate::overflow::OverflowableItem;
1111use crate :: rewrite:: { Rewrite , RewriteContext } ;
1212use crate :: shape:: Shape ;
1313use crate :: source_map:: SpanUtils ;
14+ use crate :: types:: rewrite_lifetime_param;
1415use crate :: utils:: { last_line_width, left_most_sub_expr, stmt_expr, NodeIdExt } ;
1516
1617// 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};
2425// can change whether it is treated as an expression or statement.
2526
2627pub ( crate ) fn rewrite_closure (
28+ binder : & ast:: ClosureBinder ,
2729 capture : ast:: CaptureBy ,
2830 is_async : & ast:: Async ,
2931 movability : ast:: Movability ,
@@ -36,7 +38,7 @@ pub(crate) fn rewrite_closure(
3638 debug ! ( "rewrite_closure {:?}" , body) ;
3739
3840 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,
4042 ) ?;
4143 // 1 = space between `|...|` and body.
4244 let body_shape = shape. offset_left ( extra_offset) ?;
@@ -227,6 +229,7 @@ fn rewrite_closure_block(
227229
228230// Return type is (prefix, extra_offset)
229231fn rewrite_closure_fn_decl (
232+ binder : & ast:: ClosureBinder ,
230233 capture : ast:: CaptureBy ,
231234 asyncness : & ast:: Async ,
232235 movability : ast:: Movability ,
@@ -236,6 +239,17 @@ fn rewrite_closure_fn_decl(
236239 context : & RewriteContext < ' _ > ,
237240 shape : Shape ,
238241) -> 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+
239253 let immovable = if movability == ast:: Movability :: Static {
240254 "static "
241255 } else {
@@ -250,7 +264,7 @@ fn rewrite_closure_fn_decl(
250264 // 4 = "|| {".len(), which is overconservative when the closure consists of
251265 // a single expression.
252266 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 ( ) ) ?
254268 . sub_width ( 4 ) ?;
255269
256270 // 1 = |
@@ -288,7 +302,7 @@ fn rewrite_closure_fn_decl(
288302 . tactic ( tactic)
289303 . preserve_newline ( true ) ;
290304 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) ;
292306
293307 if !ret_str. is_empty ( ) {
294308 if prefix. contains ( '\n' ) {
@@ -312,8 +326,15 @@ pub(crate) fn rewrite_last_closure(
312326 expr : & ast:: Expr ,
313327 shape : Shape ,
314328) -> 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
317338 {
318339 let body = match body. kind {
319340 ast:: ExprKind :: Block ( ref block, _)
@@ -326,7 +347,7 @@ pub(crate) fn rewrite_last_closure(
326347 _ => body,
327348 } ;
328349 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,
330351 ) ?;
331352 // If the closure goes multi line before its body, do not overflow the closure.
332353 if prefix. contains ( '\n' ) {
0 commit comments