Skip to content

Commit e7038e7

Browse files
committed
impl rewrite_result for TraitAliasBounds, WherePredicate
1 parent 46cb7d3 commit e7038e7

File tree

2 files changed

+63
-40
lines changed

2 files changed

+63
-40
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.
@@ -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

@@ -1622,7 +1628,8 @@ fn format_tuple_struct(
16221628
None,
16231629
body_hi,
16241630
option,
1625-
)?
1631+
)
1632+
.ok()?
16261633
}
16271634
None => "".to_owned(),
16281635
};
@@ -1791,7 +1798,8 @@ fn rewrite_ty<R: Rewrite>(
17911798
None,
17921799
generics.span.hi(),
17931800
option,
1794-
)?;
1801+
)
1802+
.ok()?;
17951803
result.push_str(&where_clause_str);
17961804

17971805
if let Some(ty) = rhs {
@@ -2661,7 +2669,8 @@ fn rewrite_fn_base(
26612669
Some(span.hi()),
26622670
pos_before_where,
26632671
option,
2664-
)?;
2672+
)
2673+
.ok()?;
26652674
// If there are neither where-clause nor return type, we may be missing comments between
26662675
// params and `{`.
26672676
if where_clause_str.is_empty() {
@@ -2937,7 +2946,7 @@ fn rewrite_where_clause_rfc_style(
29372946
span_end: Option<BytePos>,
29382947
span_end_before_where: BytePos,
29392948
where_clause_option: WhereClauseOption,
2940-
) -> Option<String> {
2949+
) -> RewriteResult {
29412950
let (where_keyword, allow_single_line) = rewrite_where_keyword(
29422951
context,
29432952
predicates,
@@ -2951,8 +2960,9 @@ fn rewrite_where_clause_rfc_style(
29512960
let clause_shape = shape
29522961
.block()
29532962
.with_max_width(context.config)
2954-
.block_left(context.config.tab_spaces())?
2955-
.sub_width(1)?;
2963+
.block_left(context.config.tab_spaces())
2964+
.and_then(|s| s.sub_width(1))
2965+
.max_width_error(shape.width, where_span)?;
29562966
let force_single_line = context.config.where_single_line()
29572967
&& predicates.len() == 1
29582968
&& !where_clause_option.veto_single_line;
@@ -2977,7 +2987,7 @@ fn rewrite_where_clause_rfc_style(
29772987
clause_shape.indent.to_string_with_newline(context.config)
29782988
};
29792989

2980-
Some(format!("{where_keyword}{clause_sep}{preds_str}"))
2990+
Ok(format!("{where_keyword}{clause_sep}{preds_str}"))
29812991
}
29822992

29832993
/// Rewrite `where` and comment around it.
@@ -2988,12 +2998,13 @@ fn rewrite_where_keyword(
29882998
shape: Shape,
29892999
span_end_before_where: BytePos,
29903000
where_clause_option: WhereClauseOption,
2991-
) -> Option<(String, bool)> {
3001+
) -> Result<(String, bool), RewriteError> {
29923002
let block_shape = shape.block().with_max_width(context.config);
29933003
// 1 = `,`
29943004
let clause_shape = block_shape
2995-
.block_left(context.config.tab_spaces())?
2996-
.sub_width(1)?;
3005+
.block_left(context.config.tab_spaces())
3006+
.and_then(|s| s.sub_width(1))
3007+
.max_width_error(block_shape.width, where_span)?;
29973008

29983009
let comment_separator = |comment: &str, shape: Shape| {
29993010
if comment.is_empty() {
@@ -3024,7 +3035,7 @@ fn rewrite_where_keyword(
30243035
&& comment_before.is_empty()
30253036
&& comment_after.is_empty();
30263037

3027-
Some((result, allow_single_line))
3038+
Ok((result, allow_single_line))
30283039
}
30293040

30303041
/// Rewrite bounds on a where clause.
@@ -3036,7 +3047,7 @@ fn rewrite_bounds_on_where_clause(
30363047
span_end: Option<BytePos>,
30373048
where_clause_option: WhereClauseOption,
30383049
force_single_line: bool,
3039-
) -> Option<String> {
3050+
) -> RewriteResult {
30403051
let span_start = predicates[0].span().lo();
30413052
// If we don't have the start of the next span, then use the end of the
30423053
// predicates, but that means we miss comments.
@@ -3075,7 +3086,7 @@ fn rewrite_bounds_on_where_clause(
30753086
.tactic(shape_tactic)
30763087
.trailing_separator(comma_tactic)
30773088
.preserve_newline(preserve_newline);
3078-
write_list(&items.collect::<Vec<_>>(), &fmt).ok()
3089+
write_list(&items.collect::<Vec<_>>(), &fmt)
30793090
}
30803091

30813092
fn rewrite_where_clause(
@@ -3089,9 +3100,9 @@ fn rewrite_where_clause(
30893100
span_end: Option<BytePos>,
30903101
span_end_before_where: BytePos,
30913102
where_clause_option: WhereClauseOption,
3092-
) -> Option<String> {
3103+
) -> RewriteResult {
30933104
if predicates.is_empty() {
3094-
return Some(String::new());
3105+
return Ok(String::new());
30953106
}
30963107

30973108
if context.config.indent_style() == IndentStyle::Block {
@@ -3151,7 +3162,7 @@ fn rewrite_where_clause(
31513162
.trailing_separator(comma_tactic)
31523163
.ends_with_newline(tactic.ends_with_newline(context.config.indent_style()))
31533164
.preserve_newline(true);
3154-
let preds_str = write_list(&item_vec, &fmt).ok()?;
3165+
let preds_str = write_list(&item_vec, &fmt)?;
31553166

31563167
let end_length = if terminator == "{" {
31573168
// If the brace is on the next line we don't need to count it otherwise it needs two
@@ -3169,13 +3180,13 @@ fn rewrite_where_clause(
31693180
|| preds_str.contains('\n')
31703181
|| shape.indent.width() + " where ".len() + preds_str.len() + end_length > shape.width
31713182
{
3172-
Some(format!(
3183+
Ok(format!(
31733184
"\n{}where {}",
31743185
(shape.indent + extra_indent).to_string(context.config),
31753186
preds_str
31763187
))
31773188
} else {
3178-
Some(format!(" where {preds_str}"))
3189+
Ok(format!(" where {preds_str}"))
31793190
}
31803191
}
31813192

@@ -3196,15 +3207,14 @@ fn rewrite_comments_before_after_where(
31963207
span_before_where: Span,
31973208
span_after_where: Span,
31983209
shape: Shape,
3199-
) -> Option<(String, String)> {
3200-
let before_comment = rewrite_missing_comment(span_before_where, shape, context).ok()?;
3210+
) -> Result<(String, String), RewriteError> {
3211+
let before_comment = rewrite_missing_comment(span_before_where, shape, context)?;
32013212
let after_comment = rewrite_missing_comment(
32023213
span_after_where,
32033214
shape.block_indent(context.config.tab_spaces()),
32043215
context,
3205-
)
3206-
.ok()?;
3207-
Some((before_comment, after_comment))
3216+
)?;
3217+
Ok((before_comment, after_comment))
32083218
}
32093219

32103220
fn format_header(
@@ -3286,7 +3296,8 @@ fn format_generics(
32863296
Some(span.hi()),
32873297
span_end_before_where,
32883298
option,
3289-
)?;
3299+
)
3300+
.ok()?;
32903301
result.push_str(&where_clause_str);
32913302
(
32923303
brace_pos == BracePos::ForceSameLine || brace_style == BraceStyle::PreferSameLine,

src/types.rs

Lines changed: 24 additions & 12 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,24 +479,28 @@ impl Rewrite for ast::WherePredicate {
475479
format!("{type_str}{colon}")
476480
};
477481

478-
rewrite_assign_rhs(context, lhs, bounds, &RhsAssignKind::Bounds, shape)?
482+
rewrite_assign_rhs(context, lhs, bounds, &RhsAssignKind::Bounds, shape)
483+
.unknown_error()?
479484
}
480485
ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate {
481486
ref lifetime,
482487
ref bounds,
483-
..
484-
}) => rewrite_bounded_lifetime(lifetime, bounds, context, shape)?,
488+
span,
489+
}) => rewrite_bounded_lifetime(lifetime, bounds, span, context, shape)?,
485490
ast::WherePredicate::EqPredicate(ast::WhereEqPredicate {
486491
ref lhs_ty,
487492
ref rhs_ty,
488493
..
489494
}) => {
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)?
495+
let lhs_ty_str = lhs_ty
496+
.rewrite_result(context, shape)
497+
.map(|lhs| lhs + " =")?;
498+
rewrite_assign_rhs(context, lhs_ty_str, &**rhs_ty, &RhsAssignKind::Ty, shape)
499+
.unknown_error()?
492500
}
493501
};
494502

495-
Some(result)
503+
Ok(result)
496504
}
497505
}
498506

@@ -552,23 +560,27 @@ fn rewrite_generic_args(
552560
fn rewrite_bounded_lifetime(
553561
lt: &ast::Lifetime,
554562
bounds: &[ast::GenericBound],
563+
span: Span,
555564
context: &RewriteContext<'_>,
556565
shape: Shape,
557-
) -> Option<String> {
558-
let result = lt.rewrite(context, shape)?;
566+
) -> RewriteResult {
567+
let result = lt.rewrite_result(context, shape)?;
559568

560569
if bounds.is_empty() {
561-
Some(result)
570+
Ok(result)
562571
} else {
563572
let colon = type_bound_colon(context);
564573
let overhead = last_line_width(&result) + colon.len();
574+
let shape = shape
575+
.sub_width(overhead)
576+
.max_width_error(shape.width, span)?;
565577
let result = format!(
566578
"{}{}{}",
567579
result,
568580
colon,
569-
join_bounds(context, shape.sub_width(overhead)?, bounds, true).ok()?
581+
join_bounds(context, shape, bounds, true)?
570582
);
571-
Some(result)
583+
Ok(result)
572584
}
573585
}
574586

0 commit comments

Comments
 (0)