From 959c65c568cb62881fb189215b06d88e259a31ba Mon Sep 17 00:00:00 2001 From: ding-young Date: Sun, 30 Jun 2024 15:54:29 +0900 Subject: [PATCH] rewrite_result for GenericBounds and GenericParam --- src/types.rs | 59 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/src/types.rs b/src/types.rs index 3fc21dbba53..694a81e8fc8 100644 --- a/src/types.rs +++ b/src/types.rs @@ -531,7 +531,7 @@ fn rewrite_bounded_lifetime( "{}{}{}", result, colon, - join_bounds(context, shape.sub_width(overhead)?, bounds, true)? + join_bounds(context, shape.sub_width(overhead)?, bounds, true).ok()? ); Some(result) } @@ -590,8 +590,12 @@ impl Rewrite for ast::GenericBound { impl Rewrite for ast::GenericBounds { fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option { + self.rewrite_result(context, shape).ok() + } + + fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult { if self.is_empty() { - return Some(String::new()); + return Ok(String::new()); } join_bounds(context, shape, self, true) @@ -600,8 +604,15 @@ impl Rewrite for ast::GenericBounds { impl Rewrite for ast::GenericParam { fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option { + self.rewrite_result(context, shape).ok() + } + + fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult { // FIXME: If there are more than one attributes, this will force multiline. - let mut result = self.attrs.rewrite(context, shape).unwrap_or(String::new()); + let mut result = self + .attrs + .rewrite_result(context, shape) + .unwrap_or(String::new()); let has_attrs = !result.is_empty(); let mut param = String::with_capacity(128); @@ -615,15 +626,19 @@ impl Rewrite for ast::GenericParam { param.push_str("const "); param.push_str(rewrite_ident(context, self.ident)); param.push_str(": "); - param.push_str(&ty.rewrite(context, shape)?); + param.push_str(&ty.rewrite_result(context, shape)?); if let Some(default) = default { let eq_str = match context.config.type_punctuation_density() { TypeDensity::Compressed => "=", TypeDensity::Wide => " = ", }; param.push_str(eq_str); - let budget = shape.width.checked_sub(param.len())?; - let rewrite = default.rewrite(context, Shape::legacy(budget, shape.indent))?; + let budget = shape + .width + .checked_sub(param.len()) + .max_width_error(shape.width, self.span())?; + let rewrite = + default.rewrite_result(context, Shape::legacy(budget, shape.indent))?; param.push_str(&rewrite); } kw_span.lo() @@ -634,7 +649,7 @@ impl Rewrite for ast::GenericParam { if !self.bounds.is_empty() { param.push_str(type_bound_colon(context)); - param.push_str(&self.bounds.rewrite(context, shape)?) + param.push_str(&self.bounds.rewrite_result(context, shape)?) } if let ast::GenericParamKind::Type { default: Some(ref def), @@ -645,9 +660,12 @@ impl Rewrite for ast::GenericParam { TypeDensity::Wide => " = ", }; param.push_str(eq_str); - let budget = shape.width.checked_sub(param.len())?; + let budget = shape + .width + .checked_sub(param.len()) + .max_width_error(shape.width, self.span())?; let rewrite = - def.rewrite(context, Shape::legacy(budget, shape.indent + param.len()))?; + def.rewrite_result(context, Shape::legacy(budget, shape.indent + param.len()))?; param.push_str(&rewrite); } @@ -661,7 +679,8 @@ impl Rewrite for ast::GenericParam { mk_sp(last_attr.span.hi(), param_start), shape, !last_attr.is_doc_comment(), - )?; + ) + .unknown_error()?; } else { // When rewriting generic params, an extra newline should be put // if the attributes end with a doc comment @@ -673,7 +692,7 @@ impl Rewrite for ast::GenericParam { result.push_str(¶m); } - Some(result) + Ok(result) } } @@ -913,7 +932,7 @@ impl Rewrite for ast::Ty { let rw = if context.config.version() == Version::One { it.rewrite_result(context, shape) } else { - join_bounds(context, shape, it, false).unknown_error() + join_bounds(context, shape, it, false) }; rw.map(|it_str| { let space = if it_str.is_empty() { "" } else { " " }; @@ -1013,7 +1032,7 @@ fn join_bounds( shape: Shape, items: &[ast::GenericBound], need_indent: bool, -) -> Option { +) -> RewriteResult { join_bounds_inner(context, shape, items, need_indent, false) } @@ -1023,7 +1042,7 @@ fn join_bounds_inner( items: &[ast::GenericBound], need_indent: bool, force_newline: bool, -) -> Option { +) -> RewriteResult { debug_assert!(!items.is_empty()); let generic_bounds_in_order = is_generic_bounds_in_order(items); @@ -1116,16 +1135,17 @@ fn join_bounds_inner( }; let (extendable, trailing_str) = if i == 0 { - let bound_str = item.rewrite(context, shape)?; + let bound_str = item.rewrite_result(context, shape)?; (is_bound_extendable(&bound_str, item), bound_str) } else { - let bound_str = &item.rewrite(context, shape)?; + let bound_str = &item.rewrite_result(context, shape)?; match leading_span { Some(ls) if has_leading_comment => ( is_bound_extendable(bound_str, item), combine_strs_with_missing_comments( context, joiner, bound_str, ls, shape, true, - )?, + ) + .unknown_error()?, ), _ => ( is_bound_extendable(bound_str, item), @@ -1142,8 +1162,9 @@ fn join_bounds_inner( shape, true, ) + .unknown_error() .map(|v| (v, trailing_span, extendable)), - _ => Some((strs + &trailing_str, trailing_span, extendable)), + _ => Ok((strs + &trailing_str, trailing_span, extendable)), } }, )?; @@ -1168,7 +1189,7 @@ fn join_bounds_inner( if retry_with_force_newline { join_bounds_inner(context, shape, items, need_indent, true) } else { - Some(result.0) + Ok(result.0) } }