Skip to content

Commit c6b0894

Browse files
committed
rewrite_result for GenericBounds and GenericParam
1 parent d2d6037 commit c6b0894

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
@@ -542,7 +542,7 @@ fn rewrite_bounded_lifetime(
542542
"{}{}{}",
543543
result,
544544
colon,
545-
join_bounds(context, shape.sub_width(overhead)?, bounds, true)?
545+
join_bounds(context, shape.sub_width(overhead)?, bounds, true).ok()?
546546
);
547547
Some(result)
548548
}
@@ -605,8 +605,12 @@ impl Rewrite for ast::GenericBound {
605605

606606
impl Rewrite for ast::GenericBounds {
607607
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
608+
self.rewrite_result(context, shape).ok()
609+
}
610+
611+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
608612
if self.is_empty() {
609-
return Some(String::new());
613+
return Ok(String::new());
610614
}
611615

612616
join_bounds(context, shape, self, true)
@@ -615,8 +619,15 @@ impl Rewrite for ast::GenericBounds {
615619

616620
impl Rewrite for ast::GenericParam {
617621
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
622+
self.rewrite_result(context, shape).ok()
623+
}
624+
625+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
618626
// FIXME: If there are more than one attributes, this will force multiline.
619-
let mut result = self.attrs.rewrite(context, shape).unwrap_or(String::new());
627+
let mut result = self
628+
.attrs
629+
.rewrite_result(context, shape)
630+
.unwrap_or(String::new());
620631
let has_attrs = !result.is_empty();
621632

622633
let mut param = String::with_capacity(128);
@@ -630,15 +641,19 @@ impl Rewrite for ast::GenericParam {
630641
param.push_str("const ");
631642
param.push_str(rewrite_ident(context, self.ident));
632643
param.push_str(": ");
633-
param.push_str(&ty.rewrite(context, shape)?);
644+
param.push_str(&ty.rewrite_result(context, shape)?);
634645
if let Some(default) = default {
635646
let eq_str = match context.config.type_punctuation_density() {
636647
TypeDensity::Compressed => "=",
637648
TypeDensity::Wide => " = ",
638649
};
639650
param.push_str(eq_str);
640-
let budget = shape.width.checked_sub(param.len())?;
641-
let rewrite = default.rewrite(context, Shape::legacy(budget, shape.indent))?;
651+
let budget = shape
652+
.width
653+
.checked_sub(param.len())
654+
.max_width_error(shape.width, self.span())?;
655+
let rewrite =
656+
default.rewrite_result(context, Shape::legacy(budget, shape.indent))?;
642657
param.push_str(&rewrite);
643658
}
644659
kw_span.lo()
@@ -649,7 +664,7 @@ impl Rewrite for ast::GenericParam {
649664

650665
if !self.bounds.is_empty() {
651666
param.push_str(type_bound_colon(context));
652-
param.push_str(&self.bounds.rewrite(context, shape)?)
667+
param.push_str(&self.bounds.rewrite_result(context, shape)?)
653668
}
654669
if let ast::GenericParamKind::Type {
655670
default: Some(ref def),
@@ -660,9 +675,12 @@ impl Rewrite for ast::GenericParam {
660675
TypeDensity::Wide => " = ",
661676
};
662677
param.push_str(eq_str);
663-
let budget = shape.width.checked_sub(param.len())?;
678+
let budget = shape
679+
.width
680+
.checked_sub(param.len())
681+
.max_width_error(shape.width, self.span())?;
664682
let rewrite =
665-
def.rewrite(context, Shape::legacy(budget, shape.indent + param.len()))?;
683+
def.rewrite_result(context, Shape::legacy(budget, shape.indent + param.len()))?;
666684
param.push_str(&rewrite);
667685
}
668686

@@ -676,7 +694,8 @@ impl Rewrite for ast::GenericParam {
676694
mk_sp(last_attr.span.hi(), param_start),
677695
shape,
678696
!last_attr.is_doc_comment(),
679-
)?;
697+
)
698+
.unknown_error()?;
680699
} else {
681700
// When rewriting generic params, an extra newline should be put
682701
// if the attributes end with a doc comment
@@ -688,7 +707,7 @@ impl Rewrite for ast::GenericParam {
688707
result.push_str(&param);
689708
}
690709

691-
Some(result)
710+
Ok(result)
692711
}
693712
}
694713

@@ -924,7 +943,7 @@ impl Rewrite for ast::Ty {
924943
let rw = if context.config.version() == Version::One {
925944
it.rewrite_result(context, shape)
926945
} else {
927-
join_bounds(context, shape, it, false).unknown_error()
946+
join_bounds(context, shape, it, false)
928947
};
929948
rw.map(|it_str| {
930949
let space = if it_str.is_empty() { "" } else { " " };
@@ -1024,7 +1043,7 @@ fn join_bounds(
10241043
shape: Shape,
10251044
items: &[ast::GenericBound],
10261045
need_indent: bool,
1027-
) -> Option<String> {
1046+
) -> RewriteResult {
10281047
join_bounds_inner(context, shape, items, need_indent, false)
10291048
}
10301049

@@ -1034,7 +1053,7 @@ fn join_bounds_inner(
10341053
items: &[ast::GenericBound],
10351054
need_indent: bool,
10361055
force_newline: bool,
1037-
) -> Option<String> {
1056+
) -> RewriteResult {
10381057
debug_assert!(!items.is_empty());
10391058

10401059
let generic_bounds_in_order = is_generic_bounds_in_order(items);
@@ -1129,16 +1148,17 @@ fn join_bounds_inner(
11291148
};
11301149

11311150
let (extendable, trailing_str) = if i == 0 {
1132-
let bound_str = item.rewrite(context, shape)?;
1151+
let bound_str = item.rewrite_result(context, shape)?;
11331152
(is_bound_extendable(&bound_str, item), bound_str)
11341153
} else {
1135-
let bound_str = &item.rewrite(context, shape)?;
1154+
let bound_str = &item.rewrite_result(context, shape)?;
11361155
match leading_span {
11371156
Some(ls) if has_leading_comment => (
11381157
is_bound_extendable(bound_str, item),
11391158
combine_strs_with_missing_comments(
11401159
context, joiner, bound_str, ls, shape, true,
1141-
)?,
1160+
)
1161+
.unknown_error()?,
11421162
),
11431163
_ => (
11441164
is_bound_extendable(bound_str, item),
@@ -1155,8 +1175,9 @@ fn join_bounds_inner(
11551175
shape,
11561176
true,
11571177
)
1178+
.unknown_error()
11581179
.map(|v| (v, trailing_span, extendable)),
1159-
_ => Some((strs + &trailing_str, trailing_span, extendable)),
1180+
_ => Ok((strs + &trailing_str, trailing_span, extendable)),
11601181
}
11611182
},
11621183
)?;
@@ -1181,7 +1202,7 @@ fn join_bounds_inner(
11811202
if retry_with_force_newline {
11821203
join_bounds_inner(context, shape, items, need_indent, true)
11831204
} else {
1184-
Some(result.0)
1205+
Ok(result.0)
11851206
}
11861207
}
11871208

0 commit comments

Comments
 (0)