Skip to content

Commit c4a0e76

Browse files
committed
update rewrite_assignment to return RewriteResult
1 parent 46cb7d3 commit c4a0e76

File tree

4 files changed

+63
-51
lines changed

4 files changed

+63
-51
lines changed

src/expr.rs

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,10 @@ pub(crate) fn format_expr(
194194
rewrite_path(context, PathContext::Expr, qself, path, shape).ok()
195195
}
196196
ast::ExprKind::Assign(ref lhs, ref rhs, _) => {
197-
rewrite_assignment(context, lhs, rhs, None, shape)
197+
rewrite_assignment(context, lhs, rhs, None, shape).ok()
198198
}
199199
ast::ExprKind::AssignOp(ref op, ref lhs, ref rhs) => {
200-
rewrite_assignment(context, lhs, rhs, Some(op), shape)
200+
rewrite_assignment(context, lhs, rhs, Some(op), shape).ok()
201201
}
202202
ast::ExprKind::Continue(ref opt_label) => {
203203
let id_str = match *opt_label {
@@ -2050,15 +2050,21 @@ fn rewrite_assignment(
20502050
rhs: &ast::Expr,
20512051
op: Option<&ast::BinOp>,
20522052
shape: Shape,
2053-
) -> Option<String> {
2053+
) -> RewriteResult {
20542054
let operator_str = match op {
20552055
Some(op) => context.snippet(op.span),
20562056
None => "=",
20572057
};
20582058

20592059
// 1 = space between lhs and operator.
2060-
let lhs_shape = shape.sub_width(operator_str.len() + 1)?;
2061-
let lhs_str = format!("{} {}", lhs.rewrite(context, lhs_shape)?, operator_str);
2060+
let lhs_shape = shape
2061+
.sub_width(operator_str.len() + 1)
2062+
.max_width_error(shape.width, lhs.span())?;
2063+
let lhs_str = format!(
2064+
"{} {}",
2065+
lhs.rewrite_result(context, lhs_shape)?,
2066+
operator_str
2067+
);
20622068

20632069
rewrite_assign_rhs(
20642070
context,
@@ -2089,7 +2095,7 @@ pub(crate) fn rewrite_assign_rhs<S: Into<String>, R: Rewrite>(
20892095
ex: &R,
20902096
rhs_kind: &RhsAssignKind<'_>,
20912097
shape: Shape,
2092-
) -> Option<String> {
2098+
) -> RewriteResult {
20932099
rewrite_assign_rhs_with(context, lhs, ex, shape, rhs_kind, RhsTactics::Default)
20942100
}
20952101

@@ -2100,7 +2106,7 @@ pub(crate) fn rewrite_assign_rhs_expr<R: Rewrite>(
21002106
shape: Shape,
21012107
rhs_kind: &RhsAssignKind<'_>,
21022108
rhs_tactics: RhsTactics,
2103-
) -> Option<String> {
2109+
) -> RewriteResult {
21042110
let last_line_width = last_line_width(lhs).saturating_sub(if lhs.contains('\n') {
21052111
shape.indent.width()
21062112
} else {
@@ -2122,7 +2128,7 @@ pub(crate) fn rewrite_assign_rhs_expr<R: Rewrite>(
21222128
context,
21232129
ex,
21242130
orig_shape,
2125-
ex.rewrite(context, orig_shape),
2131+
ex.rewrite_result(context, orig_shape),
21262132
rhs_kind,
21272133
rhs_tactics,
21282134
has_rhs_comment,
@@ -2136,10 +2142,10 @@ pub(crate) fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
21362142
shape: Shape,
21372143
rhs_kind: &RhsAssignKind<'_>,
21382144
rhs_tactics: RhsTactics,
2139-
) -> Option<String> {
2145+
) -> RewriteResult {
21402146
let lhs = lhs.into();
21412147
let rhs = rewrite_assign_rhs_expr(context, &lhs, ex, shape, rhs_kind, rhs_tactics)?;
2142-
Some(lhs + &rhs)
2148+
Ok(lhs + &rhs)
21432149
}
21442150

21452151
pub(crate) fn rewrite_assign_rhs_with_comments<S: Into<String>, R: Rewrite + Spanned>(
@@ -2161,8 +2167,7 @@ pub(crate) fn rewrite_assign_rhs_with_comments<S: Into<String>, R: Rewrite + Spa
21612167
} else {
21622168
shape
21632169
};
2164-
let rhs =
2165-
rewrite_assign_rhs_expr(context, &lhs, ex, shape, rhs_kind, rhs_tactics).unknown_error()?;
2170+
let rhs = rewrite_assign_rhs_expr(context, &lhs, ex, shape, rhs_kind, rhs_tactics)?;
21662171
if contains_comment {
21672172
let rhs = rhs.trim_start();
21682173
combine_strs_with_missing_comments(context, &lhs, rhs, between_span, shape, allow_extend)
@@ -2175,48 +2180,53 @@ fn choose_rhs<R: Rewrite>(
21752180
context: &RewriteContext<'_>,
21762181
expr: &R,
21772182
shape: Shape,
2178-
orig_rhs: Option<String>,
2183+
orig_rhs: RewriteResult,
21792184
_rhs_kind: &RhsAssignKind<'_>,
21802185
rhs_tactics: RhsTactics,
21812186
has_rhs_comment: bool,
2182-
) -> Option<String> {
2187+
) -> RewriteResult {
21832188
match orig_rhs {
2184-
Some(ref new_str) if new_str.is_empty() => Some(String::new()),
2185-
Some(ref new_str)
2186-
if !new_str.contains('\n') && unicode_str_width(new_str) <= shape.width =>
2187-
{
2188-
Some(format!(" {new_str}"))
2189+
Ok(ref new_str) if new_str.is_empty() => Ok(String::new()),
2190+
Ok(ref new_str) if !new_str.contains('\n') && unicode_str_width(new_str) <= shape.width => {
2191+
Ok(format!(" {new_str}"))
21892192
}
21902193
_ => {
21912194
// Expression did not fit on the same line as the identifier.
21922195
// Try splitting the line and see if that works better.
2193-
let new_shape = shape_from_rhs_tactic(context, shape, rhs_tactics)?;
2194-
let new_rhs = expr.rewrite(context, new_shape);
2196+
let new_shape = shape_from_rhs_tactic(context, shape, rhs_tactics)
2197+
// TODO(ding-young) Ideally, we can replace unknown_error() with max_width_error(),
2198+
// but this requires either implementing the Spanned trait for ast::GenericBounds
2199+
// or grabbing the span from the call site.
2200+
.unknown_error()?;
2201+
let new_rhs = expr.rewrite_result(context, new_shape);
21952202
let new_indent_str = &shape
21962203
.indent
21972204
.block_indent(context.config)
21982205
.to_string_with_newline(context.config);
21992206
let before_space_str = if has_rhs_comment { "" } else { " " };
22002207

22012208
match (orig_rhs, new_rhs) {
2202-
(Some(ref orig_rhs), Some(ref new_rhs))
2209+
(Ok(ref orig_rhs), Ok(ref new_rhs))
22032210
if !filtered_str_fits(&new_rhs, context.config.max_width(), new_shape) =>
22042211
{
2205-
Some(format!("{before_space_str}{orig_rhs}"))
2212+
Ok(format!("{before_space_str}{orig_rhs}"))
22062213
}
2207-
(Some(ref orig_rhs), Some(ref new_rhs))
2214+
(Ok(ref orig_rhs), Ok(ref new_rhs))
22082215
if prefer_next_line(orig_rhs, new_rhs, rhs_tactics) =>
22092216
{
2210-
Some(format!("{new_indent_str}{new_rhs}"))
2217+
Ok(format!("{new_indent_str}{new_rhs}"))
22112218
}
2212-
(None, Some(ref new_rhs)) => Some(format!("{new_indent_str}{new_rhs}")),
2213-
(None, None) if rhs_tactics == RhsTactics::AllowOverflow => {
2219+
(Err(_), Ok(ref new_rhs)) => Ok(format!("{new_indent_str}{new_rhs}")),
2220+
(Err(_), Err(_)) if rhs_tactics == RhsTactics::AllowOverflow => {
22142221
let shape = shape.infinite_width();
2215-
expr.rewrite(context, shape)
2222+
expr.rewrite_result(context, shape)
22162223
.map(|s| format!("{}{}", before_space_str, s))
22172224
}
2218-
(None, None) => None,
2219-
(Some(orig_rhs), _) => Some(format!("{before_space_str}{orig_rhs}")),
2225+
// When both orig_rhs and new_rhs result in errors, we currently propagate
2226+
// the error from the second attempt since it is more generous with
2227+
// width constraints. This decision is somewhat arbitrary and is open to change.
2228+
(Err(_), Err(new_rhs_err)) => Err(new_rhs_err),
2229+
(Ok(orig_rhs), _) => Ok(format!("{before_space_str}{orig_rhs}")),
22202230
}
22212231
}
22222232
}

src/items.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ impl Rewrite for ast::Local {
137137
init,
138138
&RhsAssignKind::Expr(&init.kind, init.span),
139139
nested_shape,
140-
)
141-
.max_width_error(shape.width, self.span())?;
140+
)?;
142141

143142
if let Some(block) = else_block {
144143
let else_kw_span = init.span.between(block.span);
@@ -711,7 +710,8 @@ impl<'a> FmtVisitor<'a> {
711710
shape,
712711
&RhsAssignKind::Expr(&ex.kind, ex.span),
713712
RhsTactics::AllowOverflow,
714-
)?
713+
)
714+
.ok()?
715715
} else {
716716
variant_body
717717
};
@@ -1206,7 +1206,8 @@ pub(crate) fn format_trait(
12061206
shape,
12071207
&RhsAssignKind::Bounds,
12081208
RhsTactics::ForceNextLineWithoutIndent,
1209-
)?;
1209+
)
1210+
.ok()?;
12101211
}
12111212

12121213
// Rewrite where-clause.
@@ -1396,6 +1397,7 @@ pub(crate) fn format_trait_alias(
13961397
shape.sub_width(1)?,
13971398
)
13981399
.map(|s| s + ";")
1400+
.ok()
13991401
}
14001402

14011403
fn format_unit_struct(
@@ -1835,7 +1837,9 @@ fn rewrite_ty<R: Rewrite>(
18351837

18361838
// 1 = `;`
18371839
let shape = Shape::indented(indent, context.config).sub_width(1)?;
1838-
rewrite_assign_rhs(context, lhs, &*ty, &RhsAssignKind::Ty, shape).map(|s| s + ";")
1840+
rewrite_assign_rhs(context, lhs, &*ty, &RhsAssignKind::Ty, shape)
1841+
.map(|s| s + ";")
1842+
.ok()
18391843
} else {
18401844
Some(format!("{result};"))
18411845
}
@@ -1931,8 +1935,7 @@ pub(crate) fn rewrite_struct_field(
19311935

19321936
let is_prefix_empty = prefix.is_empty();
19331937
// We must use multiline. We are going to put attributes and a field on different lines.
1934-
let field_str = rewrite_assign_rhs(context, prefix, &*field.ty, &RhsAssignKind::Ty, shape)
1935-
.unknown_error()?;
1938+
let field_str = rewrite_assign_rhs(context, prefix, &*field.ty, &RhsAssignKind::Ty, shape)?;
19361939
// Remove a leading white-space from `rewrite_assign_rhs()` when rewriting a tuple struct.
19371940
let field_str = if is_prefix_empty {
19381941
field_str.trim_start()
@@ -3421,6 +3424,7 @@ impl Rewrite for ast::ForeignItem {
34213424
shape.sub_width(1)?,
34223425
)
34233426
.map(|s| s + ";")
3427+
.ok()
34243428
}
34253429
ast::ForeignItemKind::TyAlias(ref ty_alias) => {
34263430
let (kind, span) = (&ItemVisitorKind::ForeignItem(self), self.span);

src/macros.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,18 +1437,15 @@ fn format_lazy_static(
14371437
id,
14381438
ty.rewrite_result(context, nested_shape)?
14391439
));
1440-
result.push_str(
1441-
&rewrite_assign_rhs(
1442-
context,
1443-
stmt,
1444-
&*expr,
1445-
&RhsAssignKind::Expr(&expr.kind, expr.span),
1446-
nested_shape
1447-
.sub_width(1)
1448-
.max_width_error(nested_shape.width, expr.span)?,
1449-
)
1450-
.unknown_error()?,
1451-
);
1440+
result.push_str(&rewrite_assign_rhs(
1441+
context,
1442+
stmt,
1443+
&*expr,
1444+
&RhsAssignKind::Expr(&expr.kind, expr.span),
1445+
nested_shape
1446+
.sub_width(1)
1447+
.max_width_error(nested_shape.width, expr.span)?,
1448+
)?);
14521449
result.push(';');
14531450
if i != last {
14541451
result.push_str(&nested_shape.indent.to_string_with_newline(context.config));

src/types.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ impl Rewrite for ast::WherePredicate {
475475
format!("{type_str}{colon}")
476476
};
477477

478-
rewrite_assign_rhs(context, lhs, bounds, &RhsAssignKind::Bounds, shape)?
478+
rewrite_assign_rhs(context, lhs, bounds, &RhsAssignKind::Bounds, shape).ok()?
479479
}
480480
ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate {
481481
ref lifetime,
@@ -488,7 +488,8 @@ impl Rewrite for ast::WherePredicate {
488488
..
489489
}) => {
490490
let lhs_ty_str = lhs_ty.rewrite(context, shape).map(|lhs| lhs + " =")?;
491-
rewrite_assign_rhs(context, lhs_ty_str, &**rhs_ty, &RhsAssignKind::Ty, shape)?
491+
rewrite_assign_rhs(context, lhs_ty_str, &**rhs_ty, &RhsAssignKind::Ty, shape)
492+
.ok()?
492493
}
493494
};
494495

0 commit comments

Comments
 (0)