@@ -278,7 +278,7 @@ pub(crate) fn format_expr(
278
278
)
279
279
. ok ( ) ,
280
280
ast:: ExprKind :: Index ( ref expr, ref index, _) => {
281
- rewrite_index ( & * * expr, & * * index, context, shape)
281
+ rewrite_index ( & * * expr, & * * index, context, shape) . ok ( )
282
282
}
283
283
ast:: ExprKind :: Repeat ( ref expr, ref repeats) => rewrite_pair (
284
284
& * * expr,
@@ -435,7 +435,7 @@ pub(crate) fn format_expr(
435
435
} ;
436
436
437
437
expr_rw
438
- . and_then ( |expr_str| recover_comment_removed ( expr_str, expr. span , context) )
438
+ . map ( |expr_str| recover_comment_removed ( expr_str, expr. span , context) )
439
439
. and_then ( |expr_str| {
440
440
let attrs = outer_attributes ( & expr. attrs ) ;
441
441
let attrs_str = attrs. rewrite ( context, shape) ?;
@@ -672,6 +672,7 @@ pub(crate) fn rewrite_cond(
672
672
String :: from ( "\n " ) + & shape. indent . block_only ( ) . to_string ( context. config ) ;
673
673
control_flow
674
674
. rewrite_cond ( context, shape, & alt_block_sep)
675
+ . ok ( )
675
676
. map ( |rw| rw. 0 )
676
677
} ) ,
677
678
}
@@ -896,20 +897,23 @@ impl<'a> ControlFlow<'a> {
896
897
expr : & ast:: Expr ,
897
898
shape : Shape ,
898
899
offset : usize ,
899
- ) -> Option < String > {
900
+ ) -> RewriteResult {
900
901
debug ! ( "rewrite_pat_expr {:?} {:?} {:?}" , shape, self . pat, expr) ;
901
902
902
- let cond_shape = shape. offset_left ( offset) ?;
903
+ let cond_shape = shape
904
+ . offset_left ( offset)
905
+ . max_width_error ( shape. width , expr. span ) ?;
903
906
if let Some ( pat) = self . pat {
904
907
let matcher = if self . matcher . is_empty ( ) {
905
908
self . matcher . to_owned ( )
906
909
} else {
907
910
format ! ( "{} " , self . matcher)
908
911
} ;
909
912
let pat_shape = cond_shape
910
- . offset_left ( matcher. len ( ) ) ?
911
- . sub_width ( self . connector . len ( ) ) ?;
912
- let pat_string = pat. rewrite ( context, pat_shape) ?;
913
+ . offset_left ( matcher. len ( ) )
914
+ . and_then ( |s| s. sub_width ( self . connector . len ( ) ) )
915
+ . max_width_error ( cond_shape. width , pat. span ) ?;
916
+ let pat_string = pat. rewrite_result ( context, pat_shape) ?;
913
917
let comments_lo = context
914
918
. snippet_provider
915
919
. span_after ( self . span . with_lo ( pat. span . hi ( ) ) , self . connector . trim ( ) ) ;
@@ -923,14 +927,13 @@ impl<'a> ControlFlow<'a> {
923
927
RhsTactics :: Default ,
924
928
comments_span,
925
929
true ,
926
- )
927
- . ok ( ) ;
930
+ ) ;
928
931
}
929
932
930
- let expr_rw = expr. rewrite ( context, cond_shape) ;
933
+ let expr_rw = expr. rewrite_result ( context, cond_shape) ;
931
934
// The expression may (partially) fit on the current line.
932
935
// We do not allow splitting between `if` and condition.
933
- if self . keyword == "if" || expr_rw. is_some ( ) {
936
+ if self . keyword == "if" || expr_rw. is_ok ( ) {
934
937
return expr_rw;
935
938
}
936
939
@@ -939,7 +942,7 @@ impl<'a> ControlFlow<'a> {
939
942
. block_indent ( context. config . tab_spaces ( ) )
940
943
. with_max_width ( context. config ) ;
941
944
let nested_indent_str = nested_shape. indent . to_string_with_newline ( context. config ) ;
942
- expr. rewrite ( context, nested_shape)
945
+ expr. rewrite_result ( context, nested_shape)
943
946
. map ( |expr_rw| format ! ( "{}{}" , nested_indent_str, expr_rw) )
944
947
}
945
948
@@ -948,7 +951,7 @@ impl<'a> ControlFlow<'a> {
948
951
context : & RewriteContext < ' _ > ,
949
952
shape : Shape ,
950
953
alt_block_sep : & str ,
951
- ) -> Option < ( String , usize ) > {
954
+ ) -> Result < ( String , usize ) , RewriteError > {
952
955
// Do not take the rhs overhead from the upper expressions into account
953
956
// when rewriting pattern.
954
957
let new_width = context. budget ( shape. used_width ( ) ) ;
@@ -959,7 +962,9 @@ impl<'a> ControlFlow<'a> {
959
962
let constr_shape = if self . nested_if {
960
963
// We are part of an if-elseif-else chain. Our constraints are tightened.
961
964
// 7 = "} else " .len()
962
- fresh_shape. offset_left ( 7 ) ?
965
+ fresh_shape
966
+ . offset_left ( 7 )
967
+ . max_width_error ( fresh_shape. width , self . span ) ?
963
968
} else {
964
969
fresh_shape
965
970
} ;
@@ -995,7 +1000,7 @@ impl<'a> ControlFlow<'a> {
995
1000
996
1001
if let Some ( cond_str) = trial {
997
1002
if cond_str. len ( ) <= context. config . single_line_if_else_max_width ( ) {
998
- return Some ( ( cond_str, 0 ) ) ;
1003
+ return Ok ( ( cond_str, 0 ) ) ;
999
1004
}
1000
1005
}
1001
1006
}
@@ -1048,7 +1053,7 @@ impl<'a> ControlFlow<'a> {
1048
1053
label_string. len ( ) + self . keyword . len ( ) + pat_expr_string. len ( ) + 2
1049
1054
} ;
1050
1055
1051
- Some ( (
1056
+ Ok ( (
1052
1057
format ! (
1053
1058
"{}{}{}{}{}" ,
1054
1059
label_string,
@@ -1114,13 +1119,17 @@ pub(crate) fn rewrite_else_kw_with_comments(
1114
1119
1115
1120
impl < ' a > Rewrite for ControlFlow < ' a > {
1116
1121
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
1122
+ self . rewrite_result ( context, shape) . ok ( )
1123
+ }
1124
+
1125
+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
1117
1126
debug ! ( "ControlFlow::rewrite {:?} {:?}" , self , shape) ;
1118
1127
1119
1128
let alt_block_sep = & shape. indent . to_string_with_newline ( context. config ) ;
1120
1129
let ( cond_str, used_width) = self . rewrite_cond ( context, shape, alt_block_sep) ?;
1121
1130
// If `used_width` is 0, it indicates that whole control flow is written in a single line.
1122
1131
if used_width == 0 {
1123
- return Some ( cond_str) ;
1132
+ return Ok ( cond_str) ;
1124
1133
}
1125
1134
1126
1135
let block_width = shape. width . saturating_sub ( used_width) ;
@@ -1138,8 +1147,7 @@ impl<'a> Rewrite for ControlFlow<'a> {
1138
1147
let block_str = {
1139
1148
let old_val = context. is_if_else_block . replace ( self . else_block . is_some ( ) ) ;
1140
1149
let result =
1141
- rewrite_block_with_visitor ( context, "" , self . block , None , None , block_shape, true )
1142
- . ok ( ) ;
1150
+ rewrite_block_with_visitor ( context, "" , self . block , None , None , block_shape, true ) ;
1143
1151
context. is_if_else_block . replace ( old_val) ;
1144
1152
result?
1145
1153
} ;
@@ -1165,7 +1173,7 @@ impl<'a> Rewrite for ControlFlow<'a> {
1165
1173
true ,
1166
1174
mk_sp ( else_block. span . lo ( ) , self . span . hi ( ) ) ,
1167
1175
)
1168
- . rewrite ( context, shape)
1176
+ . rewrite_result ( context, shape)
1169
1177
}
1170
1178
_ => {
1171
1179
last_in_chain = true ;
@@ -1176,6 +1184,7 @@ impl<'a> Rewrite for ControlFlow<'a> {
1176
1184
..shape
1177
1185
} ;
1178
1186
format_expr ( else_block, ExprType :: Statement , context, else_shape)
1187
+ . unknown_error ( )
1179
1188
}
1180
1189
} ;
1181
1190
@@ -1190,7 +1199,7 @@ impl<'a> Rewrite for ControlFlow<'a> {
1190
1199
result. push_str ( & rewrite?) ;
1191
1200
}
1192
1201
1193
- Some ( result)
1202
+ Ok ( result)
1194
1203
}
1195
1204
}
1196
1205
@@ -1567,8 +1576,8 @@ fn rewrite_index(
1567
1576
index : & ast:: Expr ,
1568
1577
context : & RewriteContext < ' _ > ,
1569
1578
shape : Shape ,
1570
- ) -> Option < String > {
1571
- let expr_str = expr. rewrite ( context, shape) ?;
1579
+ ) -> RewriteResult {
1580
+ let expr_str = expr. rewrite_result ( context, shape) ?;
1572
1581
1573
1582
let offset = last_line_width ( & expr_str) + 1 ;
1574
1583
let rhs_overhead = shape. rhs_overhead ( context. config ) ;
@@ -1583,37 +1592,45 @@ fn rewrite_index(
1583
1592
. and_then ( |shape| shape. sub_width ( 1 ) ) ,
1584
1593
IndentStyle :: Visual => shape. visual_indent ( offset) . sub_width ( offset + 1 ) ,
1585
1594
}
1586
- } ;
1587
- let orig_index_rw = index_shape. and_then ( |s| index. rewrite ( context, s) ) ;
1595
+ }
1596
+ . max_width_error ( shape. width , index. span ( ) ) ;
1597
+ let orig_index_rw = index_shape. and_then ( |s| index. rewrite_result ( context, s) ) ;
1588
1598
1589
1599
// Return if index fits in a single line.
1590
1600
match orig_index_rw {
1591
- Some ( ref index_str) if !index_str. contains ( '\n' ) => {
1592
- return Some ( format ! ( "{expr_str}[{index_str}]" ) ) ;
1601
+ Ok ( ref index_str) if !index_str. contains ( '\n' ) => {
1602
+ return Ok ( format ! ( "{expr_str}[{index_str}]" ) ) ;
1593
1603
}
1594
1604
_ => ( ) ,
1595
1605
}
1596
1606
1597
1607
// Try putting index on the next line and see if it fits in a single line.
1598
1608
let indent = shape. indent . block_indent ( context. config ) ;
1599
- let index_shape = Shape :: indented ( indent, context. config ) . offset_left ( 1 ) ?;
1600
- let index_shape = index_shape. sub_width ( 1 + rhs_overhead) ?;
1601
- let new_index_rw = index. rewrite ( context, index_shape) ;
1609
+ let index_shape = Shape :: indented ( indent, context. config )
1610
+ . offset_left ( 1 )
1611
+ . max_width_error ( shape. width , index. span ( ) ) ?;
1612
+ let index_shape = index_shape
1613
+ . sub_width ( 1 + rhs_overhead)
1614
+ . max_width_error ( index_shape. width , index. span ( ) ) ?;
1615
+ let new_index_rw = index. rewrite_result ( context, index_shape) ;
1602
1616
match ( orig_index_rw, new_index_rw) {
1603
- ( _, Some ( ref new_index_str) ) if !new_index_str. contains ( '\n' ) => Some ( format ! (
1617
+ ( _, Ok ( ref new_index_str) ) if !new_index_str. contains ( '\n' ) => Ok ( format ! (
1604
1618
"{}{}[{}]" ,
1605
1619
expr_str,
1606
1620
indent. to_string_with_newline( context. config) ,
1607
1621
new_index_str,
1608
1622
) ) ,
1609
- ( None , Some ( ref new_index_str) ) => Some ( format ! (
1623
+ ( Err ( _ ) , Ok ( ref new_index_str) ) => Ok ( format ! (
1610
1624
"{}{}[{}]" ,
1611
1625
expr_str,
1612
1626
indent. to_string_with_newline( context. config) ,
1613
1627
new_index_str,
1614
1628
) ) ,
1615
- ( Some ( ref index_str) , _) => Some ( format ! ( "{expr_str}[{index_str}]" ) ) ,
1616
- _ => None ,
1629
+ ( Ok ( ref index_str) , _) => Ok ( format ! ( "{expr_str}[{index_str}]" ) ) ,
1630
+ // When both orig_index_rw and new_index_rw result in errors, we currently propagate the
1631
+ // error from the second attempt since it is more generous with width constraints.
1632
+ // This decision is somewhat arbitrary and is open to change.
1633
+ ( Err ( _) , Err ( new_index_rw_err) ) => Err ( new_index_rw_err) ,
1617
1634
}
1618
1635
}
1619
1636
0 commit comments