@@ -40,7 +40,7 @@ pub(crate) fn rewrite_path(
40
40
qself : & Option < ptr:: P < ast:: QSelf > > ,
41
41
path : & ast:: Path ,
42
42
shape : Shape ,
43
- ) -> Option < String > {
43
+ ) -> RewriteResult {
44
44
let skip_count = qself. as_ref ( ) . map_or ( 0 , |x| x. position ) ;
45
45
46
46
// 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(
56
56
if let Some ( qself) = qself {
57
57
result. push ( '<' ) ;
58
58
59
- let fmt_ty = qself. ty . rewrite ( context, shape) ?;
59
+ let fmt_ty = qself. ty . rewrite_result ( context, shape) ?;
60
60
result. push_str ( & fmt_ty) ;
61
61
62
62
if skip_count > 0 {
@@ -66,7 +66,7 @@ pub(crate) fn rewrite_path(
66
66
}
67
67
68
68
// 3 = ">::".len()
69
- let shape = shape. sub_width ( 3 ) ?;
69
+ let shape = shape. sub_width ( 3 ) . max_width_error ( shape . width , path . span ) ?;
70
70
71
71
result = rewrite_path_segments (
72
72
PathContext :: Type ,
@@ -102,7 +102,7 @@ fn rewrite_path_segments<'a, I>(
102
102
span_hi : BytePos ,
103
103
context : & RewriteContext < ' _ > ,
104
104
shape : Shape ,
105
- ) -> Option < String >
105
+ ) -> RewriteResult
106
106
where
107
107
I : Iterator < Item = & ' a ast:: PathSegment > ,
108
108
{
@@ -121,7 +121,9 @@ where
121
121
}
122
122
123
123
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) ) ?;
125
127
let segment_string = rewrite_segment (
126
128
path_context,
127
129
segment,
@@ -134,7 +136,7 @@ where
134
136
buffer. push_str ( & segment_string) ;
135
137
}
136
138
137
- Some ( buffer)
139
+ Ok ( buffer)
138
140
}
139
141
140
142
#[ derive( Debug ) ]
@@ -183,8 +185,12 @@ impl<'a> Rewrite for SegmentParam<'a> {
183
185
184
186
impl Rewrite for ast:: PreciseCapturingArg {
185
187
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 {
186
192
match self {
187
- ast:: PreciseCapturingArg :: Lifetime ( lt) => lt. rewrite ( context, shape) ,
193
+ ast:: PreciseCapturingArg :: Lifetime ( lt) => lt. rewrite_result ( context, shape) ,
188
194
ast:: PreciseCapturingArg :: Arg ( p, _) => {
189
195
rewrite_path ( context, PathContext :: Type , & None , p, shape)
190
196
}
@@ -194,13 +200,20 @@ impl Rewrite for ast::PreciseCapturingArg {
194
200
195
201
impl Rewrite for ast:: AssocItemConstraint {
196
202
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 {
197
207
use ast:: AssocItemConstraintKind :: { Bound , Equality } ;
198
208
199
209
let mut result = String :: with_capacity ( 128 ) ;
200
210
result. push_str ( rewrite_ident ( context, self . ident ) ) ;
201
211
202
212
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 ) ?;
204
217
let shape = Shape :: legacy ( budget, shape. indent + result. len ( ) ) ;
205
218
let gen_str = rewrite_generic_args ( gen_args, context, shape, gen_args. span ( ) ) ?;
206
219
result. push_str ( & gen_str) ;
@@ -213,12 +226,15 @@ impl Rewrite for ast::AssocItemConstraint {
213
226
} ;
214
227
result. push_str ( infix) ;
215
228
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 ) ?;
217
233
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) ?;
219
235
result. push_str ( & rewrite) ;
220
236
221
- Some ( result)
237
+ Ok ( result)
222
238
}
223
239
}
224
240
@@ -255,16 +271,17 @@ fn rewrite_segment(
255
271
span_hi : BytePos ,
256
272
context : & RewriteContext < ' _ > ,
257
273
shape : Shape ,
258
- ) -> Option < String > {
274
+ ) -> RewriteResult {
259
275
let mut result = String :: with_capacity ( 128 ) ;
260
276
result. push_str ( rewrite_ident ( context, segment. ident ) ) ;
261
277
262
278
let ident_len = result. len ( ) ;
263
279
let shape = if context. use_block_indent ( ) {
264
- shape. offset_left ( ident_len) ?
280
+ shape. offset_left ( ident_len)
265
281
} 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) ) ?;
268
285
269
286
if let Some ( ref args) = segment. args {
270
287
let generics_str = rewrite_generic_args ( args, context, shape, mk_sp ( * span_lo, span_hi) ) ?;
@@ -295,7 +312,7 @@ fn rewrite_segment(
295
312
result. push_str ( & generics_str)
296
313
}
297
314
298
- Some ( result)
315
+ Ok ( result)
299
316
}
300
317
301
318
fn format_function_type < ' a , I > (
@@ -498,7 +515,7 @@ fn rewrite_generic_args(
498
515
context : & RewriteContext < ' _ > ,
499
516
shape : Shape ,
500
517
span : Span ,
501
- ) -> Option < String > {
518
+ ) -> RewriteResult {
502
519
match gen_args {
503
520
ast:: GenericArgs :: AngleBracketed ( ref data) if !data. args . is_empty ( ) => {
504
521
let args = data
@@ -515,6 +532,7 @@ fn rewrite_generic_args(
515
532
. collect :: < Vec < _ > > ( ) ;
516
533
517
534
overflow:: rewrite_with_angle_brackets ( context, "" , args. iter ( ) , shape, span)
535
+ . unknown_error ( )
518
536
}
519
537
ast:: GenericArgs :: Parenthesized ( ref data) => format_function_type (
520
538
data. inputs . iter ( ) . map ( |x| & * * x) ,
@@ -523,9 +541,8 @@ fn rewrite_generic_args(
523
541
data. span ,
524
542
context,
525
543
shape,
526
- )
527
- . ok ( ) ,
528
- _ => Some ( "" . to_owned ( ) ) ,
544
+ ) ,
545
+ _ => Ok ( "" . to_owned ( ) ) ,
529
546
}
530
547
}
531
548
@@ -717,23 +734,32 @@ impl Rewrite for ast::GenericParam {
717
734
718
735
impl Rewrite for ast:: PolyTraitRef {
719
736
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 {
720
741
if let Some ( lifetime_str) = rewrite_bound_params ( context, shape, & self . bound_generic_params )
721
742
{
722
743
// 6 is "for<> ".len()
723
744
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) ?;
727
749
728
- Some ( format ! ( "for<{lifetime_str}> {path_str}" ) )
750
+ Ok ( format ! ( "for<{lifetime_str}> {path_str}" ) )
729
751
} else {
730
- self . trait_ref . rewrite ( context, shape)
752
+ self . trait_ref . rewrite_result ( context, shape)
731
753
}
732
754
}
733
755
}
734
756
735
757
impl Rewrite for ast:: TraitRef {
736
758
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 {
737
763
rewrite_path ( context, PathContext :: Type , & None , & self . path , shape)
738
764
}
739
765
}
@@ -912,7 +938,7 @@ impl Rewrite for ast::Ty {
912
938
ast:: TyKind :: AnonStruct ( ..) => Ok ( context. snippet ( self . span ) . to_owned ( ) ) ,
913
939
ast:: TyKind :: AnonUnion ( ..) => Ok ( context. snippet ( self . span ) . to_owned ( ) ) ,
914
940
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)
916
942
}
917
943
ast:: TyKind :: Array ( ref ty, ref repeats) => rewrite_pair (
918
944
& * * ty,
0 commit comments