Skip to content

Commit 959c65c

Browse files
committed
rewrite_result for GenericBounds and GenericParam
1 parent 68ee602 commit 959c65c

File tree

1 file changed

+40
-19
lines changed

1 file changed

+40
-19
lines changed

src/types.rs

+40-19
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ fn rewrite_bounded_lifetime(
531531
"{}{}{}",
532532
result,
533533
colon,
534-
join_bounds(context, shape.sub_width(overhead)?, bounds, true)?
534+
join_bounds(context, shape.sub_width(overhead)?, bounds, true).ok()?
535535
);
536536
Some(result)
537537
}
@@ -590,8 +590,12 @@ impl Rewrite for ast::GenericBound {
590590

591591
impl Rewrite for ast::GenericBounds {
592592
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
593+
self.rewrite_result(context, shape).ok()
594+
}
595+
596+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
593597
if self.is_empty() {
594-
return Some(String::new());
598+
return Ok(String::new());
595599
}
596600

597601
join_bounds(context, shape, self, true)
@@ -600,8 +604,15 @@ impl Rewrite for ast::GenericBounds {
600604

601605
impl Rewrite for ast::GenericParam {
602606
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
607+
self.rewrite_result(context, shape).ok()
608+
}
609+
610+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
603611
// FIXME: If there are more than one attributes, this will force multiline.
604-
let mut result = self.attrs.rewrite(context, shape).unwrap_or(String::new());
612+
let mut result = self
613+
.attrs
614+
.rewrite_result(context, shape)
615+
.unwrap_or(String::new());
605616
let has_attrs = !result.is_empty();
606617

607618
let mut param = String::with_capacity(128);
@@ -615,15 +626,19 @@ impl Rewrite for ast::GenericParam {
615626
param.push_str("const ");
616627
param.push_str(rewrite_ident(context, self.ident));
617628
param.push_str(": ");
618-
param.push_str(&ty.rewrite(context, shape)?);
629+
param.push_str(&ty.rewrite_result(context, shape)?);
619630
if let Some(default) = default {
620631
let eq_str = match context.config.type_punctuation_density() {
621632
TypeDensity::Compressed => "=",
622633
TypeDensity::Wide => " = ",
623634
};
624635
param.push_str(eq_str);
625-
let budget = shape.width.checked_sub(param.len())?;
626-
let rewrite = default.rewrite(context, Shape::legacy(budget, shape.indent))?;
636+
let budget = shape
637+
.width
638+
.checked_sub(param.len())
639+
.max_width_error(shape.width, self.span())?;
640+
let rewrite =
641+
default.rewrite_result(context, Shape::legacy(budget, shape.indent))?;
627642
param.push_str(&rewrite);
628643
}
629644
kw_span.lo()
@@ -634,7 +649,7 @@ impl Rewrite for ast::GenericParam {
634649

635650
if !self.bounds.is_empty() {
636651
param.push_str(type_bound_colon(context));
637-
param.push_str(&self.bounds.rewrite(context, shape)?)
652+
param.push_str(&self.bounds.rewrite_result(context, shape)?)
638653
}
639654
if let ast::GenericParamKind::Type {
640655
default: Some(ref def),
@@ -645,9 +660,12 @@ impl Rewrite for ast::GenericParam {
645660
TypeDensity::Wide => " = ",
646661
};
647662
param.push_str(eq_str);
648-
let budget = shape.width.checked_sub(param.len())?;
663+
let budget = shape
664+
.width
665+
.checked_sub(param.len())
666+
.max_width_error(shape.width, self.span())?;
649667
let rewrite =
650-
def.rewrite(context, Shape::legacy(budget, shape.indent + param.len()))?;
668+
def.rewrite_result(context, Shape::legacy(budget, shape.indent + param.len()))?;
651669
param.push_str(&rewrite);
652670
}
653671

@@ -661,7 +679,8 @@ impl Rewrite for ast::GenericParam {
661679
mk_sp(last_attr.span.hi(), param_start),
662680
shape,
663681
!last_attr.is_doc_comment(),
664-
)?;
682+
)
683+
.unknown_error()?;
665684
} else {
666685
// When rewriting generic params, an extra newline should be put
667686
// if the attributes end with a doc comment
@@ -673,7 +692,7 @@ impl Rewrite for ast::GenericParam {
673692
result.push_str(&param);
674693
}
675694

676-
Some(result)
695+
Ok(result)
677696
}
678697
}
679698

@@ -913,7 +932,7 @@ impl Rewrite for ast::Ty {
913932
let rw = if context.config.version() == Version::One {
914933
it.rewrite_result(context, shape)
915934
} else {
916-
join_bounds(context, shape, it, false).unknown_error()
935+
join_bounds(context, shape, it, false)
917936
};
918937
rw.map(|it_str| {
919938
let space = if it_str.is_empty() { "" } else { " " };
@@ -1013,7 +1032,7 @@ fn join_bounds(
10131032
shape: Shape,
10141033
items: &[ast::GenericBound],
10151034
need_indent: bool,
1016-
) -> Option<String> {
1035+
) -> RewriteResult {
10171036
join_bounds_inner(context, shape, items, need_indent, false)
10181037
}
10191038

@@ -1023,7 +1042,7 @@ fn join_bounds_inner(
10231042
items: &[ast::GenericBound],
10241043
need_indent: bool,
10251044
force_newline: bool,
1026-
) -> Option<String> {
1045+
) -> RewriteResult {
10271046
debug_assert!(!items.is_empty());
10281047

10291048
let generic_bounds_in_order = is_generic_bounds_in_order(items);
@@ -1116,16 +1135,17 @@ fn join_bounds_inner(
11161135
};
11171136

11181137
let (extendable, trailing_str) = if i == 0 {
1119-
let bound_str = item.rewrite(context, shape)?;
1138+
let bound_str = item.rewrite_result(context, shape)?;
11201139
(is_bound_extendable(&bound_str, item), bound_str)
11211140
} else {
1122-
let bound_str = &item.rewrite(context, shape)?;
1141+
let bound_str = &item.rewrite_result(context, shape)?;
11231142
match leading_span {
11241143
Some(ls) if has_leading_comment => (
11251144
is_bound_extendable(bound_str, item),
11261145
combine_strs_with_missing_comments(
11271146
context, joiner, bound_str, ls, shape, true,
1128-
)?,
1147+
)
1148+
.unknown_error()?,
11291149
),
11301150
_ => (
11311151
is_bound_extendable(bound_str, item),
@@ -1142,8 +1162,9 @@ fn join_bounds_inner(
11421162
shape,
11431163
true,
11441164
)
1165+
.unknown_error()
11451166
.map(|v| (v, trailing_span, extendable)),
1146-
_ => Some((strs + &trailing_str, trailing_span, extendable)),
1167+
_ => Ok((strs + &trailing_str, trailing_span, extendable)),
11471168
}
11481169
},
11491170
)?;
@@ -1168,7 +1189,7 @@ fn join_bounds_inner(
11681189
if retry_with_force_newline {
11691190
join_bounds_inner(context, shape, items, need_indent, true)
11701191
} else {
1171-
Some(result.0)
1192+
Ok(result.0)
11721193
}
11731194
}
11741195

0 commit comments

Comments
 (0)