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