@@ -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,27 @@ 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
- let result = formatter. join_rewrites ( context, child_shape) ?;
550
- wrap_str ( result, context. config . max_width ( ) , shape)
562
+ let result = formatter
563
+ . join_rewrites ( context, child_shape)
564
+ . max_width_error ( child_shape. width , children_span) ?;
565
+ wrap_str ( result, context. config . max_width ( ) , shape) . max_width_error ( shape. width , full_span)
551
566
}
552
567
}
553
568
@@ -569,15 +584,19 @@ trait ChainFormatter {
569
584
parent : & ChainItem ,
570
585
context : & RewriteContext < ' _ > ,
571
586
shape : Shape ,
572
- ) -> Option < ( ) > ;
587
+ ) -> Result < ( ) , RewriteError > ;
573
588
fn child_shape ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < Shape > ;
574
- fn format_children ( & mut self , context : & RewriteContext < ' _ > , child_shape : Shape ) -> Option < ( ) > ;
589
+ fn format_children (
590
+ & mut self ,
591
+ context : & RewriteContext < ' _ > ,
592
+ child_shape : Shape ,
593
+ ) -> Result < ( ) , RewriteError > ;
575
594
fn format_last_child (
576
595
& mut self ,
577
596
context : & RewriteContext < ' _ > ,
578
597
shape : Shape ,
579
598
child_shape : Shape ,
580
- ) -> Option < ( ) > ;
599
+ ) -> Result < ( ) , RewriteError > ;
581
600
fn join_rewrites ( & self , context : & RewriteContext < ' _ > , child_shape : Shape ) -> Option < String > ;
582
601
// Returns `Some` if the chain is only a root, None otherwise.
583
602
fn pure_root ( & mut self ) -> Option < String > ;
@@ -621,12 +640,16 @@ impl<'a> ChainFormatterShared<'a> {
621
640
}
622
641
}
623
642
624
- fn format_children ( & mut self , context : & RewriteContext < ' _ > , child_shape : Shape ) -> Option < ( ) > {
643
+ fn format_children (
644
+ & mut self ,
645
+ context : & RewriteContext < ' _ > ,
646
+ child_shape : Shape ,
647
+ ) -> Result < ( ) , RewriteError > {
625
648
for item in & self . children [ ..self . children . len ( ) - 1 ] {
626
649
let rewrite = format_chain_item ( item, context, child_shape, self . allow_overflow ) ?;
627
650
self . rewrites . push ( rewrite) ;
628
651
}
629
- Some ( ( ) )
652
+ Ok ( ( ) )
630
653
}
631
654
632
655
// Rewrite the last child. The last child of a chain requires special treatment. We need to
@@ -667,8 +690,8 @@ impl<'a> ChainFormatterShared<'a> {
667
690
context : & RewriteContext < ' _ > ,
668
691
shape : Shape ,
669
692
child_shape : Shape ,
670
- ) -> Option < ( ) > {
671
- let last = self . children . last ( ) ?;
693
+ ) -> Result < ( ) , RewriteError > {
694
+ let last = self . children . last ( ) . unknown_error ( ) ?;
672
695
let extendable = may_extend && last_line_extendable ( & self . rewrites [ 0 ] ) ;
673
696
let prev_last_line_width = last_line_width ( & self . rewrites [ 0 ] ) ;
674
697
@@ -692,11 +715,17 @@ impl<'a> ChainFormatterShared<'a> {
692
715
&& self . rewrites . iter ( ) . all ( |s| !s. contains ( '\n' ) )
693
716
&& one_line_budget > 0 ;
694
717
let last_shape = if all_in_one_line {
695
- shape. sub_width ( last. tries ) ?
718
+ shape
719
+ . sub_width ( last. tries )
720
+ . max_width_error ( shape. width , last. span ) ?
696
721
} else if extendable {
697
- child_shape. sub_width ( last. tries ) ?
722
+ child_shape
723
+ . sub_width ( last. tries )
724
+ . max_width_error ( child_shape. width , last. span ) ?
698
725
} else {
699
- child_shape. sub_width ( shape. rhs_overhead ( context. config ) + last. tries ) ?
726
+ child_shape
727
+ . sub_width ( shape. rhs_overhead ( context. config ) + last. tries )
728
+ . max_width_error ( child_shape. width , last. span ) ?
700
729
} ;
701
730
702
731
let mut last_subexpr_str = None ;
@@ -712,7 +741,7 @@ impl<'a> ChainFormatterShared<'a> {
712
741
} ;
713
742
714
743
if let Some ( one_line_shape) = one_line_shape {
715
- if let Some ( rw) = last. rewrite ( context, one_line_shape) {
744
+ if let Ok ( rw) = last. rewrite_result ( context, one_line_shape) {
716
745
// We allow overflowing here only if both of the following conditions match:
717
746
// 1. The entire chain fits in a single line except the last child.
718
747
// 2. `last_child_str.lines().count() >= 5`.
@@ -727,17 +756,18 @@ impl<'a> ChainFormatterShared<'a> {
727
756
// last child on its own line, and compare two rewrites to choose which is
728
757
// better.
729
758
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 => {
759
+ . sub_width ( shape. rhs_overhead ( context. config ) + last. tries )
760
+ . max_width_error ( child_shape. width , last. span ) ?;
761
+ match last. rewrite_result ( context, last_shape) {
762
+ Ok ( ref new_rw) if !could_fit_single_line => {
733
763
last_subexpr_str = Some ( new_rw. clone ( ) ) ;
734
764
}
735
- Some ( ref new_rw) if new_rw. lines ( ) . count ( ) >= line_count => {
765
+ Ok ( ref new_rw) if new_rw. lines ( ) . count ( ) >= line_count => {
736
766
last_subexpr_str = Some ( rw) ;
737
767
self . fits_single_line = could_fit_single_line && all_in_one_line;
738
768
}
739
- new_rw @ Some ( .. ) => {
740
- last_subexpr_str = new_rw;
769
+ Ok ( new_rw ) => {
770
+ last_subexpr_str = Some ( new_rw) ;
741
771
}
742
772
_ => {
743
773
last_subexpr_str = Some ( rw) ;
@@ -752,12 +782,15 @@ impl<'a> ChainFormatterShared<'a> {
752
782
let last_shape = if context. use_block_indent ( ) {
753
783
last_shape
754
784
} else {
755
- child_shape. sub_width ( shape. rhs_overhead ( context. config ) + last. tries ) ?
785
+ child_shape
786
+ . sub_width ( shape. rhs_overhead ( context. config ) + last. tries )
787
+ . max_width_error ( child_shape. width , last. span ) ?
756
788
} ;
757
789
758
- last_subexpr_str = last_subexpr_str. or_else ( || last. rewrite ( context, last_shape) ) ;
759
- self . rewrites . push ( last_subexpr_str?) ;
760
- Some ( ( ) )
790
+ let last_subexpr_str =
791
+ last_subexpr_str. unwrap_or ( last. rewrite_result ( context, last_shape) ?) ;
792
+ self . rewrites . push ( last_subexpr_str) ;
793
+ Ok ( ( ) )
761
794
}
762
795
763
796
fn join_rewrites ( & self , context : & RewriteContext < ' _ > , child_shape : Shape ) -> Option < String > {
@@ -766,6 +799,7 @@ impl<'a> ChainFormatterShared<'a> {
766
799
Cow :: from ( "" )
767
800
} else {
768
801
// Use new lines.
802
+ // TODO question new variant or max width err
769
803
if context. force_one_line_chain . get ( ) {
770
804
return None ;
771
805
}
@@ -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,7 +894,7 @@ 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
}
@@ -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,7 +991,7 @@ 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
}
0 commit comments