Box ExprKind::{Closure,MethodCall}, and QSelf in expressions, types, and patterns.

This commit is contained in:
Nicholas Nethercote 2022-09-08 10:52:51 +10:00
parent 826fb78beb
commit 4a4addc598
7 changed files with 49 additions and 44 deletions

@ -290,10 +290,10 @@ 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)?
}
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)?;
let has_trailing_comma = crate::expr::span_ends_with_comma(context, self.span);
overflow::rewrite_with_parens(
context,
@ -311,7 +311,7 @@ impl Rewrite for ast::MetaItem {
)?
}
ast::MetaItemKind::NameValue(ref literal) => {
let path = rewrite_path(context, PathContext::Type, None, &self.path, shape)?;
let path = rewrite_path(context, PathContext::Type, &None, &self.path, shape)?;
// 3 = ` = `
let lit_shape = shape.shrink_left(path.len() + 3)?;
// `rewrite_literal` returns `None` when `literal` exceeds max

@ -145,8 +145,8 @@ impl ChainItemKind {
fn from_ast(context: &RewriteContext<'_>, expr: &ast::Expr) -> (ChainItemKind, Span) {
let (kind, span) = match expr.kind {
ast::ExprKind::MethodCall(ref segment, ref receiver, ref expressions, _) => {
let types = if let Some(ref generic_args) = segment.args {
ast::ExprKind::MethodCall(ref call) => {
let types = if let Some(ref generic_args) = call.seg.args {
if let ast::GenericArgs::AngleBracketed(ref data) = **generic_args {
data.args
.iter()
@ -163,8 +163,8 @@ impl ChainItemKind {
} else {
vec![]
};
let span = mk_sp(receiver.span.hi(), expr.span.hi());
let kind = ChainItemKind::MethodCall(segment.clone(), types, expressions.clone());
let span = mk_sp(call.receiver.span.hi(), expr.span.hi());
let kind = ChainItemKind::MethodCall(call.seg.clone(), types, call.args.clone());
(kind, span)
}
ast::ExprKind::Field(ref nested, field) => {
@ -400,9 +400,7 @@ impl Chain {
// is a try! macro, we'll convert it to shorthand when the option is set.
fn pop_expr_chain(expr: &ast::Expr, context: &RewriteContext<'_>) -> Option<ast::Expr> {
match expr.kind {
ast::ExprKind::MethodCall(_, ref receiver, _, _) => {
Some(Self::convert_try(&receiver, context))
}
ast::ExprKind::MethodCall(ref call) => Some(Self::convert_try(&call.receiver, context)),
ast::ExprKind::Field(ref subexpr, _)
| ast::ExprKind::Try(ref subexpr)
| ast::ExprKind::Await(ref subexpr) => Some(Self::convert_try(subexpr, context)),

@ -326,16 +326,16 @@ pub(crate) fn rewrite_last_closure(
expr: &ast::Expr,
shape: Shape,
) -> Option<String> {
if let ast::ExprKind::Closure(
ref binder,
capture,
ref is_async,
movability,
ref fn_decl,
ref body,
_,
) = expr.kind
{
if let ast::ExprKind::Closure(ref closure) = expr.kind {
let ast::Closure {
ref binder,
capture_clause,
ref asyncness,
movability,
ref fn_decl,
ref body,
fn_decl_span: _,
} = **closure;
let body = match body.kind {
ast::ExprKind::Block(ref block, _)
if !is_unsafe_block(block)
@ -347,7 +347,15 @@ pub(crate) fn rewrite_last_closure(
_ => body,
};
let (prefix, extra_offset) = rewrite_closure_fn_decl(
binder, capture, is_async, movability, fn_decl, body, expr.span, context, shape,
binder,
capture_clause,
asyncness,
movability,
fn_decl,
body,
expr.span,
context,
shape,
)?;
// If the closure goes multi line before its body, do not overflow the closure.
if prefix.contains('\n') {

@ -116,7 +116,7 @@ pub(crate) fn format_expr(
rewrite_struct_lit(
context,
path,
qself.as_ref(),
qself,
fields,
rest,
&expr.attrs,
@ -169,7 +169,7 @@ pub(crate) fn format_expr(
rewrite_match(context, cond, arms, shape, expr.span, &expr.attrs)
}
ast::ExprKind::Path(ref qself, ref path) => {
rewrite_path(context, PathContext::Expr, qself.as_ref(), path, shape)
rewrite_path(context, PathContext::Expr, qself, path, shape)
}
ast::ExprKind::Assign(ref lhs, ref rhs, _) => {
rewrite_assignment(context, lhs, rhs, None, shape)
@ -203,16 +203,16 @@ pub(crate) fn format_expr(
Some("yield".to_string())
}
}
ast::ExprKind::Closure(
ref binder,
capture,
ref is_async,
movability,
ref fn_decl,
ref body,
_,
) => closures::rewrite_closure(
binder, capture, is_async, movability, fn_decl, body, expr.span, context, shape,
ast::ExprKind::Closure(ref cl) => closures::rewrite_closure(
&cl.binder,
cl.capture_clause,
&cl.asyncness,
cl.movability,
&cl.fn_decl,
&cl.body,
expr.span,
context,
shape,
),
ast::ExprKind::Try(..)
| ast::ExprKind::Field(..)
@ -1537,7 +1537,7 @@ fn struct_lit_can_be_aligned(fields: &[ast::ExprField], has_base: bool) -> bool
fn rewrite_struct_lit<'a>(
context: &RewriteContext<'_>,
path: &ast::Path,
qself: Option<&ast::QSelf>,
qself: &Option<ptr::P<ast::QSelf>>,
fields: &'a [ast::ExprField],
struct_rest: &ast::StructRest,
attrs: &[ast::Attribute],

@ -227,11 +227,10 @@ 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.as_ref(), path, shape)
rewrite_path(context, PathContext::Expr, q_self, path, shape)
}
PatKind::TupleStruct(ref q_self, ref path, ref pat_vec) => {
let path_str =
rewrite_path(context, PathContext::Expr, q_self.as_ref(), path, shape)?;
let path_str = rewrite_path(context, PathContext::Expr, q_self, path, shape)?;
rewrite_tuple_pat(pat_vec, Some(path_str), self.span, context, shape)
}
PatKind::Lit(ref expr) => expr.rewrite(context, shape),
@ -271,7 +270,7 @@ impl Rewrite for Pat {
}
fn rewrite_struct_pat(
qself: &Option<ast::QSelf>,
qself: &Option<ptr::P<ast::QSelf>>,
path: &ast::Path,
fields: &[ast::PatField],
ellipsis: bool,
@ -281,7 +280,7 @@ fn rewrite_struct_pat(
) -> Option<String> {
// 2 = ` {`
let path_shape = shape.sub_width(2)?;
let path_str = rewrite_path(context, PathContext::Expr, qself.as_ref(), path, path_shape)?;
let path_str = rewrite_path(context, PathContext::Expr, qself, path, path_shape)?;
if fields.is_empty() && !ellipsis {
return Some(format!("{} {{}}", path_str));

@ -38,11 +38,11 @@ pub(crate) enum PathContext {
pub(crate) fn rewrite_path(
context: &RewriteContext<'_>,
path_context: PathContext,
qself: Option<&ast::QSelf>,
qself: &Option<ptr::P<ast::QSelf>>,
path: &ast::Path,
shape: Shape,
) -> Option<String> {
let skip_count = qself.map_or(0, |x| x.position);
let skip_count = qself.as_ref().map_or(0, |x| x.position);
let mut result = if path.is_global() && qself.is_none() && path_context != PathContext::Import {
"::".to_owned()
@ -655,7 +655,7 @@ impl Rewrite for ast::PolyTraitRef {
impl Rewrite for ast::TraitRef {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
rewrite_path(context, PathContext::Type, None, &self.path, shape)
rewrite_path(context, PathContext::Type, &None, &self.path, shape)
}
}
@ -800,7 +800,7 @@ impl Rewrite for ast::Ty {
rewrite_tuple(context, items.iter(), self.span, shape, items.len() == 1)
}
ast::TyKind::Path(ref q_self, ref path) => {
rewrite_path(context, PathContext::Type, q_self.as_ref(), path, shape)
rewrite_path(context, PathContext::Type, q_self, path, shape)
}
ast::TyKind::Array(ref ty, ref repeats) => rewrite_pair(
&**ty,

@ -479,9 +479,9 @@ pub(crate) fn is_block_expr(context: &RewriteContext<'_>, expr: &ast::Expr, repr
| ast::ExprKind::Binary(_, _, ref expr)
| ast::ExprKind::Index(_, ref expr)
| ast::ExprKind::Unary(_, ref expr)
| ast::ExprKind::Closure(_, _, _, _, _, ref expr, _)
| ast::ExprKind::Try(ref expr)
| ast::ExprKind::Yield(Some(ref expr)) => is_block_expr(context, expr, repr),
ast::ExprKind::Closure(ref closure) => is_block_expr(context, &closure.body, repr),
// This can only be a string lit
ast::ExprKind::Lit(_) => {
repr.contains('\n') && trimmed_last_line_width(repr) <= context.config.tab_spaces()