From 3cbc91e9d6e8342e13d780966bc896911ae535b4 Mon Sep 17 00:00:00 2001 From: ding-young Date: Wed, 10 Jul 2024 16:27:27 +0900 Subject: [PATCH] modify rewrite_path and impl rewrite_result for related AST nodes --- src/attr.rs | 8 +++-- src/expr.rs | 4 +-- src/patterns.rs | 7 +++-- src/types.rs | 78 ++++++++++++++++++++++++++++++++----------------- 4 files changed, 63 insertions(+), 34 deletions(-) diff --git a/src/attr.rs b/src/attr.rs index 433b9256202..a263e253dd9 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -276,10 +276,11 @@ impl Rewrite for ast::MetaItem { fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option { Some(match self.kind { ast::MetaItemKind::Word => { - rewrite_path(context, PathContext::Type, &None, &self.path, shape)? + rewrite_path(context, PathContext::Type, &None, &self.path, shape).ok()? } ast::MetaItemKind::List(ref list) => { - let path = rewrite_path(context, PathContext::Type, &None, &self.path, shape)?; + let path = + rewrite_path(context, PathContext::Type, &None, &self.path, shape).ok()?; let has_trailing_comma = crate::expr::span_ends_with_comma(context, self.span); overflow::rewrite_with_parens( context, @@ -297,7 +298,8 @@ impl Rewrite for ast::MetaItem { )? } ast::MetaItemKind::NameValue(ref lit) => { - let path = rewrite_path(context, PathContext::Type, &None, &self.path, shape)?; + let path = + rewrite_path(context, PathContext::Type, &None, &self.path, shape).ok()?; // 3 = ` = ` let lit_shape = shape.shrink_left(path.len() + 3)?; // `rewrite_literal` returns `None` when `lit` exceeds max diff --git a/src/expr.rs b/src/expr.rs index 629d3c4f0eb..e71d923baf4 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -185,7 +185,7 @@ pub(crate) fn format_expr( rewrite_match(context, cond, arms, shape, expr.span, &expr.attrs, kind) } ast::ExprKind::Path(ref qself, ref path) => { - rewrite_path(context, PathContext::Expr, qself, path, shape) + rewrite_path(context, PathContext::Expr, qself, path, shape).ok() } ast::ExprKind::Assign(ref lhs, ref rhs, _) => { rewrite_assignment(context, lhs, rhs, None, shape) @@ -1614,7 +1614,7 @@ fn rewrite_struct_lit<'a>( // 2 = " {".len() let path_shape = shape.sub_width(2)?; - let path_str = rewrite_path(context, PathContext::Expr, qself, path, path_shape)?; + let path_str = rewrite_path(context, PathContext::Expr, qself, path, path_shape).ok()?; let has_base_or_rest = match struct_rest { ast::StructRest::None if fields.is_empty() => return Some(format!("{path_str} {{}}")), diff --git a/src/patterns.rs b/src/patterns.rs index d8cb26a20f1..5f2047306ca 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -254,10 +254,11 @@ impl Rewrite for Pat { } PatKind::Tuple(ref items) => rewrite_tuple_pat(items, None, self.span, context, shape), PatKind::Path(ref q_self, ref path) => { - rewrite_path(context, PathContext::Expr, q_self, path, shape) + rewrite_path(context, PathContext::Expr, q_self, path, shape).ok() } PatKind::TupleStruct(ref q_self, ref path, ref pat_vec) => { - let path_str = rewrite_path(context, PathContext::Expr, q_self, path, shape)?; + let path_str = + rewrite_path(context, PathContext::Expr, q_self, path, shape).ok()?; rewrite_tuple_pat(pat_vec, Some(path_str), self.span, context, shape) } PatKind::Lit(ref expr) => expr.rewrite(context, shape), @@ -315,7 +316,7 @@ fn rewrite_struct_pat( ) -> Option { // 2 = ` {` let path_shape = shape.sub_width(2)?; - let path_str = rewrite_path(context, PathContext::Expr, qself, path, path_shape)?; + let path_str = rewrite_path(context, PathContext::Expr, qself, path, path_shape).ok()?; if fields.is_empty() && !ellipsis { return Some(format!("{path_str} {{}}")); diff --git a/src/types.rs b/src/types.rs index 942e31f3c92..c15ee2f3c5b 100644 --- a/src/types.rs +++ b/src/types.rs @@ -40,7 +40,7 @@ pub(crate) fn rewrite_path( qself: &Option>, path: &ast::Path, shape: Shape, -) -> Option { +) -> RewriteResult { let skip_count = qself.as_ref().map_or(0, |x| x.position); // 32 covers almost all path lengths measured when compiling core, and there isn't a big @@ -56,7 +56,7 @@ pub(crate) fn rewrite_path( if let Some(qself) = qself { result.push('<'); - let fmt_ty = qself.ty.rewrite(context, shape)?; + let fmt_ty = qself.ty.rewrite_result(context, shape)?; result.push_str(&fmt_ty); if skip_count > 0 { @@ -66,7 +66,7 @@ pub(crate) fn rewrite_path( } // 3 = ">::".len() - let shape = shape.sub_width(3)?; + let shape = shape.sub_width(3).max_width_error(shape.width, path.span)?; result = rewrite_path_segments( PathContext::Type, @@ -102,7 +102,7 @@ fn rewrite_path_segments<'a, I>( span_hi: BytePos, context: &RewriteContext<'_>, shape: Shape, -) -> Option +) -> RewriteResult where I: Iterator, { @@ -121,7 +121,9 @@ where } let extra_offset = extra_offset(&buffer, shape); - let new_shape = shape.shrink_left(extra_offset)?; + let new_shape = shape + .shrink_left(extra_offset) + .max_width_error(shape.width, mk_sp(span_lo, span_hi))?; let segment_string = rewrite_segment( path_context, segment, @@ -134,7 +136,7 @@ where buffer.push_str(&segment_string); } - Some(buffer) + Ok(buffer) } #[derive(Debug)] @@ -183,8 +185,12 @@ impl<'a> Rewrite for SegmentParam<'a> { impl Rewrite for ast::PreciseCapturingArg { fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option { + self.rewrite_result(context, shape).ok() + } + + fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult { match self { - ast::PreciseCapturingArg::Lifetime(lt) => lt.rewrite(context, shape), + ast::PreciseCapturingArg::Lifetime(lt) => lt.rewrite_result(context, shape), ast::PreciseCapturingArg::Arg(p, _) => { rewrite_path(context, PathContext::Type, &None, p, shape) } @@ -194,13 +200,20 @@ impl Rewrite for ast::PreciseCapturingArg { impl Rewrite for ast::AssocItemConstraint { fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option { + self.rewrite_result(context, shape).ok() + } + + fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult { use ast::AssocItemConstraintKind::{Bound, Equality}; let mut result = String::with_capacity(128); result.push_str(rewrite_ident(context, self.ident)); if let Some(ref gen_args) = self.gen_args { - let budget = shape.width.checked_sub(result.len())?; + let budget = shape + .width + .checked_sub(result.len()) + .max_width_error(shape.width, self.span)?; let shape = Shape::legacy(budget, shape.indent + result.len()); let gen_str = rewrite_generic_args(gen_args, context, shape, gen_args.span())?; result.push_str(&gen_str); @@ -213,12 +226,15 @@ impl Rewrite for ast::AssocItemConstraint { }; result.push_str(infix); - let budget = shape.width.checked_sub(result.len())?; + let budget = shape + .width + .checked_sub(result.len()) + .max_width_error(shape.width, self.span)?; let shape = Shape::legacy(budget, shape.indent + result.len()); - let rewrite = self.kind.rewrite(context, shape)?; + let rewrite = self.kind.rewrite_result(context, shape)?; result.push_str(&rewrite); - Some(result) + Ok(result) } } @@ -255,16 +271,17 @@ fn rewrite_segment( span_hi: BytePos, context: &RewriteContext<'_>, shape: Shape, -) -> Option { +) -> RewriteResult { let mut result = String::with_capacity(128); result.push_str(rewrite_ident(context, segment.ident)); let ident_len = result.len(); let shape = if context.use_block_indent() { - shape.offset_left(ident_len)? + shape.offset_left(ident_len) } else { - shape.shrink_left(ident_len)? - }; + shape.shrink_left(ident_len) + } + .max_width_error(shape.width, mk_sp(*span_lo, span_hi))?; if let Some(ref args) = segment.args { let generics_str = rewrite_generic_args(args, context, shape, mk_sp(*span_lo, span_hi))?; @@ -295,7 +312,7 @@ fn rewrite_segment( result.push_str(&generics_str) } - Some(result) + Ok(result) } fn format_function_type<'a, I>( @@ -498,7 +515,7 @@ fn rewrite_generic_args( context: &RewriteContext<'_>, shape: Shape, span: Span, -) -> Option { +) -> RewriteResult { match gen_args { ast::GenericArgs::AngleBracketed(ref data) if !data.args.is_empty() => { let args = data @@ -515,6 +532,7 @@ fn rewrite_generic_args( .collect::>(); overflow::rewrite_with_angle_brackets(context, "", args.iter(), shape, span) + .unknown_error() } ast::GenericArgs::Parenthesized(ref data) => format_function_type( data.inputs.iter().map(|x| &**x), @@ -523,9 +541,8 @@ fn rewrite_generic_args( data.span, context, shape, - ) - .ok(), - _ => Some("".to_owned()), + ), + _ => Ok("".to_owned()), } } @@ -717,23 +734,32 @@ impl Rewrite for ast::GenericParam { impl Rewrite for ast::PolyTraitRef { fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option { + self.rewrite_result(context, shape).ok() + } + + fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult { if let Some(lifetime_str) = rewrite_bound_params(context, shape, &self.bound_generic_params) { // 6 is "for<> ".len() let extra_offset = lifetime_str.len() + 6; - let path_str = self - .trait_ref - .rewrite(context, shape.offset_left(extra_offset)?)?; + let shape = shape + .offset_left(extra_offset) + .max_width_error(shape.width, self.span)?; + let path_str = self.trait_ref.rewrite_result(context, shape)?; - Some(format!("for<{lifetime_str}> {path_str}")) + Ok(format!("for<{lifetime_str}> {path_str}")) } else { - self.trait_ref.rewrite(context, shape) + self.trait_ref.rewrite_result(context, shape) } } } impl Rewrite for ast::TraitRef { fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option { + self.rewrite_result(context, shape).ok() + } + + fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult { rewrite_path(context, PathContext::Type, &None, &self.path, shape) } } @@ -912,7 +938,7 @@ impl Rewrite for ast::Ty { ast::TyKind::AnonStruct(..) => Ok(context.snippet(self.span).to_owned()), ast::TyKind::AnonUnion(..) => Ok(context.snippet(self.span).to_owned()), ast::TyKind::Path(ref q_self, ref path) => { - rewrite_path(context, PathContext::Type, q_self, path, shape).unknown_error() + rewrite_path(context, PathContext::Type, q_self, path, shape) } ast::TyKind::Array(ref ty, ref repeats) => rewrite_pair( &**ty,