@@ -531,7 +531,7 @@ fn rewrite_bounded_lifetime(
531
531
"{}{}{}" ,
532
532
result,
533
533
colon,
534
- join_bounds( context, shape. sub_width( overhead) ?, bounds, true ) ?
534
+ join_bounds( context, shape. sub_width( overhead) ?, bounds, true ) . ok ( ) ?
535
535
) ;
536
536
Some ( result)
537
537
}
@@ -590,8 +590,12 @@ impl Rewrite for ast::GenericBound {
590
590
591
591
impl Rewrite for ast:: GenericBounds {
592
592
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
593
+ self . rewrite_result ( context, shape) . ok ( )
594
+ }
595
+
596
+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
593
597
if self . is_empty ( ) {
594
- return Some ( String :: new ( ) ) ;
598
+ return Ok ( String :: new ( ) ) ;
595
599
}
596
600
597
601
join_bounds ( context, shape, self , true )
@@ -600,8 +604,15 @@ impl Rewrite for ast::GenericBounds {
600
604
601
605
impl Rewrite for ast:: GenericParam {
602
606
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
607
+ self . rewrite_result ( context, shape) . ok ( )
608
+ }
609
+
610
+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
603
611
// FIXME: If there are more than one attributes, this will force multiline.
604
- let mut result = self . attrs . rewrite ( context, shape) . unwrap_or ( String :: new ( ) ) ;
612
+ let mut result = self
613
+ . attrs
614
+ . rewrite_result ( context, shape)
615
+ . unwrap_or ( String :: new ( ) ) ;
605
616
let has_attrs = !result. is_empty ( ) ;
606
617
607
618
let mut param = String :: with_capacity ( 128 ) ;
@@ -615,15 +626,19 @@ impl Rewrite for ast::GenericParam {
615
626
param. push_str ( "const " ) ;
616
627
param. push_str ( rewrite_ident ( context, self . ident ) ) ;
617
628
param. push_str ( ": " ) ;
618
- param. push_str ( & ty. rewrite ( context, shape) ?) ;
629
+ param. push_str ( & ty. rewrite_result ( context, shape) ?) ;
619
630
if let Some ( default) = default {
620
631
let eq_str = match context. config . type_punctuation_density ( ) {
621
632
TypeDensity :: Compressed => "=" ,
622
633
TypeDensity :: Wide => " = " ,
623
634
} ;
624
635
param. push_str ( eq_str) ;
625
- let budget = shape. width . checked_sub ( param. len ( ) ) ?;
626
- let rewrite = default. rewrite ( context, Shape :: legacy ( budget, shape. indent ) ) ?;
636
+ let budget = shape
637
+ . width
638
+ . checked_sub ( param. len ( ) )
639
+ . max_width_error ( shape. width , self . span ( ) ) ?;
640
+ let rewrite =
641
+ default. rewrite_result ( context, Shape :: legacy ( budget, shape. indent ) ) ?;
627
642
param. push_str ( & rewrite) ;
628
643
}
629
644
kw_span. lo ( )
@@ -634,7 +649,7 @@ impl Rewrite for ast::GenericParam {
634
649
635
650
if !self . bounds . is_empty ( ) {
636
651
param. push_str ( type_bound_colon ( context) ) ;
637
- param. push_str ( & self . bounds . rewrite ( context, shape) ?)
652
+ param. push_str ( & self . bounds . rewrite_result ( context, shape) ?)
638
653
}
639
654
if let ast:: GenericParamKind :: Type {
640
655
default : Some ( ref def) ,
@@ -645,9 +660,12 @@ impl Rewrite for ast::GenericParam {
645
660
TypeDensity :: Wide => " = " ,
646
661
} ;
647
662
param. push_str ( eq_str) ;
648
- let budget = shape. width . checked_sub ( param. len ( ) ) ?;
663
+ let budget = shape
664
+ . width
665
+ . checked_sub ( param. len ( ) )
666
+ . max_width_error ( shape. width , self . span ( ) ) ?;
649
667
let rewrite =
650
- def. rewrite ( context, Shape :: legacy ( budget, shape. indent + param. len ( ) ) ) ?;
668
+ def. rewrite_result ( context, Shape :: legacy ( budget, shape. indent + param. len ( ) ) ) ?;
651
669
param. push_str ( & rewrite) ;
652
670
}
653
671
@@ -661,7 +679,8 @@ impl Rewrite for ast::GenericParam {
661
679
mk_sp ( last_attr. span . hi ( ) , param_start) ,
662
680
shape,
663
681
!last_attr. is_doc_comment ( ) ,
664
- ) ?;
682
+ )
683
+ . unknown_error ( ) ?;
665
684
} else {
666
685
// When rewriting generic params, an extra newline should be put
667
686
// if the attributes end with a doc comment
@@ -673,7 +692,7 @@ impl Rewrite for ast::GenericParam {
673
692
result. push_str ( & param) ;
674
693
}
675
694
676
- Some ( result)
695
+ Ok ( result)
677
696
}
678
697
}
679
698
@@ -913,7 +932,7 @@ impl Rewrite for ast::Ty {
913
932
let rw = if context. config . version ( ) == Version :: One {
914
933
it. rewrite_result ( context, shape)
915
934
} else {
916
- join_bounds ( context, shape, it, false ) . unknown_error ( )
935
+ join_bounds ( context, shape, it, false )
917
936
} ;
918
937
rw. map ( |it_str| {
919
938
let space = if it_str. is_empty ( ) { "" } else { " " } ;
@@ -1013,7 +1032,7 @@ fn join_bounds(
1013
1032
shape : Shape ,
1014
1033
items : & [ ast:: GenericBound ] ,
1015
1034
need_indent : bool ,
1016
- ) -> Option < String > {
1035
+ ) -> RewriteResult {
1017
1036
join_bounds_inner ( context, shape, items, need_indent, false )
1018
1037
}
1019
1038
@@ -1023,7 +1042,7 @@ fn join_bounds_inner(
1023
1042
items : & [ ast:: GenericBound ] ,
1024
1043
need_indent : bool ,
1025
1044
force_newline : bool ,
1026
- ) -> Option < String > {
1045
+ ) -> RewriteResult {
1027
1046
debug_assert ! ( !items. is_empty( ) ) ;
1028
1047
1029
1048
let generic_bounds_in_order = is_generic_bounds_in_order ( items) ;
@@ -1116,16 +1135,17 @@ fn join_bounds_inner(
1116
1135
} ;
1117
1136
1118
1137
let ( extendable, trailing_str) = if i == 0 {
1119
- let bound_str = item. rewrite ( context, shape) ?;
1138
+ let bound_str = item. rewrite_result ( context, shape) ?;
1120
1139
( is_bound_extendable ( & bound_str, item) , bound_str)
1121
1140
} else {
1122
- let bound_str = & item. rewrite ( context, shape) ?;
1141
+ let bound_str = & item. rewrite_result ( context, shape) ?;
1123
1142
match leading_span {
1124
1143
Some ( ls) if has_leading_comment => (
1125
1144
is_bound_extendable ( bound_str, item) ,
1126
1145
combine_strs_with_missing_comments (
1127
1146
context, joiner, bound_str, ls, shape, true ,
1128
- ) ?,
1147
+ )
1148
+ . unknown_error ( ) ?,
1129
1149
) ,
1130
1150
_ => (
1131
1151
is_bound_extendable ( bound_str, item) ,
@@ -1142,8 +1162,9 @@ fn join_bounds_inner(
1142
1162
shape,
1143
1163
true ,
1144
1164
)
1165
+ . unknown_error ( )
1145
1166
. map ( |v| ( v, trailing_span, extendable) ) ,
1146
- _ => Some ( ( strs + & trailing_str, trailing_span, extendable) ) ,
1167
+ _ => Ok ( ( strs + & trailing_str, trailing_span, extendable) ) ,
1147
1168
}
1148
1169
} ,
1149
1170
) ?;
@@ -1168,7 +1189,7 @@ fn join_bounds_inner(
1168
1189
if retry_with_force_newline {
1169
1190
join_bounds_inner ( context, shape, items, need_indent, true )
1170
1191
} else {
1171
- Some ( result. 0 )
1192
+ Ok ( result. 0 )
1172
1193
}
1173
1194
}
1174
1195
0 commit comments