@@ -17,7 +17,7 @@ use crate::lists::{
17
17
use crate :: macros:: { rewrite_macro, MacroPosition } ;
18
18
use crate :: overflow;
19
19
use crate :: pairs:: { rewrite_pair, PairParts } ;
20
- use crate :: rewrite:: { Rewrite , RewriteContext } ;
20
+ use crate :: rewrite:: { Rewrite , RewriteContext , RewriteError , RewriteErrorExt , RewriteResult } ;
21
21
use crate :: shape:: Shape ;
22
22
use crate :: source_map:: SpanUtils ;
23
23
use crate :: spanned:: Spanned ;
@@ -209,12 +209,16 @@ impl Rewrite for ast::AssocItemConstraint {
209
209
210
210
impl Rewrite for ast:: AssocItemConstraintKind {
211
211
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
212
+ self . rewrite_result ( context, shape) . ok ( )
213
+ }
214
+
215
+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
212
216
match self {
213
217
ast:: AssocItemConstraintKind :: Equality { term } => match term {
214
- Term :: Ty ( ty) => ty. rewrite ( context, shape) ,
215
- Term :: Const ( c) => c. rewrite ( context, shape) ,
218
+ Term :: Ty ( ty) => ty. rewrite_result ( context, shape) ,
219
+ Term :: Const ( c) => c. rewrite_result ( context, shape) ,
216
220
} ,
217
- ast:: AssocItemConstraintKind :: Bound { bounds } => bounds. rewrite ( context, shape) ,
221
+ ast:: AssocItemConstraintKind :: Bound { bounds } => bounds. rewrite_result ( context, shape) ,
218
222
}
219
223
}
220
224
}
@@ -286,7 +290,7 @@ fn format_function_type<'a, I>(
286
290
span : Span ,
287
291
context : & RewriteContext < ' _ > ,
288
292
shape : Shape ,
289
- ) -> Option < String >
293
+ ) -> RewriteResult
290
294
where
291
295
I : ExactSizeIterator ,
292
296
<I as Iterator >:: Item : Deref ,
@@ -296,12 +300,12 @@ where
296
300
297
301
let ty_shape = match context. config . indent_style ( ) {
298
302
// 4 = " -> "
299
- IndentStyle :: Block => shape. offset_left ( 4 ) ?,
300
- IndentStyle :: Visual => shape. block_left ( 4 ) ?,
303
+ IndentStyle :: Block => shape. offset_left ( 4 ) . max_width_error ( shape . width , span ) ?,
304
+ IndentStyle :: Visual => shape. block_left ( 4 ) . max_width_error ( shape . width , span ) ?,
301
305
} ;
302
306
let output = match * output {
303
307
FnRetTy :: Ty ( ref ty) => {
304
- let type_str = ty. rewrite ( context, ty_shape) ?;
308
+ let type_str = ty. rewrite_result ( context, ty_shape) ?;
305
309
format ! ( " -> {type_str}" )
306
310
}
307
311
FnRetTy :: Default ( ..) => String :: new ( ) ,
@@ -314,7 +318,10 @@ where
314
318
)
315
319
} else {
316
320
// 2 for ()
317
- let budget = shape. width . checked_sub ( 2 ) ?;
321
+ let budget = shape
322
+ . width
323
+ . checked_sub ( 2 )
324
+ . max_width_error ( shape. width , span) ?;
318
325
// 1 for (
319
326
let offset = shape. indent + 1 ;
320
327
Shape :: legacy ( budget, offset)
@@ -327,7 +334,8 @@ where
327
334
let list_hi = context. snippet_provider . span_before ( span, ")" ) ;
328
335
let comment = context
329
336
. snippet_provider
330
- . span_to_snippet ( mk_sp ( list_lo, list_hi) ) ?
337
+ . span_to_snippet ( mk_sp ( list_lo, list_hi) )
338
+ . unknown_error ( ) ?
331
339
. trim ( ) ;
332
340
let comment = if comment. starts_with ( "//" ) {
333
341
format ! (
@@ -367,7 +375,7 @@ where
367
375
. trailing_separator ( trailing_separator)
368
376
. ends_with_newline ( tactic. ends_with_newline ( context. config . indent_style ( ) ) )
369
377
. preserve_newline ( true ) ;
370
- ( write_list ( & item_vec, & fmt) ?, tactic)
378
+ ( write_list ( & item_vec, & fmt) . unknown_error ( ) ?, tactic)
371
379
} ;
372
380
373
381
let args = if tactic == DefinitiveListTactic :: Horizontal
@@ -384,9 +392,9 @@ where
384
392
)
385
393
} ;
386
394
if output. is_empty ( ) || last_line_width ( & args) + first_line_width ( & output) <= shape. width {
387
- Some ( format ! ( "{args}{output}" ) )
395
+ Ok ( format ! ( "{args}{output}" ) )
388
396
} else {
389
- Some ( format ! (
397
+ Ok ( format ! (
390
398
"{}\n {}{}" ,
391
399
args,
392
400
list_shape. indent. to_string( context. config) ,
@@ -458,10 +466,14 @@ impl Rewrite for ast::WherePredicate {
458
466
459
467
impl Rewrite for ast:: GenericArg {
460
468
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
469
+ self . rewrite_result ( context, shape) . ok ( )
470
+ }
471
+
472
+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
461
473
match * self {
462
- ast:: GenericArg :: Lifetime ( ref lt) => lt. rewrite ( context, shape) ,
463
- ast:: GenericArg :: Type ( ref ty) => ty. rewrite ( context, shape) ,
464
- ast:: GenericArg :: Const ( ref const_) => const_. rewrite ( context, shape) ,
474
+ ast:: GenericArg :: Lifetime ( ref lt) => lt. rewrite_result ( context, shape) ,
475
+ ast:: GenericArg :: Type ( ref ty) => ty. rewrite_result ( context, shape) ,
476
+ ast:: GenericArg :: Const ( ref const_) => const_. rewrite_result ( context, shape) ,
465
477
}
466
478
}
467
479
}
@@ -496,7 +508,8 @@ fn rewrite_generic_args(
496
508
data. span ,
497
509
context,
498
510
shape,
499
- ) ,
511
+ )
512
+ . ok ( ) ,
500
513
_ => Some ( "" . to_owned ( ) ) ,
501
514
}
502
515
}
@@ -538,6 +551,10 @@ impl Rewrite for ast::Lifetime {
538
551
539
552
impl Rewrite for ast:: GenericBound {
540
553
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
554
+ self . rewrite_result ( context, shape) . ok ( )
555
+ }
556
+
557
+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
541
558
match * self {
542
559
ast:: GenericBound :: Trait (
543
560
ref poly_trait_ref,
@@ -558,13 +575,15 @@ impl Rewrite for ast::GenericBound {
558
575
asyncness. push ( ' ' ) ;
559
576
}
560
577
let polarity = polarity. as_str ( ) ;
561
- let shape = shape. offset_left ( constness. len ( ) + polarity. len ( ) ) ?;
578
+ let shape = shape
579
+ . offset_left ( constness. len ( ) + polarity. len ( ) )
580
+ . max_width_error ( shape. width , self . span ( ) ) ?;
562
581
poly_trait_ref
563
- . rewrite ( context, shape)
582
+ . rewrite_result ( context, shape)
564
583
. map ( |s| format ! ( "{constness}{asyncness}{polarity}{s}" ) )
565
584
. map ( |s| if has_paren { format ! ( "({})" , s) } else { s } )
566
585
}
567
- ast:: GenericBound :: Outlives ( ref lifetime) => lifetime. rewrite ( context, shape) ,
586
+ ast:: GenericBound :: Outlives ( ref lifetime) => lifetime. rewrite_result ( context, shape) ,
568
587
}
569
588
}
570
589
}
@@ -683,15 +702,29 @@ impl Rewrite for ast::TraitRef {
683
702
684
703
impl Rewrite for ast:: Ty {
685
704
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
705
+ self . rewrite_result ( context, shape) . ok ( )
706
+ }
707
+
708
+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
686
709
match self . kind {
687
710
ast:: TyKind :: TraitObject ( ref bounds, tobj_syntax) => {
688
711
// we have to consider 'dyn' keyword is used or not!!!
689
712
let ( shape, prefix) = match tobj_syntax {
690
- ast:: TraitObjectSyntax :: Dyn => ( shape. offset_left ( 4 ) ?, "dyn " ) ,
691
- ast:: TraitObjectSyntax :: DynStar => ( shape. offset_left ( 5 ) ?, "dyn* " ) ,
713
+ ast:: TraitObjectSyntax :: Dyn => (
714
+ shape
715
+ . offset_left ( 4 )
716
+ . max_width_error ( shape. width , self . span ( ) ) ?,
717
+ "dyn " ,
718
+ ) ,
719
+ ast:: TraitObjectSyntax :: DynStar => (
720
+ shape
721
+ . offset_left ( 5 )
722
+ . max_width_error ( shape. width , self . span ( ) ) ?,
723
+ "dyn* " ,
724
+ ) ,
692
725
ast:: TraitObjectSyntax :: None => ( shape, "" ) ,
693
726
} ;
694
- let mut res = bounds. rewrite ( context, shape) ?;
727
+ let mut res = bounds. rewrite_result ( context, shape) ?;
695
728
// We may have falsely removed a trailing `+` inside macro call.
696
729
if context. inside_macro ( )
697
730
&& bounds. len ( ) == 1
@@ -700,15 +733,15 @@ impl Rewrite for ast::Ty {
700
733
{
701
734
res. push ( '+' ) ;
702
735
}
703
- Some ( format ! ( "{prefix}{res}" ) )
736
+ Ok ( format ! ( "{prefix}{res}" ) )
704
737
}
705
738
ast:: TyKind :: Ptr ( ref mt) => {
706
739
let prefix = match mt. mutbl {
707
740
Mutability :: Mut => "*mut " ,
708
741
Mutability :: Not => "*const " ,
709
742
} ;
710
743
711
- rewrite_unary_prefix ( context, prefix, & * mt. ty , shape)
744
+ rewrite_unary_prefix ( context, prefix, & * mt. ty , shape) . unknown_error ( )
712
745
}
713
746
ast:: TyKind :: Ref ( ref lifetime, ref mt) => {
714
747
let mut_str = format_mutability ( mt. mutbl ) ;
@@ -719,8 +752,11 @@ impl Rewrite for ast::Ty {
719
752
let mut cmnt_lo = ref_hi;
720
753
721
754
if let Some ( ref lifetime) = * lifetime {
722
- let lt_budget = shape. width . checked_sub ( 2 + mut_len) ?;
723
- let lt_str = lifetime. rewrite (
755
+ let lt_budget = shape
756
+ . width
757
+ . checked_sub ( 2 + mut_len)
758
+ . max_width_error ( shape. width , self . span ( ) ) ?;
759
+ let lt_str = lifetime. rewrite_result (
724
760
context,
725
761
Shape :: legacy ( lt_budget, shape. indent + 2 + mut_len) ,
726
762
) ?;
@@ -733,7 +769,8 @@ impl Rewrite for ast::Ty {
733
769
before_lt_span,
734
770
shape,
735
771
true ,
736
- ) ?;
772
+ )
773
+ . unknown_error ( ) ?;
737
774
} else {
738
775
result. push_str ( & lt_str) ;
739
776
}
@@ -752,7 +789,8 @@ impl Rewrite for ast::Ty {
752
789
before_mut_span,
753
790
shape,
754
791
true ,
755
- ) ?;
792
+ )
793
+ . unknown_error ( ) ?;
756
794
} else {
757
795
result. push_str ( mut_str) ;
758
796
}
@@ -764,39 +802,47 @@ impl Rewrite for ast::Ty {
764
802
result = combine_strs_with_missing_comments (
765
803
context,
766
804
result. trim_end ( ) ,
767
- & mt. ty . rewrite ( context, shape) ?,
805
+ & mt. ty . rewrite_result ( context, shape) ?,
768
806
before_ty_span,
769
807
shape,
770
808
true ,
771
- ) ?;
809
+ )
810
+ . unknown_error ( ) ?;
772
811
} else {
773
812
let used_width = last_line_width ( & result) ;
774
- let budget = shape. width . checked_sub ( used_width) ?;
775
- let ty_str = mt
776
- . ty
777
- . rewrite ( context, Shape :: legacy ( budget, shape. indent + used_width) ) ?;
813
+ let budget = shape
814
+ . width
815
+ . checked_sub ( used_width)
816
+ . max_width_error ( shape. width , self . span ( ) ) ?;
817
+ let ty_str = mt. ty . rewrite_result (
818
+ context,
819
+ Shape :: legacy ( budget, shape. indent + used_width) ,
820
+ ) ?;
778
821
result. push_str ( & ty_str) ;
779
822
}
780
823
781
- Some ( result)
824
+ Ok ( result)
782
825
}
783
826
// FIXME: we drop any comments here, even though it's a silly place to put
784
827
// comments.
785
828
ast:: TyKind :: Paren ( ref ty) => {
786
829
if context. config . version ( ) == Version :: One
787
830
|| context. config . indent_style ( ) == IndentStyle :: Visual
788
831
{
789
- let budget = shape. width . checked_sub ( 2 ) ?;
832
+ let budget = shape
833
+ . width
834
+ . checked_sub ( 2 )
835
+ . max_width_error ( shape. width , self . span ( ) ) ?;
790
836
return ty
791
- . rewrite ( context, Shape :: legacy ( budget, shape. indent + 1 ) )
837
+ . rewrite_result ( context, Shape :: legacy ( budget, shape. indent + 1 ) )
792
838
. map ( |ty_str| format ! ( "({})" , ty_str) ) ;
793
839
}
794
840
795
841
// 2 = ()
796
842
if let Some ( sh) = shape. sub_width ( 2 ) {
797
- if let Some ( ref s) = ty. rewrite ( context, sh) {
843
+ if let Ok ( ref s) = ty. rewrite_result ( context, sh) {
798
844
if !s. contains ( '\n' ) {
799
- return Some ( format ! ( "({s})" ) ) ;
845
+ return Ok ( format ! ( "({s})" ) ) ;
800
846
}
801
847
}
802
848
}
@@ -805,26 +851,30 @@ impl Rewrite for ast::Ty {
805
851
let shape = shape
806
852
. block_indent ( context. config . tab_spaces ( ) )
807
853
. with_max_width ( context. config ) ;
808
- let rw = ty. rewrite ( context, shape) ?;
809
- Some ( format ! (
854
+ let rw = ty. rewrite_result ( context, shape) ?;
855
+ Ok ( format ! (
810
856
"({}{}{})" ,
811
857
shape. to_string_with_newline( context. config) ,
812
858
rw,
813
859
indent_str
814
860
) )
815
861
}
816
862
ast:: TyKind :: Slice ( ref ty) => {
817
- let budget = shape. width . checked_sub ( 4 ) ?;
818
- ty. rewrite ( context, Shape :: legacy ( budget, shape. indent + 1 ) )
863
+ let budget = shape
864
+ . width
865
+ . checked_sub ( 4 )
866
+ . max_width_error ( shape. width , self . span ( ) ) ?;
867
+ ty. rewrite_result ( context, Shape :: legacy ( budget, shape. indent + 1 ) )
819
868
. map ( |ty_str| format ! ( "[{}]" , ty_str) )
820
869
}
821
870
ast:: TyKind :: Tup ( ref items) => {
822
871
rewrite_tuple ( context, items. iter ( ) , self . span , shape, items. len ( ) == 1 )
872
+ . unknown_error ( )
823
873
}
824
- ast:: TyKind :: AnonStruct ( ..) => Some ( context. snippet ( self . span ) . to_owned ( ) ) ,
825
- ast:: TyKind :: AnonUnion ( ..) => Some ( context. snippet ( self . span ) . to_owned ( ) ) ,
874
+ ast:: TyKind :: AnonStruct ( ..) => Ok ( context. snippet ( self . span ) . to_owned ( ) ) ,
875
+ ast:: TyKind :: AnonUnion ( ..) => Ok ( context. snippet ( self . span ) . to_owned ( ) ) ,
826
876
ast:: TyKind :: Path ( ref q_self, ref path) => {
827
- rewrite_path ( context, PathContext :: Type , q_self, path, shape)
877
+ rewrite_path ( context, PathContext :: Type , q_self, path, shape) . unknown_error ( )
828
878
}
829
879
ast:: TyKind :: Array ( ref ty, ref repeats) => rewrite_pair (
830
880
& * * ty,
@@ -833,52 +883,57 @@ impl Rewrite for ast::Ty {
833
883
context,
834
884
shape,
835
885
SeparatorPlace :: Back ,
836
- ) ,
886
+ )
887
+ . unknown_error ( ) ,
837
888
ast:: TyKind :: Infer => {
838
889
if shape. width >= 1 {
839
- Some ( "_" . to_owned ( ) )
890
+ Ok ( "_" . to_owned ( ) )
840
891
} else {
841
- None
892
+ Err ( RewriteError :: ExceedsMaxWidth {
893
+ configured_width : shape. width ,
894
+ span : self . span ( ) ,
895
+ } )
842
896
}
843
897
}
844
898
ast:: TyKind :: BareFn ( ref bare_fn) => rewrite_bare_fn ( bare_fn, self . span , context, shape) ,
845
- ast:: TyKind :: Never => Some ( String :: from ( "!" ) ) ,
899
+ ast:: TyKind :: Never => Ok ( String :: from ( "!" ) ) ,
846
900
ast:: TyKind :: MacCall ( ref mac) => {
847
- rewrite_macro ( mac, None , context, shape, MacroPosition :: Expression )
901
+ rewrite_macro ( mac, None , context, shape, MacroPosition :: Expression ) . unknown_error ( )
848
902
}
849
- ast:: TyKind :: ImplicitSelf => Some ( String :: from ( "" ) ) ,
903
+ ast:: TyKind :: ImplicitSelf => Ok ( String :: from ( "" ) ) ,
850
904
ast:: TyKind :: ImplTrait ( _, ref it, ref captures) => {
851
905
// FIXME(precise_capturing): Implement formatting.
852
906
if captures. is_some ( ) {
853
- return None ;
907
+ return Err ( RewriteError :: Unknown ) ;
854
908
}
855
909
// Empty trait is not a parser error.
856
910
if it. is_empty ( ) {
857
- return Some ( "impl" . to_owned ( ) ) ;
911
+ return Ok ( "impl" . to_owned ( ) ) ;
858
912
}
859
913
let rw = if context. config . version ( ) == Version :: One {
860
- it. rewrite ( context, shape)
914
+ it. rewrite_result ( context, shape)
861
915
} else {
862
- join_bounds ( context, shape, it, false )
916
+ join_bounds ( context, shape, it, false ) . unknown_error ( )
863
917
} ;
864
918
rw. map ( |it_str| {
865
919
let space = if it_str. is_empty ( ) { "" } else { " " } ;
866
920
format ! ( "impl{}{}" , space, it_str)
867
921
} )
868
922
}
869
- ast:: TyKind :: CVarArgs => Some ( "..." . to_owned ( ) ) ,
870
- ast:: TyKind :: Dummy | ast:: TyKind :: Err ( _) => Some ( context. snippet ( self . span ) . to_owned ( ) ) ,
923
+ ast:: TyKind :: CVarArgs => Ok ( "..." . to_owned ( ) ) ,
924
+ ast:: TyKind :: Dummy | ast:: TyKind :: Err ( _) => Ok ( context. snippet ( self . span ) . to_owned ( ) ) ,
871
925
ast:: TyKind :: Typeof ( ref anon_const) => rewrite_call (
872
926
context,
873
927
"typeof" ,
874
928
& [ anon_const. value . clone ( ) ] ,
875
929
self . span ,
876
930
shape,
877
- ) ,
931
+ )
932
+ . unknown_error ( ) ,
878
933
ast:: TyKind :: Pat ( ref ty, ref pat) => {
879
- let ty = ty. rewrite ( context, shape) ?;
880
- let pat = pat. rewrite ( context, shape) ?;
881
- Some ( format ! ( "{ty} is {pat}" ) )
934
+ let ty = ty. rewrite_result ( context, shape) ?;
935
+ let pat = pat. rewrite_result ( context, shape) ?;
936
+ Ok ( format ! ( "{ty} is {pat}" ) )
882
937
}
883
938
}
884
939
}
@@ -889,7 +944,7 @@ fn rewrite_bare_fn(
889
944
span : Span ,
890
945
context : & RewriteContext < ' _ > ,
891
946
shape : Shape ,
892
- ) -> Option < String > {
947
+ ) -> RewriteResult {
893
948
debug ! ( "rewrite_bare_fn {:#?}" , shape) ;
894
949
895
950
let mut result = String :: with_capacity ( 128 ) ;
@@ -913,9 +968,14 @@ fn rewrite_bare_fn(
913
968
result. push_str ( "fn" ) ;
914
969
915
970
let func_ty_shape = if context. use_block_indent ( ) {
916
- shape. offset_left ( result. len ( ) ) ?
971
+ shape
972
+ . offset_left ( result. len ( ) )
973
+ . max_width_error ( shape. width , span) ?
917
974
} else {
918
- shape. visual_indent ( result. len ( ) ) . sub_width ( result. len ( ) ) ?
975
+ shape
976
+ . visual_indent ( result. len ( ) )
977
+ . sub_width ( result. len ( ) )
978
+ . max_width_error ( shape. width , span) ?
919
979
} ;
920
980
921
981
let rewrite = format_function_type (
@@ -929,7 +989,7 @@ fn rewrite_bare_fn(
929
989
930
990
result. push_str ( & rewrite) ;
931
991
932
- Some ( result)
992
+ Ok ( result)
933
993
}
934
994
935
995
fn is_generic_bounds_in_order ( generic_bounds : & [ ast:: GenericBound ] ) -> bool {
0 commit comments