Skip to content

Commit 2563a76

Browse files
committed
refactor visit_struct, visit_enum to propagate RewriteResult
1 parent 777e25a commit 2563a76

File tree

3 files changed

+57
-51
lines changed

3 files changed

+57
-51
lines changed

src/expr.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,8 +1671,7 @@ fn rewrite_struct_lit<'a>(
16711671
v_shape,
16721672
mk_sp(body_lo, span.hi()),
16731673
one_line_width,
1674-
)
1675-
.unknown_error()?
1674+
)?
16761675
} else {
16771676
let field_iter = fields.iter().map(StructLitField::Regular).chain(
16781677
match struct_rest {

src/items.rs

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ impl<'a> FmtVisitor<'a> {
527527
};
528528
let rewrite = format_struct(&self.get_context(), struct_parts, self.block_indent, None)
529529
.map(|s| if is_tuple { s + ";" } else { s });
530-
self.push_rewrite(struct_parts.span, rewrite);
530+
self.push_rewrite(struct_parts.span, rewrite.ok());
531531
}
532532

533533
pub(crate) fn visit_enum(
@@ -565,9 +565,10 @@ impl<'a> FmtVisitor<'a> {
565565
self.last_pos = body_start;
566566

567567
match self.format_variant_list(enum_def, body_start, span.hi()) {
568-
Some(ref s) if enum_def.variants.is_empty() => self.push_str(s),
568+
Ok(ref s) if enum_def.variants.is_empty() => self.push_str(s),
569+
569570
rw => {
570-
self.push_rewrite(mk_sp(body_start, span.hi()), rw);
571+
self.push_rewrite(mk_sp(body_start, span.hi()), rw.ok());
571572
self.block_indent = self.block_indent.block_unindent(self.config);
572573
}
573574
}
@@ -579,7 +580,7 @@ impl<'a> FmtVisitor<'a> {
579580
enum_def: &ast::EnumDef,
580581
body_lo: BytePos,
581582
body_hi: BytePos,
582-
) -> Option<String> {
583+
) -> RewriteResult {
583584
if enum_def.variants.is_empty() {
584585
let mut buffer = String::with_capacity(128);
585586
// 1 = "}"
@@ -592,7 +593,7 @@ impl<'a> FmtVisitor<'a> {
592593
"",
593594
"}",
594595
);
595-
return Some(buffer);
596+
return Ok(buffer);
596597
}
597598
let mut result = String::with_capacity(1024);
598599
let original_offset = self.block_indent;
@@ -629,10 +630,7 @@ impl<'a> FmtVisitor<'a> {
629630
}
630631
},
631632
|f| f.span.hi(),
632-
|f| {
633-
self.format_variant(f, one_line_width, pad_discrim_ident_to)
634-
.unknown_error()
635-
},
633+
|f| self.format_variant(f, one_line_width, pad_discrim_ident_to),
636634
body_lo,
637635
body_hi,
638636
false,
@@ -648,16 +646,19 @@ impl<'a> FmtVisitor<'a> {
648646
items = itemize_list_with(0);
649647
}
650648

651-
let shape = self.shape().sub_width(2)?;
649+
let shape = self
650+
.shape()
651+
.sub_width(2)
652+
.max_width_error(self.shape().width, mk_sp(body_lo, body_hi))?;
652653
let fmt = ListFormatting::new(shape, self.config)
653654
.trailing_separator(self.config.trailing_comma())
654655
.preserve_newline(true);
655656

656-
let list = write_list(&items, &fmt).ok()?;
657+
let list = write_list(&items, &fmt)?;
657658
result.push_str(&list);
658659
result.push_str(&original_offset.to_string_with_newline(self.config));
659660
result.push('}');
660-
Some(result)
661+
Ok(result)
661662
}
662663

663664
// Variant of an enum.
@@ -666,23 +667,30 @@ impl<'a> FmtVisitor<'a> {
666667
field: &ast::Variant,
667668
one_line_width: usize,
668669
pad_discrim_ident_to: usize,
669-
) -> Option<String> {
670+
) -> RewriteResult {
670671
if contains_skip(&field.attrs) {
671672
let lo = field.attrs[0].span.lo();
672673
let span = mk_sp(lo, field.span.hi());
673-
return Some(self.snippet(span).to_owned());
674+
return Ok(self.snippet(span).to_owned());
674675
}
675676

676677
let context = self.get_context();
677678
let shape = self.shape();
678679
let attrs_str = if context.config.style_edition() >= StyleEdition::Edition2024 {
679-
field.attrs.rewrite(&context, shape)?
680+
field.attrs.rewrite_result(&context, shape)?
680681
} else {
681682
// StyleEdition::Edition20{15|18|21} formatting that was off by 1. See issue #5801
682-
field.attrs.rewrite(&context, shape.sub_width(1)?)?
683+
field.attrs.rewrite_result(
684+
&context,
685+
shape
686+
.sub_width(1)
687+
.max_width_error(shape.width, field.span)?,
688+
)?
683689
};
684690
// sub_width(1) to take the trailing comma into account
685-
let shape = shape.sub_width(1)?;
691+
let shape = shape
692+
.sub_width(1)
693+
.max_width_error(shape.width, field.span)?;
686694

687695
let lo = field
688696
.attrs
@@ -710,14 +718,12 @@ impl<'a> FmtVisitor<'a> {
710718
shape,
711719
&RhsAssignKind::Expr(&ex.kind, ex.span),
712720
RhsTactics::AllowOverflow,
713-
)
714-
.ok()?
721+
)?
715722
} else {
716723
variant_body
717724
};
718725

719726
combine_strs_with_missing_comments(&context, &attrs_str, &variant_body, span, shape, false)
720-
.ok()
721727
}
722728

723729
fn visit_impl_items(&mut self, items: &[ptr::P<ast::AssocItem>]) {
@@ -1145,7 +1151,7 @@ fn format_struct(
11451151
struct_parts: &StructParts<'_>,
11461152
offset: Indent,
11471153
one_line_width: Option<usize>,
1148-
) -> Option<String> {
1154+
) -> RewriteResult {
11491155
match struct_parts.def {
11501156
ast::VariantData::Unit(..) => format_unit_struct(context, struct_parts, offset),
11511157
ast::VariantData::Tuple(fields, _) => {
@@ -1410,7 +1416,7 @@ fn format_unit_struct(
14101416
context: &RewriteContext<'_>,
14111417
p: &StructParts<'_>,
14121418
offset: Indent,
1413-
) -> Option<String> {
1419+
) -> RewriteResult {
14141420
let header_str = format_header(context, p.prefix, p.ident, p.vis, offset);
14151421
let generics_str = if let Some(generics) = p.generics {
14161422
let hi = context.snippet_provider.span_before_last(p.span, ";");
@@ -1427,7 +1433,7 @@ fn format_unit_struct(
14271433
} else {
14281434
String::new()
14291435
};
1430-
Some(format!("{header_str}{generics_str};"))
1436+
Ok(format!("{header_str}{generics_str};"))
14311437
}
14321438

14331439
pub(crate) fn format_struct_struct(
@@ -1436,7 +1442,7 @@ pub(crate) fn format_struct_struct(
14361442
fields: &[ast::FieldDef],
14371443
offset: Indent,
14381444
one_line_width: Option<usize>,
1439-
) -> Option<String> {
1445+
) -> RewriteResult {
14401446
let mut result = String::with_capacity(1024);
14411447
let span = struct_parts.span;
14421448

@@ -1496,18 +1502,20 @@ pub(crate) fn format_struct_struct(
14961502
if fields.is_empty() {
14971503
let inner_span = mk_sp(body_lo, span.hi() - BytePos(1));
14981504
format_empty_struct_or_tuple(context, inner_span, offset, &mut result, "", "}");
1499-
return Some(result);
1505+
return Ok(result);
15001506
}
15011507

15021508
// 3 = ` ` and ` }`
15031509
let one_line_budget = context.budget(result.len() + 3 + offset.width());
15041510
let one_line_budget =
15051511
one_line_width.map_or(0, |one_line_width| min(one_line_width, one_line_budget));
15061512

1513+
let shape = Shape::indented(offset.block_indent(context.config), context.config);
1514+
let shape = shape.sub_width(1).max_width_error(shape.width, span)?;
15071515
let items_str = rewrite_with_alignment(
15081516
fields,
15091517
context,
1510-
Shape::indented(offset.block_indent(context.config), context.config).sub_width(1)?,
1518+
shape,
15111519
mk_sp(body_lo, span.hi()),
15121520
one_line_budget,
15131521
)?;
@@ -1517,9 +1525,9 @@ pub(crate) fn format_struct_struct(
15171525
&& items_str.len() <= one_line_budget
15181526
&& !last_line_contains_single_line_comment(&items_str)
15191527
{
1520-
Some(format!("{result} {items_str} }}"))
1528+
Ok(format!("{result} {items_str} }}"))
15211529
} else {
1522-
Some(format!(
1530+
Ok(format!(
15231531
"{}\n{}{}\n{}}}",
15241532
result,
15251533
offset
@@ -1582,7 +1590,7 @@ fn format_tuple_struct(
15821590
struct_parts: &StructParts<'_>,
15831591
fields: &[ast::FieldDef],
15841592
offset: Indent,
1585-
) -> Option<String> {
1593+
) -> RewriteResult {
15861594
let mut result = String::with_capacity(1024);
15871595
let span = struct_parts.span;
15881596

@@ -1614,7 +1622,7 @@ fn format_tuple_struct(
16141622
Some(generics) => {
16151623
let budget = context.budget(last_line_width(&header_str));
16161624
let shape = Shape::legacy(budget, offset);
1617-
let generics_str = rewrite_generics(context, "", generics, shape).ok()?;
1625+
let generics_str = rewrite_generics(context, "", generics, shape)?;
16181626
result.push_str(&generics_str);
16191627

16201628
let where_budget = context.budget(last_line_width(&result));
@@ -1630,8 +1638,7 @@ fn format_tuple_struct(
16301638
None,
16311639
body_hi,
16321640
option,
1633-
)
1634-
.ok()?
1641+
)?
16351642
}
16361643
None => "".to_owned(),
16371644
};
@@ -1643,7 +1650,8 @@ fn format_tuple_struct(
16431650
let inner_span = mk_sp(body_lo, body_hi);
16441651
format_empty_struct_or_tuple(context, inner_span, offset, &mut result, "(", ")");
16451652
} else {
1646-
let shape = Shape::indented(offset, context.config).sub_width(1)?;
1653+
let shape = Shape::indented(offset, context.config);
1654+
let shape = shape.sub_width(1).max_width_error(shape.width, span)?;
16471655
let lo = if let Some(generics) = struct_parts.generics {
16481656
generics.span.hi()
16491657
} else {
@@ -1657,8 +1665,7 @@ fn format_tuple_struct(
16571665
mk_sp(lo, span.hi()),
16581666
context.config.fn_call_width(),
16591667
None,
1660-
)
1661-
.ok()?;
1668+
)?;
16621669
}
16631670

16641671
if !where_clause_str.is_empty()
@@ -1676,7 +1683,7 @@ fn format_tuple_struct(
16761683
}
16771684
result.push_str(&where_clause_str);
16781685

1679-
Some(result)
1686+
Ok(result)
16801687
}
16811688

16821689
pub(crate) enum ItemVisitorKind<'a> {
@@ -3321,9 +3328,9 @@ fn format_generics(
33213328
offset: Indent,
33223329
span: Span,
33233330
used_width: usize,
3324-
) -> Option<String> {
3331+
) -> RewriteResult {
33253332
let shape = Shape::legacy(context.budget(used_width + offset.width()), offset);
3326-
let mut result = rewrite_generics(context, "", generics, shape).ok()?;
3333+
let mut result = rewrite_generics(context, "", generics, shape)?;
33273334

33283335
// If the generics are not parameterized then generics.span.hi() == 0,
33293336
// so we use span.lo(), which is the position after `struct Foo`.
@@ -3349,8 +3356,7 @@ fn format_generics(
33493356
Some(span.hi()),
33503357
span_end_before_where,
33513358
option,
3352-
)
3353-
.ok()?;
3359+
)?;
33543360
result.push_str(&where_clause_str);
33553361
(
33563362
brace_pos == BracePos::ForceSameLine || brace_style == BraceStyle::PreferSameLine,
@@ -3389,7 +3395,7 @@ fn format_generics(
33893395
!is_block
33903396
});
33913397
if brace_pos == BracePos::None {
3392-
return Some(result);
3398+
return Ok(result);
33933399
}
33943400
let total_used_width = last_line_used_width(&result, used_width);
33953401
let remaining_budget = context.budget(total_used_width);
@@ -3412,7 +3418,7 @@ fn format_generics(
34123418
}
34133419
result.push('{');
34143420

3415-
Some(result)
3421+
Ok(result)
34163422
}
34173423

34183424
impl Rewrite for ast::ForeignItem {

src/vertical.rs

Lines changed: 8 additions & 7 deletions
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
ListFormatting, ListItem, Separator, definitive_tactic, itemize_list, write_list,
1515
};
16-
use crate::rewrite::{Rewrite, RewriteContext, RewriteResult};
16+
use crate::rewrite::{Rewrite, RewriteContext, RewriteErrorExt, RewriteResult};
1717
use crate::shape::{Indent, Shape};
1818
use crate::source_map::SpanUtils;
1919
use crate::spanned::Spanned;
@@ -114,7 +114,7 @@ pub(crate) fn rewrite_with_alignment<T: AlignedItem>(
114114
shape: Shape,
115115
span: Span,
116116
one_line_width: usize,
117-
) -> Option<String> {
117+
) -> RewriteResult {
118118
let (spaces, group_index) = if context.config.struct_field_align_threshold() > 0 {
119119
group_aligned_items(context, fields)
120120
} else {
@@ -172,11 +172,11 @@ pub(crate) fn rewrite_with_alignment<T: AlignedItem>(
172172
force_separator,
173173
)?;
174174
if rest.is_empty() {
175-
Some(result + spaces)
175+
Ok(result + spaces)
176176
} else {
177177
let rest_span = mk_sp(init_last_pos, span.hi());
178178
let rest_str = rewrite_with_alignment(rest, context, shape, rest_span, one_line_width)?;
179-
Some(format!(
179+
Ok(format!(
180180
"{}{}\n{}{}",
181181
result,
182182
spaces,
@@ -211,9 +211,10 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
211211
offset: Indent,
212212
one_line_width: usize,
213213
force_trailing_separator: bool,
214-
) -> Option<String> {
214+
) -> RewriteResult {
215215
// 1 = ","
216-
let item_shape = Shape::indented(offset, context.config).sub_width(1)?;
216+
let shape = Shape::indented(offset, context.config);
217+
let item_shape = shape.sub_width(1).max_width_error(shape.width, span)?;
217218
let (mut field_prefix_max_width, field_prefix_min_width) =
218219
struct_field_prefix_max_min_width(context, fields, item_shape);
219220
let max_diff = field_prefix_max_width.saturating_sub(field_prefix_min_width);
@@ -266,7 +267,7 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
266267
.tactic(tactic)
267268
.trailing_separator(separator_tactic)
268269
.preserve_newline(true);
269-
write_list(&items, &fmt).ok()
270+
write_list(&items, &fmt)
270271
}
271272

272273
/// Returns the index in `fields` up to which a field belongs to the current group.

0 commit comments

Comments
 (0)