@@ -22,7 +22,7 @@ use crate::macros::{rewrite_macro, MacroPosition};
22
22
use crate :: matches:: rewrite_match;
23
23
use crate :: overflow:: { self , IntoOverflowableItem , OverflowableItem } ;
24
24
use crate :: pairs:: { rewrite_all_pairs, rewrite_pair, PairParts } ;
25
- use crate :: rewrite:: { Rewrite , RewriteContext } ;
25
+ use crate :: rewrite:: { Rewrite , RewriteContext , RewriteError , RewriteErrorExt , RewriteResult } ;
26
26
use crate :: shape:: { Indent , Shape } ;
27
27
use crate :: source_map:: { LineRangeUtils , SpanUtils } ;
28
28
use crate :: spanned:: Spanned ;
@@ -145,7 +145,7 @@ pub(crate) fn format_expr(
145
145
// not the `ast::Block` node we're about to rewrite. To prevent dropping inner
146
146
// attributes call `rewrite_block` directly.
147
147
// 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 ( ) ?
149
149
}
150
150
_ => anon_const. rewrite ( context, shape) ?,
151
151
} ;
@@ -155,7 +155,7 @@ pub(crate) fn format_expr(
155
155
match expr_type {
156
156
ExprType :: Statement => {
157
157
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 ( )
159
159
} else if let rw @ Some ( _) =
160
160
rewrite_empty_block ( context, block, Some ( & expr. attrs ) , opt_label, "" , shape)
161
161
{
@@ -173,10 +173,11 @@ pub(crate) fn format_expr(
173
173
shape,
174
174
true ,
175
175
)
176
+ . ok ( )
176
177
}
177
178
}
178
179
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 ( )
180
181
}
181
182
}
182
183
}
@@ -352,10 +353,10 @@ pub(crate) fn format_expr(
352
353
// https://github.com/rust-dev-tools/fmt-rfcs/issues/152
353
354
ast:: ExprKind :: InlineAsm ( ..) => Some ( context. snippet ( expr. span ) . to_owned ( ) ) ,
354
355
ast:: ExprKind :: TryBlock ( ref block) => {
355
- if let rw @ Some ( _) =
356
+ if let rw @ Ok ( _) =
356
357
rewrite_single_line_block ( context, "try " , block, Some ( & expr. attrs ) , None , shape)
357
358
{
358
- rw
359
+ rw. ok ( )
359
360
} else {
360
361
// 9 = `try `
361
362
let budget = shape. width . saturating_sub ( 9 ) ;
@@ -368,7 +369,8 @@ pub(crate) fn format_expr(
368
369
None ,
369
370
context,
370
371
Shape :: legacy( budget, shape. indent)
371
- ) ?
372
+ )
373
+ . ok( ) ?
372
374
) )
373
375
}
374
376
}
@@ -378,15 +380,15 @@ pub(crate) fn format_expr(
378
380
} else {
379
381
""
380
382
} ;
381
- if let rw @ Some ( _) = rewrite_single_line_block (
383
+ if let rw @ Ok ( _) = rewrite_single_line_block (
382
384
context,
383
385
format ! ( "{kind} {mover}" ) . as_str ( ) ,
384
386
block,
385
387
Some ( & expr. attrs ) ,
386
388
None ,
387
389
shape,
388
390
) {
389
- rw
391
+ rw. ok ( )
390
392
} else {
391
393
// 6 = `async `
392
394
let budget = shape. width . saturating_sub ( 6 ) ;
@@ -398,7 +400,8 @@ pub(crate) fn format_expr(
398
400
None ,
399
401
context,
400
402
Shape :: legacy( budget, shape. indent)
401
- ) ?
403
+ )
404
+ . ok( ) ?
402
405
) )
403
406
}
404
407
}
@@ -522,17 +525,19 @@ fn rewrite_single_line_block(
522
525
attrs : Option < & [ ast:: Attribute ] > ,
523
526
label : Option < ast:: Label > ,
524
527
shape : Shape ,
525
- ) -> Option < String > {
528
+ ) -> RewriteResult {
526
529
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) ?;
529
534
let label_str = rewrite_label ( label) ;
530
535
let result = format ! ( "{prefix}{label_str}{{ {expr_str} }}" ) ;
531
536
if result. len ( ) <= shape. width && !result. contains ( '\n' ) {
532
- return Some ( result) ;
537
+ return Ok ( result) ;
533
538
}
534
539
}
535
- None
540
+ Err ( RewriteError :: Unknown )
536
541
}
537
542
538
543
pub ( crate ) fn rewrite_block_with_visitor (
@@ -543,9 +548,9 @@ pub(crate) fn rewrite_block_with_visitor(
543
548
label : Option < ast:: Label > ,
544
549
shape : Shape ,
545
550
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 ) ;
549
554
}
550
555
551
556
let mut visitor = FmtVisitor :: from_context ( context) ;
@@ -554,7 +559,7 @@ pub(crate) fn rewrite_block_with_visitor(
554
559
match ( block. rules , label) {
555
560
( ast:: BlockCheckMode :: Unsafe ( ..) , _) | ( ast:: BlockCheckMode :: Default , Some ( _) ) => {
556
561
let snippet = context. snippet ( block. span ) ;
557
- let open_pos = snippet. find_uncommented ( "{" ) ?;
562
+ let open_pos = snippet. find_uncommented ( "{" ) . unknown_error ( ) ?;
558
563
visitor. last_pos = block. span . lo ( ) + BytePos ( open_pos as u32 )
559
564
}
560
565
( ast:: BlockCheckMode :: Default , None ) => visitor. last_pos = block. span . lo ( ) ,
@@ -568,11 +573,15 @@ pub(crate) fn rewrite_block_with_visitor(
568
573
. skipped_range
569
574
. borrow_mut ( )
570
575
. append ( & mut visitor_context. skipped_range . borrow_mut ( ) ) ;
571
- Some ( format ! ( "{}{}{}" , prefix, label_str, visitor. buffer) )
576
+ Ok ( format ! ( "{}{}{}" , prefix, label_str, visitor. buffer) )
572
577
}
573
578
574
579
impl Rewrite for ast:: Block {
575
580
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 {
576
585
rewrite_block ( self , None , None , context, shape)
577
586
}
578
587
}
@@ -583,7 +592,7 @@ fn rewrite_block(
583
592
label : Option < ast:: Label > ,
584
593
context : & RewriteContext < ' _ > ,
585
594
shape : Shape ,
586
- ) -> Option < String > {
595
+ ) -> RewriteResult {
587
596
rewrite_block_inner ( block, attrs, label, true , context, shape)
588
597
}
589
598
@@ -594,27 +603,24 @@ fn rewrite_block_inner(
594
603
allow_single_line : bool ,
595
604
context : & RewriteContext < ' _ > ,
596
605
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 ( ) ?;
599
608
600
609
// shape.width is used only for the single line case: either the empty block `{}`,
601
610
// 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 ) ;
604
613
}
605
614
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;
614
621
}
615
622
}
616
-
617
- result
623
+ Ok ( result_str)
618
624
}
619
625
620
626
/// Rewrite the divergent block of a `let-else` statement.
@@ -623,7 +629,7 @@ pub(crate) fn rewrite_let_else_block(
623
629
allow_single_line : bool ,
624
630
context : & RewriteContext < ' _ > ,
625
631
shape : Shape ,
626
- ) -> Option < String > {
632
+ ) -> RewriteResult {
627
633
rewrite_block_inner ( block, None , None , allow_single_line, context, shape)
628
634
}
629
635
@@ -1112,7 +1118,8 @@ impl<'a> Rewrite for ControlFlow<'a> {
1112
1118
let block_str = {
1113
1119
let old_val = context. is_if_else_block . replace ( self . else_block . is_some ( ) ) ;
1114
1120
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 ( ) ;
1116
1123
context. is_if_else_block . replace ( old_val) ;
1117
1124
result?
1118
1125
} ;
0 commit comments