Skip to content

Commit 2639370

Browse files
ding-youngytmimi
authored andcommitted
impl rewrite_result for TraitAliasBounds, WherePredicate
1 parent 1a70f40 commit 2639370

File tree

2 files changed

+51
-35
lines changed

2 files changed

+51
-35
lines changed

Diff for: src/items.rs

+39-28
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,8 @@ pub(crate) fn format_impl(
840840
where_span_end,
841841
self_ty.span.hi(),
842842
option,
843-
)?;
843+
)
844+
.ok()?;
844845

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

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

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

1368-
Some(format!("{generic_bounds_str}{space}{where_str}"))
1374+
Ok(format!("{generic_bounds_str}{space}{where_str}"))
13691375
}
13701376
}
13711377

@@ -1623,7 +1629,8 @@ fn format_tuple_struct(
16231629
None,
16241630
body_hi,
16251631
option,
1626-
)?
1632+
)
1633+
.ok()?
16271634
}
16281635
None => "".to_owned(),
16291636
};
@@ -1792,7 +1799,8 @@ fn rewrite_ty<R: Rewrite>(
17921799
None,
17931800
generics.span.hi(),
17941801
option,
1795-
)?;
1802+
)
1803+
.ok()?;
17961804
result.push_str(&where_clause_str);
17971805

