Skip to content

Commit 0fc846f

Browse files
refactor: maintain more AST info when formatting a RHS
1 parent a21f1b6 commit 0fc846f

File tree

4 files changed

+86
-19
lines changed

4 files changed

+86
-19
lines changed

src/expr.rs

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,10 @@ pub(crate) fn format_expr(
196196
capture, is_async, movability, fn_decl, body, expr.span, context, shape,
197197
)
198198
}
199-
ast::ExprKind::Try(..) | ast::ExprKind::Field(..) | ast::ExprKind::MethodCall(..) => {
200-
rewrite_chain(expr, context, shape)
201-
}
199+
ast::ExprKind::Try(..)
200+
| ast::ExprKind::Field(..)
201+
| ast::ExprKind::MethodCall(..)
202+
| ast::ExprKind::Await(_) => rewrite_chain(expr, context, shape),
202203
ast::ExprKind::MacCall(ref mac) => {
203204
rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|| {
204205
wrap_str(
@@ -377,7 +378,6 @@ pub(crate) fn format_expr(
377378
))
378379
}
379380
}
380-
ast::ExprKind::Await(_) => rewrite_chain(expr, context, shape),
381381
ast::ExprKind::Underscore => Some("_".to_owned()),
382382
ast::ExprKind::Err => None,
383383
};
@@ -829,6 +829,7 @@ impl<'a> ControlFlow<'a> {
829829
&format!("{}{}{}", matcher, pat_string, self.connector),
830830
expr,
831831
cond_shape,
832+
&RhsAssignKind::Expr(&expr.kind, expr.span),
832833
RhsTactics::Default,
833834
comments_span,
834835
true,
@@ -1839,6 +1840,34 @@ fn rewrite_unary_op(
18391840
rewrite_unary_prefix(context, ast::UnOp::to_string(op), expr, shape)
18401841
}
18411842

1843+
pub(crate) enum RhsAssignKind<'ast> {
1844+
Expr(&'ast ast::ExprKind, Span),
1845+
Bounds,
1846+
Ty,
1847+
}
1848+
1849+
impl<'ast> RhsAssignKind<'ast> {
1850+
// TODO(calebcartwright)
1851+
// Preemptive addition for handling RHS with chains, not yet utilized.
1852+
// It may make more sense to construct the chain first and then check
1853+
// whether there are actually chain elements.
1854+
#[allow(dead_code)]
1855+
fn is_chain(&self) -> bool {
1856+
match self {
1857+
RhsAssignKind::Expr(kind, _) => {
1858+
matches!(
1859+
kind,
1860+
ast::ExprKind::Try(..)
1861+
| ast::ExprKind::Field(..)
1862+
| ast::ExprKind::MethodCall(..)
1863+
| ast::ExprKind::Await(_)
1864+
)
1865+
}
1866+
_ => false,
1867+
}
1868+
}
1869+
}
1870+
18421871
fn rewrite_assignment(
18431872
context: &RewriteContext<'_>,
18441873
lhs: &ast::Expr,
@@ -1855,7 +1884,13 @@ fn rewrite_assignment(
18551884
let lhs_shape = shape.sub_width(operator_str.len() + 1)?;
18561885
let lhs_str = format!("{} {}", lhs.rewrite(context, lhs_shape)?, operator_str);
18571886

1858-
rewrite_assign_rhs(context, lhs_str, rhs, shape)
1887+
rewrite_assign_rhs(
1888+
context,
1889+
lhs_str,
1890+
rhs,
1891+
&RhsAssignKind::Expr(&rhs.kind, rhs.span),
1892+
shape,
1893+
)
18591894
}
18601895

18611896
/// Controls where to put the rhs.
@@ -1876,16 +1911,18 @@ pub(crate) fn rewrite_assign_rhs<S: Into<String>, R: Rewrite>(
18761911
context: &RewriteContext<'_>,
18771912
lhs: S,
18781913
ex: &R,
1914+
rhs_kind: &RhsAssignKind<'_>,
18791915
shape: Shape,
18801916
) -> Option<String> {
1881-
rewrite_assign_rhs_with(context, lhs, ex, shape, RhsTactics::Default)
1917+
rewrite_assign_rhs_with(context, lhs, ex, shape, rhs_kind, RhsTactics::Default)
18821918
}
18831919

18841920
pub(crate) fn rewrite_assign_rhs_expr<R: Rewrite>(
18851921
context: &RewriteContext<'_>,
18861922
lhs: &str,
18871923
ex: &R,
18881924
shape: Shape,
1925+
rhs_kind: &RhsAssignKind<'_>,
18891926
rhs_tactics: RhsTactics,
18901927
) -> Option<String> {
18911928
let last_line_width = last_line_width(lhs).saturating_sub(if lhs.contains('\n') {
@@ -1910,6 +1947,7 @@ pub(crate) fn rewrite_assign_rhs_expr<R: Rewrite>(
19101947
ex,
19111948
orig_shape,
19121949
ex.rewrite(context, orig_shape),
1950+
rhs_kind,
19131951
rhs_tactics,
19141952
has_rhs_comment,
19151953
)
@@ -1920,10 +1958,11 @@ pub(crate) fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
19201958
lhs: S,
19211959
ex: &R,
19221960
shape: Shape,
1961+
rhs_kind: &RhsAssignKind<'_>,
19231962
rhs_tactics: RhsTactics,
19241963
) -> Option<String> {
19251964
let lhs = lhs.into();
1926-
let rhs = rewrite_assign_rhs_expr(context, &lhs, ex, shape, rhs_tactics)?;
1965+
let rhs = rewrite_assign_rhs_expr(context, &lhs, ex, shape, rhs_kind, rhs_tactics)?;
19271966
Some(lhs + &rhs)
19281967
}
19291968

@@ -1932,6 +1971,7 @@ pub(crate) fn rewrite_assign_rhs_with_comments<S: Into<String>, R: Rewrite>(
19321971
lhs: S,
19331972
ex: &R,
19341973
shape: Shape,
1974+
rhs_kind: &RhsAssignKind<'_>,
19351975
rhs_tactics: RhsTactics,
19361976
between_span: Span,
19371977
allow_extend: bool,
@@ -1943,7 +1983,7 @@ pub(crate) fn rewrite_assign_rhs_with_comments<S: Into<String>, R: Rewrite>(
19431983
} else {
19441984
shape
19451985
};
1946-
let rhs = rewrite_assign_rhs_expr(context, &lhs, ex, shape, rhs_tactics)?;
1986+
let rhs = rewrite_assign_rhs_expr(context, &lhs, ex, shape, rhs_kind, rhs_tactics)?;
19471987

19481988
if contains_comment {
19491989
let rhs = rhs.trim_start();
@@ -1958,6 +1998,7 @@ fn choose_rhs<R: Rewrite>(
19581998
expr: &R,
19591999
shape: Shape,
19602000
orig_rhs: Option<String>,
2001+
_rhs_kind: &RhsAssignKind<'_>,
19612002
rhs_tactics: RhsTactics,
19622003
has_rhs_comment: bool,
19632004
) -> Option<String> {

src/items.rs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::config::lists::*;
1818
use crate::config::{BraceStyle, Config, IndentStyle, Version};
1919
use crate::expr::{
2020
is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_with,
21-
rewrite_assign_rhs_with_comments, RhsTactics,
21+
rewrite_assign_rhs_with_comments, RhsAssignKind, RhsTactics,
2222
};
2323
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
2424
use crate::macros::{rewrite_macro, MacroPosition};
@@ -116,7 +116,13 @@ impl Rewrite for ast::Local {
116116
// 1 = trailing semicolon;
117117
let nested_shape = shape.sub_width(1)?;
118118

119-
result = rewrite_assign_rhs(context, result, init, nested_shape)?;
119+
result = rewrite_assign_rhs(
120+
context,
121+
result,
122+
init,
123+
&RhsAssignKind::Expr(&init.kind, init.span),
124+
nested_shape,
125+
)?;
120126
// todo else
121127
}
122128

@@ -564,11 +570,13 @@ impl<'a> FmtVisitor<'a> {
564570

565571
let variant_body = if let Some(ref expr) = field.disr_expr {
566572
let lhs = format!("{:1$} =", variant_body, pad_discrim_ident_to);
573+
let ex = &*expr.value;
567574
rewrite_assign_rhs_with(
568575
&context,
569576
lhs,
570-
&*expr.value,
577+
ex,
571578
shape,
579+
&RhsAssignKind::Expr(&ex.kind, ex.span),
572580
RhsTactics::AllowOverflow,
573581
)?
574582
} else {
@@ -1033,6 +1041,7 @@ pub(crate) fn format_trait(
10331041
result + ":",
10341042
bounds,
10351043
shape,
1044+
&RhsAssignKind::Bounds,
10361045
RhsTactics::ForceNextLineWithoutIndent,
10371046
)?;
10381047
}
@@ -1213,7 +1222,14 @@ pub(crate) fn format_trait_alias(
12131222
generic_bounds,
12141223
generics,
12151224
};
1216-
rewrite_assign_rhs(context, lhs, &trait_alias_bounds, shape.sub_width(1)?).map(|s| s + ";")
1225+
rewrite_assign_rhs(
1226+
context,
1227+
lhs,
1228+
&trait_alias_bounds,
1229+
&RhsAssignKind::Bounds,
1230+
shape.sub_width(1)?,
1231+
)
1232+
.map(|s| s + ";")
12171233
}
12181234

12191235
fn format_unit_struct(
@@ -1630,7 +1646,7 @@ fn rewrite_ty<R: Rewrite>(
16301646

16311647
// 1 = `;`
16321648
let shape = Shape::indented(indent, context.config).sub_width(1)?;
1633-
rewrite_assign_rhs(context, lhs, &*ty, shape).map(|s| s + ";")
1649+
rewrite_assign_rhs(context, lhs, &*ty, &RhsAssignKind::Ty, shape).map(|s| s + ";")
16341650
} else {
16351651
Some(format!("{};", result))
16361652
}
@@ -1720,7 +1736,7 @@ pub(crate) fn rewrite_struct_field(
17201736

17211737
let is_prefix_empty = prefix.is_empty();
17221738
// We must use multiline. We are going to put attributes and a field on different lines.
1723-
let field_str = rewrite_assign_rhs(context, prefix, &*field.ty, shape)?;
1739+
let field_str = rewrite_assign_rhs(context, prefix, &*field.ty, &RhsAssignKind::Ty, shape)?;
17241740
// Remove a leading white-space from `rewrite_assign_rhs()` when rewriting a tuple struct.
17251741
let field_str = if is_prefix_empty {
17261742
field_str.trim_start()
@@ -1850,6 +1866,7 @@ fn rewrite_static(
18501866
&lhs,
18511867
&**expr,
18521868
Shape::legacy(remaining_width, offset.block_only()),
1869+
&RhsAssignKind::Expr(&expr.kind, expr.span),
18531870
RhsTactics::Default,
18541871
comments_span,
18551872
true,
@@ -3147,7 +3164,14 @@ impl Rewrite for ast::ForeignItem {
31473164
rewrite_ident(context, self.ident)
31483165
);
31493166
// 1 = ;
3150-
rewrite_assign_rhs(context, prefix, &**ty, shape.sub_width(1)?).map(|s| s + ";")
3167+
rewrite_assign_rhs(
3168+
context,
3169+
prefix,
3170+
&**ty,
3171+
&RhsAssignKind::Ty,
3172+
shape.sub_width(1)?,
3173+
)
3174+
.map(|s| s + ";")
31513175
}
31523176
ast::ForeignItemKind::TyAlias(ref ty_alias) => {
31533177
let (kind, span) = (&ItemVisitorKind::ForeignItem(&self), self.span);

src/macros.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::comment::{
2727
contains_comment, CharClasses, FindUncommented, FullCodeCharKind, LineClasses,
2828
};
2929
use crate::config::lists::*;
30-
use crate::expr::rewrite_array;
30+
use crate::expr::{rewrite_array, rewrite_assign_rhs, RhsAssignKind};
3131
use crate::lists::{itemize_list, write_list, ListFormatting};
3232
use crate::overflow;
3333
use crate::rewrite::{Rewrite, RewriteContext};
@@ -1468,10 +1468,11 @@ fn format_lazy_static(
14681468
id,
14691469
ty.rewrite(context, nested_shape)?
14701470
));
1471-
result.push_str(&crate::expr::rewrite_assign_rhs(
1471+
result.push_str(&rewrite_assign_rhs(
14721472
context,
14731473
stmt,
14741474
&*expr,
1475+
&RhsAssignKind::Expr(&expr.kind, expr.span),
14751476
nested_shape.sub_width(1)?,
14761477
)?);
14771478
result.push(';');

src/types.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::config::lists::*;
1010
use crate::config::{IndentStyle, TypeDensity, Version};
1111
use crate::expr::{
1212
format_expr, rewrite_assign_rhs, rewrite_call, rewrite_tuple, rewrite_unary_prefix, ExprType,
13+
RhsAssignKind,
1314
};
1415
use crate::lists::{
1516
definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator,
@@ -430,7 +431,7 @@ impl Rewrite for ast::WherePredicate {
430431
format!("{}{}", type_str, colon)
431432
};
432433

433-
rewrite_assign_rhs(context, lhs, bounds, shape)?
434+
rewrite_assign_rhs(context, lhs, bounds, &RhsAssignKind::Bounds, shape)?
434435
}
435436
ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate {
436437
ref lifetime,
@@ -443,7 +444,7 @@ impl Rewrite for ast::WherePredicate {
443444
..
444445
}) => {
445446
let lhs_ty_str = lhs_ty.rewrite(context, shape).map(|lhs| lhs + " =")?;
446-
rewrite_assign_rhs(context, lhs_ty_str, &**rhs_ty, shape)?
447+
rewrite_assign_rhs(context, lhs_ty_str, &**rhs_ty, &RhsAssignKind::Ty, shape)?
447448
}
448449
};
449450

0 commit comments

Comments
 (0)