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