Skip to content

Commit

Permalink
modify rewrite_path and impl rewrite_result for related AST nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
ding-young authored and ytmimi committed Jul 18, 2024
1 parent 7cdb426 commit 3cbc91e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 34 deletions.
8 changes: 5 additions & 3 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,11 @@ impl Rewrite for ast::MetaItem {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
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,
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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} {{}}")),
Expand Down
7 changes: 4 additions & 3 deletions src/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -315,7 +316,7 @@ fn rewrite_struct_pat(
) -> Option<String> {
// 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} {{}}"));
Expand Down
78 changes: 52 additions & 26 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub(crate) fn rewrite_path(
qself: &Option<ptr::P<ast::QSelf>>,
path: &ast::Path,
shape: Shape,
) -> Option<String> {
) -> 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
Expand All @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -102,7 +102,7 @@ fn rewrite_path_segments<'a, I>(
span_hi: BytePos,
context: &RewriteContext<'_>,
shape: Shape,
) -> Option<String>
) -> RewriteResult
where
I: Iterator<Item = &'a ast::PathSegment>,
{
Expand All @@ -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,
Expand All @@ -134,7 +136,7 @@ where
buffer.push_str(&segment_string);
}

Some(buffer)
Ok(buffer)
}

#[derive(Debug)]
Expand Down Expand Up @@ -183,8 +185,12 @@ impl<'a> Rewrite for SegmentParam<'a> {

impl Rewrite for ast::PreciseCapturingArg {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
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)
}
Expand All @@ -194,13 +200,20 @@ impl Rewrite for ast::PreciseCapturingArg {

impl Rewrite for ast::AssocItemConstraint {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
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);
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -255,16 +271,17 @@ fn rewrite_segment(
span_hi: BytePos,
context: &RewriteContext<'_>,
shape: Shape,
) -> Option<String> {
) -> 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))?;
Expand Down Expand Up @@ -295,7 +312,7 @@ fn rewrite_segment(
result.push_str(&generics_str)
}

Some(result)
Ok(result)
}

fn format_function_type<'a, I>(
Expand Down Expand Up @@ -498,7 +515,7 @@ fn rewrite_generic_args(
context: &RewriteContext<'_>,
shape: Shape,
span: Span,
) -> Option<String> {
) -> RewriteResult {
match gen_args {
ast::GenericArgs::AngleBracketed(ref data) if !data.args.is_empty() => {
let args = data
Expand All @@ -515,6 +532,7 @@ fn rewrite_generic_args(
.collect::<Vec<_>>();

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),
Expand All @@ -523,9 +541,8 @@ fn rewrite_generic_args(
data.span,
context,
shape,
)
.ok(),
_ => Some("".to_owned()),
),
_ => Ok("".to_owned()),
}
}

Expand Down Expand Up @@ -717,23 +734,32 @@ impl Rewrite for ast::GenericParam {

impl Rewrite for ast::PolyTraitRef {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
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<String> {
self.rewrite_result(context, shape).ok()
}

fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
rewrite_path(context, PathContext::Type, &None, &self.path, shape)
}
}
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 3cbc91e

Please sign in to comment.