From b2a7a34d9776c878066e8356f0e9c65c118c1b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Campinas?= Date: Mon, 5 Aug 2019 23:53:06 +0200 Subject: [PATCH] version-gate the changes from #3618 and #3632 --- src/attr.rs | 8 +++- src/chains.rs | 10 ++++- src/closures.rs | 1 + src/expr.rs | 9 +++- src/imports.rs | 1 + src/items.rs | 9 +++- src/lists.rs | 41 ++++++++++++++----- src/matches.rs | 4 +- src/overflow.rs | 7 +++- src/types.rs | 14 +++++-- src/vertical.rs | 1 + .../{unicode.rs => unicode/version-two.rs} | 17 ++++++++ .../{unicode.rs => unicode/version-two.rs} | 11 +++++ 13 files changed, 112 insertions(+), 21 deletions(-) rename tests/source/{unicode.rs => unicode/version-two.rs} (77%) rename tests/target/{unicode.rs => unicode/version-two.rs} (81%) diff --git a/src/attr.rs b/src/attr.rs index 1c9092ea629..d6b390a78a6 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -295,7 +295,13 @@ where ListTactic::HorizontalVertical }; - let tactic = definitive_tactic(&item_vec, tactic, Separator::Comma, shape.width); + let tactic = definitive_tactic( + context.config.version(), + &item_vec, + tactic, + Separator::Comma, + shape.width, + ); let fmt = ListFormatting::new(shape, context.config) .tactic(tactic) .ends_with_newline(false); diff --git a/src/chains.rs b/src/chains.rs index 81708625acb..72c6bfa717b 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -62,7 +62,7 @@ use syntax::source_map::{BytePos, Span}; use syntax::{ast, ptr}; use crate::comment::{rewrite_comment, CharClasses, FullCodeCharKind, RichChar}; -use crate::config::IndentStyle; +use crate::config::{IndentStyle, Version}; use crate::expr::rewrite_call; use crate::lists::extract_pre_comment; use crate::macros::convert_try_mac; @@ -559,7 +559,13 @@ impl<'a> ChainFormatterShared<'a> { } else { self.rewrites .iter() - .map(|rw| utils::unicode_str_width(&rw)) + .map(|rw| { + if context.config.version() == Version::One { + rw.len() + } else { + utils::unicode_str_width(&rw) + } + }) .sum() } + last.tries; let one_line_budget = if self.child_count == 1 { diff --git a/src/closures.rs b/src/closures.rs index c0ecc91db52..758a223d832 100644 --- a/src/closures.rs +++ b/src/closures.rs @@ -252,6 +252,7 @@ fn rewrite_closure_fn_decl( // 1 = space between arguments and return type. let horizontal_budget = nested_shape.width.saturating_sub(ret_str.len() + 1); let tactic = definitive_tactic( + context.config.version(), &item_vec, ListTactic::HorizontalVertical, Separator::Comma, diff --git a/src/expr.rs b/src/expr.rs index 9030d876b40..dcca73a9e22 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1164,6 +1164,7 @@ pub(crate) fn rewrite_multiple_patterns( DefinitiveListTactic::Mixed } else { definitive_tactic( + context.config.version(), &items, ListTactic::HorizontalVertical, Separator::VerticalBar, @@ -1745,6 +1746,7 @@ fn rewrite_tuple_in_visual_indent_style<'a, T: 'a + IntoOverflowableItem<'a>>( ); let item_vec: Vec<_> = items.collect(); let tactic = definitive_tactic( + context.config.version(), &item_vec, ListTactic::HorizontalVertical, Separator::Comma, @@ -1910,7 +1912,12 @@ fn choose_rhs( ) -> Option { match orig_rhs { Some(ref new_str) - if !new_str.contains('\n') && unicode_str_width(new_str) <= shape.width => + if !new_str.contains('\n') + && if context.config.version() == Version::One { + new_str.len() + } else { + unicode_str_width(new_str) + } <= shape.width => { Some(format!(" {}", new_str)) } diff --git a/src/imports.rs b/src/imports.rs index a85889884ca..70945c9f3a4 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -753,6 +753,7 @@ fn rewrite_nested_use_tree( }; let tactic = definitive_tactic( + context.config.version(), &list_items, context.config.imports_layout(), Separator::Comma, diff --git a/src/items.rs b/src/items.rs index 73b752dfe78..43f2834fd75 100644 --- a/src/items.rs +++ b/src/items.rs @@ -2360,6 +2360,7 @@ fn rewrite_args( .collect(); let tactic = definitive_tactic( + context.config.version(), &arg_items, context .config @@ -2706,7 +2707,13 @@ fn rewrite_where_clause( ); let item_vec = items.collect::>(); // FIXME: we don't need to collect here - let tactic = definitive_tactic(&item_vec, ListTactic::Vertical, Separator::Comma, budget); + let tactic = definitive_tactic( + context.config.version(), + &item_vec, + ListTactic::Vertical, + Separator::Comma, + budget, + ); let mut comma_tactic = context.config.trailing_comma(); // Kind of a hack because we don't usually have trailing commas in where-clauses. diff --git a/src/lists.rs b/src/lists.rs index ac83d7082c8..0c0a3124da7 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -7,7 +7,7 @@ use syntax::source_map::BytePos; use crate::comment::{find_comment_end, rewrite_comment, FindUncommented}; use crate::config::lists::*; -use crate::config::{Config, IndentStyle}; +use crate::config::{Config, IndentStyle, Version}; use crate::rewrite::RewriteContext; use crate::shape::{Indent, Shape}; use crate::utils::{ @@ -223,6 +223,7 @@ impl Separator { } pub(crate) fn definitive_tactic( + version: Version, items: I, tactic: ListTactic, sep: Separator, @@ -245,7 +246,7 @@ where ListTactic::Mixed | ListTactic::HorizontalVertical => width, }; - let (sep_count, total_width) = calculate_width(items.clone()); + let (sep_count, total_width) = calculate_width(version, items.clone()); let total_sep_len = sep.len() * sep_count.saturating_sub(1); let real_total = total_width + total_sep_len; @@ -332,7 +333,8 @@ where result.push_str(indent_str); } DefinitiveListTactic::Mixed => { - let total_width = total_item_width(item) + item_sep_len; + let total_width = + total_item_width(formatting.config.version(), item) + item_sep_len; // 1 is space between separator and item. if (line_len > 0 && line_len + 1 + total_width > formatting.shape.width) @@ -380,7 +382,8 @@ where } else { // We will try to keep the comment on the same line with the item here. // 1 = ` ` - let total_width = total_item_width(item) + item_sep_len + 1; + let total_width = + total_item_width(formatting.config.version(), item) + item_sep_len + 1; total_width <= formatting.shape.width }; if keep_comment { @@ -389,7 +392,13 @@ where result.push('\n'); result.push_str(indent_str); // This is the width of the item (without comments). - line_len = item.item.as_ref().map_or(0, |s| unicode_str_width(&s)); + line_len = item.item.as_ref().map_or(0, |s| { + if formatting.config.version() == Version::One { + s.len() + } else { + unicode_str_width(&s) + } + }); } } else { result.push(' '); @@ -801,21 +810,27 @@ where } /// Returns the count and total width of the list items. -fn calculate_width(items: I) -> (usize, usize) +fn calculate_width(version: Version, items: I) -> (usize, usize) where I: IntoIterator, T: AsRef, { items .into_iter() - .map(|item| total_item_width(item.as_ref())) + .map(|item| total_item_width(version, item.as_ref())) .fold((0, 0), |acc, l| (acc.0 + 1, acc.1 + l)) } -pub(crate) fn total_item_width(item: &ListItem) -> usize { +pub(crate) fn total_item_width(version: Version, item: &ListItem) -> usize { comment_len(item.pre_comment.as_ref().map(|x| &(*x)[..])) + comment_len(item.post_comment.as_ref().map(|x| &(*x)[..])) - + &item.item.as_ref().map_or(0, |s| unicode_str_width(&s)) + + &item.item.as_ref().map_or(0, |s| { + if version == Version::One { + s.len() + } else { + unicode_str_width(&s) + } + }) } fn comment_len(comment: Option<&str>) -> usize { @@ -874,7 +889,13 @@ pub(crate) fn struct_lit_tactic( _ if context.config.struct_lit_single_line() => ListTactic::HorizontalVertical, _ => ListTactic::Vertical, }; - definitive_tactic(items, prelim_tactic, Separator::Comma, h_shape.width) + definitive_tactic( + context.config.version(), + items, + prelim_tactic, + Separator::Comma, + h_shape.width, + ) } else { DefinitiveListTactic::Vertical } diff --git a/src/matches.rs b/src/matches.rs index b7a1c374323..b952300750f 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -468,7 +468,9 @@ fn rewrite_match_body( next_line_body_shape.width, ); match (orig_body, next_line_body) { - (Some(ref orig_str), Some(ref next_line_str)) if orig_str == next_line_str => { + (Some(ref orig_str), Some(ref next_line_str)) + if context.config.version() == Version::Two && orig_str == next_line_str => + { combine_orig_body(orig_str) } (Some(ref orig_str), Some(ref next_line_str)) diff --git a/src/overflow.rs b/src/overflow.rs index d8e1d0e2590..16a0ce4a3ad 100644 --- a/src/overflow.rs +++ b/src/overflow.rs @@ -446,6 +446,7 @@ impl<'a> Context<'a> { fn default_tactic(&self, list_items: &[ListItem]) -> DefinitiveListTactic { definitive_tactic( + self.context.config.version(), list_items, ListTactic::LimitedHorizontalVertical(self.item_max_width), Separator::Comma, @@ -499,6 +500,7 @@ impl<'a> Context<'a> { }; let mut tactic = definitive_tactic( + self.context.config.version(), &*list_items, ListTactic::LimitedHorizontalVertical(self.item_max_width), Separator::Comma, @@ -548,7 +550,8 @@ impl<'a> Context<'a> { && self.one_line_width != 0 && !list_items[0].has_comment() && !list_items[0].inner_as_ref().contains('\n') - && crate::lists::total_item_width(&list_items[0]) <= self.one_line_width + && crate::lists::total_item_width(self.context.config.version(), &list_items[0]) + <= self.one_line_width { tactic = DefinitiveListTactic::Horizontal; } else { @@ -560,12 +563,14 @@ impl<'a> Context<'a> { { let one_line = all_simple && definitive_tactic( + self.context.config.version(), &list_items[..num_args_before], ListTactic::HorizontalVertical, Separator::Comma, self.nested_shape.width, ) == DefinitiveListTactic::Horizontal && definitive_tactic( + self.context.config.version(), &list_items[num_args_before + 1..], ListTactic::HorizontalVertical, Separator::Comma, diff --git a/src/types.rs b/src/types.rs index d09e8c01e38..9b99706683e 100644 --- a/src/types.rs +++ b/src/types.rs @@ -6,7 +6,7 @@ use syntax::source_map::{self, BytePos, Span}; use syntax::symbol::kw; use crate::config::lists::*; -use crate::config::{IndentStyle, TypeDensity}; +use crate::config::{IndentStyle, TypeDensity, Version}; use crate::expr::{format_expr, rewrite_assign_rhs, rewrite_tuple, rewrite_unary_prefix, ExprType}; use crate::lists::{ definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator, @@ -339,7 +339,7 @@ where let is_inputs_empty = inputs.len() == 0; let list_lo = context.snippet_provider.span_after(span, "("); let (list_str, tactic) = if is_inputs_empty { - let tactic = get_tactics(&[], &output, shape); + let tactic = get_tactics(context.config.version(), &[], &output, shape); let list_hi = context.snippet_provider.span_before(span, ")"); let comment = context .snippet_provider @@ -371,7 +371,7 @@ where ); let item_vec: Vec<_> = items.collect(); - let tactic = get_tactics(&item_vec, &output, shape); + let tactic = get_tactics(context.config.version(), &item_vec, &output, shape); let trailing_separator = if !context.use_block_indent() || variadic { SeparatorTactic::Never } else { @@ -417,11 +417,17 @@ fn type_bound_colon(context: &RewriteContext<'_>) -> &'static str { // If the return type is multi-lined, then force to use multiple lines for // arguments as well. -fn get_tactics(item_vec: &[ListItem], output: &str, shape: Shape) -> DefinitiveListTactic { +fn get_tactics( + version: Version, + item_vec: &[ListItem], + output: &str, + shape: Shape, +) -> DefinitiveListTactic { if output.contains('\n') { DefinitiveListTactic::Vertical } else { definitive_tactic( + version, item_vec, ListTactic::HorizontalVertical, Separator::Comma, diff --git a/src/vertical.rs b/src/vertical.rs index 62cc7d8b33d..1859fe185d6 100644 --- a/src/vertical.rs +++ b/src/vertical.rs @@ -228,6 +228,7 @@ fn rewrite_aligned_items_inner( .collect::>(); let tactic = definitive_tactic( + context.config.version(), &items, ListTactic::HorizontalVertical, Separator::Comma, diff --git a/tests/source/unicode.rs b/tests/source/unicode/version-two.rs similarity index 77% rename from tests/source/unicode.rs rename to tests/source/unicode/version-two.rs index 4c2119af575..f685985ec41 100644 --- a/tests/source/unicode.rs +++ b/tests/source/unicode/version-two.rs @@ -1,3 +1,4 @@ +// rustfmt-version: Two // rustfmt-wrap_comments: true fn foo() { @@ -31,3 +32,19 @@ fn baz() { NotUnicode(_) => Err(e).chain_err(|| "env var was very very very bork文字化ã"), }); } + +// issue #3721 +mod test { + fn test() { + let a = vec![ + ( + Datum::Bytes("数据库".as_bytes().to_vec()), + Datum::I64(230), + ), + ( + Datum::Bytes("Αθήνα".as_bytes().to_vec()), + Datum::I64(206), + ), + ]; + } +} diff --git a/tests/target/unicode.rs b/tests/target/unicode/version-two.rs similarity index 81% rename from tests/target/unicode.rs rename to tests/target/unicode/version-two.rs index 34a4f463479..0c839b33741 100644 --- a/tests/target/unicode.rs +++ b/tests/target/unicode/version-two.rs @@ -1,3 +1,4 @@ +// rustfmt-version: Two // rustfmt-wrap_comments: true fn foo() { @@ -28,3 +29,13 @@ fn baz() { NotUnicode(_) => Err(e).chain_err(|| "env var was very very very bork文字化ã"), }); } + +// issue #3721 +mod test { + fn test() { + let a = vec![ + (Datum::Bytes("数据库".as_bytes().to_vec()), Datum::I64(230)), + (Datum::Bytes("Αθήνα".as_bytes().to_vec()), Datum::I64(206)), + ]; + } +}