diff --git a/src/attr.rs b/src/attr.rs index a263e253dd9..30fed287e55 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -99,7 +99,7 @@ fn format_derive( ",", |span| span.lo(), |span| span.hi(), - |span| Some(context.snippet(*span).to_owned()), + |span| Ok(context.snippet(*span).to_owned()), // We update derive attribute spans to start after the opening '(' // This helps us focus parsing to just what's inside #[derive(...)] context.snippet_provider.span_after(attr.span, "("), diff --git a/src/closures.rs b/src/closures.rs index 6adfb20db8b..8a8f136bbcc 100644 --- a/src/closures.rs +++ b/src/closures.rs @@ -312,7 +312,7 @@ fn rewrite_closure_fn_decl( ",", |param| span_lo_for_param(param), |param| span_hi_for_param(context, param), - |param| param.rewrite(context, param_shape), + |param| param.rewrite_result(context, param_shape), context.snippet_provider.span_after(span, "|"), body.span.lo(), false, diff --git a/src/expr.rs b/src/expr.rs index 7eaaadb1850..2481323446e 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1671,15 +1671,25 @@ fn rewrite_struct_lit<'a>( let rewrite = |item: &StructLitField<'_>| match *item { StructLitField::Regular(field) => { // The 1 taken from the v_budget is for the comma. - let v_shape = v_shape.sub_width(1)?; - rewrite_field(context, field, v_shape, 0).ok() + rewrite_field( + context, + field, + v_shape.sub_width(1).max_width_error(v_shape.width, span)?, + 0, + ) + .unknown_error() } StructLitField::Base(expr) => { // 2 = .. - let v_shape = v_shape.sub_width(2)?; - expr.rewrite(context, v_shape).map(|s| format!("..{}", s)) + expr.rewrite_result( + context, + v_shape + .offset_left(2) + .max_width_error(v_shape.width, span)?, + ) + .map(|s| format!("..{}", s)) } - StructLitField::Rest(_) => Some("..".to_owned()), + StructLitField::Rest(_) => Ok("..".to_owned()), }; let items = itemize_list( @@ -1843,7 +1853,7 @@ fn rewrite_tuple_in_visual_indent_style<'a, T: 'a + IntoOverflowableItem<'a>>( ",", |item| item.span().lo(), |item| item.span().hi(), - |item| item.rewrite(context, nested_shape), + |item| item.rewrite_result(context, nested_shape), list_lo, span.hi() - BytePos(1), false, diff --git a/src/imports.rs b/src/imports.rs index 12c178e136f..eaa71312434 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -470,7 +470,7 @@ impl UseTree { ",", |tree| tree.span.lo(), |tree| tree.span.hi(), - |_| Some("".to_owned()), // We only need comments for now. + |_| Ok("".to_owned()), // We only need comments for now. context.snippet_provider.span_after(a.span, "{"), a.span.hi(), false, diff --git a/src/items.rs b/src/items.rs index 0848ec624ca..86eebc5ac85 100644 --- a/src/items.rs +++ b/src/items.rs @@ -629,7 +629,10 @@ impl<'a> FmtVisitor<'a> { } }, |f| f.span.hi(), - |f| self.format_variant(f, one_line_width, pad_discrim_ident_to), + |f| { + self.format_variant(f, one_line_width, pad_discrim_ident_to) + .unknown_error() + }, body_lo, body_hi, false, @@ -2777,8 +2780,8 @@ fn rewrite_params( |param| param.ty.span.hi(), |param| { param - .rewrite(context, Shape::legacy(multi_line_budget, param_indent)) - .or_else(|| Some(context.snippet(param.span()).to_owned())) + .rewrite_result(context, Shape::legacy(multi_line_budget, param_indent)) + .or_else(|_| Ok(context.snippet(param.span()).to_owned())) }, span.lo(), span.hi(), @@ -3048,7 +3051,7 @@ fn rewrite_bounds_on_where_clause( ",", |pred| pred.span().lo(), |pred| pred.span().hi(), - |pred| pred.rewrite(context, shape), + |pred| pred.rewrite_result(context, shape), span_start, span_end, false, @@ -3129,7 +3132,7 @@ fn rewrite_where_clause( ",", |pred| pred.span().lo(), |pred| pred.span().hi(), - |pred| pred.rewrite(context, Shape::legacy(budget, offset)), + |pred| pred.rewrite_result(context, Shape::legacy(budget, offset)), span_start, span_end, false, diff --git a/src/lists.rs b/src/lists.rs index 41afef279e9..884d5cde61f 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -8,7 +8,7 @@ use rustc_span::BytePos; use crate::comment::{find_comment_end, rewrite_comment, FindUncommented}; use crate::config::lists::*; use crate::config::{Config, IndentStyle}; -use crate::rewrite::RewriteContext; +use crate::rewrite::{RewriteContext, RewriteResult}; use crate::shape::{Indent, Shape}; use crate::utils::{ count_newlines, first_line_width, last_line_width, mk_sp, starts_with_newline, @@ -281,6 +281,7 @@ where let indent_str = &formatting.shape.indent.to_string(formatting.config); while let Some((i, item)) = iter.next() { let item = item.as_ref(); + // TODO here Is it possible to 실제로 list item이 없으면.. let inner_item = item.item.as_ref()?; let first = i == 0; let last = iter.peek().is_none(); @@ -741,7 +742,7 @@ where I: Iterator, F1: Fn(&T) -> BytePos, F2: Fn(&T) -> BytePos, - F3: Fn(&T) -> Option, + F3: Fn(&T) -> RewriteResult, { type Item = ListItem; @@ -778,7 +779,7 @@ where item: if self.inner.peek().is_none() && self.leave_last { None } else { - (self.get_item_string)(&item) + (self.get_item_string)(&item).ok() }, post_comment, new_lines, @@ -805,7 +806,7 @@ where I: Iterator, F1: Fn(&T) -> BytePos, F2: Fn(&T) -> BytePos, - F3: Fn(&T) -> Option, + F3: Fn(&T) -> RewriteResult, { ListItems { snippet_provider, diff --git a/src/macros.rs b/src/macros.rs index a0582b061c0..8b28d19e5c1 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -31,7 +31,7 @@ use crate::lists::{itemize_list, write_list, ListFormatting}; use crate::overflow; use crate::parse::macros::lazy_static::parse_lazy_static; use crate::parse::macros::{parse_expr, parse_macro_args, ParsedMacroArgs}; -use crate::rewrite::{Rewrite, RewriteContext}; +use crate::rewrite::{Rewrite, RewriteContext, RewriteError}; use crate::shape::{Indent, Shape}; use crate::source_map::SpanUtils; use crate::spanned::Spanned; @@ -452,13 +452,13 @@ pub(crate) fn rewrite_macro_def( |branch| branch.span.lo(), |branch| branch.span.hi(), |branch| match branch.rewrite(context, arm_shape, multi_branch_style) { - Some(v) => Some(v), + Some(v) => Ok(v), // if the rewrite returned None because a macro could not be rewritten, then return the // original body None if context.macro_rewrite_failure.get() => { - Some(context.snippet(branch.body).trim().to_string()) + Ok(context.snippet(branch.body).trim().to_string()) } - None => None, + None => Err(RewriteError::Unknown), }, context.snippet_provider.span_after(span, "{"), span.hi(), diff --git a/src/matches.rs b/src/matches.rs index 0dbbb6f1cac..2f8e2183996 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -225,7 +225,7 @@ fn rewrite_match_arms( "|", |arm| arm.span().lo(), |arm| arm.span().hi(), - |arm| arm.rewrite(context, arm_shape), + |arm| arm.rewrite_result(context, arm_shape), open_brace_pos, span.hi(), false, diff --git a/src/overflow.rs b/src/overflow.rs index 43d05e56807..b47882467d5 100644 --- a/src/overflow.rs +++ b/src/overflow.rs @@ -623,7 +623,7 @@ impl<'a> Context<'a> { ",", |item| item.span().lo(), |item| item.span().hi(), - |item| item.rewrite(self.context, self.nested_shape), + |item| item.rewrite_result(self.context, self.nested_shape), span.lo(), span.hi(), true, diff --git a/src/patterns.rs b/src/patterns.rs index da5a0c49aa2..476cf354458 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -343,7 +343,7 @@ fn rewrite_struct_pat( } }, |f| f.span.hi(), - |f| f.rewrite(context, v_shape), + |f| f.rewrite_result(context, v_shape), context.snippet_provider.span_after(span, "{"), span.hi(), false, @@ -551,7 +551,7 @@ fn count_wildcard_suffix_len( ",", |item| item.span().lo(), |item| item.span().hi(), - |item| item.rewrite(context, shape), + |item| item.rewrite_result(context, shape), context.snippet_provider.span_after(span, "("), span.hi() - BytePos(1), false, diff --git a/src/reorder.rs b/src/reorder.rs index fdbed939af5..9618098ffb7 100644 --- a/src/reorder.rs +++ b/src/reorder.rs @@ -15,7 +15,7 @@ use crate::config::{Config, GroupImportsTactic}; use crate::imports::{normalize_use_trees_with_granularity, UseSegmentKind, UseTree}; use crate::items::{is_mod_decl, rewrite_extern_crate, rewrite_mod}; use crate::lists::{itemize_list, write_list, ListFormatting, ListItem}; -use crate::rewrite::RewriteContext; +use crate::rewrite::{RewriteContext, RewriteErrorExt}; use crate::shape::Shape; use crate::source_map::LineRangeUtils; use crate::spanned::Spanned; @@ -99,7 +99,7 @@ fn rewrite_reorderable_or_regroupable_items( ";", |item| item.span().lo(), |item| item.span().hi(), - |_item| Some("".to_owned()), + |_item| Ok("".to_owned()), span.lo(), span.hi(), false, @@ -151,7 +151,7 @@ fn rewrite_reorderable_or_regroupable_items( ";", |item| item.span().lo(), |item| item.span().hi(), - |item| rewrite_reorderable_item(context, item, shape), + |item| rewrite_reorderable_item(context, item, shape).unknown_error(), span.lo(), span.hi(), false, diff --git a/src/types.rs b/src/types.rs index c15ee2f3c5b..26c33ea4e43 100644 --- a/src/types.rs +++ b/src/types.rs @@ -388,7 +388,7 @@ where ",", |arg| arg.span().lo(), |arg| arg.span().hi(), - |arg| arg.rewrite(context, list_shape), + |arg| arg.rewrite_result(context, list_shape), list_lo, span.hi(), false, diff --git a/src/vertical.rs b/src/vertical.rs index 691759803e5..f455c2781e9 100644 --- a/src/vertical.rs +++ b/src/vertical.rs @@ -231,7 +231,6 @@ fn rewrite_aligned_items_inner( |field| { field .rewrite_aligned_item(context, item_shape, field_prefix_max_width) - .ok() }, span.lo(), span.hi(),