@@ -67,7 +67,9 @@ use crate::config::{IndentStyle, StyleEdition};
6767use crate :: expr:: rewrite_call;
6868use crate :: lists:: extract_pre_comment;
6969use 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+ } ;
7173use crate :: shape:: Shape ;
7274use crate :: source_map:: SpanUtils ;
7375use crate :: utils:: {
@@ -127,14 +129,15 @@ fn get_visual_style_child_shape(
127129 shape : Shape ,
128130 offset : usize ,
129131 parent_overflowing : bool ,
130- ) -> Option < Shape > {
132+ span : Span ,
133+ ) -> Result < Shape , ExceedsMaxWidthError > {
131134 if !parent_overflowing {
132135 shape
133136 . with_max_width ( context. config )
134- . offset_left ( offset)
137+ . offset_left ( offset, span )
135138 . map ( |s| s. visual_indent ( 0 ) )
136139 } else {
137- Some ( shape. visual_indent ( offset) )
140+ Ok ( shape. visual_indent ( offset) )
138141 }
139142}
140143
@@ -280,9 +283,7 @@ impl Rewrite for ChainItem {
280283 }
281284
282285 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 ) ?;
286287 let rewrite = match self . kind {
287288 ChainItemKind :: Parent {
288289 ref expr,
@@ -559,9 +560,7 @@ impl Rewrite for Chain {
559560 let full_span = self . parent . span . with_hi ( children_span. hi ( ) ) ;
560561
561562 // 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) ?;
565564
566565 formatter. format_children ( context, child_shape) ?;
567566 formatter. format_last_child ( context, shape, child_shape) ?;
@@ -590,7 +589,12 @@ trait ChainFormatter {
590589 context : & RewriteContext < ' _ > ,
591590 shape : Shape ,
592591 ) -> 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 > ;
594598 fn format_children (
595599 & mut self ,
596600 context : & RewriteContext < ' _ > ,
@@ -720,29 +724,23 @@ impl<'a> ChainFormatterShared<'a> {
720724 && self . rewrites . iter ( ) . all ( |s| !s. contains ( '\n' ) )
721725 && one_line_budget > 0 ;
722726 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 ) ?
726728 } 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 ) ?
730730 } 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 ) ?
734732 } ;
735733
736734 let mut last_subexpr_str = None ;
737735 if all_in_one_line || extendable {
738736 // First we try to 'overflow' the last child and see if it looks better than using
739737 // vertical layout.
740738 let one_line_shape = if context. use_block_indent ( ) {
741- last_shape. offset_left ( almost_total)
739+ last_shape. offset_left_opt ( almost_total)
742740 } else {
743741 last_shape
744742 . visual_indent ( almost_total)
745- . sub_width ( almost_total)
743+ . sub_width_opt ( almost_total)
746744 } ;
747745
748746 if let Some ( one_line_shape) = one_line_shape {
@@ -760,9 +758,10 @@ impl<'a> ChainFormatterShared<'a> {
760758 // layout, just by looking at the overflowed rewrite. Now we rewrite the
761759 // last child on its own line, and compare two rewrites to choose which is
762760 // 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+ ) ?;
766765 match last. rewrite_result ( context, last_shape) {
767766 Ok ( ref new_rw) if !could_fit_single_line => {
768767 last_subexpr_str = Some ( new_rw. clone ( ) ) ;
@@ -787,9 +786,7 @@ impl<'a> ChainFormatterShared<'a> {
787786 let last_shape = if context. use_block_indent ( ) {
788787 last_shape
789788 } 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 ) ?
793790 } ;
794791
795792 let last_subexpr_str =
@@ -863,9 +860,7 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
863860 if let ChainItemKind :: Comment ( ..) = item. kind {
864861 break ;
865862 }
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 ) ?;
869864 match & item. rewrite_result ( context, shape) {
870865 Ok ( rewrite) => root_rewrite. push_str ( rewrite) ,
871866 Err ( _) => break ,
@@ -883,9 +878,14 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
883878 Ok ( ( ) )
884879 }
885880
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 > {
887887 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) )
889889 }
890890
891891 fn format_children (
@@ -955,8 +955,7 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
955955 }
956956 let child_shape = parent_shape
957957 . 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 ) ?;
960959 let rewrite = item. rewrite_result ( context, child_shape) ?;
961960 if filtered_str_fits ( & rewrite, context. config . max_width ( ) , shape) {
962961 root_rewrite. push_str ( & rewrite) ;
@@ -975,13 +974,19 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
975974 Ok ( ( ) )
976975 }
977976
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 > {
979983 get_visual_style_child_shape (
980984 context,
981985 shape,
982986 self . offset ,
983987 // TODO(calebcartwright): self.shared.permissibly_overflowing_parent,
984988 false ,
989+ span,
985990 )
986991 }
987992
0 commit comments