@@ -92,12 +92,13 @@ fn format_chain_item(
92
92
context : & RewriteContext < ' _ > ,
93
93
rewrite_shape : Shape ,
94
94
allow_overflow : bool ,
95
- ) -> Option < String > {
95
+ ) -> RewriteResult {
96
+ // TODO this should only match RewriteError::ExceedsMaxWidth
96
97
if allow_overflow {
97
- item. rewrite ( context, rewrite_shape)
98
- . or_else ( || format_overflow_style ( item. span , context) )
98
+ item. rewrite_result ( context, rewrite_shape)
99
+ . or_else ( |_ | format_overflow_style ( item. span , context) . unknown_error ( ) )
99
100
} else {
100
- item. rewrite ( context, rewrite_shape)
101
+ item. rewrite_result ( context, rewrite_shape)
101
102
}
102
103
}
103
104
@@ -134,17 +135,17 @@ pub(crate) fn rewrite_chain(
134
135
expr : & ast:: Expr ,
135
136
context : & RewriteContext < ' _ > ,
136
137
shape : Shape ,
137
- ) -> Option < String > {
138
+ ) -> RewriteResult {
138
139
let chain = Chain :: from_ast ( expr, context) ;
139
140
debug ! ( "rewrite_chain {:?} {:?}" , chain, shape) ;
140
141
141
142
// If this is just an expression with some `?`s, then format it trivially and
142
143
// return early.
143
144
if chain. children . is_empty ( ) {
144
- return chain. parent . rewrite ( context, shape) ;
145
+ return chain. parent . rewrite_result ( context, shape) ;
145
146
}
146
147
147
- chain. rewrite ( context, shape)
148
+ chain. rewrite_result ( context, shape)
148
149
}
149
150
150
151
#[ derive( Debug ) ]
@@ -524,6 +525,10 @@ impl Chain {
524
525
525
526
impl Rewrite for Chain {
526
527
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
528
+ self . rewrite_result ( context, shape) . ok ( )
529
+ }
530
+
531
+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
527
532
debug ! ( "rewrite chain {:?} {:?}" , self , shape) ;
528
533
529
534
let mut formatter = match context. config . indent_style ( ) {
@@ -537,17 +542,25 @@ impl Rewrite for Chain {
537
542
538
543
formatter. format_root ( & self . parent , context, shape) ?;
539
544
if let Some ( result) = formatter. pure_root ( ) {
540
- return wrap_str ( result, context. config . max_width ( ) , shape) ;
545
+ return wrap_str ( result, context. config . max_width ( ) , shape)
546
+ . max_width_error ( shape. width , self . parent . span ) ;
541
547
}
542
548
549
+ let first = self . children . first ( ) . unwrap_or ( & self . parent ) ;
550
+ let last = self . children . last ( ) . unwrap_or ( & self . parent ) ;
551
+ let children_span = mk_sp ( first. span . lo ( ) , last. span . hi ( ) ) ;
552
+ let full_span = self . parent . span . with_hi ( children_span. hi ( ) ) ;
553
+
543
554
// Decide how to layout the rest of the chain.
544
- let child_shape = formatter. child_shape ( context, shape) ?;
555
+ let child_shape = formatter
556
+ . child_shape ( context, shape)
557
+ . max_width_error ( shape. width , children_span) ?;
545
558
546
559
formatter. format_children ( context, child_shape) ?;
547
560
formatter. format_last_child ( context, shape, child_shape) ?;
548
561
549
562
let result = formatter. join_rewrites ( context, child_shape) ?;
550
- wrap_str ( result, context. config . max_width ( ) , shape)
563
+ wrap_str ( result, context. config . max_width ( ) , shape) . max_width_error ( shape . width , full_span )
551
564
}
552
565
}
553
566
@@ -569,16 +582,20 @@ trait ChainFormatter {
569
582
parent : & ChainItem ,
570
583
context : & RewriteContext < ' _ > ,
571
584
shape : Shape ,
572
- ) -> Option < ( ) > ;
585
+ ) -> Result < ( ) , RewriteError > ;
573
586
fn child_shape ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < Shape > ;
574
- fn format_children ( & mut self , context : & RewriteContext < ' _ > , child_shape : Shape ) -> Option < ( ) > ;
587
+ fn format_children (
588
+ & mut self ,
589
+ context : & RewriteContext < ' _ > ,
590
+ child_shape : Shape ,
591
+ ) -> Result < ( ) , RewriteError > ;
575
592
fn format_last_child (
576
593
& mut self ,
577
594
context : & RewriteContext < ' _ > ,
578
595
shape : Shape ,
579
596
child_shape : Shape ,
580
- ) -> Option < ( ) > ;
581
- fn join_rewrites ( & self , context : & RewriteContext < ' _ > , child_shape : Shape ) -> Option < String > ;
597
+ ) -> Result < ( ) , RewriteError > ;
598
+ fn join_rewrites ( & self , context : & RewriteContext < ' _ > , child_shape : Shape ) -> RewriteResult ;
582
599
// Returns `Some` if the chain is only a root, None otherwise.
583
600
fn pure_root ( & mut self ) -> Option < String > ;
584
601
}
@@ -621,12 +638,16 @@ impl<'a> ChainFormatterShared<'a> {
621
638
}
622
639
}
623
640
624
- fn format_children ( & mut self , context : & RewriteContext < ' _ > , child_shape : Shape ) -> Option < ( ) > {
641
+ fn format_children (
642
+ & mut self ,
643
+ context : & RewriteContext < ' _ > ,
644
+ child_shape : Shape ,
645
+ ) -> Result < ( ) , RewriteError > {
625
646
for item in & self . children [ ..self . children . len ( ) - 1 ] {
626
647
let rewrite = format_chain_item ( item, context, child_shape, self . allow_overflow ) ?;
627
648
self . rewrites . push ( rewrite) ;
628
649
}
629
- Some ( ( ) )
650
+ Ok ( ( ) )
630
651
}
631
652
632
653
// Rewrite the last child. The last child of a chain requires special treatment. We need to
@@ -667,8 +688,8 @@ impl<'a> ChainFormatterShared<'a> {
667
688
context : & RewriteContext < ' _ > ,
668
689
shape : Shape ,
669
690
child_shape : Shape ,
670
- ) -> Option < ( ) > {
671
- let last = self . children . last ( ) ?;
691
+ ) -> Result < ( ) , RewriteError > {
692
+ let last = self . children . last ( ) . unknown_error ( ) ?;
672
693
let extendable = may_extend && last_line_extendable ( & self . rewrites [ 0 ] ) ;
673
694
let prev_last_line_width = last_line_width ( & self . rewrites [ 0 ] ) ;
674
695
@@ -692,11 +713,17 @@ impl<'a> ChainFormatterShared<'a> {
692
713
&& self . rewrites . iter ( ) . all ( |s| !s. contains ( '\n' ) )
693
714
&& one_line_budget > 0 ;
694
715
let last_shape = if all_in_one_line {
695
- shape. sub_width ( last. tries ) ?
716
+ shape
717
+ . sub_width ( last. tries )
718
+ . max_width_error ( shape. width , last. span ) ?
696
719
} else if extendable {
697
- child_shape. sub_width ( last. tries ) ?
720
+ child_shape
721
+ . sub_width ( last. tries )
722
+ . max_width_error ( child_shape. width , last. span ) ?
698
723
} else {
699
- child_shape. sub_width ( shape. rhs_overhead ( context. config ) + last. tries ) ?
724
+ child_shape
725
+ . sub_width ( shape. rhs_overhead ( context. config ) + last. tries )
726
+ . max_width_error ( child_shape. width , last. span ) ?
700
727
} ;
701
728
702
729
let mut last_subexpr_str = None ;
@@ -712,7 +739,7 @@ impl<'a> ChainFormatterShared<'a> {
712
739
} ;
713
740
714
741
if let Some ( one_line_shape) = one_line_shape {
715
- if let Some ( rw) = last. rewrite ( context, one_line_shape) {
742
+ if let Ok ( rw) = last. rewrite_result ( context, one_line_shape) {
716
743
// We allow overflowing here only if both of the following conditions match:
717
744
// 1. The entire chain fits in a single line except the last child.
718
745
// 2. `last_child_str.lines().count() >= 5`.
@@ -727,17 +754,18 @@ impl<'a> ChainFormatterShared<'a> {
727
754
// last child on its own line, and compare two rewrites to choose which is
728
755
// better.
729
756
let last_shape = child_shape
730
- . sub_width ( shape. rhs_overhead ( context. config ) + last. tries ) ?;
731
- match last. rewrite ( context, last_shape) {
732
- Some ( ref new_rw) if !could_fit_single_line => {
757
+ . sub_width ( shape. rhs_overhead ( context. config ) + last. tries )
758
+ . max_width_error ( child_shape. width , last. span ) ?;
759
+ match last. rewrite_result ( context, last_shape) {
760
+ Ok ( ref new_rw) if !could_fit_single_line => {
733
761
last_subexpr_str = Some ( new_rw. clone ( ) ) ;
734
762
}
735
- Some ( ref new_rw) if new_rw. lines ( ) . count ( ) >= line_count => {
763
+ Ok ( ref new_rw) if new_rw. lines ( ) . count ( ) >= line_count => {
736
764
last_subexpr_str = Some ( rw) ;
737
765
self . fits_single_line = could_fit_single_line && all_in_one_line;
738
766
}
739
- new_rw @ Some ( .. ) => {
740
- last_subexpr_str = new_rw;
767
+ Ok ( new_rw ) => {
768
+ last_subexpr_str = Some ( new_rw) ;
741
769
}
742
770
_ => {
743
771
last_subexpr_str = Some ( rw) ;
@@ -752,22 +780,28 @@ impl<'a> ChainFormatterShared<'a> {
752
780
let last_shape = if context. use_block_indent ( ) {
753
781
last_shape
754
782
} else {
755
- child_shape. sub_width ( shape. rhs_overhead ( context. config ) + last. tries ) ?
783
+ child_shape
784
+ . sub_width ( shape. rhs_overhead ( context. config ) + last. tries )
785
+ . max_width_error ( child_shape. width , last. span ) ?
756
786
} ;
757
787
758
- last_subexpr_str = last_subexpr_str. or_else ( || last. rewrite ( context, last_shape) ) ;
759
- self . rewrites . push ( last_subexpr_str?) ;
760
- Some ( ( ) )
788
+ let last_subexpr_str =
789
+ last_subexpr_str. unwrap_or ( last. rewrite_result ( context, last_shape) ?) ;
790
+ self . rewrites . push ( last_subexpr_str) ;
791
+ Ok ( ( ) )
761
792
}
762
793
763
- fn join_rewrites ( & self , context : & RewriteContext < ' _ > , child_shape : Shape ) -> Option < String > {
794
+ fn join_rewrites ( & self , context : & RewriteContext < ' _ > , child_shape : Shape ) -> RewriteResult {
764
795
let connector = if self . fits_single_line {
765
796
// Yay, we can put everything on one line.
766
797
Cow :: from ( "" )
767
798
} else {
768
799
// Use new lines.
769
800
if context. force_one_line_chain . get ( ) {
770
- return None ;
801
+ return Err ( RewriteError :: ExceedsMaxWidth {
802
+ configured_width : child_shape. width ,
803
+ span : self . children . last ( ) . unknown_error ( ) ?. span ,
804
+ } ) ;
771
805
}
772
806
child_shape. to_string_with_newline ( context. config )
773
807
} ;
@@ -786,7 +820,7 @@ impl<'a> ChainFormatterShared<'a> {
786
820
result. push_str ( rewrite) ;
787
821
}
788
822
789
- Some ( result)
823
+ Ok ( result)
790
824
}
791
825
}
792
826
@@ -811,8 +845,8 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
811
845
parent : & ChainItem ,
812
846
context : & RewriteContext < ' _ > ,
813
847
shape : Shape ,
814
- ) -> Option < ( ) > {
815
- let mut root_rewrite: String = parent. rewrite ( context, shape) ?;
848
+ ) -> Result < ( ) , RewriteError > {
849
+ let mut root_rewrite: String = parent. rewrite_result ( context, shape) ?;
816
850
817
851
let mut root_ends_with_block = parent. kind . is_block_like ( context, & root_rewrite) ;
818
852
let tab_width = context. config . tab_spaces ( ) . saturating_sub ( shape. offset ) ;
@@ -822,10 +856,12 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
822
856
if let ChainItemKind :: Comment ( ..) = item. kind {
823
857
break ;
824
858
}
825
- let shape = shape. offset_left ( root_rewrite. len ( ) ) ?;
826
- match & item. rewrite ( context, shape) {
827
- Some ( rewrite) => root_rewrite. push_str ( rewrite) ,
828
- None => break ,
859
+ let shape = shape
860
+ . offset_left ( root_rewrite. len ( ) )
861
+ . max_width_error ( shape. width , item. span ) ?;
862
+ match & item. rewrite_result ( context, shape) {
863
+ Ok ( rewrite) => root_rewrite. push_str ( rewrite) ,
864
+ Err ( _) => break ,
829
865
}
830
866
831
867
root_ends_with_block = last_line_extendable ( & root_rewrite) ;
@@ -837,15 +873,19 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
837
873
}
838
874
self . shared . rewrites . push ( root_rewrite) ;
839
875
self . root_ends_with_block = root_ends_with_block;
840
- Some ( ( ) )
876
+ Ok ( ( ) )
841
877
}
842
878
843
879
fn child_shape ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < Shape > {
844
880
let block_end = self . root_ends_with_block ;
845
881
Some ( get_block_child_shape ( block_end, context, shape) )
846
882
}
847
883
848
- fn format_children ( & mut self , context : & RewriteContext < ' _ > , child_shape : Shape ) -> Option < ( ) > {
884
+ fn format_children (
885
+ & mut self ,
886
+ context : & RewriteContext < ' _ > ,
887
+ child_shape : Shape ,
888
+ ) -> Result < ( ) , RewriteError > {
849
889
self . shared . format_children ( context, child_shape)
850
890
}
851
891
@@ -854,12 +894,12 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
854
894
context : & RewriteContext < ' _ > ,
855
895
shape : Shape ,
856
896
child_shape : Shape ,
857
- ) -> Option < ( ) > {
897
+ ) -> Result < ( ) , RewriteError > {
858
898
self . shared
859
899
. format_last_child ( true , context, shape, child_shape)
860
900
}
861
901
862
- fn join_rewrites ( & self , context : & RewriteContext < ' _ > , child_shape : Shape ) -> Option < String > {
902
+ fn join_rewrites ( & self , context : & RewriteContext < ' _ > , child_shape : Shape ) -> RewriteResult {
863
903
self . shared . join_rewrites ( context, child_shape)
864
904
}
865
905
@@ -890,9 +930,9 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
890
930
parent : & ChainItem ,
891
931
context : & RewriteContext < ' _ > ,
892
932
shape : Shape ,
893
- ) -> Option < ( ) > {
933
+ ) -> Result < ( ) , RewriteError > {
894
934
let parent_shape = shape. visual_indent ( 0 ) ;
895
- let mut root_rewrite = parent. rewrite ( context, parent_shape) ?;
935
+ let mut root_rewrite = parent. rewrite_result ( context, parent_shape) ?;
896
936
let multiline = root_rewrite. contains ( '\n' ) ;
897
937
self . offset = if multiline {
898
938
last_line_width ( & root_rewrite) . saturating_sub ( shape. used_width ( ) )
@@ -904,18 +944,19 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
904
944
let item = & self . shared . children [ 0 ] ;
905
945
if let ChainItemKind :: Comment ( ..) = item. kind {
906
946
self . shared . rewrites . push ( root_rewrite) ;
907
- return Some ( ( ) ) ;
947
+ return Ok ( ( ) ) ;
908
948
}
909
949
let child_shape = parent_shape
910
950
. visual_indent ( self . offset )
911
- . sub_width ( self . offset ) ?;
912
- let rewrite = item. rewrite ( context, child_shape) ?;
951
+ . sub_width ( self . offset )
952
+ . max_width_error ( parent_shape. width , item. span ) ?;
953
+ let rewrite = item. rewrite_result ( context, child_shape) ?;
913
954
if filtered_str_fits ( & rewrite, context. config . max_width ( ) , shape) {
914
955
root_rewrite. push_str ( & rewrite) ;
915
956
} else {
916
957
// We couldn't fit in at the visual indent, try the last
917
958
// indent.
918
- let rewrite = item. rewrite ( context, parent_shape) ?;
959
+ let rewrite = item. rewrite_result ( context, parent_shape) ?;
919
960
root_rewrite. push_str ( & rewrite) ;
920
961
self . offset = 0 ;
921
962
}
@@ -924,7 +965,7 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
924
965
}
925
966
926
967
self . shared . rewrites . push ( root_rewrite) ;
927
- Some ( ( ) )
968
+ Ok ( ( ) )
928
969
}
929
970
930
971
fn child_shape ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < Shape > {
@@ -937,7 +978,11 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
937
978
)
938
979
}
939
980
940
- fn format_children ( & mut self , context : & RewriteContext < ' _ > , child_shape : Shape ) -> Option < ( ) > {
981
+ fn format_children (
982
+ & mut self ,
983
+ context : & RewriteContext < ' _ > ,
984
+ child_shape : Shape ,
985
+ ) -> Result < ( ) , RewriteError > {
941
986
self . shared . format_children ( context, child_shape)
942
987
}
943
988
@@ -946,12 +991,12 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
946
991
context : & RewriteContext < ' _ > ,
947
992
shape : Shape ,
948
993
child_shape : Shape ,
949
- ) -> Option < ( ) > {
994
+ ) -> Result < ( ) , RewriteError > {
950
995
self . shared
951
996
. format_last_child ( false , context, shape, child_shape)
952
997
}
953
998
954
- fn join_rewrites ( & self , context : & RewriteContext < ' _ > , child_shape : Shape ) -> Option < String > {
999
+ fn join_rewrites ( & self , context : & RewriteContext < ' _ > , child_shape : Shape ) -> RewriteResult {
955
1000
self . shared . join_rewrites ( context, child_shape)
956
1001
}
957
1002
0 commit comments