Skip to content

Commit babc2f9

Browse files
ding-youngytmimi
authored andcommitted
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 3cbc91e commit babc2f9

File tree

3 files changed

+58
-45
lines changed

3 files changed

+58
-45
lines changed

src/expr.rs

+34-28
Original file line numberDiff line numberDiff line change
@@ -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)
@@ -1603,7 +1604,7 @@ fn rewrite_struct_lit<'a>(
16031604
attrs: &[ast::Attribute],
16041605
span: Span,
16051606
shape: Shape,
1606-
) -> Option<String> {
1607+
) -> RewriteResult {
16071608
debug!("rewrite_struct_lit: shape {:?}", shape);
16081609

16091610
enum StructLitField<'a> {
@@ -1613,20 +1614,21 @@ fn rewrite_struct_lit<'a>(
16131614
}
16141615

16151616
// 2 = " {".len()
1616-
let path_shape = shape.sub_width(2)?;
1617-
let path_str = rewrite_path(context, PathContext::Expr, qself, path, path_shape).ok()?;
1617+
let path_shape = shape.sub_width(2).max_width_error(shape.width, span)?;
1618+
let path_str = rewrite_path(context, PathContext::Expr, qself, path, path_shape)?;
16181619

16191620
let has_base_or_rest = match struct_rest {
1620-
ast::StructRest::None if fields.is_empty() => return Some(format!("{path_str} {{}}")),
1621+
ast::StructRest::None if fields.is_empty() => return Ok(format!("{path_str} {{}}")),
16211622
ast::StructRest::Rest(_) if fields.is_empty() => {
1622-
return Some(format!("{path_str} {{ .. }}"));
1623+
return Ok(format!("{path_str} {{ .. }}"));
16231624
}
16241625
ast::StructRest::Rest(_) | ast::StructRest::Base(_) => true,
16251626
_ => false,
16261627
};
16271628

16281629
// Foo { a: Foo } - indent is +3, width is -5.
1629-
let (h_shape, v_shape) = struct_lit_shape(shape, context, path_str.len() + 3, 2)?;
1630+
let (h_shape, v_shape) = struct_lit_shape(shape, context, path_str.len() + 3, 2)
1631+
.max_width_error(shape.width, span)?;
16301632

16311633
let one_line_width = h_shape.map_or(0, |shape| shape.width);
16321634
let body_lo = context.snippet_provider.span_after(span, "{");
@@ -1639,7 +1641,8 @@ fn rewrite_struct_lit<'a>(
16391641
v_shape,
16401642
mk_sp(body_lo, span.hi()),
16411643
one_line_width,
1642-
)?
1644+
)
1645+
.unknown_error()?
16431646
} else {
16441647
let field_iter = fields.iter().map(StructLitField::Regular).chain(
16451648
match struct_rest {
@@ -1668,12 +1671,13 @@ fn rewrite_struct_lit<'a>(
16681671
let rewrite = |item: &StructLitField<'_>| match *item {
16691672
StructLitField::Regular(field) => {
16701673
// The 1 taken from the v_budget is for the comma.
1671-
rewrite_field(context, field, v_shape.sub_width(1)?, 0)
1674+
let v_shape = v_shape.sub_width(1)?;
1675+
rewrite_field(context, field, v_shape, 0).ok()
16721676
}
16731677
StructLitField::Base(expr) => {
16741678
// 2 = ..
1675-
expr.rewrite(context, v_shape.offset_left(2)?)
1676-
.map(|s| format!("..{}", s))
1679+
let v_shape = v_shape.sub_width(2)?;
1680+
expr.rewrite(context, v_shape).map(|s| format!("..{}", s))
16771681
}
16781682
StructLitField::Rest(_) => Some("..".to_owned()),
16791683
};
@@ -1705,12 +1709,12 @@ fn rewrite_struct_lit<'a>(
17051709
force_no_trailing_comma || has_base_or_rest || !context.use_block_indent(),
17061710
);
17071711

1708-
write_list(&item_vec, &fmt)?
1712+
write_list(&item_vec, &fmt).unknown_error()?
17091713
};
17101714

17111715
let fields_str =
17121716
wrap_struct_field(context, attrs, &fields_str, shape, v_shape, one_line_width)?;
1713-
Some(format!("{path_str} {{{fields_str}}}"))
1717+
Ok(format!("{path_str} {{{fields_str}}}"))
17141718

