Skip to content

Commit a1b4551

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

File tree

2 files changed

+101
-56
lines changed

2 files changed

+101
-56
lines changed

src/chains.rs

Lines changed: 100 additions & 55 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,25 @@ 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

549562
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)
551564
}
552565
}
553566

@@ -569,16 +582,20 @@ trait ChainFormatter {
569582
parent: &ChainItem,
570583
context: &RewriteContext<'_>,
571584
shape: Shape,
572-
) -> Option<()>;
585+
) -> Result<(), RewriteError>;
573586
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>;
575592
fn format_last_child(
576593
&mut self,
577594
context: &RewriteContext<'_>,
578595
shape: Shape,
579596
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;
582599
// Returns `Some` if the chain is only a root, None otherwise.
583600
fn pure_root(&mut self) -> Option<String>;
584601
}
@@ -621,12 +638,16 @@ impl<'a> ChainFormatterShared<'a> {
621638
}
622639
}
623640

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> {
625646
for item in &self.children[..self.children.len() - 1] {
626647
let rewrite = format_chain_item(item, context, child_shape, self.allow_overflow)?;
627648
self.rewrites.push(rewrite);
628649
}
629-
Some(())
650+
Ok(())
630651
}
631652

632653
// Rewrite the last child. The last child of a chain requires special treatment. We need to
@@ -667,8 +688,8 @@ impl<'a> ChainFormatterShared<'a> {
667688
context: &RewriteContext<'_>,
668689
shape: Shape,
669690
child_shape: Shape,
670-
) -> Option<()> {
671-
let last = self.children.last()?;
691+
) -> Result<(), RewriteError> {
692+
let last = self.children.last().unknown_error()?;
672693
let extendable = may_extend && last_line_extendable(&self.rewrites[0]);
673694
let prev_last_line_width = last_line_width(&self.rewrites[0]);
674695

@@ -692,11 +713,17 @@ impl<'a> ChainFormatterShared<'a> {
692713
&& self.rewrites.iter().all(|s| !s.contains('\n'))
693714
&& one_line_budget > 0;
694715
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)?
696719
} 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)?
698723
} 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)?
700727
};
701728

702729
let mut last_subexpr_str = None;
@@ -712,7 +739,7 @@ impl<'a> ChainFormatterShared<'a> {
712739
};
713740

714741
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) {
716743
// We allow overflowing here only if both of the following conditions match:
717744
// 1. The entire chain fits in a single line except the last child.
718745
// 2. `last_child_str.lines().count() >= 5`.
@@ -727,17 +754,18 @@ impl<'a> ChainFormatterShared<'a> {
727754
// last child on its own line, and compare two rewrites to choose which is
728755
// better.
729756
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 => {
733761
last_subexpr_str = Some(new_rw.clone());
734762
}
735-
Some(ref new_rw) if new_rw.lines().count() >= line_count => {
763+
Ok(ref new_rw) if new_rw.lines().count() >= line_count => {
736764
last_subexpr_str = Some(rw);
737765
self.fits_single_line = could_fit_single_line && all_in_one_line;
738766
}
739-
new_rw @ Some(..) => {
740-
last_subexpr_str = new_rw;
767+
Ok(new_rw) => {
768+
last_subexpr_str = Some(new_rw);
741769
}
742770
_ => {
743771
last_subexpr_str = Some(rw);
@@ -752,22 +780,28 @@ impl<'a> ChainFormatterShared<'a> {
752780
let last_shape = if context.use_block_indent() {
753781
last_shape
754782
} 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)?
756786
};
757787

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(())
761792
}
762793

763-
fn join_rewrites(&self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<String> {
794+
fn join_rewrites(&self, context: &RewriteContext<'_>, child_shape: Shape) -> RewriteResult {
764795
let connector = if self.fits_single_line {
765796
// Yay, we can put everything on one line.
766797
Cow::from("")
767798
} else {
768799
// Use new lines.
769800
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+
});
771805
}
772806
child_shape.to_string_with_newline(context.config)
773807
};
@@ -786,7 +820,7 @@ impl<'a> ChainFormatterShared<'a> {
786820
result.push_str(rewrite);
787821
}
788822

789-
Some(result)
823+
Ok(result)
790824
}
791825
}
792826

@@ -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,12 +894,12 @@ 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
}
861901

862-
fn join_rewrites(&self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<String> {
902+
fn join_rewrites(&self, context: &RewriteContext<'_>, child_shape: Shape) -> RewriteResult {
863903
self.shared.join_rewrites(context, child_shape)
864904
}
865905

@@ -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,12 +991,12 @@ 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
}
953998

954-
fn join_rewrites(&self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<String> {
999+
fn join_rewrites(&self, context: &RewriteContext<'_>, child_shape: Shape) -> RewriteResult {
9551000
self.shared.join_rewrites(context, child_shape)
9561001
}
9571002

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)