Skip to content

Commit 67d69e0

Browse files
committed
modify rewrite_struct_lit, rewrite_struct_pat
- refactor rewrite_*** functions that call rewrite_path to return RewriteResult - modify rewrite_aligned_item method of AlignedItem
1 parent b372268 commit 67d69e0

File tree

3 files changed

+59
-46
lines changed

3 files changed

+59
-46
lines changed

src/expr.rs

+35-29
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::macros::{rewrite_macro, MacroPosition};
2222
use crate::matches::rewrite_match;
2323
use crate::overflow::{self, IntoOverflowableItem, OverflowableItem};
2424
use crate::pairs::{rewrite_all_pairs, rewrite_pair, PairParts};
25-
use crate::rewrite::{Rewrite, RewriteContext};
25+
use crate::rewrite::{Rewrite, RewriteContext, RewriteErrorExt, RewriteResult};
2626
use crate::shape::{Indent, Shape};
2727
use crate::source_map::{LineRangeUtils, SpanUtils};
2828
use crate::spanned::Spanned;
@@ -128,6 +128,7 @@ pub(crate) fn format_expr(
128128
expr.span,
129129
shape,
130130
)
131+
.ok()
131132
}
132133
ast::ExprKind::Tup(ref items) => {
133134
rewrite_tuple(context, items.iter(), expr.span, shape, items.len() == 1)
@@ -1595,7 +1596,7 @@ fn rewrite_struct_lit<'a>(
15951596
attrs: &[ast::Attribute],
15961597
span: Span,
15971598
shape: Shape,
1598-
) -> Option<String> {
1599+
) -> RewriteResult {
15991600
debug!("rewrite_struct_lit: shape {:?}", shape);
16001601

16011602
enum StructLitField<'a> {
@@ -1605,20 +1606,21 @@ fn rewrite_struct_lit<'a>(
16051606
}
16061607

16071608
// 2 = " {".len()
1608-
let path_shape = shape.sub_width(2)?;
1609-
let path_str = rewrite_path(context, PathContext::Expr, qself, path, path_shape).ok()?;
1609+
let path_shape = shape.sub_width(2).max_width_error(shape.width, span)?;
1610+
let path_str = rewrite_path(context, PathContext::Expr, qself, path, path_shape)?;
16101611

16111612
let has_base_or_rest = match struct_rest {
1612-
ast::StructRest::None if fields.is_empty() => return Some(format!("{path_str} {{}}")),
1613+
ast::StructRest::None if fields.is_empty() => return Ok(format!("{path_str} {{}}")),
16131614
ast::StructRest::Rest(_) if fields.is_empty() => {
1614-
return Some(format!("{path_str} {{ .. }}"));
1615+
return Ok(format!("{path_str} {{ .. }}"));
16151616
}
16161617
ast::StructRest::Rest(_) | ast::StructRest::Base(_) => true,
16171618
_ => false,
16181619
};
16191620

16201621
// Foo { a: Foo } - indent is +3, width is -5.
1621-
let (h_shape, v_shape) = struct_lit_shape(shape, context, path_str.len() + 3, 2)?;
1622+
let (h_shape, v_shape) = struct_lit_shape(shape, context, path_str.len() + 3, 2)
1623+
.max_width_error(shape.width, span)?;
16221624

