Skip to content

Commit 72f445e

Browse files
committed
update rewrite_chain to return RewriteResult
1 parent 40f5075 commit 72f445e

File tree

2 files changed

+96
-51
lines changed

2 files changed

+96
-51
lines changed

src/chains.rs

Lines changed: 95 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,13 @@ fn format_chain_item(
9292
context: &RewriteContext<'_>,
9393
rewrite_shape: Shape,
9494
allow_overflow: bool,
95-
) -> Option<String> {
95+
) -> RewriteResult {
96+
// TODO this should only match RewriteError::ExceedsMaxWidth
9697
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())
99100
} else {
100-
item.rewrite(context, rewrite_shape)
101+
item.rewrite_result(context, rewrite_shape)
101102
}
102103
}
103104

@@ -134,17 +135,17 @@ pub(crate) fn rewrite_chain(
134135
expr: &ast::Expr,
135136
context: &RewriteContext<'_>,
136137
shape: Shape,
137-
) -> Option<String> {
138+
) -> RewriteResult {
138139
let chain = Chain::from_ast(expr, context);
139140
debug!("rewrite_chain {:?} {:?}", chain, shape);
140141

141142
// If this is just an expression with some `?`s, then format it trivially and
142143
// return early.
143144
if chain.children.is_empty() {
144-
return chain.parent.rewrite(context, shape);
145+
return chain.parent.rewrite_result(context, shape);
145146
}
146147

147-
chain.rewrite(context, shape)
148+
chain.rewrite_result(context, shape)
148149
}
149150

150151
#[derive(Debug)]
@@ -524,6 +525,10 @@ impl Chain {
524525

525526
impl Rewrite for Chain {
526527
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 {
527532
debug!("rewrite chain {:?} {:?}", self, shape);
528533

529534
let mut formatter = match context.config.indent_style() {
@@ -537,17 +542,27 @@ impl Rewrite for Chain {
537542

538543
formatter.format_root(&self.parent, context, shape)?;
539544
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);
541547
}
542548

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+
543554
// 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)?;
545558

546559
formatter.format_children(context, child_shape)?;
547560
formatter.format_last_child(context, shape, child_shape)?;
548561

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)
551566
}
552567
}
553568

@@ -569,15 +584,19 @@ trait ChainFormatter {
569584
parent: &ChainItem,
570585
context: &RewriteContext<'_>,
571586
shape: Shape,
572-
) -> Option<()>;
587+
) -> Result<(), RewriteError>;
573588
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>;
575594
fn format_last_child(
576595
&mut self,
577596
context: &RewriteContext<'_>,
578597
shape: Shape,
579598
child_shape: Shape,
580-
) -> Option<()>;
599+
) -> Result<(), RewriteError>;
581600
fn join_rewrites(&self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<String>;
582601
// Returns `Some` if the chain is only a root, None otherwise.
583602
fn pure_root(&mut self) -> Option<String>;
@@ -621,12 +640,16 @@ impl<'a> ChainFormatterShared<'a> {
621640
}
622641
}
623642

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> {
625648
for item in &self.children[..self.children.len() - 1] {
626649
let rewrite = format_chain_item(item, context, child_shape, self.allow_overflow)?;
627650
self.rewrites.push(rewrite);
628651
}
629-
Some(())
652+
Ok(())
630653
}
631654

632655
// Rewrite the last child. The last child of a chain requires special treatment. We need to
@@ -667,8 +690,8 @@ impl<'a> ChainFormatterShared<'a> {
667690
context: &RewriteContext<'_>,
668691
shape: Shape,
669692
child_shape: Shape,
670-
) -> Option<()> {
671-
let last = self.children.last()?;
693+
) -> Result<(), RewriteError> {
694+
let last = self.children.last().unknown_error()?;
672695
let extendable = may_extend && last_line_extendable(&self.rewrites[0]);
673696
let prev_last_line_width = last_line_width(&self.rewrites[0]);
674697

@@ -692,11 +715,17 @@ impl<'a> ChainFormatterShared<'a> {
692715
&& self.rewrites.iter().all(|s| !s.contains('\n'))
693716
&& one_line_budget > 0;
694717
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)?
696721
} 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)?
698725
} 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)?
700729
};
701730

702731
let mut last_subexpr_str = None;
@@ -712,7 +741,7 @@ impl<'a> ChainFormatterShared<'a> {
712741
};
713742

