Skip to content

Commit 841e42c

Browse files
committed
rewrite_result for GenericBounds, GenericParam, SegmentParam
1 parent d2d6037 commit 841e42c

File tree

1 file changed

+48
-23
lines changed

1 file changed

+48
-23
lines changed

src/types.rs

+48-23
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,15 @@ impl<'a> Spanned for SegmentParam<'a> {
168168

169169
impl<'a> Rewrite for SegmentParam<'a> {
170170
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
171+
self.rewrite_result(context, shape).ok()
172+
}
173+
174+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
171175
match *self {
172-
SegmentParam::Const(const_) => const_.rewrite(context, shape),
173-
SegmentParam::LifeTime(lt) => lt.rewrite(context, shape),
174-
SegmentParam::Type(ty) => ty.rewrite(context, shape),
175-
SegmentParam::Binding(atc) => atc.rewrite(context, shape),
176+
SegmentParam::Const(const_) => const_.rewrite_result(context, shape),
177+
SegmentParam::LifeTime(lt) => lt.rewrite_result(context, shape),
178+
SegmentParam::Type(ty) => ty.rewrite_result(context, shape),
179+
SegmentParam::Binding(atc) => atc.rewrite_result(context, shape),
176180
}
177181
}
178182
}
@@ -542,7 +546,7 @@ fn rewrite_bounded_lifetime(
542546
"{}{}{}",
543547
result,
544548
colon,
545-
join_bounds(context, shape.sub_width(overhead)?, bounds, true)?
549+
join_bounds(context, shape.sub_width(overhead)?, bounds, true).ok()?
546550
);
547551
Some(result)
548552
}
@@ -605,8 +609,12 @@ impl Rewrite for ast::GenericBound {
605609

606610
impl Rewrite for ast::GenericBounds {
607611
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
612+
self.rewrite_result(context, shape).ok()
613+
}
614+
615+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
608616
if self.is_empty() {
609-
return Some(String::new());
617+
return Ok(String::new());
610618
}
611619

612620
join_bounds(context, shape, self, true)
@@ -615,8 +623,15 @@ impl Rewrite for ast::GenericBounds {
615623

616624
impl Rewrite for ast::GenericParam {
617625
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
626+
self.rewrite_result(context, shape).ok()
627+
}
628+
629+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
618630
// 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());
631+
let mut result = self
632+
.attrs
633+
.rewrite_result(context, shape)
634+
.unwrap_or(String::new());
620635
let has_attrs = !result.is_empty();
621636

622637
let mut param = String::with_capacity(128);
@@ -630,15 +645,19 @@ impl Rewrite for ast::GenericParam {
630645
param.push_str("const ");
631646
param.push_str(rewrite_ident(context, self.ident));
632647
param.push_str(": ");
633-
param.push_str(&ty.rewrite(context, shape)?);
648+
param.push_str(&ty.rewrite_result(context, shape)?);
634649
if let Some(default) = default {
635650
let eq_str = match context.config.type_punctuation_density() {
636651
TypeDensity::Compressed => "=",
637652
TypeDensity::Wide => " = ",
638653
};
639654
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))?;
655+
let budget = shape
656+
.width
657+
.checked_sub(param.len())
658+
.max_width_error(shape.width, self.span())?;
659+
let rewrite =
660+
default.rewrite_result(context, Shape::legacy(budget, shape.indent))?;
642661
param.push_str(&rewrite);
643662
}
644663
kw_span.lo()
@@ -649,7 +668,7 @@ impl Rewrite for ast::GenericParam {
649668

650669
if !self.bounds.is_empty() {
651670
param.push_str(type_bound_colon(context));
652-
param.push_str(&self.bounds.rewrite(context, shape)?)
671+
param.push_str(&self.bounds.rewrite_result(context, shape)?)
653672
}
654673
if let ast::GenericParamKind::Type {
655674
default: Some(ref def),
@@ -660,9 +679,12 @@ impl Rewrite for ast::GenericParam {
660679
TypeDensity::Wide => " = ",
661680
};
662681
param.push_str(eq_str);
663-
let budget = shape.width.checked_sub(param.len())?;
682+
let budget = shape
683+
.width
684+
.checked_sub(param.len())
685+
.max_width_error(shape.width, self.span())?;
664686
let rewrite =
665-
def.rewrite(context, Shape::legacy(budget, shape.indent + param.len()))?;
687+
def.rewrite_result(context, Shape::legacy(budget, shape.indent + param.len()))?;
666688
param.push_str(&rewrite);
667689
}
668690

@@ -676,7 +698,8 @@ impl Rewrite for ast::GenericParam {
676698
mk_sp(last_attr.span.hi(), param_start),
677699
shape,
678700
!last_attr.is_doc_comment(),
679-
)?;
701+
)
702+
.unknown_error()?;
680703
} else {
681704
// When rewriting generic params, an extra newline should be put
682705
// if the attributes end with a doc comment
@@ -688,7 +711,7 @@ impl Rewrite for ast::GenericParam {
688711
result.push_str(&param);
689712
}
690713

691-
Some(result)
714+
Ok(result)
692715
}
693716
}
694717

