Skip to content

Commit 6fb0210

Browse files
committed
impl rewrite_result for TraitAliasBounds, WherePredicate
1 parent 40909b4 commit 6fb0210

File tree

2 files changed

+51
-35
lines changed

2 files changed

+51
-35
lines changed

src/items.rs

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,8 @@ pub(crate) fn format_impl(
841841
where_span_end,
842842
self_ty.span.hi(),
843843
option,
844-
)?;
844+
)
845+
.ok()?;
845846

846847
// If there is no where-clause, we may have missing comments between the trait name and
847848
// the opening brace.
@@ -1232,7 +1233,8 @@ pub(crate) fn format_trait(
12321233
None,
12331234
pos_before_where,
12341235
option,
1235-
)?;
1236+
)
1237+
.ok()?;
12361238
// If the where-clause cannot fit on the same line,
12371239
// put the where-clause on a new line
12381240
if !where_clause_str.contains('\n')
@@ -1337,7 +1339,11 @@ pub(crate) struct TraitAliasBounds<'a> {
13371339

13381340
impl<'a> Rewrite for TraitAliasBounds<'a> {
13391341
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
1340-
let generic_bounds_str = self.generic_bounds.rewrite(context, shape)?;
1342+
self.rewrite_result(context, shape).ok()
1343+
}
1344+
1345+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
1346+
let generic_bounds_str = self.generic_bounds.rewrite_result(context, shape)?;
13411347

13421348
let mut option = WhereClauseOption::new(true, WhereClauseSpace::None);
13431349
option.allow_single_line();
@@ -1366,7 +1372,7 @@ impl<'a> Rewrite for TraitAliasBounds<'a> {
13661372
shape.indent.to_string_with_newline(context.config)
13671373
};
13681374

1369-
Some(format!("{generic_bounds_str}{space}{where_str}"))
1375+
Ok(format!("{generic_bounds_str}{space}{where_str}"))
13701376
}
13711377
}
13721378

@@ -1624,7 +1630,8 @@ fn format_tuple_struct(
16241630
None,
16251631
body_hi,
16261632
option,
1627-
)?
1633+
)
1634+
.ok()?
16281635
}
16291636
None => "".to_owned(),
16301637
};
@@ -1793,7 +1800,8 @@ fn rewrite_ty<R: Rewrite>(
17931800
None,
17941801
generics.span.hi(),
17951802
option,
1796-
)?;
1803+
)
1804+
.ok()?;
17971805
result.push_str(&where_clause_str);
17981806