16231625
let one_line_width = h_shape.map_or(0, |shape| shape.width);
16241626
let body_lo = context.snippet_provider.span_after(span, "{");
@@ -1631,7 +1633,8 @@ fn rewrite_struct_lit<'a>(
16311633
v_shape,
16321634
mk_sp(body_lo, span.hi()),
16331635
one_line_width,
1634-
)?
1636+
)
1637+
.unknown_error()?
16351638
} else {
16361639
let field_iter = fields.iter().map(StructLitField::Regular).chain(
16371640
match struct_rest {
@@ -1660,12 +1663,13 @@ fn rewrite_struct_lit<'a>(
16601663
let rewrite = |item: &StructLitField<'_>| match *item {
16611664
StructLitField::Regular(field) => {
16621665
// The 1 taken from the v_budget is for the comma.
1663-
rewrite_field(context, field, v_shape.sub_width(1)?, 0)
1666+
let v_shape = v_shape.sub_width(1)?;
1667+
rewrite_field(context, field, v_shape, 0).ok()
16641668
}
16651669
StructLitField::Base(expr) => {
16661670
// 2 = ..
1667-
expr.rewrite(context, v_shape.offset_left(2)?)
1668-
.map(|s| format!("..{}", s))
1671+
let v_shape = v_shape.sub_width(2)?;
1672+
expr.rewrite(context, v_shape).map(|s| format!("..{}", s))
16691673
}
16701674
StructLitField::Rest(_) => Some("..".to_owned()),
16711675
};
@@ -1697,12 +1701,12 @@ fn rewrite_struct_lit<'a>(
16971701
force_no_trailing_comma || has_base_or_rest || !context.use_block_indent(),
16981702
);
16991703

1700-
write_list(&item_vec, &fmt)?
1704+
write_list(&item_vec, &fmt).unknown_error()?
17011705
};
17021706

17031707
let fields_str =
17041708
wrap_struct_field(context, attrs, &fields_str, shape, v_shape, one_line_width)?;
1705-
Some(format!("{path_str} {{{fields_str}}}"))
1709+
Ok(format!("{path_str} {{{fields_str}}}"))
17061710

