Skip to content

Commit e9d37d9

Browse files
committed
impl rewrite_result for ForeignItem
1 parent e7038e7 commit e9d37d9

File tree

2 files changed

+79
-55
lines changed

2 files changed

+79
-55
lines changed

src/items.rs

Lines changed: 69 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ impl<'a> FmtVisitor<'a> {
435435

436436
let mut fn_brace_style = newline_for_brace(self.config, &fn_sig.generics.where_clause);
437437
let (result, _, force_newline_brace) =
438-
rewrite_fn_base(&context, indent, ident, fn_sig, span, fn_brace_style)?;
438+
rewrite_fn_base(&context, indent, ident, fn_sig, span, fn_brace_style).ok()?;
439439

440440
// 2 = ` {`
441441
if self.config.brace_style() == BraceStyle::AlwaysNextLine
@@ -456,7 +456,7 @@ impl<'a> FmtVisitor<'a> {
456456
vis: &ast::Visibility,
457457
generics: &ast::Generics,
458458
span: Span,
459-
) -> Option<String> {
459+
) -> RewriteResult {
460460
// Drop semicolon or it will be interpreted as comment.
461461
let span = mk_sp(span.lo(), span.hi() - BytePos(1));
462462
let context = self.get_context();
@@ -478,7 +478,7 @@ impl<'a> FmtVisitor<'a> {
478478
// Re-attach semicolon
479479
result.push(';');
480480

481-
Some(result)
481+
Ok(result)
482482
}
483483

484484
pub(crate) fn single_line_fn(
@@ -978,7 +978,7 @@ fn format_impl_ref_and_type(
978978
0,
979979
)?
980980
};
981-
let generics_str = rewrite_generics(context, "impl", generics, shape)?;
981+
let generics_str = rewrite_generics(context, "impl", generics, shape).ok()?;
982982
result.push_str(&generics_str);
983983
result.push_str(format_constness_right(constness));
984984

@@ -1186,7 +1186,7 @@ pub(crate) fn format_trait(
11861186

11871187
let shape = Shape::indented(offset, context.config).offset_left(result.len())?;
11881188
let generics_str =
1189-
rewrite_generics(context, rewrite_ident(context, item.ident), generics, shape)?;
1189+
rewrite_generics(context, rewrite_ident(context, item.ident), generics, shape).ok()?;
11901190
result.push_str(&generics_str);
11911191

11921192
// FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
@@ -1386,7 +1386,7 @@ pub(crate) fn format_trait_alias(
13861386
let alias = rewrite_ident(context, ident);
13871387
// 6 = "trait ", 2 = " ="
13881388
let g_shape = shape.offset_left(6)?.sub_width(2)?;
1389-
let generics_str = rewrite_generics(context, alias, generics, g_shape)?;
1389+
let generics_str = rewrite_generics(context, alias, generics, g_shape).ok()?;
13901390
let vis_str = format_visibility(context, vis);
13911391
let lhs = format!("{vis_str}trait {generics_str} =");
13921392
// 1 = ";"
@@ -1612,7 +1612,7 @@ fn format_tuple_struct(
16121612
Some(generics) => {
16131613
let budget = context.budget(last_line_width(&header_str));
16141614
let shape = Shape::legacy(budget, offset);
1615-
let generics_str = rewrite_generics(context, "", generics, shape)?;
1615+
let generics_str = rewrite_generics(context, "", generics, shape).ok()?;
16161616
result.push_str(&generics_str);
16171617

16181618
let where_budget = context.budget(last_line_width(&result));
@@ -1699,7 +1699,7 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
16991699
indent: Indent,
17001700
visitor_kind: &ItemVisitorKind<'b>,
17011701
span: Span,
1702-
) -> Option<String> {
1702+
) -> RewriteResult {
17031703
use ItemVisitorKind::*;
17041704

17051705
let ast::TyAlias {
@@ -1737,8 +1737,8 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
17371737
rewrite_ty(rw_info, Some(bounds), ty_opt, vis)
17381738
}?;
17391739
match defaultness {
1740-
ast::Defaultness::Default(..) => Some(format!("default {result}")),
1741-
_ => Some(result),
1740+
ast::Defaultness::Default(..) => Ok(format!("default {result}")),
1741+
_ => Ok(result),
17421742
}
17431743
}
17441744
}
@@ -1749,15 +1749,15 @@ fn rewrite_ty<R: Rewrite>(
17491749
generic_bounds_opt: Option<&ast::GenericBounds>,
17501750
rhs: Option<&R>,
17511751
vis: &ast::Visibility,
1752-
) -> Option<String> {
1752+
) -> RewriteResult {
17531753
let mut result = String::with_capacity(128);
17541754
let TyAliasRewriteInfo(context, indent, generics, where_clauses, ident, span) = *rw_info;
17551755
let (before_where_predicates, after_where_predicates) = generics
17561756
.where_clause
17571757
.predicates
17581758
.split_at(where_clauses.split);
17591759
if !after_where_predicates.is_empty() {
1760-
return None;
1760+
return Err(RewriteError::Unknown);
17611761
}
17621762
result.push_str(&format!("{}type ", format_visibility(context, vis)));
17631763
let ident_str = rewrite_ident(context, ident);
@@ -1766,18 +1766,25 @@ fn rewrite_ty<R: Rewrite>(
17661766
result.push_str(ident_str)
17671767
} else {
17681768
// 2 = `= `
1769-
let g_shape = Shape::indented(indent, context.config)
1770-
.offset_left(result.len())?
1771-
.sub_width(2)?;
1769+
let g_shape = Shape::indented(indent, context.config);
1770+
let g_shape = g_shape
1771+
.offset_left(result.len())
1772+
.and_then(|s| s.sub_width(2))
1773+
.max_width_error(g_shape.width, span)?;
17721774
let generics_str = rewrite_generics(context, ident_str, generics, g_shape)?;
17731775
result.push_str(&generics_str);
17741776
}
17751777

17761778
if let Some(bounds) = generic_bounds_opt {
17771779
if !bounds.is_empty() {
17781780
// 2 = `: `
1779-
let shape = Shape::indented(indent, context.config).offset_left(result.len() + 2)?;
1780-
let type_bounds = bounds.rewrite(context, shape).map(|s| format!(": {}", s))?;
1781+
let shape = Shape::indented(indent, context.config);
1782+
let shape = shape
1783+
.offset_left(result.len() + 2)
1784+
.max_width_error(shape.width, span)?;
1785+
let type_bounds = bounds
1786+
.rewrite_result(context, shape)
1787+
.map(|s| format!(": {}", s))?;
17811788
result.push_str(&type_bounds);
17821789
}
17831790
}
@@ -1798,8 +1805,7 @@ fn rewrite_ty<R: Rewrite>(
17981805
None,
17991806
generics.span.hi(),
18001807
option,
1801-
)
1802-
.ok()?;
1808+
)?;
18031809
result.push_str(&where_clause_str);
18041810

18051811
if let Some(ty) = rhs {
@@ -1819,13 +1825,20 @@ fn rewrite_ty<R: Rewrite>(
18191825

18201826
let lhs = match comment_span {
18211827
Some(comment_span)
1822-
if contains_comment(context.snippet_provider.span_to_snippet(comment_span)?) =>
1828+
if contains_comment(
1829+
context
1830+
.snippet_provider
1831+
.span_to_snippet(comment_span)
1832+
.unknown_error()?,
1833+
) =>
18231834
{
18241835
let comment_shape = if has_where {
18251836
Shape::indented(indent, context.config)
18261837
} else {
1827-
Shape::indented(indent, context.config)
1828-
.block_left(context.config.tab_spaces())?
1838+
let shape = Shape::indented(indent, context.config);
1839+
shape
1840+
.block_left(context.config.tab_spaces())
1841+
.max_width_error(shape.width, span)?
18291842
};
18301843

18311844
combine_strs_with_missing_comments(
@@ -1835,17 +1848,19 @@ fn rewrite_ty<R: Rewrite>(
18351848
comment_span,
18361849
comment_shape,
18371850
true,
1838-
)
1839-
.ok()?
1851+
)?
18401852
}
18411853
_ => format!("{result}="),
18421854
};
18431855

18441856
// 1 = `;`
1845-
let shape = Shape::indented(indent, context.config).sub_width(1)?;
1846-
rewrite_assign_rhs(context, lhs, &*ty, &RhsAssignKind::Ty, shape).map(|s| s + ";")
1857+
let shape = Shape::indented(indent, context.config);
1858+
let shape = shape.sub_width(1).max_width_error(shape.width, span)?;
1859+
rewrite_assign_rhs(context, lhs, &*ty, &RhsAssignKind::Ty, shape)
1860+
.map(|s| s + ";")
1861+
.unknown_error()
18471862
} else {
1848-
Some(format!("{result};"))
1863+
Ok(format!("{result};"))
18491864
}
18501865
}
18511866

@@ -2390,7 +2405,7 @@ fn rewrite_fn_base(
23902405
fn_sig: &FnSig<'_>,
23912406
span: Span,
23922407
fn_brace_style: FnBraceStyle,
2393-
) -> Option<(String, bool, bool)> {
2408+
) -> Result<(String, bool, bool), RewriteError> {
23942409
let mut force_new_line_for_brace = false;
23952410

23962411
let where_clause = &fn_sig.generics.where_clause;
@@ -2434,7 +2449,7 @@ fn rewrite_fn_base(
24342449
// return type later anyway.
24352450
let ret_str = fd
24362451
.output
2437-
.rewrite(context, Shape::indented(indent, context.config))?;
2452+
.rewrite_result(context, Shape::indented(indent, context.config))?;
24382453

24392454
let multi_line_ret_str = ret_str.contains('\n');
24402455
let ret_str_len = if multi_line_ret_str { 0 } else { ret_str.len() };
@@ -2447,7 +2462,7 @@ fn rewrite_fn_base(
24472462
ret_str_len,
24482463
fn_brace_style,
24492464
multi_line_ret_str,
2450-
)?;
2465+
);
24512466

24522467
debug!(
24532468
"rewrite_fn_base: one_line_budget: {}, multi_line_budget: {}, param_indent: {:?}",
@@ -2609,7 +2624,7 @@ fn rewrite_fn_base(
26092624
if multi_line_ret_str || ret_should_indent {
26102625
// Now that we know the proper indent and width, we need to
26112626
// re-layout the return type.
2612-
let ret_str = fd.output.rewrite(context, ret_shape)?;
2627+
let ret_str = fd.output.rewrite_result(context, ret_shape)?;
26132628
result.push_str(&ret_str);
26142629
} else {
26152630
result.push_str(&ret_str);
@@ -2669,8 +2684,7 @@ fn rewrite_fn_base(
26692684
Some(span.hi()),
26702685
pos_before_where,
26712686
option,
2672-
)
2673-
.ok()?;
2687+
)?;
26742688
// If there are neither where-clause nor return type, we may be missing comments between
26752689
// params and `{`.
26762690
if where_clause_str.is_empty() {
@@ -2697,7 +2711,7 @@ fn rewrite_fn_base(
26972711
force_new_line_for_brace |= ends_with_comment;
26982712
force_new_line_for_brace |=
26992713
is_params_multi_lined && context.config.where_single_line() && !where_clause_str.is_empty();
2700-
Some((result, ends_with_comment, force_new_line_for_brace))
2714+
Ok((result, ends_with_comment, force_new_line_for_brace))
27012715
}
27022716

27032717
/// Kind of spaces to put before `where`.
@@ -2768,7 +2782,7 @@ fn rewrite_params(
27682782
param_indent: Indent,
27692783
span: Span,
27702784
variadic: bool,
2771-
) -> Option<String> {
2785+
) -> RewriteResult {
27722786
if params.is_empty() {
27732787
let comment = context
27742788
.snippet(mk_sp(
@@ -2777,7 +2791,7 @@ fn rewrite_params(
27772791
span.hi() - BytePos(1),
27782792
))
27792793
.trim();
2780-
return Some(comment.to_owned());
2794+
return Ok(comment.to_owned());
27812795
}
27822796
let param_items: Vec<_> = itemize_list(
27832797
context.snippet_provider,
@@ -2827,7 +2841,7 @@ fn rewrite_params(
28272841
.trailing_separator(trailing_separator)
28282842
.ends_with_newline(tactic.ends_with_newline(context.config.indent_style()))
28292843
.preserve_newline(true);
2830-
write_list(&param_items, &fmt).ok()
2844+
write_list(&param_items, &fmt)
28312845
}
28322846

28332847
fn compute_budgets_for_params(
@@ -2837,7 +2851,7 @@ fn compute_budgets_for_params(
28372851
ret_str_len: usize,
28382852
fn_brace_style: FnBraceStyle,
28392853
force_vertical_layout: bool,
2840-
) -> Option<(usize, usize, Indent)> {
2854+
) -> (usize, usize, Indent) {
28412855
debug!(
28422856
"compute_budgets_for_params {} {:?}, {}, {:?}",
28432857
result.len(),
@@ -2874,7 +2888,7 @@ fn compute_budgets_for_params(
28742888
}
28752889
};
28762890

2877-
return Some((one_line_budget, multi_line_budget, indent));
2891+
return (one_line_budget, multi_line_budget, indent);
28782892
}
28792893
}
28802894

@@ -2886,7 +2900,7 @@ fn compute_budgets_for_params(
28862900
// Account for `)` and possibly ` {`.
28872901
IndentStyle::Visual => new_indent.width() + if ret_str_len == 0 { 1 } else { 3 },
28882902
};
2889-
Some((0, context.budget(used_space), new_indent))
2903+
(0, context.budget(used_space), new_indent)
28902904
}
28912905

28922906
fn newline_for_brace(config: &Config, where_clause: &ast::WhereClause) -> FnBraceStyle {
@@ -2911,16 +2925,16 @@ fn rewrite_generics(
29112925
ident: &str,
29122926
generics: &ast::Generics,
29132927
shape: Shape,
2914-
) -> Option<String> {
2928+
) -> RewriteResult {
29152929
// FIXME: convert bounds to where-clauses where they get too big or if
29162930
// there is a where-clause at all.
29172931

29182932
if generics.params.is_empty() {
2919-
return Some(ident.to_owned());
2933+
return Ok(ident.to_owned());
29202934
}
29212935

29222936
let params = generics.params.iter();
2923-
overflow::rewrite_with_angle_brackets(context, ident, params, shape, generics.span).ok()
2937+
overflow::rewrite_with_angle_brackets(context, ident, params, shape, generics.span)
29242938
}
29252939

29262940
fn generics_shape_from_config(config: &Config, shape: Shape, offset: usize) -> Option<Shape> {
@@ -3270,7 +3284,7 @@ fn format_generics(
32703284
used_width: usize,
32713285
) -> Option<String> {
32723286
let shape = Shape::legacy(context.budget(used_width + offset.width()), offset);
3273-
let mut result = rewrite_generics(context, "", generics, shape)?;
3287+
let mut result = rewrite_generics(context, "", generics, shape).ok()?;
32743288

32753289
// If the generics are not parameterized then generics.span.hi() == 0,
32763290
// so we use span.lo(), which is the position after `struct Foo`.
@@ -3364,7 +3378,11 @@ fn format_generics(
33643378

33653379
impl Rewrite for ast::ForeignItem {
33663380
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
3367-
let attrs_str = self.attrs.rewrite(context, shape)?;
3381+
self.rewrite_result(context, shape).ok()
3382+
}
3383+
3384+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
3385+
let attrs_str = self.attrs.rewrite_result(context, shape)?;
33683386
// Drop semicolon or it will be interpreted as comment.
33693387
// FIXME: this may be a faulty span from libsyntax.
33703388
let span = mk_sp(self.span.lo(), self.span.hi() - BytePos(1));
@@ -3397,7 +3415,7 @@ impl Rewrite for ast::ForeignItem {
33973415
defaultness,
33983416
Some(&inner_attrs),
33993417
);
3400-
Some(visitor.buffer.to_owned())
3418+
Ok(visitor.buffer.to_owned())
34013419
} else {
34023420
rewrite_fn_base(
34033421
context,
@@ -3429,16 +3447,19 @@ impl Rewrite for ast::ForeignItem {
34293447
prefix,
34303448
&static_foreign_item.ty,
34313449
&RhsAssignKind::Ty,
3432-
shape.sub_width(1)?,
3450+
shape
3451+
.sub_width(1)
3452+
.max_width_error(shape.width, static_foreign_item.ty.span)?, // TODO
34333453
)
3454+
.unknown_error()
34343455
.map(|s| s + ";")
34353456
}
34363457
ast::ForeignItemKind::TyAlias(ref ty_alias) => {
34373458
let (kind, span) = (&ItemVisitorKind::ForeignItem(self), self.span);
34383459
rewrite_type_alias(ty_alias, context, shape.indent, kind, span)
34393460
}
34403461
ast::ForeignItemKind::MacCall(ref mac) => {
3441-
rewrite_macro(mac, None, context, shape, MacroPosition::Item).ok()
3462+
rewrite_macro(mac, None, context, shape, MacroPosition::Item)
34423463
}
34433464
}?;
34443465

@@ -3455,7 +3476,6 @@ impl Rewrite for ast::ForeignItem {
34553476
shape,
34563477
false,
34573478
)
3458-
.ok()
34593479
}
34603480
}
34613481

0 commit comments

Comments
 (0)