@@ -924,7 +947,7 @@ impl Rewrite for ast::Ty {
924947
let rw = if context.config.version() == Version::One {
925948
it.rewrite_result(context, shape)
926949
} else {
927-
join_bounds(context, shape, it, false).unknown_error()
950+
join_bounds(context, shape, it, false)
928951
};
929952
rw.map(|it_str| {
930953
let space = if it_str.is_empty() { "" } else { " " };
@@ -1024,7 +1047,7 @@ fn join_bounds(
10241047
shape: Shape,
10251048
items: &[ast::GenericBound],
10261049
need_indent: bool,
1027-
) -> Option<String> {
1050+
) -> RewriteResult {
10281051
join_bounds_inner(context, shape, items, need_indent, false)
10291052
}
10301053

@@ -1034,7 +1057,7 @@ fn join_bounds_inner(
10341057
items: &[ast::GenericBound],
10351058
need_indent: bool,
10361059
force_newline: bool,
1037-
) -> Option<String> {
1060+
) -> RewriteResult {
10381061
debug_assert!(!items.is_empty());
10391062

10401063
let generic_bounds_in_order = is_generic_bounds_in_order(items);
@@ -1129,16 +1152,17 @@ fn join_bounds_inner(
11291152
};
11301153

11311154
let (extendable, trailing_str) = if i == 0 {
1132-
let bound_str = item.rewrite(context, shape)?;
1155+
let bound_str = item.rewrite_result(context, shape)?;
11331156
(is_bound_extendable(&bound_str, item), bound_str)
11341157
} else {
1135-
let bound_str = &item.rewrite(context, shape)?;
1158+
let bound_str = &item.rewrite_result(context, shape)?;
11361159
match leading_span {
11371160
Some(ls) if has_leading_comment => (
11381161
is_bound_extendable(bound_str, item),
11391162
combine_strs_with_missing_comments(
11401163
context, joiner, bound_str, ls, shape, true,
1141-
)?,
1164+
)
1165+
.unknown_error()?,
11421166
),
11431167
_ => (
11441168
is_bound_extendable(bound_str, item),
@@ -1155,8 +1179,9 @@ fn join_bounds_inner(
11551179
shape,
11561180
true,
11571181
)
1182+
.unknown_error()
11581183
.map(|v| (v, trailing_span, extendable)),
1159-
_ => Some((strs + &trailing_str, trailing_span, extendable)),
1184+
_ => Ok((strs + &trailing_str, trailing_span, extendable)),
11601185
}
11611186
},
11621187
)?;
@@ -1181,7 +1206,7 @@ fn join_bounds_inner(
11811206
if retry_with_force_newline {
11821207
join_bounds_inner(context, shape, items, need_indent, true)
11831208
} else {
1184-
Some(result.0)
1209+
Ok(result.0)
11851210
}
11861211
}
11871212

0 commit comments

Comments
 (0)