17071711
// FIXME if context.config.indent_style() == Visual, but we run out
17081712
// of space, we should fall back to BlockIndent.
@@ -1715,7 +1719,7 @@ pub(crate) fn wrap_struct_field(
17151719
shape: Shape,
17161720
nested_shape: Shape,
17171721
one_line_width: usize,
1718-
) -> Option<String> {
1722+
) -> RewriteResult {
17191723
let should_vertical = context.config.indent_style() == IndentStyle::Block
17201724
&& (fields_str.contains('\n')
17211725
|| !context.config.struct_lit_single_line()
@@ -1724,21 +1728,21 @@ pub(crate) fn wrap_struct_field(
17241728
let inner_attrs = &inner_attributes(attrs);
17251729
if inner_attrs.is_empty() {
17261730
if should_vertical {
1727-
Some(format!(
1731+
Ok(format!(
17281732
"{}{}{}",
17291733
nested_shape.indent.to_string_with_newline(context.config),
17301734
fields_str,
17311735
shape.indent.to_string_with_newline(context.config)
17321736
))
17331737
} else {
17341738
// One liner or visual indent.
1735-
Some(format!(" {fields_str} "))
1739+
Ok(format!(" {fields_str} "))
17361740
}
17371741
} else {
1738-
Some(format!(
1742+
Ok(format!(
17391743
"{}{}{}{}{}",
17401744
nested_shape.indent.to_string_with_newline(context.config),
1741-
inner_attrs.rewrite(context, shape)?,
1745+
inner_attrs.rewrite_result(context, shape)?,
17421746
nested_shape.indent.to_string_with_newline(context.config),
17431747
fields_str,
17441748
shape.indent.to_string_with_newline(context.config)
@@ -1755,38 +1759,40 @@ pub(crate) fn rewrite_field(
17551759
field: &ast::ExprField,
17561760
shape: Shape,
17571761
prefix_max_width: usize,
1758-
) -> Option<String> {
1762+
) -> RewriteResult {
17591763
if contains_skip(&field.attrs) {
1760-
return Some(context.snippet(field.span()).to_owned());
1764+
return Ok(context.snippet(field.span()).to_owned());
17611765
}
1762-
let mut attrs_str = field.attrs.rewrite(context, shape)?;
1766+
let mut attrs_str = field.attrs.rewrite_result(context, shape)?;
17631767
if !attrs_str.is_empty() {
17641768
attrs_str.push_str(&shape.indent.to_string_with_newline(context.config));
17651769
};
17661770
let name = context.snippet(field.ident.span);
17671771
if field.is_shorthand {
1768-
Some(attrs_str + name)
1772+
Ok(attrs_str + name)
17691773
} else {
17701774
let mut separator = String::from(struct_lit_field_separator(context.config));
17711775
for _ in 0..prefix_max_width.saturating_sub(name.len()) {
17721776
separator.push(' ');
17731777
}
17741778
let overhead = name.len() + separator.len();
1775-
let expr_shape = shape.offset_left(overhead)?;
1776-
let expr = field.expr.rewrite(context, expr_shape);
1779+
let expr_shape = shape
1780+
.offset_left(overhead)
1781+
.max_width_error(shape.width, field.span)?;
1782+
let expr = field.expr.rewrite_result(context, expr_shape);
17771783
let is_lit = matches!(field.expr.kind, ast::ExprKind::Lit(_));
17781784
match expr {
1779-
Some(ref e)
1785+
Ok(ref e)
17801786
if !is_lit && e.as_str() == name && context.config.use_field_init_shorthand() =>
17811787
{
1782-
Some(attrs_str + name)
1788+
Ok(attrs_str + name)
17831789
}
1784-
Some(e) => Some(format!("{attrs_str}{name}{separator}{e}")),
1785-
None => {
1790+
Ok(e) => Ok(format!("{attrs_str}{name}{separator}{e}")),
1791+
Err(_) => {
17861792
let expr_offset = shape.indent.block_indent(context.config);
17871793
let expr = field
17881794
.expr
1789-
.rewrite(context, Shape::indented(expr_offset, context.config));
1795+
.rewrite_result(context, Shape::indented(expr_offset, context.config));
17901796
expr.map(|s| {
17911797
format!(
17921798
"{}{}:\n{}{}",

src/patterns.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::lists::{
1313
use crate::macros::{rewrite_macro, MacroPosition};
1414
use crate::overflow;
1515
use crate::pairs::{rewrite_pair, PairParts};
16-
use crate::rewrite::{Rewrite, RewriteContext};
16+
use crate::rewrite::{Rewrite, RewriteContext, RewriteErrorExt, RewriteResult};
1717
use crate::shape::Shape;
1818
use crate::source_map::SpanUtils;
1919
use crate::spanned::Spanned;
@@ -292,7 +292,8 @@ impl Rewrite for Pat {
292292
self.span,
293293
context,
294294
shape,
295-
),
295+
)
296+
.ok(),
296297
PatKind::MacCall(ref mac) => {
297298
rewrite_macro(mac, None, context, shape, MacroPosition::Pat)
298299
}
@@ -313,20 +314,21 @@ fn rewrite_struct_pat(
313314
span: Span,
314315
context: &RewriteContext<'_>,
315316
shape: Shape,
316-
) -> Option<String> {
317+
) -> RewriteResult {
317318
// 2 = ` {`
318-
let path_shape = shape.sub_width(2)?;
319-
let path_str = rewrite_path(context, PathContext::Expr, qself, path, path_shape).ok()?;
319+
let path_shape = shape.sub_width(2).max_width_error(shape.width, span)?;
320+
let path_str = rewrite_path(context, PathContext::Expr, qself, path, path_shape)?;
320321

321322
if fields.is_empty() && !ellipsis {
322-
return Some(format!("{path_str} {{}}"));
323+
return Ok(format!("{path_str} {{}}"));
323324
}
324325

325326
let (ellipsis_str, terminator) = if ellipsis { (", ..", "..") } else { ("", "}") };
326327

327328
// 3 = ` { `, 2 = ` }`.
328329
let (h_shape, v_shape) =
329-
struct_lit_shape(shape, context, path_str.len() + 3, ellipsis_str.len() + 2)?;
330+
struct_lit_shape(shape, context, path_str.len() + 3, ellipsis_str.len() + 2)
331+
.max_width_error(shape.width, span)?;
330332

331333
let items = itemize_list(
332334
context.snippet_provider,
@@ -352,7 +354,7 @@ fn rewrite_struct_pat(
352354
let nested_shape = shape_for_tactic(tactic, h_shape, v_shape);
353355
let fmt = struct_lit_formatting(nested_shape, tactic, context, false);
354356

355-
let mut fields_str = write_list(&item_vec, &fmt)?;
357+
let mut fields_str = write_list(&item_vec, &fmt).unknown_error()?;
356358
let one_line_width = h_shape.map_or(0, |shape| shape.width);
357359

358360
let has_trailing_comma = fmt.needs_trailing_separator();
@@ -380,7 +382,7 @@ fn rewrite_struct_pat(
380382

381383
// ast::Pat doesn't have attrs so use &[]
382384
let fields_str = wrap_struct_field(context, &[], &fields_str, shape, v_shape, one_line_width)?;
383-
Some(format!("{path_str} {{{fields_str}}}"))
385+
Ok(format!("{path_str} {{{fields_str}}}"))
384386
}
385387

386388
impl Rewrite for PatField {

src/vertical.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::items::{rewrite_struct_field, rewrite_struct_field_prefix};
1313
use crate::lists::{
1414
definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator,
1515
};
16-
use crate::rewrite::{Rewrite, RewriteContext};
16+
use crate::rewrite::{Rewrite, RewriteContext, RewriteResult};
1717
use crate::shape::{Indent, Shape};
1818
use crate::source_map::SpanUtils;
1919
use crate::spanned::Spanned;
@@ -30,7 +30,7 @@ pub(crate) trait AlignedItem {
3030
context: &RewriteContext<'_>,
3131
shape: Shape,
3232
prefix_max_width: usize,
33-
) -> Option<String>;
33+
) -> RewriteResult;
3434
}
3535

3636
impl AlignedItem for ast::FieldDef {
@@ -66,8 +66,8 @@ impl AlignedItem for ast::FieldDef {
6666
context: &RewriteContext<'_>,
6767
shape: Shape,
6868
prefix_max_width: usize,
69-
) -> Option<String> {
70-
rewrite_struct_field(context, self, shape, prefix_max_width).ok()
69+
) -> RewriteResult {
70+
rewrite_struct_field(context, self, shape, prefix_max_width)
7171
}
7272
}
7373

@@ -103,7 +103,7 @@ impl AlignedItem for ast::ExprField {
103103
context: &RewriteContext<'_>,
104104
shape: Shape,
105105
prefix_max_width: usize,
106-
) -> Option<String> {
106+
) -> RewriteResult {
107107
rewrite_field(context, self, shape, prefix_max_width)
108108
}
109109
}
@@ -228,7 +228,11 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
228228
",",
229229
|field| field.get_span().lo(),
230230
|field| field.get_span().hi(),
231-
|field| field.rewrite_aligned_item(context, item_shape, field_prefix_max_width),
231+
|field| {
232+
field
233+
.rewrite_aligned_item(context, item_shape, field_prefix_max_width)
234+
.ok()
235+
},
232236
span.lo(),
233237
span.hi(),
234238
false,
@@ -244,8 +248,9 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
244248

245249
if tactic == DefinitiveListTactic::Horizontal {
246250
// since the items fits on a line, there is no need to align them
247-
let do_rewrite =
248-
|field: &T| -> Option<String> { field.rewrite_aligned_item(context, item_shape, 0) };
251+
let do_rewrite = |field: &T| -> Option<String> {
252+
field.rewrite_aligned_item(context, item_shape, 0).ok()
253+
};
249254
fields
250255
.iter()
251256
.zip(items.iter_mut())

0 commit comments

Comments
 (0)