714743
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) {
716745
// We allow overflowing here only if both of the following conditions match:
717746
// 1. The entire chain fits in a single line except the last child.
718747
// 2. `last_child_str.lines().count() >= 5`.
@@ -727,17 +756,18 @@ impl<'a> ChainFormatterShared<'a> {
727756
// last child on its own line, and compare two rewrites to choose which is
728757
// better.
729758
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 => {
733763
last_subexpr_str = Some(new_rw.clone());
734764
}
735-
Some(ref new_rw) if new_rw.lines().count() >= line_count => {
765+
Ok(ref new_rw) if new_rw.lines().count() >= line_count => {
736766
last_subexpr_str = Some(rw);
737767
self.fits_single_line = could_fit_single_line && all_in_one_line;
738768
}
739-
new_rw @ Some(..) => {
740-
last_subexpr_str = new_rw;
769+
Ok(new_rw) => {
770+
last_subexpr_str = Some(new_rw);
741771
}
742772
_ => {
743773
last_subexpr_str = Some(rw);
@@ -752,12 +782,15 @@ impl<'a> ChainFormatterShared<'a> {
752782
let last_shape = if context.use_block_indent() {
753783
last_shape
754784
} 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)?
756788
};
757789

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(())
761794
}
762795

763796
fn join_rewrites(&self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<String> {
@@ -766,6 +799,7 @@ impl<'a> ChainFormatterShared<'a> {
766799
Cow::from("")
767800
} else {
768801
// Use new lines.
802+
// TODO question new variant or max width err
769803
if context.force_one_line_chain.get() {
770804
return None;
771805
}
@@ -811,8 +845,8 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
811845
parent: &ChainItem,
812846
context: &RewriteContext<'_>,
813847
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)?;
816850

817851
let mut root_ends_with_block = parent.kind.is_block_like(context, &root_rewrite);
818852
let tab_width = context.config.tab_spaces().saturating_sub(shape.offset);
@@ -822,10 +856,12 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
822856
if let ChainItemKind::Comment(..) = item.kind {
823857
break;
824858
}
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,
829865
}
830866

831867
root_ends_with_block = last_line_extendable(&root_rewrite);
@@ -837,15 +873,19 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
837873
}
838874
self.shared.rewrites.push(root_rewrite);
839875
self.root_ends_with_block = root_ends_with_block;
840-
Some(())
876+
Ok(())
841877
}
842878

843879
fn child_shape(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<Shape> {
844880
let block_end = self.root_ends_with_block;
845881
Some(get_block_child_shape(block_end, context, shape))
846882
}
847883

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> {
849889
self.shared.format_children(context, child_shape)
850890
}
851891

@@ -854,7 +894,7 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
854894
context: &RewriteContext<'_>,
855895
shape: Shape,
856896
child_shape: Shape,
857-
) -> Option<()> {
897+
) -> Result<(), RewriteError> {
858898
self.shared
859899
.format_last_child(true, context, shape, child_shape)
860900
}
@@ -890,9 +930,9 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
890930
parent: &ChainItem,
891931
context: &RewriteContext<'_>,
892932
shape: Shape,
893-
) -> Option<()> {
933+
) -> Result<(), RewriteError> {
894934
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)?;
896936
let multiline = root_rewrite.contains('\n');
897937
self.offset = if multiline {
898938
last_line_width(&root_rewrite).saturating_sub(shape.used_width())
@@ -904,18 +944,19 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
904944
let item = &self.shared.children[0];
905945
if let ChainItemKind::Comment(..) = item.kind {
906946
self.shared.rewrites.push(root_rewrite);
907-
return Some(());
947+
return Ok(());
908948
}
909949
let child_shape = parent_shape
910950
.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)?;
913954
if filtered_str_fits(&rewrite, context.config.max_width(), shape) {
914955
root_rewrite.push_str(&rewrite);
915956
} else {
916957
// We couldn't fit in at the visual indent, try the last
917958
// indent.
918-
let rewrite = item.rewrite(context, parent_shape)?;
959+
let rewrite = item.rewrite_result(context, parent_shape)?;
919960
root_rewrite.push_str(&rewrite);
920961
self.offset = 0;
921962
}
@@ -924,7 +965,7 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
924965
}
925966

926967
self.shared.rewrites.push(root_rewrite);
927-
Some(())
968+
Ok(())
928969
}
929970

930971
fn child_shape(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<Shape> {
@@ -937,7 +978,11 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
937978
)
938979
}
939980

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> {
941986
self.shared.format_children(context, child_shape)
942987
}
943988

@@ -946,7 +991,7 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
946991
context: &RewriteContext<'_>,
947992
shape: Shape,
948993
child_shape: Shape,
949-
) -> Option<()> {
994+
) -> Result<(), RewriteError> {
950995
self.shared
951996
.format_last_child(false, context, shape, child_shape)
952997
}

src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ pub(crate) fn format_expr(
241241
ast::ExprKind::Try(..)
242242
| ast::ExprKind::Field(..)
243243
| ast::ExprKind::MethodCall(..)
244-
| ast::ExprKind::Await(_, _) => rewrite_chain(expr, context, shape),
244+
| ast::ExprKind::Await(_, _) => rewrite_chain(expr, context, shape).ok(),
245245
ast::ExprKind::MacCall(ref mac) => {
246246
rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|| {
247247
wrap_str(

0 commit comments

Comments
 (0)