diff --git a/src/attr.rs b/src/attr.rs index 30fed287e55..c47d7a2a2f1 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -147,7 +147,7 @@ fn format_derive( .tactic(tactic) .trailing_separator(trailing_separator) .ends_with_newline(false); - let item_str = write_list(&all_items, &fmt)?; + let item_str = write_list(&all_items, &fmt).ok()?; debug!("item_str: '{}'", item_str); diff --git a/src/closures.rs b/src/closures.rs index 8a8f136bbcc..20dc4eb6ec4 100644 --- a/src/closures.rs +++ b/src/closures.rs @@ -336,7 +336,7 @@ fn rewrite_closure_fn_decl( let fmt = ListFormatting::new(param_shape, context.config) .tactic(tactic) .preserve_newline(true); - let list_str = write_list(&item_vec, &fmt).unknown_error()?; + let list_str = write_list(&item_vec, &fmt)?; let mut prefix = format!("{binder}{const_}{immovable}{coro}{mover}|{list_str}|"); if !ret_str.is_empty() { diff --git a/src/expr.rs b/src/expr.rs index 2481323446e..b73b3348b3e 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1677,7 +1677,6 @@ enum StructLitField<'a> { v_shape.sub_width(1).max_width_error(v_shape.width, span)?, 0, ) - .unknown_error() } StructLitField::Base(expr) => { // 2 = .. @@ -1719,7 +1718,7 @@ enum StructLitField<'a> { force_no_trailing_comma || has_base_or_rest || !context.use_block_indent(), ); - write_list(&item_vec, &fmt).unknown_error()? + write_list(&item_vec, &fmt)? }; let fields_str = @@ -1868,7 +1867,7 @@ fn rewrite_tuple_in_visual_indent_style<'a, T: 'a + IntoOverflowableItem<'a>>( let fmt = ListFormatting::new(nested_shape, context.config) .tactic(tactic) .ends_with_newline(false); - let list_str = write_list(&item_vec, &fmt)?; + let list_str = write_list(&item_vec, &fmt).ok()?; Some(format!("({list_str})")) } diff --git a/src/imports.rs b/src/imports.rs index eaa71312434..4cea59ddc58 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -993,7 +993,7 @@ fn rewrite_nested_use_tree( }; for use_tree in use_tree_list { if let Some(mut list_item) = use_tree.list_item.clone() { - list_item.item = use_tree.rewrite(context, nested_shape); + list_item.item = use_tree.rewrite_result(context, nested_shape); list_items.push(list_item); } else { list_items.push(ListItem::from_str(use_tree.rewrite(context, nested_shape)?)); @@ -1032,7 +1032,7 @@ fn rewrite_nested_use_tree( .preserve_newline(true) .nested(has_nested_list); - let list_str = write_list(&list_items, &fmt)?; + let list_str = write_list(&list_items, &fmt).ok()?; let result = if (list_str.contains('\n') || list_str.len() > remaining_width diff --git a/src/items.rs b/src/items.rs index 86eebc5ac85..0bb27839ed3 100644 --- a/src/items.rs +++ b/src/items.rs @@ -653,7 +653,7 @@ fn format_variant_list( .trailing_separator(self.config.trailing_comma()) .preserve_newline(true); - let list = write_list(&items, &fmt)?; + let list = write_list(&items, &fmt).ok()?; result.push_str(&list); result.push_str(&original_offset.to_string_with_newline(self.config)); result.push('}'); @@ -2819,7 +2819,7 @@ fn rewrite_params( .trailing_separator(trailing_separator) .ends_with_newline(tactic.ends_with_newline(context.config.indent_style())) .preserve_newline(true); - write_list(¶m_items, &fmt) + write_list(¶m_items, &fmt).ok() } fn compute_budgets_for_params( @@ -3076,7 +3076,7 @@ fn rewrite_bounds_on_where_clause( .tactic(shape_tactic) .trailing_separator(comma_tactic) .preserve_newline(preserve_newline); - write_list(&items.collect::>(), &fmt) + write_list(&items.collect::>(), &fmt).ok() } fn rewrite_where_clause( @@ -3152,7 +3152,7 @@ fn rewrite_where_clause( .trailing_separator(comma_tactic) .ends_with_newline(tactic.ends_with_newline(context.config.indent_style())) .preserve_newline(true); - let preds_str = write_list(&item_vec, &fmt)?; + let preds_str = write_list(&item_vec, &fmt).ok()?; let end_length = if terminator == "{" { // If the brace is on the next line we don't need to count it otherwise it needs two diff --git a/src/lists.rs b/src/lists.rs index 884d5cde61f..46d40fa750e 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -8,7 +8,7 @@ use crate::comment::{find_comment_end, rewrite_comment, FindUncommented}; use crate::config::lists::*; use crate::config::{Config, IndentStyle}; -use crate::rewrite::{RewriteContext, RewriteResult}; +use crate::rewrite::{RewriteContext, RewriteError, RewriteErrorExt, RewriteResult}; use crate::shape::{Indent, Shape}; use crate::utils::{ count_newlines, first_line_width, last_line_width, mk_sp, starts_with_newline, @@ -125,18 +125,18 @@ pub(crate) struct ListItem { pub(crate) pre_comment_style: ListItemCommentStyle, // Item should include attributes and doc comments. None indicates a failed // rewrite. - pub(crate) item: Option, + pub(crate) item: RewriteResult, pub(crate) post_comment: Option, // Whether there is extra whitespace before this item. pub(crate) new_lines: bool, } impl ListItem { - pub(crate) fn empty() -> ListItem { + pub(crate) fn from_item(item: RewriteResult) -> ListItem { ListItem { pre_comment: None, pre_comment_style: ListItemCommentStyle::None, - item: None, + item: item, post_comment: None, new_lines: false, } @@ -185,7 +185,7 @@ pub(crate) fn from_str>(s: S) -> ListItem { ListItem { pre_comment: None, pre_comment_style: ListItemCommentStyle::None, - item: Some(s.into()), + item: Ok(s.into()), post_comment: None, new_lines: false, } @@ -197,7 +197,11 @@ fn empty(s: &Option) -> bool { !matches!(*s, Some(ref s) if !s.is_empty()) } - !(empty(&self.pre_comment) && empty(&self.item) && empty(&self.post_comment)) + fn empty_result(s: &RewriteResult) -> bool { + !matches!(*s, Ok(ref s) if !s.is_empty()) + } + + !(empty(&self.pre_comment) && empty_result(&self.item) && empty(&self.post_comment)) } } @@ -257,7 +261,7 @@ pub(crate) fn definitive_tactic( } // Format a list of commented items into a string. -pub(crate) fn write_list(items: I, formatting: &ListFormatting<'_>) -> Option +pub(crate) fn write_list(items: I, formatting: &ListFormatting<'_>) -> RewriteResult where I: IntoIterator + Clone, T: AsRef, @@ -281,8 +285,7 @@ pub(crate) fn write_list(items: I, formatting: &ListFormatting<'_>) -> Opt 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 inner_item = item.item.as_ref().or_else(|err| Err(err.clone()))?; let first = i == 0; let last = iter.peek().is_none(); let mut separate = match sep_place { @@ -363,8 +366,8 @@ pub(crate) fn write_list(items: I, formatting: &ListFormatting<'_>) -> Opt // Block style in non-vertical mode. let block_mode = tactic == DefinitiveListTactic::Horizontal; // Width restriction is only relevant in vertical mode. - let comment = - rewrite_comment(comment, block_mode, formatting.shape, formatting.config)?; + let comment = rewrite_comment(comment, block_mode, formatting.shape, formatting.config) + .unknown_error()?; result.push_str(&comment); if !inner_item.is_empty() { @@ -410,7 +413,8 @@ pub(crate) fn write_list(items: I, formatting: &ListFormatting<'_>) -> Opt true, Shape::legacy(formatting.shape.width, Indent::empty()), formatting.config, - )?; + ) + .unknown_error()?; result.push(' '); result.push_str(&formatted_comment); @@ -461,7 +465,8 @@ pub(crate) fn write_list(items: I, formatting: &ListFormatting<'_>) -> Opt ) }; - let mut formatted_comment = rewrite_post_comment(&mut item_max_width)?; + let mut formatted_comment = + rewrite_post_comment(&mut item_max_width).unknown_error()?; if !starts_with_newline(comment) { if formatting.align_comments { @@ -474,7 +479,8 @@ pub(crate) fn write_list(items: I, formatting: &ListFormatting<'_>) -> Opt > formatting.config.max_width() { item_max_width = None; - formatted_comment = rewrite_post_comment(&mut item_max_width)?; + formatted_comment = + rewrite_post_comment(&mut item_max_width).unknown_error()?; comment_alignment = post_comment_alignment(item_max_width, unicode_str_width(inner_item)); } @@ -517,7 +523,7 @@ pub(crate) fn write_list(items: I, formatting: &ListFormatting<'_>) -> Opt prev_item_is_nested_import = inner_item.contains("::"); } - Some(result) + Ok(result) } fn max_width_of_item_with_post_comment( @@ -776,10 +782,11 @@ fn next(&mut self) -> Option { ListItem { pre_comment, pre_comment_style, + // leave_last is set to true only for rewrite_items item: if self.inner.peek().is_none() && self.leave_last { - None + Err(RewriteError::SkipFormatting) } else { - (self.get_item_string)(&item).ok() + (self.get_item_string)(&item) }, post_comment, new_lines, diff --git a/src/macros.rs b/src/macros.rs index 8b28d19e5c1..a948aaba083 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -477,8 +477,8 @@ pub(crate) fn rewrite_macro_def( } match write_list(&branch_items, &fmt) { - Some(ref s) => result += s, - None => return snippet, + Ok(ref s) => result += s, + Err(_) => return snippet, } if multi_branch_style { diff --git a/src/matches.rs b/src/matches.rs index 2f8e2183996..5884d02af15 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -236,7 +236,7 @@ fn rewrite_match_arms( .separator("") .preserve_newline(true); - write_list(&arms_vec, &fmt).unknown_error() + write_list(&arms_vec, &fmt) } fn rewrite_match_arm( diff --git a/src/overflow.rs b/src/overflow.rs index b47882467d5..1a0d8bf15f6 100644 --- a/src/overflow.rs +++ b/src/overflow.rs @@ -19,7 +19,7 @@ }; use crate::macros::MacroArg; use crate::patterns::{can_be_overflowed_pat, TuplePatField}; -use crate::rewrite::{Rewrite, RewriteContext}; +use crate::rewrite::{Rewrite, RewriteContext, RewriteErrorExt}; use crate::shape::Shape; use crate::source_map::SpanUtils; use crate::spanned::Spanned; @@ -456,7 +456,7 @@ fn rewrite_last_item_with_overflow( if let Some(rewrite) = rewrite { // splitn(2, *).next().unwrap() is always safe. - let rewrite_first_line = Some(rewrite.splitn(2, '\n').next().unwrap().to_owned()); + let rewrite_first_line = Ok(rewrite.splitn(2, '\n').next().unwrap().to_owned()); last_list_item.item = rewrite_first_line; Some(rewrite) } else { @@ -544,22 +544,23 @@ fn try_overflow_last_item(&self, list_items: &mut Vec) -> DefinitiveLi .and_then(|last_item| last_item.rewrite(self.context, self.nested_shape)); let no_newline = rw.as_ref().map_or(false, |s| !s.contains('\n')); if no_newline { - list_items[self.items.len() - 1].item = rw; + list_items[self.items.len() - 1].item = rw.unknown_error(); } else { - list_items[self.items.len() - 1].item = Some(overflowed.to_owned()); + list_items[self.items.len() - 1].item = Ok(overflowed.to_owned()); } } else { - list_items[self.items.len() - 1].item = Some(overflowed.to_owned()); + list_items[self.items.len() - 1].item = Ok(overflowed.to_owned()); } } (true, DefinitiveListTactic::Horizontal, placeholder @ Some(..)) => { - list_items[self.items.len() - 1].item = placeholder; + list_items[self.items.len() - 1].item = placeholder.unknown_error(); } _ if !self.items.is_empty() => { list_items[self.items.len() - 1].item = self .items .last() - .and_then(|last_item| last_item.rewrite(self.context, self.nested_shape)); + .and_then(|last_item| last_item.rewrite(self.context, self.nested_shape)) + .unknown_error(); // Use horizontal layout for a function with a single argument as long as // everything fits in a single line. @@ -656,6 +657,7 @@ fn rewrite_items(&self) -> Option<(bool, String)> { .ends_with_newline(ends_with_newline); write_list(&list_items, &fmt) + .ok() .map(|items_str| (tactic == DefinitiveListTactic::Horizontal, items_str)) } diff --git a/src/patterns.rs b/src/patterns.rs index 476cf354458..df435a3069d 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -101,7 +101,7 @@ fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option .separator(" |") .separator_place(context.config.binop_separator()) .ends_with_newline(false); - write_list(&items, &fmt) + write_list(&items, &fmt).ok() } PatKind::Box(ref pat) => rewrite_unary_prefix(context, "box ", &**pat, shape), PatKind::Ident(BindingMode(by_ref, mutability), ident, ref sub_pat) => { @@ -354,7 +354,7 @@ fn rewrite_struct_pat( let nested_shape = shape_for_tactic(tactic, h_shape, v_shape); let fmt = struct_lit_formatting(nested_shape, tactic, context, false); - let mut fields_str = write_list(&item_vec, &fmt).unknown_error()?; + let mut fields_str = write_list(&item_vec, &fmt)?; let one_line_width = h_shape.map_or(0, |shape| shape.width); let has_trailing_comma = fmt.needs_trailing_separator(); @@ -561,7 +561,7 @@ fn count_wildcard_suffix_len( for item in items .iter() .rev() - .take_while(|i| matches!(i.item, Some(ref internal_string) if internal_string == "_")) + .take_while(|i| matches!(i.item, Ok(ref internal_string) if internal_string == "_")) { suffix_len += 1; diff --git a/src/reorder.rs b/src/reorder.rs index 9618098ffb7..8a546a78297 100644 --- a/src/reorder.rs +++ b/src/reorder.rs @@ -59,7 +59,7 @@ fn wrap_reorderable_items( let fmt = ListFormatting::new(shape, context.config) .separator("") .align_comments(false); - write_list(list_items, &fmt) + write_list(list_items, &fmt).ok() } fn rewrite_reorderable_item( @@ -131,9 +131,18 @@ fn rewrite_reorderable_or_regroupable_items( .map(|use_group| { let item_vec: Vec<_> = use_group .into_iter() - .map(|use_tree| ListItem { - item: use_tree.rewrite_top_level(context, nested_shape), - ..use_tree.list_item.unwrap_or_else(ListItem::empty) + .map(|use_tree| { + let item = use_tree + .rewrite_top_level(context, nested_shape) + .unknown_error(); + if let Some(list_item) = use_tree.list_item { + ListItem { + item: item, + ..list_item + } + } else { + ListItem::from_item(item) + } }) .collect(); wrap_reorderable_items(context, &item_vec, nested_shape) diff --git a/src/rewrite.rs b/src/rewrite.rs index acf4cc1d60b..6644e4b27a8 100644 --- a/src/rewrite.rs +++ b/src/rewrite.rs @@ -30,7 +30,7 @@ fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option } } -#[derive(Error, Debug)] +#[derive(Clone, Error, Debug)] pub(crate) enum RewriteError { #[error("Formatting was skipped due to skip attribute or out of file range.")] SkipFormatting, diff --git a/src/types.rs b/src/types.rs index 26c33ea4e43..f43c60db02a 100644 --- a/src/types.rs +++ b/src/types.rs @@ -407,7 +407,7 @@ fn format_function_type<'a, I>( .trailing_separator(trailing_separator) .ends_with_newline(tactic.ends_with_newline(context.config.indent_style())) .preserve_newline(true); - (write_list(&item_vec, &fmt).unknown_error()?, tactic) + (write_list(&item_vec, &fmt)?, tactic) }; let args = if tactic == DefinitiveListTactic::Horizontal diff --git a/src/vertical.rs b/src/vertical.rs index f455c2781e9..670fa5a21e4 100644 --- a/src/vertical.rs +++ b/src/vertical.rs @@ -228,10 +228,7 @@ fn rewrite_aligned_items_inner( ",", |field| field.get_span().lo(), |field| field.get_span().hi(), - |field| { - field - .rewrite_aligned_item(context, item_shape, field_prefix_max_width) - }, + |field| field.rewrite_aligned_item(context, item_shape, field_prefix_max_width), span.lo(), span.hi(), false, @@ -247,14 +244,13 @@ fn rewrite_aligned_items_inner( if tactic == DefinitiveListTactic::Horizontal { // since the items fits on a line, there is no need to align them - let do_rewrite = |field: &T| -> Option { - field.rewrite_aligned_item(context, item_shape, 0).ok() - }; + let do_rewrite = + |field: &T| -> RewriteResult { field.rewrite_aligned_item(context, item_shape, 0) }; fields .iter() .zip(items.iter_mut()) .for_each(|(field, list_item): (&T, &mut ListItem)| { - if list_item.item.is_some() { + if list_item.item.is_ok() { list_item.item = do_rewrite(field); } }); @@ -270,7 +266,7 @@ fn rewrite_aligned_items_inner( .tactic(tactic) .trailing_separator(separator_tactic) .preserve_newline(true); - write_list(&items, &fmt) + write_list(&items, &fmt).ok() } /// Returns the index in `fields` up to which a field belongs to the current group.