Skip to content

Commit 2a64f21

Browse files
camsteffenytmimi
authored andcommitted
Return Result from Shape methods
1 parent 1872642 commit 2a64f21

19 files changed

+284
-340
lines changed

src/attr.rs

+14-19
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,22 @@ fn argument_shape(
5656
shape: Shape,
5757
context: &RewriteContext<'_>,
5858
) -> Option<Shape> {
59-
match context.config.indent_style() {
59+
let shape = match context.config.indent_style() {
6060
IndentStyle::Block => {
6161
if combine {
62-
shape.offset_left(left)
62+
shape.offset_left_opt(left)?
6363
} else {
64-
Some(
65-
shape
66-
.block_indent(context.config.tab_spaces())
67-
.with_max_width(context.config),
68-
)
64+
shape
65+
.block_indent(context.config.tab_spaces())
66+
.with_max_width(context.config)
6967
}
7068
}
7169
IndentStyle::Visual => shape
7270
.visual_indent(0)
73-
.shrink_left(left)
74-
.and_then(|s| s.sub_width(right)),
75-
}
71+
.shrink_left_opt(left)?
72+
.sub_width_opt(right)?,
73+
};
74+
Some(shape)
7675
}
7776

7877
fn format_derive(
@@ -127,8 +126,8 @@ fn format_derive(
127126
context,
128127
)?;
129128
let one_line_shape = shape
130-
.offset_left("[derive()]".len() + prefix.len())?
131-
.sub_width("()]".len())?;
129+
.offset_left_opt("[derive()]".len() + prefix.len())?
130+
.sub_width_opt("()]".len())?;
132131
let one_line_budget = one_line_shape.width;
133132

134133
let tactic = definitive_tactic(
@@ -297,7 +296,7 @@ impl Rewrite for ast::MetaItem {
297296
&path,
298297
list.iter(),
299298
// 1 = "]"
300-
shape.sub_width(1).max_width_error(shape.width, self.span)?,
299+
shape.sub_width(1, self.span)?,
301300
self.span,
302301
context.config.attr_fn_like_width(),
303302
Some(if has_trailing_comma {
@@ -310,9 +309,7 @@ impl Rewrite for ast::MetaItem {
310309
ast::MetaItemKind::NameValue(ref lit) => {
311310
let path = rewrite_path(context, PathContext::Type, &None, &self.path, shape)?;
312311
// 3 = ` = `
313-
let lit_shape = shape
314-
.shrink_left(path.len() + 3)
315-
.max_width_error(shape.width, self.span)?;
312+
let lit_shape = shape.shrink_left(path.len() + 3, self.span)?;
316313
// `rewrite_literal` returns `None` when `lit` exceeds max
317314
// width. Since a literal is basically unformattable unless it
318315
// is a string literal (and only if `format_strings` is set),
@@ -369,9 +366,7 @@ impl Rewrite for ast::Attribute {
369366
}
370367

371368
// 1 = `[`
372-
let shape = shape
373-
.offset_left(prefix.len() + 1)
374-
.max_width_error(shape.width, self.span)?;
369+
let shape = shape.offset_left(prefix.len() + 1, self.span)?;
375370
Ok(meta.rewrite_result(context, shape).map_or_else(
376371
|_| snippet.to_owned(),
377372
|rw| match &self.kind {

src/chains.rs

+41-36
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ use crate::config::{IndentStyle, StyleEdition};
6767
use crate::expr::rewrite_call;
6868
use crate::lists::extract_pre_comment;
6969
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+
};
7173
use crate::shape::Shape;
7274
use crate::source_map::SpanUtils;
7375
use 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

src/closures.rs

+6-17
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ pub(crate) fn rewrite_closure(
5353
shape,
5454
)?;
5555
// 1 = space between `|...|` and body.
56-
let body_shape = shape
57-
.offset_left(extra_offset)
58-
.max_width_error(shape.width, span)?;
56+
let body_shape = shape.offset_left(extra_offset, span)?;
5957

6058
if let ast::ExprKind::Block(ref block, _) = body.kind {
6159
// The body of the closure is an empty block.
@@ -293,17 +291,12 @@ fn rewrite_closure_fn_decl(
293291
};
294292
// 4 = "|| {".len(), which is overconservative when the closure consists of
295293
// a single expression.
296-
let nested_shape = shape
297-
.shrink_left(binder.len() + const_.len() + immovable.len() + coro.len() + mover.len())
298-
.and_then(|shape| shape.sub_width(4))
299-
.max_width_error(shape.width, span)?;
294+
let offset = binder.len() + const_.len() + immovable.len() + coro.len() + mover.len();
295+
let nested_shape = shape.shrink_left(offset, span)?.sub_width(4, span)?;
300296

301297
// 1 = |
302298
let param_offset = nested_shape.indent + 1;
303-
let param_shape = nested_shape
304-
.offset_left(1)
305-
.max_width_error(nested_shape.width, span)?
306-
.visual_indent(0);
299+
let param_shape = nested_shape.offset_left(1, span)?.visual_indent(0);
307300
let ret_str = fn_decl.output.rewrite_result(context, param_shape)?;
308301

309302
let param_items = itemize_list(
@@ -328,9 +321,7 @@ fn rewrite_closure_fn_decl(
328321
horizontal_budget,
329322
);
330323
let param_shape = match tactic {
331-
DefinitiveListTactic::Horizontal => param_shape
332-
.sub_width(ret_str.len() + 1)
333-
.max_width_error(param_shape.width, span)?,
324+
DefinitiveListTactic::Horizontal => param_shape.sub_width(ret_str.len() + 1, span)?,
334325
_ => param_shape,
335326
};
336327

@@ -401,9 +392,7 @@ pub(crate) fn rewrite_last_closure(
401392
return Err(RewriteError::Unknown);
402393
}
403394

404-
let body_shape = shape
405-
.offset_left(extra_offset)
406-
.max_width_error(shape.width, expr.span)?;
395+
let body_shape = shape.offset_left(extra_offset, expr.span)?;
407396

408397
// We force to use block for the body of the closure for certain kinds of expressions.
409398
if is_block_closure_forced(context, body) {

0 commit comments

Comments
 (0)