@@ -542,7 +542,7 @@ fn rewrite_bounded_lifetime(
542
542
"{}{}{}" ,
543
543
result,
544
544
colon,
545
- join_bounds( context, shape. sub_width( overhead) ?, bounds, true ) ?
545
+ join_bounds( context, shape. sub_width( overhead) ?, bounds, true ) . ok ( ) ?
546
546
) ;
547
547
Some ( result)
548
548
}
@@ -605,8 +605,12 @@ impl Rewrite for ast::GenericBound {
605
605
606
606
impl Rewrite for ast:: GenericBounds {
607
607
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
608
+ self . rewrite_result ( context, shape) . ok ( )
609
+ }
610
+
611
+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
608
612
if self . is_empty ( ) {
609
- return Some ( String :: new ( ) ) ;
613
+ return Ok ( String :: new ( ) ) ;
610
614
}
611
615
612
616
join_bounds ( context, shape, self , true )
@@ -615,8 +619,15 @@ impl Rewrite for ast::GenericBounds {
615
619
616
620
impl Rewrite for ast:: GenericParam {
617
621
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
622
+ self . rewrite_result ( context, shape) . ok ( )
623
+ }
624
+
625
+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
618
626
// FIXME: If there are more than one attributes, this will force multiline.
619
- let mut result = self . attrs . rewrite ( context, shape) . unwrap_or ( String :: new ( ) ) ;
627
+ let mut result = self
628
+ . attrs
629
+ . rewrite_result ( context, shape)
630
+ . unwrap_or ( String :: new ( ) ) ;
620
631
let has_attrs = !result. is_empty ( ) ;
621
632
622
633
let mut param = String :: with_capacity ( 128 ) ;
@@ -630,15 +641,19 @@ impl Rewrite for ast::GenericParam {
630
641
param. push_str ( "const " ) ;
631
642
param. push_str ( rewrite_ident ( context, self . ident ) ) ;
632
643
param. push_str ( ": " ) ;
633
- param. push_str ( & ty. rewrite ( context, shape) ?) ;
644
+ param. push_str ( & ty. rewrite_result ( context, shape) ?) ;
634
645
if let Some ( default) = default {
635
646
let eq_str = match context. config . type_punctuation_density ( ) {
636
647
TypeDensity :: Compressed => "=" ,
637
648
TypeDensity :: Wide => " = " ,
638
649
} ;
639
650
param. push_str ( eq_str) ;
640
- let budget = shape. width . checked_sub ( param. len ( ) ) ?;
641
- let rewrite = default. rewrite ( context, Shape :: legacy ( budget, shape. indent ) ) ?;
651
+ let budget = shape
652
+ . width
653
+ . checked_sub ( param. len ( ) )
654
+ . max_width_error ( shape. width , self . span ( ) ) ?;
655
+ let rewrite =
656
+ default. rewrite_result ( context, Shape :: legacy ( budget, shape. indent ) ) ?;
642
657
param. push_str ( & rewrite) ;
643
658
}
644
659
kw_span. lo ( )
@@ -649,7 +664,7 @@ impl Rewrite for ast::GenericParam {
649
664
650
665
if !self . bounds . is_empty ( ) {
651
666
param. push_str ( type_bound_colon ( context) ) ;
652
- param. push_str ( & self . bounds . rewrite ( context, shape) ?)
667
+ param. push_str ( & self . bounds . rewrite_result ( context, shape) ?)
653
668
}
654
669
if let ast:: GenericParamKind :: Type {
655
670
default : Some ( ref def) ,
@@ -660,9 +675,12 @@ impl Rewrite for ast::GenericParam {
660
675
TypeDensity :: Wide => " = " ,
661
676
} ;
662
677
param. push_str ( eq_str) ;
663
- let budget = shape. width . checked_sub ( param. len ( ) ) ?;
678
+ let budget = shape
679
+ . width
680
+ . checked_sub ( param. len ( ) )
681
+ . max_width_error ( shape. width , self . span ( ) ) ?;
664
682
let rewrite =
665
- def. rewrite ( context, Shape :: legacy ( budget, shape. indent + param. len ( ) ) ) ?;
683
+ def. rewrite_result ( context, Shape :: legacy ( budget, shape. indent + param. len ( ) ) ) ?;
666
684
param. push_str ( & rewrite) ;
667
685
}
668
686
@@ -676,7 +694,8 @@ impl Rewrite for ast::GenericParam {
676
694
mk_sp ( last_attr. span . hi ( ) , param_start) ,
677
695
shape,
678
696
!last_attr. is_doc_comment ( ) ,
679
- ) ?;
697
+ )
698
+ . unknown_error ( ) ?;
680
699
} else {
681
700
// When rewriting generic params, an extra newline should be put
682
701
// if the attributes end with a doc comment
@@ -688,7 +707,7 @@ impl Rewrite for ast::GenericParam {
688
707
result. push_str ( & param) ;
689
708
}
690
709
691
- Some ( result)
710
+ Ok ( result)
692
711
}
693
712
}
694
713
@@ -924,7 +943,7 @@ impl Rewrite for ast::Ty {
924
943
let rw = if context. config . version ( ) == Version :: One {
925
944
it. rewrite_result ( context, shape)
926
945
} else {
927
- join_bounds ( context, shape, it, false ) . unknown_error ( )
946
+ join_bounds ( context, shape, it, false )
928
947
} ;
929
948
rw. map ( |it_str| {
930
949
let space = if it_str. is_empty ( ) { "" } else { " " } ;
@@ -1024,7 +1043,7 @@ fn join_bounds(
1024
1043
shape : Shape ,
1025
1044
items : & [ ast:: GenericBound ] ,
1026
1045
need_indent : bool ,
1027
- ) -> Option < String > {
1046
+ ) -> RewriteResult {
1028
1047
join_bounds_inner ( context, shape, items, need_indent, false )
1029
1048
}
1030
1049
@@ -1034,7 +1053,7 @@ fn join_bounds_inner(
1034
1053
items : & [ ast:: GenericBound ] ,
1035
1054
need_indent : bool ,
1036
1055
force_newline : bool ,
1037
- ) -> Option < String > {
1056
+ ) -> RewriteResult {
1038
1057
debug_assert ! ( !items. is_empty( ) ) ;
1039
1058
1040
1059
let generic_bounds_in_order = is_generic_bounds_in_order ( items) ;
@@ -1129,16 +1148,17 @@ fn join_bounds_inner(
1129
1148
} ;
1130
1149
1131
1150
let ( extendable, trailing_str) = if i == 0 {
1132
- let bound_str = item. rewrite ( context, shape) ?;
1151
+ let bound_str = item. rewrite_result ( context, shape) ?;
1133
1152
( is_bound_extendable ( & bound_str, item) , bound_str)
1134
1153
} else {
1135
- let bound_str = & item. rewrite ( context, shape) ?;
1154
+ let bound_str = & item. rewrite_result ( context, shape) ?;
1136
1155
match leading_span {
1137
1156
Some ( ls) if has_leading_comment => (
1138
1157
is_bound_extendable ( bound_str, item) ,
1139
1158
combine_strs_with_missing_comments (
1140
1159
context, joiner, bound_str, ls, shape, true ,
1141
- ) ?,
1160
+ )
1161
+ . unknown_error ( ) ?,
1142
1162
) ,
1143
1163
_ => (
1144
1164
is_bound_extendable ( bound_str, item) ,
@@ -1155,8 +1175,9 @@ fn join_bounds_inner(
1155
1175
shape,
1156
1176
true ,
1157
1177
)
1178
+ . unknown_error ( )
1158
1179
. map ( |v| ( v, trailing_span, extendable) ) ,
1159
- _ => Some ( ( strs + & trailing_str, trailing_span, extendable) ) ,
1180
+ _ => Ok ( ( strs + & trailing_str, trailing_span, extendable) ) ,
1160
1181
}
1161
1182
} ,
1162
1183
) ?;
@@ -1181,7 +1202,7 @@ fn join_bounds_inner(
1181
1202
if retry_with_force_newline {
1182
1203
join_bounds_inner ( context, shape, items, need_indent, true )
1183
1204
} else {
1184
- Some ( result. 0 )
1205
+ Ok ( result. 0 )
1185
1206
}
1186
1207
}
1187
1208
0 commit comments