Skip to content

Commit 77f5b71

Browse files
committed
modify rewrite_literal
1 parent ea02de2 commit 77f5b71

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

src/attr.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use crate::config::IndentStyle;
1111
use crate::expr::rewrite_literal;
1212
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
1313
use crate::overflow;
14-
use crate::rewrite::{Rewrite, RewriteContext};
14+
use crate::rewrite::RewriteErrorExt;
15+
use crate::rewrite::{Rewrite, RewriteContext, RewriteResult};
1516
use crate::shape::Shape;
1617
use crate::source_map::SpanUtils;
1718
use crate::types::{rewrite_path, PathContext};
@@ -244,8 +245,14 @@ fn rewrite_initial_doc_comments(
244245

245246
impl Rewrite for ast::NestedMetaItem {
246247
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
248+
self.rewrite_result(context, shape).ok()
249+
}
250+
251+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
247252
match self {
248-
ast::NestedMetaItem::MetaItem(ref meta_item) => meta_item.rewrite(context, shape),
253+
ast::NestedMetaItem::MetaItem(ref meta_item) => {
254+
meta_item.rewrite_result(context, shape)
255+
}
249256
ast::NestedMetaItem::Lit(ref l) => {
250257
rewrite_literal(context, l.as_token_lit(), l.span, shape)
251258
}
@@ -274,42 +281,47 @@ fn has_newlines_before_after_comment(comment: &str) -> (&str, &str) {
274281

275282
impl Rewrite for ast::MetaItem {
276283
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
277-
Some(match self.kind {
284+
self.rewrite_result(context, shape).ok()
285+
}
286+
287+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
288+
Ok(match self.kind {
278289
ast::MetaItemKind::Word => {
279-
rewrite_path(context, PathContext::Type, &None, &self.path, shape).ok()?
290+
rewrite_path(context, PathContext::Type, &None, &self.path, shape)?
280291
}
281292
ast::MetaItemKind::List(ref list) => {
282-
let path =
283-
rewrite_path(context, PathContext::Type, &None, &self.path, shape).ok()?;
293+
let path = rewrite_path(context, PathContext::Type, &None, &self.path, shape)?;
284294
let has_trailing_comma = crate::expr::span_ends_with_comma(context, self.span);
285295
overflow::rewrite_with_parens(
286296
context,
287297
&path,
288298
list.iter(),
289299
// 1 = "]"
290-
shape.sub_width(1)?,
300+
shape.sub_width(1).max_width_error(shape.width, self.span)?,
291301
self.span,
292302
context.config.attr_fn_like_width(),
293303
Some(if has_trailing_comma {
294304
SeparatorTactic::Always
295305
} else {
296306
SeparatorTactic::Never
297307
}),
298-
)?
308+
)
309+
.unknown_error()? // TODO rebase?
299310
}
300311
ast::MetaItemKind::NameValue(ref lit) => {
301-
let path =
302-
rewrite_path(context, PathContext::Type, &None, &self.path, shape).ok()?;
312+
let path = rewrite_path(context, PathContext::Type, &None, &self.path, shape)?;
303313
// 3 = ` = `
304-
let lit_shape = shape.shrink_left(path.len() + 3)?;
314+
let lit_shape = shape
315+
.shrink_left(path.len() + 3)
316+
.max_width_error(shape.width, self.span)?;
305317
// `rewrite_literal` returns `None` when `lit` exceeds max
306318
// width. Since a literal is basically unformattable unless it
307319
// is a string literal (and only if `format_strings` is set),
308320
// we might be better off ignoring the fact that the attribute
309321
// is longer than the max width and continue on formatting.
310322
// See #2479 for example.
311323
let value = rewrite_literal(context, lit.as_token_lit(), lit.span, lit_shape)
312-
.unwrap_or_else(|| context.snippet(lit.span).to_owned());
324+
.unwrap_or_else(|_| context.snippet(lit.span).to_owned());
313325
format!("{path} = {value}")
314326
}
315327
})

src/expr.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub(crate) fn format_expr(
8181
None,
8282
),
8383
ast::ExprKind::Lit(token_lit) => {
84-
if let Some(expr_rw) = rewrite_literal(context, token_lit, expr.span, shape) {
84+
if let Ok(expr_rw) = rewrite_literal(context, token_lit, expr.span, shape) {
8585
Some(expr_rw)
8686
} else {
8787
if let LitKind::StrRaw(_) = token_lit.kind {
@@ -1256,19 +1256,20 @@ pub(crate) fn rewrite_literal(
12561256
token_lit: token::Lit,
12571257
span: Span,
12581258
shape: Shape,
1259-
) -> Option<String> {
1259+
) -> RewriteResult {
12601260
match token_lit.kind {
12611261
token::LitKind::Str => rewrite_string_lit(context, span, shape),
12621262
token::LitKind::Integer => rewrite_int_lit(context, token_lit, span, shape),
12631263
_ => wrap_str(
12641264
context.snippet(span).to_owned(),
12651265
context.config.max_width(),
12661266
shape,
1267-
),
1267+
)
1268+
.max_width_error(shape.width, span),
12681269
}
12691270
}
12701271

1271-
fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) -> Option<String> {
1272+
fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) -> RewriteResult {
12721273
let string_lit = context.snippet(span);
12731274

12741275
if !context.config.format_strings() {
@@ -1278,9 +1279,10 @@ fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) ->
12781279
.all(|line| line.ends_with('\\'))
12791280
&& context.config.version() == Version::Two
12801281
{
1281-
return Some(string_lit.to_owned());
1282+
return Ok(string_lit.to_owned());
12821283
} else {
1283-
return wrap_str(string_lit.to_owned(), context.config.max_width(), shape);
1284+
return wrap_str(string_lit.to_owned(), context.config.max_width(), shape)
1285+
.max_width_error(shape.width, span);
12841286
}
12851287
}
12861288

@@ -1292,14 +1294,15 @@ fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) ->
12921294
&StringFormat::new(shape.visual_indent(0), context.config),
12931295
shape.width.saturating_sub(2),
12941296
)
1297+
.max_width_error(shape.width, span) // - 2 ?
12951298
}
12961299

12971300
fn rewrite_int_lit(
12981301
context: &RewriteContext<'_>,
12991302
token_lit: token::Lit,
13001303
span: Span,
13011304
shape: Shape,
1302-
) -> Option<String> {
1305+
) -> RewriteResult {
13031306
let symbol = token_lit.symbol.as_str();
13041307

13051308
if let Some(symbol_stripped) = symbol.strip_prefix("0x") {
@@ -1317,7 +1320,8 @@ fn rewrite_int_lit(
13171320
),
13181321
context.config.max_width(),
13191322
shape,
1320-
);
1323+
)
1324+
.max_width_error(shape.width, span);
13211325
}
13221326
}
13231327

@@ -1326,6 +1330,7 @@ fn rewrite_int_lit(
13261330
context.config.max_width(),
13271331
shape,
13281332
)
1333+
.max_width_error(shape.width, span)
13291334
}
13301335

13311336
fn choose_separator_tactic(context: &RewriteContext<'_>, span: Span) -> Option<SeparatorTactic> {

0 commit comments

Comments
 (0)