@@ -67,7 +67,9 @@ use crate::config::{IndentStyle, StyleEdition};
67
67
use crate :: expr:: rewrite_call;
68
68
use crate :: lists:: extract_pre_comment;
69
69
use crate :: macros:: convert_try_mac;
70
- use crate :: rewrite:: { Rewrite , RewriteContext , RewriteError , RewriteErrorExt , RewriteResult } ;
70
+ use crate :: rewrite:: {
71
+ ExceedsMaxWidthError , Rewrite , RewriteContext , RewriteError , RewriteErrorExt , RewriteResult ,
72
+ } ;
71
73
use crate :: shape:: Shape ;
72
74
use crate :: source_map:: SpanUtils ;
73
75
use crate :: utils:: {
@@ -127,14 +129,15 @@ fn get_visual_style_child_shape(
127
129
shape : Shape ,
128
130
offset : usize ,
129
131
parent_overflowing : bool ,
130
- ) -> Option < Shape > {
132
+ span : Span ,
133
+ ) -> Result < Shape , ExceedsMaxWidthError > {
131
134
if !parent_overflowing {
132
135
shape
133
136
. with_max_width ( context. config )
134
- . offset_left ( offset)
137
+ . offset_left ( offset, span )
135
138
. map ( |s| s. visual_indent ( 0 ) )
136
139
} else {
137
- Some ( shape. visual_indent ( offset) )
140
+ Ok ( shape. visual_indent ( offset) )
138
141
}
139
142
}
140
143
@@ -280,9 +283,7 @@ impl Rewrite for ChainItem {
280
283
}
281
284
282
285
fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
283
- let shape = shape
284
- . sub_width ( self . tries )
285
- . max_width_error ( shape. width , self . span ) ?;
286
+ let shape = shape. sub_width ( self . tries , self . span ) ?;
286
287
let rewrite = match self . kind {
287
288
ChainItemKind :: Parent {
288
289
ref expr,
@@ -559,9 +560,7 @@ impl Rewrite for Chain {
559
560
let full_span = self . parent . span . with_hi ( children_span. hi ( ) ) ;
560
561
561
562
// Decide how to layout the rest of the chain.
562
- let child_shape = formatter
563
- . child_shape ( context, shape)
564
- . max_width_error ( shape. width , children_span) ?;
563
+ let child_shape = formatter. child_shape ( context, shape, children_span) ?;
565
564
566
565
formatter. format_children ( context, child_shape) ?;
567
566
formatter. format_last_child ( context, shape, child_shape) ?;
@@ -590,7 +589,12 @@ trait ChainFormatter {
590
589
context : & RewriteContext < ' _ > ,
591
590
shape : Shape ,
592
591
) -> Result < ( ) , RewriteError > ;
593
- fn child_shape ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < Shape > ;
592
+ fn child_shape (
593
+ & self ,
594
+ context : & RewriteContext < ' _ > ,
595
+ shape : Shape ,
596
+ span : Span ,
597
+ ) -> Result < Shape , ExceedsMaxWidthError > ;
594
598
fn format_children (
595
599
& mut self ,
596
600
context : & RewriteContext < ' _ > ,
@@ -720,29 +724,23 @@ impl<'a> ChainFormatterShared<'a> {
720
724
&& self . rewrites . iter ( ) . all ( |s| !s. contains ( '\n' ) )
721
725
&& one_line_budget > 0 ;
722
726
let last_shape = if all_in_one_line {
723
- shape
724
- . sub_width ( last. tries )
725
- . max_width_error ( shape. width , last. span ) ?
727
+ shape. sub_width ( last. tries , last. span ) ?
726
728
} else if extendable {
727
- child_shape
728
- . sub_width ( last. tries )
729
- . max_width_error ( child_shape. width , last. span ) ?
729
+ child_shape. sub_width ( last. tries , last. span ) ?
730
730
} else {
731
- child_shape
732
- . sub_width ( shape. rhs_overhead ( context. config ) + last. tries )
733
- . max_width_error ( child_shape. width , last. span ) ?
731
+ child_shape. sub_width ( shape. rhs_overhead ( context. config ) + last. tries , last. span ) ?
734
732
} ;
735
733
736
734
let mut last_subexpr_str = None ;
737
735
if all_in_one_line || extendable {
738
736
// First we try to 'overflow' the last child and see if it looks better than using
739
737
// vertical layout.
740
738
let one_line_shape = if context. use_block_indent ( ) {
741
- last_shape. offset_left ( almost_total)
739
+ last_shape. offset_left_opt ( almost_total)
742
740
} else {
743
741
last_shape
744
742
. visual_indent ( almost_total)
745
- . sub_width ( almost_total)
743
+ . sub_width_opt ( almost_total)
746
744
} ;
747
745
748
746
if let Some ( one_line_shape) = one_line_shape {
@@ -760,9 +758,10 @@ impl<'a> ChainFormatterShared<'a> {
760
758
// layout, just by looking at the overflowed rewrite. Now we rewrite the
761
759
// last child on its own line, and compare two rewrites to choose which is
762
760
// better.
763
- let last_shape = child_shape
764
- . sub_width ( shape. rhs_overhead ( context. config ) + last. tries )
765
- . max_width_error ( child_shape. width , last. span ) ?;
761
+ let last_shape = child_shape. sub_width (
762
+ shape. rhs_overhead ( context. config ) + last. tries ,
763
+ last. span ,
764
+ ) ?;
766
765
match last. rewrite_result ( context, last_shape) {
767
766
Ok ( ref new_rw) if !could_fit_single_line => {
768
767
last_subexpr_str = Some ( new_rw. clone ( ) ) ;
@@ -787,9 +786,7 @@ impl<'a> ChainFormatterShared<'a> {
787
786
let last_shape = if context. use_block_indent ( ) {
788
787
last_shape
789
788
} else {
790
- child_shape
791
- . sub_width ( shape. rhs_overhead ( context. config ) + last. tries )
792
- . max_width_error ( child_shape. width , last. span ) ?
789
+ child_shape. sub_width ( shape. rhs_overhead ( context. config ) + last. tries , last. span ) ?
793
790
} ;
794
791
795
792
let last_subexpr_str =
@@ -863,9 +860,7 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
863
860
if let ChainItemKind :: Comment ( ..) = item. kind {
864
861
break ;
865
862
}
866
- let shape = shape
867
- . offset_left ( root_rewrite. len ( ) )
868
- . max_width_error ( shape. width , item. span ) ?;
863
+ let shape = shape. offset_left ( root_rewrite. len ( ) , item. span ) ?;
869
864
match & item. rewrite_result ( context, shape) {
870
865
Ok ( rewrite) => root_rewrite. push_str ( rewrite) ,
871
866
Err ( _) => break ,
@@ -883,9 +878,14 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
883
878
Ok ( ( ) )
884
879
}
885
880
886
- fn child_shape ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < Shape > {
881
+ fn child_shape (
882
+ & self ,
883
+ context : & RewriteContext < ' _ > ,
884
+ shape : Shape ,
885
+ _span : Span ,
886
+ ) -> Result < Shape , ExceedsMaxWidthError > {
887
887
let block_end = self . root_ends_with_block ;
888
- Some ( get_block_child_shape ( block_end, context, shape) )
888
+ Ok ( get_block_child_shape ( block_end, context, shape) )
889
889
}
890
890
891
891
fn format_children (
@@ -955,8 +955,7 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
955
955
}
956
956
let child_shape = parent_shape
957
957
. visual_indent ( self . offset )
958
- . sub_width ( self . offset )
959
- . max_width_error ( parent_shape. width , item. span ) ?;
958
+ . sub_width ( self . offset , item. span ) ?;
960
959
let rewrite = item. rewrite_result ( context, child_shape) ?;
961
960
if filtered_str_fits ( & rewrite, context. config . max_width ( ) , shape) {
962
961
root_rewrite. push_str ( & rewrite) ;
@@ -975,13 +974,19 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
975
974
Ok ( ( ) )
976
975
}
977
976
978
- fn child_shape ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < Shape > {
977
+ fn child_shape (
978
+ & self ,
979
+ context : & RewriteContext < ' _ > ,
980
+ shape : Shape ,
981
+ span : Span ,
982
+ ) -> Result < Shape , ExceedsMaxWidthError > {
979
983
get_visual_style_child_shape (
980
984
context,
981
985
shape,
982
986
self . offset ,
983
987
// TODO(calebcartwright): self.shared.permissibly_overflowing_parent,
984
988
false ,
989
+ span,
985
990
)
986
991
}
987
992
0 commit comments