17981806
if let Some(ty) = rhs {
@@ -2663,7 +2671,8 @@ fn rewrite_fn_base(
26632671
Some(span.hi()),
26642672
pos_before_where,
26652673
option,
2666-
)?;
2674+
)
2675+
.ok()?;
26672676
// If there are neither where-clause nor return type, we may be missing comments between
26682677
// params and `{`.
26692678
if where_clause_str.is_empty() {
@@ -2939,7 +2948,7 @@ fn rewrite_where_clause_rfc_style(
29392948
span_end: Option<BytePos>,
29402949
span_end_before_where: BytePos,
29412950
where_clause_option: WhereClauseOption,
2942-
) -> Option<String> {
2951+
) -> RewriteResult {
29432952
let (where_keyword, allow_single_line) = rewrite_where_keyword(
29442953
context,
29452954
predicates,
@@ -2953,8 +2962,9 @@ fn rewrite_where_clause_rfc_style(
29532962
let clause_shape = shape
29542963
.block()
29552964
.with_max_width(context.config)
2956-
.block_left(context.config.tab_spaces())?
2957-
.sub_width(1)?;
2965+
.block_left(context.config.tab_spaces())
2966+
.and_then(|s| s.sub_width(1))
2967+
.max_width_error(shape.width, where_span)?;
29582968
let force_single_line = context.config.where_single_line()
29592969
&& predicates.len() == 1
29602970
&& !where_clause_option.veto_single_line;
@@ -2979,7 +2989,7 @@ fn rewrite_where_clause_rfc_style(
29792989
clause_shape.indent.to_string_with_newline(context.config)
29802990
};
29812991

2982-
Some(format!("{where_keyword}{clause_sep}{preds_str}"))
2992+
Ok(format!("{where_keyword}{clause_sep}{preds_str}"))
29832993
}
29842994

29852995
/// Rewrite `where` and comment around it.
@@ -2990,12 +3000,13 @@ fn rewrite_where_keyword(
29903000
shape: Shape,
29913001
span_end_before_where: BytePos,
29923002
where_clause_option: WhereClauseOption,
2993-
) -> Option<(String, bool)> {
3003+
) -> Result<(String, bool), RewriteError> {
29943004
let block_shape = shape.block().with_max_width(context.config);
29953005
// 1 = `,`
29963006
let clause_shape = block_shape
2997-
.block_left(context.config.tab_spaces())?
2998-
.sub_width(1)?;
3007+
.block_left(context.config.tab_spaces())
3008+
.and_then(|s| s.sub_width(1))
3009+
.max_width_error(block_shape.width, where_span)?;
29993010

30003011
let comment_separator = |comment: &str, shape: Shape| {
30013012
if comment.is_empty() {
@@ -3026,7 +3037,7 @@ fn rewrite_where_keyword(
30263037
&& comment_before.is_empty()
30273038
&& comment_after.is_empty();
30283039

3029-
Some((result, allow_single_line))
3040+
Ok((result, allow_single_line))
30303041
}
30313042

30323043
/// Rewrite bounds on a where clause.
@@ -3038,7 +3049,7 @@ fn rewrite_bounds_on_where_clause(
30383049
span_end: Option<BytePos>,
30393050
where_clause_option: WhereClauseOption,
30403051
force_single_line: bool,
3041-
) -> Option<String> {
3052+
) -> RewriteResult {
30423053
let span_start = predicates[0].span().lo();
30433054
// If we don't have the start of the next span, then use the end of the
30443055
// predicates, but that means we miss comments.
@@ -3077,7 +3088,7 @@ fn rewrite_bounds_on_where_clause(
30773088
.tactic(shape_tactic)
30783089
.trailing_separator(comma_tactic)
30793090
.preserve_newline(preserve_newline);
3080-
write_list(&items.collect::<Vec<_>>(), &fmt).ok()
3091+
write_list(&items.collect::<Vec<_>>(), &fmt)
30813092
}
30823093

30833094
fn rewrite_where_clause(
@@ -3091,9 +3102,9 @@ fn rewrite_where_clause(
30913102
span_end: Option<BytePos>,
30923103
span_end_before_where: BytePos,
30933104
where_clause_option: WhereClauseOption,
3094-
) -> Option<String> {
3105+
) -> RewriteResult {
30953106
if predicates.is_empty() {
3096-
return Some(String::new());
3107+
return Ok(String::new());
30973108
}
30983109

30993110
if context.config.indent_style() == IndentStyle::Block {
@@ -3153,7 +3164,7 @@ fn rewrite_where_clause(
31533164
.trailing_separator(comma_tactic)
31543165
.ends_with_newline(tactic.ends_with_newline(context.config.indent_style()))
31553166
.preserve_newline(true);
3156-
let preds_str = write_list(&item_vec, &fmt).ok()?;
3167+
let preds_str = write_list(&item_vec, &fmt)?;
31573168

31583169
let end_length = if terminator == "{" {
31593170
// If the brace is on the next line we don't need to count it otherwise it needs two
@@ -3171,13 +3182,13 @@ fn rewrite_where_clause(
31713182
|| preds_str.contains('\n')
31723183
|| shape.indent.width() + " where ".len() + preds_str.len() + end_length > shape.width
31733184
{
3174-
Some(format!(
3185+
Ok(format!(
31753186
"\n{}where {}",
31763187
(shape.indent + extra_indent).to_string(context.config),
31773188
preds_str
31783189
))
31793190
} else {
3180-
Some(format!(" where {preds_str}"))
3191+
Ok(format!(" where {preds_str}"))
31813192
}
31823193
}
31833194

@@ -3198,15 +3209,14 @@ fn rewrite_comments_before_after_where(
31983209
span_before_where: Span,
31993210
span_after_where: Span,
32003211
shape: Shape,
3201-
) -> Option<(String, String)> {
3202-
let before_comment = rewrite_missing_comment(span_before_where, shape, context).ok()?;
3212+
) -> Result<(String, String), RewriteError> {
3213+
let before_comment = rewrite_missing_comment(span_before_where, shape, context)?;
32033214
let after_comment = rewrite_missing_comment(
32043215
span_after_where,
32053216
shape.block_indent(context.config.tab_spaces()),
32063217
context,
3207-
)
3208-
.ok()?;
3209-
Some((before_comment, after_comment))
3218+
)?;
3219+
Ok((before_comment, after_comment))
32103220
}
32113221

32123222
fn format_header(
@@ -3288,7 +3298,8 @@ fn format_generics(
32883298
Some(span.hi()),
32893299
span_end_before_where,
32903300
option,
3291-
)?;
3301+
)
3302+
.ok()?;
32923303
result.push_str(&where_clause_str);
32933304
(
32943305
brace_pos == BracePos::ForceSameLine || brace_style == BraceStyle::PreferSameLine,

Diff for: src/types.rs

+12-7
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)