Skip to content

version-gate the changes from #3618 and #3632 #3730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 8 additions & 2 deletions src/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,7 @@ pub(crate) fn rewrite_multiple_patterns(
DefinitiveListTactic::Mixed
} else {
definitive_tactic(
context.config.version(),
&items,
ListTactic::HorizontalVertical,
Separator::VerticalBar,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1910,7 +1912,12 @@ fn choose_rhs<R: Rewrite>(
) -> Option<String> {
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))
}
Expand Down
1 change: 1 addition & 0 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,7 @@ fn rewrite_nested_use_tree(
};

let tactic = definitive_tactic(
context.config.version(),
&list_items,
context.config.imports_layout(),
Separator::Comma,
Expand Down
9 changes: 8 additions & 1 deletion src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2360,6 +2360,7 @@ fn rewrite_args(
.collect();

let tactic = definitive_tactic(
context.config.version(),
&arg_items,
context
.config
Expand Down Expand Up @@ -2706,7 +2707,13 @@ fn rewrite_where_clause(
);
let item_vec = items.collect::<Vec<_>>();
// 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.
Expand Down
41 changes: 31 additions & 10 deletions src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -223,6 +223,7 @@ impl Separator {
}

pub(crate) fn definitive_tactic<I, T>(
version: Version,
items: I,
tactic: ListTactic,
sep: Separator,
Expand All @@ -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;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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(' ');
Expand Down Expand Up @@ -801,21 +810,27 @@ where
}

/// Returns the count and total width of the list items.
fn calculate_width<I, T>(items: I) -> (usize, usize)
fn calculate_width<I, T>(version: Version, items: I) -> (usize, usize)
where
I: IntoIterator<Item = T>,
T: AsRef<ListItem>,
{
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 {
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
7 changes: 6 additions & 1 deletion src/overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
14 changes: 10 additions & 4 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/vertical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
.collect::<Vec<_>>();

let tactic = definitive_tactic(
context.config.version(),
&items,
ListTactic::HorizontalVertical,
Separator::Comma,
Expand Down
17 changes: 17 additions & 0 deletions tests/source/unicode.rs → tests/source/unicode/version-two.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// rustfmt-version: Two
// rustfmt-wrap_comments: true

fn foo() {
Expand Down Expand Up @@ -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),
),
];
}
}
11 changes: 11 additions & 0 deletions tests/target/unicode.rs → tests/target/unicode/version-two.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// rustfmt-version: Two
// rustfmt-wrap_comments: true

fn foo() {
Expand Down Expand Up @@ -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)),
];
}
}