@@ -22,7 +22,7 @@ use crate::macros::{rewrite_macro, MacroPosition};
2222use crate :: matches:: rewrite_match;
2323use crate :: overflow:: { self , IntoOverflowableItem , OverflowableItem } ;
2424use crate :: pairs:: { rewrite_all_pairs, rewrite_pair, PairParts } ;
25- use crate :: rewrite:: { Rewrite , RewriteContext } ;
25+ use crate :: rewrite:: { Rewrite , RewriteContext , RewriteError , RewriteErrorExt , RewriteResult } ;
2626use crate :: shape:: { Indent , Shape } ;
2727use crate :: source_map:: { LineRangeUtils , SpanUtils } ;
2828use crate :: spanned:: Spanned ;
@@ -145,7 +145,7 @@ pub(crate) fn format_expr(
145145 // not the `ast::Block` node we're about to rewrite. To prevent dropping inner
146146 // attributes call `rewrite_block` directly.
147147 // See https://github.com/rust-lang/rustfmt/issues/6158
148- rewrite_block ( block, Some ( & expr. attrs ) , opt_label, context, shape) ?
148+ rewrite_block ( block, Some ( & expr. attrs ) , opt_label, context, shape) . ok ( ) ?
149149 }
150150 _ => anon_const. rewrite ( context, shape) ?,
151151 } ;
@@ -155,7 +155,7 @@ pub(crate) fn format_expr(
155155 match expr_type {
156156 ExprType :: Statement => {
157157 if is_unsafe_block ( block) {
158- rewrite_block ( block, Some ( & expr. attrs ) , opt_label, context, shape)
158+ rewrite_block ( block, Some ( & expr. attrs ) , opt_label, context, shape) . ok ( )
159159 } else if let rw @ Some ( _) =
160160 rewrite_empty_block ( context, block, Some ( & expr. attrs ) , opt_label, "" , shape)
161161 {
@@ -173,10 +173,11 @@ pub(crate) fn format_expr(
173173 shape,
174174 true ,
175175 )
176+ . ok ( )
176177 }
177178 }
178179 ExprType :: SubExpression => {
179- rewrite_block ( block, Some ( & expr. attrs ) , opt_label, context, shape)
180+ rewrite_block ( block, Some ( & expr. attrs ) , opt_label, context, shape) . ok ( )
180181 }
181182 }
182183 }
@@ -352,10 +353,10 @@ pub(crate) fn format_expr(
352353 // https://github.com/rust-dev-tools/fmt-rfcs/issues/152
353354 ast:: ExprKind :: InlineAsm ( ..) => Some ( context. snippet ( expr. span ) . to_owned ( ) ) ,
354355 ast:: ExprKind :: TryBlock ( ref block) => {
355- if let rw @ Some ( _) =
356+ if let rw @ Ok ( _) =
356357 rewrite_single_line_block ( context, "try " , block, Some ( & expr. attrs ) , None , shape)
357358 {
358- rw
359+ rw. ok ( )
359360 } else {
360361 // 9 = `try `
361362 let budget = shape. width . saturating_sub ( 9 ) ;
@@ -368,7 +369,8 @@ pub(crate) fn format_expr(
368369 None ,
369370 context,
370371 Shape :: legacy( budget, shape. indent)
371- ) ?
372+ )
373+ . ok( ) ?
372374 ) )
373375 }
374376 }
@@ -378,15 +380,15 @@ pub(crate) fn format_expr(
378380 } else {
379381 ""
380382 } ;
381- if let rw @ Some ( _) = rewrite_single_line_block (
383+ if let rw @ Ok ( _) = rewrite_single_line_block (
382384 context,
383385 format ! ( "{kind} {mover}" ) . as_str ( ) ,
384386 block,
385387 Some ( & expr. attrs ) ,
386388 None ,
387389 shape,
388390 ) {
389- rw
391+ rw. ok ( )
390392 } else {
391393 // 6 = `async `
392394 let budget = shape. width . saturating_sub ( 6 ) ;
@@ -398,7 +400,8 @@ pub(crate) fn format_expr(
398400 None ,
399401 context,
400402 Shape :: legacy( budget, shape. indent)
401- ) ?
403+ )
404+ . ok( ) ?
402405 ) )
403406 }
404407 }
@@ -522,17 +525,19 @@ fn rewrite_single_line_block(
522525 attrs : Option < & [ ast:: Attribute ] > ,
523526 label : Option < ast:: Label > ,
524527 shape : Shape ,
525- ) -> Option < String > {
528+ ) -> RewriteResult {
526529 if let Some ( block_expr) = stmt:: Stmt :: from_simple_block ( context, block, attrs) {
527- let expr_shape = shape. offset_left ( last_line_width ( prefix) ) ?;
528- let expr_str = block_expr. rewrite ( context, expr_shape) ?;
530+ let expr_shape = shape
531+ . offset_left ( last_line_width ( prefix) )
532+ . max_width_error ( shape. width , block_expr. span ( ) ) ?;
533+ let expr_str = block_expr. rewrite_result ( context, expr_shape) ?;
529534 let label_str = rewrite_label ( label) ;
530535 let result = format ! ( "{prefix}{label_str}{{ {expr_str} }}" ) ;
531536 if result. len ( ) <= shape. width && !result. contains ( '\n' ) {
532- return Some ( result) ;
537+ return Ok ( result) ;
533538 }
534539 }
535- None
540+ Err ( RewriteError :: Unknown )
536541}
537542
538543pub ( crate ) fn rewrite_block_with_visitor (
@@ -543,9 +548,9 @@ pub(crate) fn rewrite_block_with_visitor(
543548 label : Option < ast:: Label > ,
544549 shape : Shape ,
545550 has_braces : bool ,
546- ) -> Option < String > {
547- if let rw @ Some ( _ ) = rewrite_empty_block ( context, block, attrs, label, prefix, shape) {
548- return rw ;
551+ ) -> RewriteResult {
552+ if let Some ( rw_str ) = rewrite_empty_block ( context, block, attrs, label, prefix, shape) {
553+ return Ok ( rw_str ) ;
549554 }
550555
551556 let mut visitor = FmtVisitor :: from_context ( context) ;
@@ -554,7 +559,7 @@ pub(crate) fn rewrite_block_with_visitor(
554559 match ( block. rules , label) {
555560 ( ast:: BlockCheckMode :: Unsafe ( ..) , _) | ( ast:: BlockCheckMode :: Default , Some ( _) ) => {
556561 let snippet = context. snippet ( block. span ) ;
557- let open_pos = snippet. find_uncommented ( "{" ) ?;
562+ let open_pos = snippet. find_uncommented ( "{" ) . unknown_error ( ) ?;
558563 visitor. last_pos = block. span . lo ( ) + BytePos ( open_pos as u32 )
559564 }
560565 ( ast:: BlockCheckMode :: Default , None ) => visitor. last_pos = block. span . lo ( ) ,
@@ -568,11 +573,15 @@ pub(crate) fn rewrite_block_with_visitor(
568573 . skipped_range
569574 . borrow_mut ( )
570575 . append ( & mut visitor_context. skipped_range . borrow_mut ( ) ) ;
571- Some ( format ! ( "{}{}{}" , prefix, label_str, visitor. buffer) )
576+ Ok ( format ! ( "{}{}{}" , prefix, label_str, visitor. buffer) )
572577}
573578
574579impl Rewrite for ast:: Block {
575580 fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
581+ self . rewrite_result ( context, shape) . ok ( )
582+ }
583+
584+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
576585 rewrite_block ( self , None , None , context, shape)
577586 }
578587}
@@ -583,7 +592,7 @@ fn rewrite_block(
583592 label : Option < ast:: Label > ,
584593 context : & RewriteContext < ' _ > ,
585594 shape : Shape ,
586- ) -> Option < String > {
595+ ) -> RewriteResult {
587596 rewrite_block_inner ( block, attrs, label, true , context, shape)
588597}
589598
@@ -594,27 +603,24 @@ fn rewrite_block_inner(
594603 allow_single_line : bool ,
595604 context : & RewriteContext < ' _ > ,
596605 shape : Shape ,
597- ) -> Option < String > {
598- let prefix = block_prefix ( context, block, shape) ?;
606+ ) -> RewriteResult {
607+ let prefix = block_prefix ( context, block, shape) . unknown_error ( ) ?;
599608
600609 // shape.width is used only for the single line case: either the empty block `{}`,
601610 // or an unsafe expression `unsafe { e }`.
602- if let rw @ Some ( _ ) = rewrite_empty_block ( context, block, attrs, label, & prefix, shape) {
603- return rw ;
611+ if let Some ( rw_str ) = rewrite_empty_block ( context, block, attrs, label, & prefix, shape) {
612+ return Ok ( rw_str ) ;
604613 }
605614
606- let result = rewrite_block_with_visitor ( context, & prefix, block, attrs, label, shape, true ) ;
607- if let Some ( ref result_str) = result {
608- if allow_single_line && result_str. lines ( ) . count ( ) <= 3 {
609- if let rw @ Some ( _) =
610- rewrite_single_line_block ( context, & prefix, block, attrs, label, shape)
611- {
612- return rw;
613- }
615+ let result_str =
616+ rewrite_block_with_visitor ( context, & prefix, block, attrs, label, shape, true ) ?;
617+ if allow_single_line && result_str. lines ( ) . count ( ) <= 3 {
618+ if let rw @ Ok ( _) = rewrite_single_line_block ( context, & prefix, block, attrs, label, shape)
619+ {
620+ return rw;
614621 }
615622 }
616-
617- result
623+ Ok ( result_str)
618624}
619625
620626/// Rewrite the divergent block of a `let-else` statement.
@@ -623,7 +629,7 @@ pub(crate) fn rewrite_let_else_block(
623629 allow_single_line : bool ,
624630 context : & RewriteContext < ' _ > ,
625631 shape : Shape ,
626- ) -> Option < String > {
632+ ) -> RewriteResult {
627633 rewrite_block_inner ( block, None , None , allow_single_line, context, shape)
628634}
629635
@@ -1112,7 +1118,8 @@ impl<'a> Rewrite for ControlFlow<'a> {
11121118 let block_str = {
11131119 let old_val = context. is_if_else_block . replace ( self . else_block . is_some ( ) ) ;
11141120 let result =
1115- rewrite_block_with_visitor ( context, "" , self . block , None , None , block_shape, true ) ;
1121+ rewrite_block_with_visitor ( context, "" , self . block , None , None , block_shape, true )
1122+ . ok ( ) ;
11161123 context. is_if_else_block . replace ( old_val) ;
11171124 result?
11181125 } ;
0 commit comments