17151719
// FIXME if context.config.indent_style() == Visual, but we run out
17161720
// of space, we should fall back to BlockIndent.
@@ -1723,7 +1727,7 @@ pub(crate) fn wrap_struct_field(
17231727
shape: Shape,
17241728
nested_shape: Shape,
17251729
one_line_width: usize,
1726-
) -> Option<String> {
1730+
) -> RewriteResult {
17271731
let should_vertical = context.config.indent_style() == IndentStyle::Block
17281732
&& (fields_str.contains('\n')
17291733
|| !context.config.struct_lit_single_line()
@@ -1732,21 +1736,21 @@ pub(crate) fn wrap_struct_field(
17321736
let inner_attrs = &inner_attributes(attrs);
17331737
if inner_attrs.is_empty() {
17341738
if should_vertical {
1735-
Some(format!(
1739+
Ok(format!(
17361740
"{}{}{}",
17371741
nested_shape.indent.to_string_with_newline(context.config),
17381742
fields_str,
17391743
shape.indent.to_string_with_newline(context.config)
17401744
))
17411745
} else {
17421746
// One liner or visual indent.
1743-
Some(format!(" {fields_str} "))
1747+
Ok(format!(" {fields_str} "))
17441748
}
17451749
} else {
1746-
Some(format!(
1750+
Ok(format!(
17471751
"{}{}{}{}{}",
17481752
nested_shape.indent.to_string_with_newline(context.config),
1749-
inner_attrs.rewrite(context, shape)?,
1753+
inner_attrs.rewrite_result(context, shape)?,
17501754
nested_shape.indent.to_string_with_newline(context.config),
17511755
fields_str,
17521756
shape.indent.to_string_with_newline(context.config)
@@ -1763,38 +1767,40 @@ pub(crate) fn rewrite_field(
17631767
field: &ast::ExprField,
17641768
shape: Shape,
17651769
prefix_max_width: usize,
1766-
) -> Option<String> {
1770+
) -> RewriteResult {
17671771
if contains_skip(&field.attrs) {
1768-
return Some(context.snippet(field.span()).to_owned());
1772+
return Ok(context.snippet(field.span()).to_owned());
17691773
}
1770-
let mut attrs_str = field.attrs.rewrite(context, shape)?;
1774+
let mut attrs_str = field.attrs.rewrite_result(context, shape)?;
17711775
if !attrs_str.is_empty() {
17721776
attrs_str.push_str(&shape.indent.to_string_with_newline(context.config));
17731777
};
17741778
let name = context.snippet(field.ident.span);
17751779
if field.is_shorthand {
1776-
Some(attrs_str + name)
1780+
Ok(attrs_str + name)
17771781
} else {
17781782
let mut separator = String::from(struct_lit_field_separator(context.config));
17791783
for _ in 0..prefix_max_width.saturating_sub(name.len()) {
17801784
separator.push(' ');
17811785
}
17821786
let overhead = name.len() + separator.len();
1783-
let expr_shape = shape.offset_left(overhead)?;
1784-
let expr = field.expr.rewrite(context, expr_shape);
1787+
let expr_shape = shape
1788+
.offset_left(overhead)
1789+
.max_width_error(shape.width, field.span)?;
1790+
let expr = field.expr.rewrite_result(context, expr_shape);
17851791
let is_lit = matches!(field.expr.kind, ast::ExprKind::Lit(_));
17861792
match expr {
1787-
Some(ref e)
1793+
Ok(ref e)
17881794
if !is_lit && e.as_str() == name && context.config.use_field_init_shorthand() =>
17891795
{
1790-
Some(attrs_str + name)
1796+
Ok(attrs_str + name)
17911797
}
1792-
Some(e) => Some(format!("{attrs_str}{name}{separator}{e}")),
1793-
None => {
1798+
Ok(e) => Ok(format!("{attrs_str}{name}{separator}{e}")),
1799+
Err(_) => {
17941800
let expr_offset = shape.indent.block_indent(context.config);
17951801
let expr = field
17961802
.expr
1797-
.rewrite(context, Shape::indented(expr_offset, context.config));
1803+
.rewrite_result(context, Shape::indented(expr_offset, context.config));
17981804
expr.map(|s| {
17991805
format!(
18001806
"{}{}:\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)