@@ -40,7 +40,7 @@ pub(crate) fn rewrite_path(
4040 qself : & Option < ptr:: P < ast:: QSelf > > ,
4141 path : & ast:: Path ,
4242 shape : Shape ,
43- ) -> Option < String > {
43+ ) -> RewriteResult {
4444 let skip_count = qself. as_ref ( ) . map_or ( 0 , |x| x. position ) ;
4545
4646 // 32 covers almost all path lengths measured when compiling core, and there isn't a big
@@ -56,7 +56,7 @@ pub(crate) fn rewrite_path(
5656 if let Some ( qself) = qself {
5757 result. push ( '<' ) ;
5858
59- let fmt_ty = qself. ty . rewrite ( context, shape) ?;
59+ let fmt_ty = qself. ty . rewrite_result ( context, shape) ?;
6060 result. push_str ( & fmt_ty) ;
6161
6262 if skip_count > 0 {
@@ -66,7 +66,7 @@ pub(crate) fn rewrite_path(
6666 }
6767
6868 // 3 = ">::".len()
69- let shape = shape. sub_width ( 3 ) ?;
69+ let shape = shape. sub_width ( 3 ) . max_width_error ( shape . width , path . span ) ?;
7070
7171 result = rewrite_path_segments (
7272 PathContext :: Type ,
@@ -102,7 +102,7 @@ fn rewrite_path_segments<'a, I>(
102102 span_hi : BytePos ,
103103 context : & RewriteContext < ' _ > ,
104104 shape : Shape ,
105- ) -> Option < String >
105+ ) -> RewriteResult
106106where
107107 I : Iterator < Item = & ' a ast:: PathSegment > ,
108108{
@@ -121,7 +121,9 @@ where
121121 }
122122
123123 let extra_offset = extra_offset ( & buffer, shape) ;
124- let new_shape = shape. shrink_left ( extra_offset) ?;
124+ let new_shape = shape
125+ . shrink_left ( extra_offset)
126+ . max_width_error ( shape. width , mk_sp ( span_lo, span_hi) ) ?;
125127 let segment_string = rewrite_segment (
126128 path_context,
127129 segment,
@@ -134,7 +136,7 @@ where
134136 buffer. push_str ( & segment_string) ;
135137 }
136138
137- Some ( buffer)
139+ Ok ( buffer)
138140}
139141
140142#[ derive( Debug ) ]
@@ -183,8 +185,12 @@ impl<'a> Rewrite for SegmentParam<'a> {
183185
184186impl Rewrite for ast:: PreciseCapturingArg {
185187 fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
188+ self . rewrite_result ( context, shape) . ok ( )
189+ }
190+
191+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
186192 match self {
187- ast:: PreciseCapturingArg :: Lifetime ( lt) => lt. rewrite ( context, shape) ,
193+ ast:: PreciseCapturingArg :: Lifetime ( lt) => lt. rewrite_result ( context, shape) ,
188194 ast:: PreciseCapturingArg :: Arg ( p, _) => {
189195 rewrite_path ( context, PathContext :: Type , & None , p, shape)
190196 }
@@ -194,13 +200,20 @@ impl Rewrite for ast::PreciseCapturingArg {
194200
195201impl Rewrite for ast:: AssocItemConstraint {
196202 fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
203+ self . rewrite_result ( context, shape) . ok ( )
204+ }
205+
206+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
197207 use ast:: AssocItemConstraintKind :: { Bound , Equality } ;
198208
199209 let mut result = String :: with_capacity ( 128 ) ;
200210 result. push_str ( rewrite_ident ( context, self . ident ) ) ;
201211
202212 if let Some ( ref gen_args) = self . gen_args {
203- let budget = shape. width . checked_sub ( result. len ( ) ) ?;
213+ let budget = shape
214+ . width
215+ . checked_sub ( result. len ( ) )
216+ . max_width_error ( shape. width , self . span ) ?;
204217 let shape = Shape :: legacy ( budget, shape. indent + result. len ( ) ) ;
205218 let gen_str = rewrite_generic_args ( gen_args, context, shape, gen_args. span ( ) ) ?;
206219 result. push_str ( & gen_str) ;
@@ -213,12 +226,15 @@ impl Rewrite for ast::AssocItemConstraint {
213226 } ;
214227 result. push_str ( infix) ;
215228
216- let budget = shape. width . checked_sub ( result. len ( ) ) ?;
229+ let budget = shape
230+ . width
231+ . checked_sub ( result. len ( ) )
232+ . max_width_error ( shape. width , self . span ) ?;
217233 let shape = Shape :: legacy ( budget, shape. indent + result. len ( ) ) ;
218- let rewrite = self . kind . rewrite ( context, shape) ?;
234+ let rewrite = self . kind . rewrite_result ( context, shape) ?;
219235 result. push_str ( & rewrite) ;
220236
221- Some ( result)
237+ Ok ( result)
222238 }
223239}
224240
@@ -255,16 +271,17 @@ fn rewrite_segment(
255271 span_hi : BytePos ,
256272 context : & RewriteContext < ' _ > ,
257273 shape : Shape ,
258- ) -> Option < String > {
274+ ) -> RewriteResult {
259275 let mut result = String :: with_capacity ( 128 ) ;
260276 result. push_str ( rewrite_ident ( context, segment. ident ) ) ;
261277
262278 let ident_len = result. len ( ) ;
263279 let shape = if context. use_block_indent ( ) {
264- shape. offset_left ( ident_len) ?
280+ shape. offset_left ( ident_len)
265281 } else {
266- shape. shrink_left ( ident_len) ?
267- } ;
282+ shape. shrink_left ( ident_len)
283+ }
284+ . max_width_error ( shape. width , mk_sp ( * span_lo, span_hi) ) ?;
268285
269286 if let Some ( ref args) = segment. args {
270287 let generics_str = rewrite_generic_args ( args, context, shape, mk_sp ( * span_lo, span_hi) ) ?;
@@ -295,7 +312,7 @@ fn rewrite_segment(
295312 result. push_str ( & generics_str)
296313 }
297314
298- Some ( result)
315+ Ok ( result)
299316}
300317
301318fn format_function_type < ' a , I > (
@@ -498,7 +515,7 @@ fn rewrite_generic_args(
498515 context : & RewriteContext < ' _ > ,
499516 shape : Shape ,
500517 span : Span ,
501- ) -> Option < String > {
518+ ) -> RewriteResult {
502519 match gen_args {
503520 ast:: GenericArgs :: AngleBracketed ( ref data) if !data. args . is_empty ( ) => {
504521 let args = data
@@ -515,6 +532,7 @@ fn rewrite_generic_args(
515532 . collect :: < Vec < _ > > ( ) ;
516533
517534 overflow:: rewrite_with_angle_brackets ( context, "" , args. iter ( ) , shape, span)
535+ . unknown_error ( )
518536 }
519537 ast:: GenericArgs :: Parenthesized ( ref data) => format_function_type (
520538 data. inputs . iter ( ) . map ( |x| & * * x) ,
@@ -523,9 +541,8 @@ fn rewrite_generic_args(
523541 data. span ,
524542 context,
525543 shape,
526- )
527- . ok ( ) ,
528- _ => Some ( "" . to_owned ( ) ) ,
544+ ) ,
545+ _ => Ok ( "" . to_owned ( ) ) ,
529546 }
530547}
531548
@@ -717,23 +734,32 @@ impl Rewrite for ast::GenericParam {
717734
718735impl Rewrite for ast:: PolyTraitRef {
719736 fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
737+ self . rewrite_result ( context, shape) . ok ( )
738+ }
739+
740+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
720741 if let Some ( lifetime_str) = rewrite_bound_params ( context, shape, & self . bound_generic_params )
721742 {
722743 // 6 is "for<> ".len()
723744 let extra_offset = lifetime_str. len ( ) + 6 ;
724- let path_str = self
725- . trait_ref
726- . rewrite ( context, shape. offset_left ( extra_offset) ?) ?;
745+ let shape = shape
746+ . offset_left ( extra_offset)
747+ . max_width_error ( shape. width , self . span ) ?;
748+ let path_str = self . trait_ref . rewrite_result ( context, shape) ?;
727749
728- Some ( format ! ( "for<{lifetime_str}> {path_str}" ) )
750+ Ok ( format ! ( "for<{lifetime_str}> {path_str}" ) )
729751 } else {
730- self . trait_ref . rewrite ( context, shape)
752+ self . trait_ref . rewrite_result ( context, shape)
731753 }
732754 }
733755}
734756
735757impl Rewrite for ast:: TraitRef {
736758 fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
759+ self . rewrite_result ( context, shape) . ok ( )
760+ }
761+
762+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
737763 rewrite_path ( context, PathContext :: Type , & None , & self . path , shape)
738764 }
739765}
@@ -912,7 +938,7 @@ impl Rewrite for ast::Ty {
912938 ast:: TyKind :: AnonStruct ( ..) => Ok ( context. snippet ( self . span ) . to_owned ( ) ) ,
913939 ast:: TyKind :: AnonUnion ( ..) => Ok ( context. snippet ( self . span ) . to_owned ( ) ) ,
914940 ast:: TyKind :: Path ( ref q_self, ref path) => {
915- rewrite_path ( context, PathContext :: Type , q_self, path, shape) . unknown_error ( )
941+ rewrite_path ( context, PathContext :: Type , q_self, path, shape)
916942 }
917943 ast:: TyKind :: Array ( ref ty, ref repeats) => rewrite_pair (
918944 & * * ty,
0 commit comments