17991807
if let Some(ty) = rhs {
@@ -2664,7 +2672,8 @@ fn rewrite_fn_base(
26642672
Some(span.hi()),
26652673
pos_before_where,
26662674
option,
2667-
)?;
2675+
)
2676+
.ok()?;
26682677
// If there are neither where-clause nor return type, we may be missing comments between
26692678
// params and `{`.
26702679
if where_clause_str.is_empty() {
@@ -2940,7 +2949,7 @@ fn rewrite_where_clause_rfc_style(
29402949
span_end: Option<BytePos>,
29412950
span_end_before_where: BytePos,
29422951
where_clause_option: WhereClauseOption,
2943-
) -> Option<String> {
2952+
) -> RewriteResult {
29442953
let (where_keyword, allow_single_line) = rewrite_where_keyword(
29452954
context,
29462955
predicates,
@@ -2954,8 +2963,9 @@ fn rewrite_where_clause_rfc_style(
29542963
let clause_shape = shape
29552964
.block()
29562965
.with_max_width(context.config)
2957-
.block_left(context.config.tab_spaces())?
2958-
.sub_width(1)?;
2966+
.block_left(context.config.tab_spaces())
2967+
.and_then(|s| s.sub_width(1))
2968+
.max_width_error(shape.width, where_span)?;
29592969
let force_single_line = context.config.where_single_line()
29602970
&& predicates.len() == 1
29612971
&& !where_clause_option.veto_single_line;
@@ -2980,7 +2990,7 @@ fn rewrite_where_clause_rfc_style(
29802990
clause_shape.indent.to_string_with_newline(context.config)
29812991
};
29822992

2983-
Some(format!("{where_keyword}{clause_sep}{preds_str}"))
2993+
Ok(format!("{where_keyword}{clause_sep}{preds_str}"))
29842994
}
29852995

29862996
/// Rewrite `where` and comment around it.
@@ -2991,12 +3001,13 @@ fn rewrite_where_keyword(
29913001
shape: Shape,
29923002
span_end_before_where: BytePos,
29933003
where_clause_option: WhereClauseOption,
2994-
) -> Option<(String, bool)> {
3004+
) -> Result<(String, bool), RewriteError> {
29953005
let block_shape = shape.block().with_max_width(context.config);
29963006
// 1 = `,`
29973007
let clause_shape = block_shape
2998-
.block_left(context.config.tab_spaces())?
2999-
.sub_width(1)?;
3008+
.block_left(context.config.tab_spaces())
3009+
.and_then(|s| s.sub_width(1))
3010+
.max_width_error(block_shape.width, where_span)?;
30003011

30013012
let comment_separator = |comment: &str, shape: Shape| {
30023013
if comment.is_empty() {
@@ -3027,7 +3038,7 @@ fn rewrite_where_keyword(
30273038
&& comment_before.is_empty()
30283039
&& comment_after.is_empty();
30293040

3030-
Some((result, allow_single_line))
3041+
Ok((result, allow_single_line))
30313042
}
30323043

30333044
/// Rewrite bounds on a where clause.
@@ -3039,7 +3050,7 @@ fn rewrite_bounds_on_where_clause(
30393050
span_end: Option<BytePos>,
30403051
where_clause_option: WhereClauseOption,
30413052
force_single_line: bool,
3042-
) -> Option<String> {
3053+
) -> RewriteResult {
30433054
let span_start = predicates[0].span().lo();
30443055
// If we don't have the start of the next span, then use the end of the
30453056
// predicates, but that means we miss comments.
@@ -3078,7 +3089,7 @@ fn rewrite_bounds_on_where_clause(
30783089
.tactic(shape_tactic)
30793090
.trailing_separator(comma_tactic)
30803091
.preserve_newline(preserve_newline);
3081-
write_list(&items.collect::<Vec<_>>(), &fmt).ok()
3092+
write_list(&items.collect::<Vec<_>>(), &fmt)
30823093
}
30833094

30843095
fn rewrite_where_clause(
@@ -3092,9 +3103,9 @@ fn rewrite_where_clause(
30923103
span_end: Option<BytePos>,
30933104
span_end_before_where: BytePos,
30943105
where_clause_option: WhereClauseOption,
3095-
) -> Option<String> {
3106+
) -> RewriteResult {
30963107
if predicates.is_empty() {
3097-
return Some(String::new());
3108+
return Ok(String::new());
30983109
}
30993110

31003111
if context.config.indent_style() == IndentStyle::Block {
@@ -3154,7 +3165,7 @@ fn rewrite_where_clause(
31543165
.trailing_separator(comma_tactic)
31553166
.ends_with_newline(tactic.ends_with_newline(context.config.indent_style()))
31563167
.preserve_newline(true);
3157-
let preds_str = write_list(&item_vec, &fmt).ok()?;
3168+
let preds_str = write_list(&item_vec, &fmt)?;
31583169

31593170
let end_length = if terminator == "{" {
31603171
// If the brace is on the next line we don't need to count it otherwise it needs two
@@ -3172,13 +3183,13 @@ fn rewrite_where_clause(
31723183
|| preds_str.contains('\n')
31733184
|| shape.indent.width() + " where ".len() + preds_str.len() + end_length > shape.width
31743185
{
3175-
Some(format!(
3186+
Ok(format!(
31763187
"\n{}where {}",
31773188
(shape.indent + extra_indent).to_string(context.config),
31783189
preds_str
31793190
))
31803191
} else {
3181-
Some(format!(" where {preds_str}"))
3192+
Ok(format!(" where {preds_str}"))
31823193
}
31833194
}
31843195

@@ -3199,15 +3210,14 @@ fn rewrite_comments_before_after_where(
31993210
span_before_where: Span,
32003211
span_after_where: Span,
32013212
shape: Shape,
3202-
) -> Option<(String, String)> {
3203-
let before_comment = rewrite_missing_comment(span_before_where, shape, context).ok()?;
3213+
) -> Result<(String, String), RewriteError> {
3214+
let before_comment = rewrite_missing_comment(span_before_where, shape, context)?;
32043215
let after_comment = rewrite_missing_comment(
32053216
span_after_where,
32063217
shape.block_indent(context.config.tab_spaces()),
32073218
context,
3208-
)
3209-
.ok()?;
3210-
Some((before_comment, after_comment))
3219+
)?;
3220+
Ok((before_comment, after_comment))
32113221
}
32123222

32133223
fn format_header(
@@ -3289,7 +3299,8 @@ fn format_generics(
32893299
Some(span.hi()),
32903300
span_end_before_where,
32913301
option,
3292-
)?;
3302+
)
3303+
.ok()?;
32933304
result.push_str(&where_clause_str);
32943305
(
32953306
brace_pos == BracePos::ForceSameLine || brace_style == BraceStyle::PreferSameLine,

src/types.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,10 @@ fn get_tactics(item_vec: &[ListItem], output: &str, shape: Shape) -> DefinitiveL
457457

458458
impl Rewrite for ast::WherePredicate {
459459
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
460+
self.rewrite_result(context, shape).ok()
461+
}
462+
463+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
460464
// FIXME: dead spans?
461465
let result = match *self {
462466
ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate {
@@ -465,7 +469,7 @@ impl Rewrite for ast::WherePredicate {
465469
ref bounds,
466470
..
467471
}) => {
468-
let type_str = bounded_ty.rewrite(context, shape)?;
472+
let type_str = bounded_ty.rewrite_result(context, shape)?;
469473
let colon = type_bound_colon(context).trim_end();
470474
let lhs = if let Some(binder_str) =
471475
rewrite_bound_params(context, shape, bound_generic_params)
@@ -475,25 +479,26 @@ impl Rewrite for ast::WherePredicate {
475479
format!("{type_str}{colon}")
476480
};
477481

478-
rewrite_assign_rhs(context, lhs, bounds, &RhsAssignKind::Bounds, shape).ok()?
482+
rewrite_assign_rhs(context, lhs, bounds, &RhsAssignKind::Bounds, shape)?
479483
}
480484
ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate {
481485
ref lifetime,
482486
ref bounds,
483487
span,
484-
}) => rewrite_bounded_lifetime(lifetime, bounds, span, context, shape).ok()?,
488+
}) => rewrite_bounded_lifetime(lifetime, bounds, span, context, shape)?,
485489
ast::WherePredicate::EqPredicate(ast::WhereEqPredicate {
486490
ref lhs_ty,
487491
ref rhs_ty,
488492
..
489493
}) => {
490-
let lhs_ty_str = lhs_ty.rewrite(context, shape).map(|lhs| lhs + " =")?;
491-
rewrite_assign_rhs(context, lhs_ty_str, &**rhs_ty, &RhsAssignKind::Ty, shape)
492-
.ok()?
494+
let lhs_ty_str = lhs_ty
495+
.rewrite_result(context, shape)
496+
.map(|lhs| lhs + " =")?;
497+
rewrite_assign_rhs(context, lhs_ty_str, &**rhs_ty, &RhsAssignKind::Ty, shape)?
493498
}
494499
};
495500

496-
Some(result)
501+
Ok(result)
497502
}
498503
}
499504

0 commit comments

Comments
 (0)