refactor: switch from 'version' to 'style_edition'

Updates the relevant formatting logic to utilize the
new 'style_edition' option directly instead of the now
deprecated 'version' option. 'version' has only been
soft deprecated and has auto mapping in place so there
should be zero formatting impact to current 'version' users.
This commit is contained in:
Caleb Cartwright 2024-07-19 18:21:53 -05:00 committed by Caleb Cartwright
parent c2c9be5b2d
commit 7c41e2bfba
15 changed files with 138 additions and 92 deletions

View File

@ -62,7 +62,7 @@
use rustc_span::{symbol, BytePos, Span}; use rustc_span::{symbol, BytePos, Span};
use crate::comment::{rewrite_comment, CharClasses, FullCodeCharKind, RichChar}; use crate::comment::{rewrite_comment, CharClasses, FullCodeCharKind, RichChar};
use crate::config::{IndentStyle, Version}; use crate::config::{IndentStyle, StyleEdition};
use crate::expr::rewrite_call; use crate::expr::rewrite_call;
use crate::lists::extract_pre_comment; use crate::lists::extract_pre_comment;
use crate::macros::convert_try_mac; use crate::macros::convert_try_mac;
@ -285,7 +285,7 @@ fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String>
ChainItemKind::StructField(ident) => format!(".{}", rewrite_ident(context, ident)), ChainItemKind::StructField(ident) => format!(".{}", rewrite_ident(context, ident)),
ChainItemKind::TupleField(ident, nested) => format!( ChainItemKind::TupleField(ident, nested) => format!(
"{}.{}", "{}.{}",
if nested && context.config.version() == Version::One { if nested && context.config.style_edition() <= StyleEdition::Edition2021 {
" " " "
} else { } else {
"" ""

View File

@ -4,7 +4,7 @@
use crate::attr::get_attrs_from_stmt; use crate::attr::get_attrs_from_stmt;
use crate::config::lists::*; use crate::config::lists::*;
use crate::config::Version; use crate::config::StyleEdition;
use crate::expr::{block_contains_comment, is_simple_block, is_unsafe_block, rewrite_cond}; use crate::expr::{block_contains_comment, is_simple_block, is_unsafe_block, rewrite_cond};
use crate::items::{span_hi_for_param, span_lo_for_param}; use crate::items::{span_hi_for_param, span_lo_for_param};
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator}; use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
@ -457,18 +457,18 @@ fn is_block_closure_forced(context: &RewriteContext<'_>, expr: &ast::Expr) -> bo
if context.inside_macro() { if context.inside_macro() {
false false
} else { } else {
is_block_closure_forced_inner(expr, context.config.version()) is_block_closure_forced_inner(expr, context.config.style_edition())
} }
} }
fn is_block_closure_forced_inner(expr: &ast::Expr, version: Version) -> bool { fn is_block_closure_forced_inner(expr: &ast::Expr, style_edition: StyleEdition) -> bool {
match expr.kind { match expr.kind {
ast::ExprKind::If(..) | ast::ExprKind::While(..) | ast::ExprKind::ForLoop { .. } => true, ast::ExprKind::If(..) | ast::ExprKind::While(..) | ast::ExprKind::ForLoop { .. } => true,
ast::ExprKind::Loop(..) if version == Version::Two => true, ast::ExprKind::Loop(..) if style_edition >= StyleEdition::Edition2024 => true,
ast::ExprKind::AddrOf(_, _, ref expr) ast::ExprKind::AddrOf(_, _, ref expr)
| ast::ExprKind::Try(ref expr) | ast::ExprKind::Try(ref expr)
| ast::ExprKind::Unary(_, ref expr) | ast::ExprKind::Unary(_, ref expr)
| ast::ExprKind::Cast(ref expr, _) => is_block_closure_forced_inner(expr, version), | ast::ExprKind::Cast(ref expr, _) => is_block_closure_forced_inner(expr, style_edition),
_ => false, _ => false,
} }
} }

View File

@ -13,7 +13,7 @@
rewrite_missing_comment, CharClasses, FindUncommented, rewrite_missing_comment, CharClasses, FindUncommented,
}; };
use crate::config::lists::*; use crate::config::lists::*;
use crate::config::{Config, ControlBraceStyle, HexLiteralCase, IndentStyle, Version}; use crate::config::{Config, ControlBraceStyle, HexLiteralCase, IndentStyle, StyleEdition};
use crate::lists::{ use crate::lists::{
definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape, definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape,
struct_lit_tactic, write_list, ListFormatting, Separator, struct_lit_tactic, write_list, ListFormatting, Separator,
@ -1282,7 +1282,7 @@ fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) ->
.lines() .lines()
.dropping_back(1) .dropping_back(1)
.all(|line| line.ends_with('\\')) .all(|line| line.ends_with('\\'))
&& context.config.version() == Version::Two && context.config.style_edition() >= StyleEdition::Edition2024
{ {
return Some(string_lit.to_owned()); return Some(string_lit.to_owned());
} else { } else {

View File

@ -15,7 +15,7 @@
use crate::comment::combine_strs_with_missing_comments; use crate::comment::combine_strs_with_missing_comments;
use crate::config::lists::*; use crate::config::lists::*;
use crate::config::ImportGranularity; use crate::config::ImportGranularity;
use crate::config::{Edition, IndentStyle, Version}; use crate::config::{Edition, IndentStyle, StyleEdition};
use crate::lists::{ use crate::lists::{
definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator, definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator,
}; };
@ -104,7 +104,7 @@ pub(crate) enum UseSegmentKind {
#[derive(Clone, Eq, PartialEq)] #[derive(Clone, Eq, PartialEq)]
pub(crate) struct UseSegment { pub(crate) struct UseSegment {
pub(crate) kind: UseSegmentKind, pub(crate) kind: UseSegmentKind,
pub(crate) version: Version, pub(crate) style_edition: StyleEdition,
} }
#[derive(Clone)] #[derive(Clone)]
@ -149,7 +149,7 @@ fn remove_alias(&self) -> UseSegment {
}; };
UseSegment { UseSegment {
kind, kind,
version: self.version, style_edition: self.style_edition,
} }
} }
@ -197,7 +197,7 @@ fn from_path_segment(
Some(UseSegment { Some(UseSegment {
kind, kind,
version: context.config.version(), style_edition: context.config.style_edition(),
}) })
} }
@ -444,18 +444,21 @@ fn from_ast(
} }
} }
let version = context.config.version(); let style_edition = context.config.style_edition();
match a.kind { match a.kind {
UseTreeKind::Glob => { UseTreeKind::Glob => {
// in case of a global path and the glob starts at the root, e.g., "::*" // in case of a global path and the glob starts at the root, e.g., "::*"
if a.prefix.segments.len() == 1 && leading_modsep { if a.prefix.segments.len() == 1 && leading_modsep {
let kind = UseSegmentKind::Ident("".to_owned(), None); let kind = UseSegmentKind::Ident("".to_owned(), None);
result.path.push(UseSegment { kind, version }); result.path.push(UseSegment {
kind,
style_edition,
});
} }
result.path.push(UseSegment { result.path.push(UseSegment {
kind: UseSegmentKind::Glob, kind: UseSegmentKind::Glob,
version, style_edition,
}); });
} }
UseTreeKind::Nested { UseTreeKind::Nested {
@ -480,7 +483,10 @@ fn from_ast(
// e.g., "::{foo, bar}" // e.g., "::{foo, bar}"
if a.prefix.segments.len() == 1 && leading_modsep { if a.prefix.segments.len() == 1 && leading_modsep {
let kind = UseSegmentKind::Ident("".to_owned(), None); let kind = UseSegmentKind::Ident("".to_owned(), None);
result.path.push(UseSegment { kind, version }); result.path.push(UseSegment {
kind,
style_edition,
});
} }
let kind = UseSegmentKind::List( let kind = UseSegmentKind::List(
list.iter() list.iter()
@ -490,7 +496,10 @@ fn from_ast(
}) })
.collect(), .collect(),
); );
result.path.push(UseSegment { kind, version }); result.path.push(UseSegment {
kind,
style_edition,
});
} }
UseTreeKind::Simple(ref rename) => { UseTreeKind::Simple(ref rename) => {
// If the path has leading double colons and is composed of only 2 segments, then we // If the path has leading double colons and is composed of only 2 segments, then we
@ -519,7 +528,10 @@ fn from_ast(
_ => UseSegmentKind::Ident(name, alias), _ => UseSegmentKind::Ident(name, alias),
}; };
let segment = UseSegment { kind, version }; let segment = UseSegment {
kind,
style_edition,
};
// `name` is already in result. // `name` is already in result.
result.path.pop(); result.path.pop();
@ -614,7 +626,7 @@ pub(crate) fn normalize(mut self) -> UseTree {
list.sort(); list.sort();
last = UseSegment { last = UseSegment {
kind: UseSegmentKind::List(list), kind: UseSegmentKind::List(list),
version: last.version, style_edition: last.style_edition,
}; };
} }
@ -732,9 +744,12 @@ fn nest_trailing_self(mut self) -> UseTree {
}) = self.path.last() }) = self.path.last()
{ {
let self_segment = self.path.pop().unwrap(); let self_segment = self.path.pop().unwrap();
let version = self_segment.version; let style_edition = self_segment.style_edition;
let kind = UseSegmentKind::List(vec![UseTree::from_path(vec![self_segment], DUMMY_SP)]); let kind = UseSegmentKind::List(vec![UseTree::from_path(vec![self_segment], DUMMY_SP)]);
self.path.push(UseSegment { kind, version }); self.path.push(UseSegment {
kind,
style_edition,
});
} }
self self
} }
@ -750,7 +765,7 @@ fn merge_rest(
return None; return None;
} }
if a.len() != len && b.len() != len { if a.len() != len && b.len() != len {
let version = a[len].version; let style_edition = a[len].style_edition;
if let UseSegmentKind::List(ref list) = a[len].kind { if let UseSegmentKind::List(ref list) = a[len].kind {
let mut list = list.clone(); let mut list = list.clone();
merge_use_trees_inner( merge_use_trees_inner(
@ -760,7 +775,10 @@ fn merge_rest(
); );
let mut new_path = b[..len].to_vec(); let mut new_path = b[..len].to_vec();
let kind = UseSegmentKind::List(list); let kind = UseSegmentKind::List(list);
new_path.push(UseSegment { kind, version }); new_path.push(UseSegment {
kind,
style_edition,
});
return Some(new_path); return Some(new_path);
} }
} else if len == 1 { } else if len == 1 {
@ -770,9 +788,12 @@ fn merge_rest(
(&b[0], &a[1..]) (&b[0], &a[1..])
}; };
let kind = UseSegmentKind::Slf(common.get_alias().map(ToString::to_string)); let kind = UseSegmentKind::Slf(common.get_alias().map(ToString::to_string));
let version = a[0].version; let style_edition = a[0].style_edition;
let mut list = vec![UseTree::from_path( let mut list = vec![UseTree::from_path(
vec![UseSegment { kind, version }], vec![UseSegment {
kind,
style_edition,
}],
DUMMY_SP, DUMMY_SP,
)]; )];
match rest { match rest {
@ -788,7 +809,7 @@ fn merge_rest(
b[0].clone(), b[0].clone(),
UseSegment { UseSegment {
kind: UseSegmentKind::List(list), kind: UseSegmentKind::List(list),
version, style_edition,
}, },
]); ]);
} else { } else {
@ -801,8 +822,11 @@ fn merge_rest(
list.sort(); list.sort();
let mut new_path = b[..len].to_vec(); let mut new_path = b[..len].to_vec();
let kind = UseSegmentKind::List(list); let kind = UseSegmentKind::List(list);
let version = a[0].version; let style_edition = a[0].style_edition;
new_path.push(UseSegment { kind, version }); new_path.push(UseSegment {
kind,
style_edition,
});
Some(new_path) Some(new_path)
} }
@ -892,7 +916,7 @@ fn is_upper_snake_case(s: &str) -> bool {
| (Super(ref a), Super(ref b)) | (Super(ref a), Super(ref b))
| (Crate(ref a), Crate(ref b)) => match (a, b) { | (Crate(ref a), Crate(ref b)) => match (a, b) {
(Some(sa), Some(sb)) => { (Some(sa), Some(sb)) => {
if self.version == Version::Two { if self.style_edition >= StyleEdition::Edition2024 {
sa.trim_start_matches("r#").cmp(sb.trim_start_matches("r#")) sa.trim_start_matches("r#").cmp(sb.trim_start_matches("r#"))
} else { } else {
a.cmp(b) a.cmp(b)
@ -902,7 +926,7 @@ fn is_upper_snake_case(s: &str) -> bool {
}, },
(Glob, Glob) => Ordering::Equal, (Glob, Glob) => Ordering::Equal,
(Ident(ref pia, ref aa), Ident(ref pib, ref ab)) => { (Ident(ref pia, ref aa), Ident(ref pib, ref ab)) => {
let (ia, ib) = if self.version == Version::Two { let (ia, ib) = if self.style_edition >= StyleEdition::Edition2024 {
(pia.trim_start_matches("r#"), pib.trim_start_matches("r#")) (pia.trim_start_matches("r#"), pib.trim_start_matches("r#"))
} else { } else {
(pia.as_str(), pib.as_str()) (pia.as_str(), pib.as_str())
@ -928,7 +952,7 @@ fn is_upper_snake_case(s: &str) -> bool {
(None, Some(_)) => Ordering::Less, (None, Some(_)) => Ordering::Less,
(Some(_), None) => Ordering::Greater, (Some(_), None) => Ordering::Greater,
(Some(aas), Some(abs)) => { (Some(aas), Some(abs)) => {
if self.version == Version::Two { if self.style_edition >= StyleEdition::Edition2024 {
aas.trim_start_matches("r#") aas.trim_start_matches("r#")
.cmp(abs.trim_start_matches("r#")) .cmp(abs.trim_start_matches("r#"))
} else { } else {
@ -1114,7 +1138,7 @@ fn parse_use_tree(s: &str) -> UseTree {
struct Parser<'a> { struct Parser<'a> {
input: Peekable<Chars<'a>>, input: Peekable<Chars<'a>>,
version: Version, style_edition: StyleEdition,
} }
impl<'a> Parser<'a> { impl<'a> Parser<'a> {
@ -1132,7 +1156,7 @@ fn push_segment(
buf: &mut String, buf: &mut String,
alias_buf: &mut Option<String>, alias_buf: &mut Option<String>,
) { ) {
let version = self.version; let style_edition = self.style_edition;
if !buf.is_empty() { if !buf.is_empty() {
let mut alias = None; let mut alias = None;
swap(alias_buf, &mut alias); swap(alias_buf, &mut alias);
@ -1140,19 +1164,28 @@ fn push_segment(
match buf.as_ref() { match buf.as_ref() {
"self" => { "self" => {
let kind = UseSegmentKind::Slf(alias); let kind = UseSegmentKind::Slf(alias);
result.push(UseSegment { kind, version }); result.push(UseSegment {
kind,
style_edition,
});
*buf = String::new(); *buf = String::new();
*alias_buf = None; *alias_buf = None;
} }
"super" => { "super" => {
let kind = UseSegmentKind::Super(alias); let kind = UseSegmentKind::Super(alias);
result.push(UseSegment { kind, version }); result.push(UseSegment {
kind,
style_edition,
});
*buf = String::new(); *buf = String::new();
*alias_buf = None; *alias_buf = None;
} }
"crate" => { "crate" => {
let kind = UseSegmentKind::Crate(alias); let kind = UseSegmentKind::Crate(alias);
result.push(UseSegment { kind, version }); result.push(UseSegment {
kind,
style_edition,
});
*buf = String::new(); *buf = String::new();
*alias_buf = None; *alias_buf = None;
} }
@ -1160,7 +1193,10 @@ fn push_segment(
let mut name = String::new(); let mut name = String::new();
swap(buf, &mut name); swap(buf, &mut name);
let kind = UseSegmentKind::Ident(name, alias); let kind = UseSegmentKind::Ident(name, alias);
result.push(UseSegment { kind, version }); result.push(UseSegment {
kind,
style_edition,
});
} }
} }
} }
@ -1178,7 +1214,7 @@ fn parse_in_list(&mut self) -> UseTree {
let kind = UseSegmentKind::List(self.parse_list()); let kind = UseSegmentKind::List(self.parse_list());
result.push(UseSegment { result.push(UseSegment {
kind, kind,
version: self.version, style_edition: self.style_edition,
}); });
self.eat('}'); self.eat('}');
} }
@ -1188,7 +1224,7 @@ fn parse_in_list(&mut self) -> UseTree {
let kind = UseSegmentKind::Glob; let kind = UseSegmentKind::Glob;
result.push(UseSegment { result.push(UseSegment {
kind, kind,
version: self.version, style_edition: self.style_edition,
}); });
} }
':' => { ':' => {
@ -1249,7 +1285,7 @@ fn parse_list(&mut self) -> Vec<UseTree> {
let mut parser = Parser { let mut parser = Parser {
input: s.chars().peekable(), input: s.chars().peekable(),
version: Version::One, style_edition: StyleEdition::Edition2015,
}; };
parser.parse_in_list() parser.parse_in_list()
} }

View File

@ -15,7 +15,7 @@
FindUncommented, FindUncommented,
}; };
use crate::config::lists::*; use crate::config::lists::*;
use crate::config::{BraceStyle, Config, IndentStyle, Version}; use crate::config::{BraceStyle, Config, IndentStyle, StyleEdition};
use crate::expr::{ use crate::expr::{
is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_with, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_with,
rewrite_assign_rhs_with_comments, rewrite_else_kw_with_comments, rewrite_let_else_block, rewrite_assign_rhs_with_comments, rewrite_else_kw_with_comments, rewrite_let_else_block,
@ -145,7 +145,8 @@ fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteR
let else_kw_span = init.span.between(block.span); let else_kw_span = init.span.between(block.span);
// Strip attributes and comments to check if newline is needed before the else // Strip attributes and comments to check if newline is needed before the else
// keyword from the initializer part. (#5901) // keyword from the initializer part. (#5901)
let init_str = if context.config.version() == Version::Two { let style_edition = context.config.style_edition();
let init_str = if style_edition >= StyleEdition::Edition2024 {
&result[let_kw_offset..] &result[let_kw_offset..]
} else { } else {
result.as_str() result.as_str()
@ -169,7 +170,8 @@ fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteR
std::cmp::min(shape.width, context.config.single_line_let_else_max_width()); std::cmp::min(shape.width, context.config.single_line_let_else_max_width());
// If available_space hits zero we know for sure this will be a multi-lined block // If available_space hits zero we know for sure this will be a multi-lined block
let assign_str_with_else_kw = if context.config.version() == Version::Two { let style_edition = context.config.style_edition();
let assign_str_with_else_kw = if style_edition >= StyleEdition::Edition2024 {
&result[let_kw_offset..] &result[let_kw_offset..]
} else { } else {
result.as_str() result.as_str()
@ -675,10 +677,10 @@ fn format_variant(
let context = self.get_context(); let context = self.get_context();
let shape = self.shape(); let shape = self.shape();
let attrs_str = if context.config.version() == Version::Two { let attrs_str = if context.config.style_edition() >= StyleEdition::Edition2024 {
field.attrs.rewrite(&context, shape)? field.attrs.rewrite(&context, shape)?
} else { } else {
// Version::One formatting that was off by 1. See issue #5801 // StyleEdition::Edition20{15|18|21} formatting that was off by 1. See issue #5801
field.attrs.rewrite(&context, shape.sub_width(1)?)? field.attrs.rewrite(&context, shape.sub_width(1)?)?
}; };
// sub_width(1) to take the trailing comma into account // sub_width(1) to take the trailing comma into account
@ -966,7 +968,7 @@ fn format_impl_ref_and_type(
result.push_str(format_defaultness(defaultness)); result.push_str(format_defaultness(defaultness));
result.push_str(format_safety(safety)); result.push_str(format_safety(safety));
let shape = if context.config.version() == Version::Two { let shape = if context.config.style_edition() >= StyleEdition::Edition2024 {
Shape::indented(offset + last_line_width(&result), context.config) Shape::indented(offset + last_line_width(&result), context.config)
} else { } else {
generics_shape_from_config( generics_shape_from_config(
@ -2107,7 +2109,7 @@ fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteR
ast::FnRetTy::Default(_) => Ok(String::new()), ast::FnRetTy::Default(_) => Ok(String::new()),
ast::FnRetTy::Ty(ref ty) => { ast::FnRetTy::Ty(ref ty) => {
let arrow_width = "-> ".len(); let arrow_width = "-> ".len();
if context.config.version() == Version::One if context.config.style_edition() <= StyleEdition::Edition2021
|| context.config.indent_style() == IndentStyle::Visual || context.config.indent_style() == IndentStyle::Visual
{ {
let inner_width = shape let inner_width = shape
@ -2510,7 +2512,7 @@ fn rewrite_fn_base(
.last() .last()
.map_or(false, |last_line| last_line.contains("//")); .map_or(false, |last_line| last_line.contains("//"));
if context.config.version() == Version::Two { if context.config.style_edition() >= StyleEdition::Edition2024 {
if closing_paren_overflow_max_width { if closing_paren_overflow_max_width {
result.push(')'); result.push(')');
result.push_str(&indent.to_string_with_newline(context.config)); result.push_str(&indent.to_string_with_newline(context.config));
@ -2553,7 +2555,7 @@ fn rewrite_fn_base(
} }
}; };
let ret_shape = if ret_should_indent { let ret_shape = if ret_should_indent {
if context.config.version() == Version::One if context.config.style_edition() <= StyleEdition::Edition2021
|| context.config.indent_style() == IndentStyle::Visual || context.config.indent_style() == IndentStyle::Visual
{ {
let indent = if param_str.is_empty() { let indent = if param_str.is_empty() {
@ -2586,7 +2588,7 @@ fn rewrite_fn_base(
ret_shape ret_shape
} }
} else { } else {
if context.config.version() == Version::Two { if context.config.style_edition() >= StyleEdition::Edition2024 {
if !param_str.is_empty() || !no_params_and_over_max_width { if !param_str.is_empty() || !no_params_and_over_max_width {
result.push(' '); result.push(' ');
} }
@ -3071,7 +3073,7 @@ fn rewrite_bounds_on_where_clause(
DefinitiveListTactic::Vertical DefinitiveListTactic::Vertical
}; };
let preserve_newline = context.config.version() == Version::One; let preserve_newline = context.config.style_edition() <= StyleEdition::Edition2021;
let fmt = ListFormatting::new(shape, context.config) let fmt = ListFormatting::new(shape, context.config)
.tactic(shape_tactic) .tactic(shape_tactic)

View File

@ -48,7 +48,7 @@
pub use crate::config::{ pub use crate::config::{
load_config, CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName, NewlineStyle, load_config, CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName, NewlineStyle,
Range, Verbosity, Range, StyleEdition, Verbosity,
}; };
pub use crate::format_report_formatter::{FormatReportFormatter, FormatReportFormatterBuilder}; pub use crate::format_report_formatter::{FormatReportFormatter, FormatReportFormatterBuilder};

View File

@ -25,7 +25,7 @@
contains_comment, CharClasses, FindUncommented, FullCodeCharKind, LineClasses, contains_comment, CharClasses, FindUncommented, FullCodeCharKind, LineClasses,
}; };
use crate::config::lists::*; use crate::config::lists::*;
use crate::config::Version; use crate::config::StyleEdition;
use crate::expr::{rewrite_array, rewrite_assign_rhs, RhsAssignKind}; use crate::expr::{rewrite_array, rewrite_assign_rhs, RhsAssignKind};
use crate::lists::{itemize_list, write_list, ListFormatting}; use crate::lists::{itemize_list, write_list, ListFormatting};
use crate::overflow; use crate::overflow;
@ -1251,7 +1251,7 @@ fn rewrite(
let old_body = context.snippet(self.body).trim(); let old_body = context.snippet(self.body).trim();
let has_block_body = old_body.starts_with('{'); let has_block_body = old_body.starts_with('{');
let mut prefix_width = 5; // 5 = " => {" let mut prefix_width = 5; // 5 = " => {"
if context.config.version() == Version::Two { if context.config.style_edition() >= StyleEdition::Edition2024 {
if has_block_body { if has_block_body {
prefix_width = 6; // 6 = " => {{" prefix_width = 6; // 6 = " => {{"
} }

View File

@ -7,7 +7,7 @@
use crate::comment::{combine_strs_with_missing_comments, rewrite_comment, FindUncommented}; use crate::comment::{combine_strs_with_missing_comments, rewrite_comment, FindUncommented};
use crate::config::lists::*; use crate::config::lists::*;
use crate::config::{Config, ControlBraceStyle, IndentStyle, MatchArmLeadingPipe, Version}; use crate::config::{Config, ControlBraceStyle, IndentStyle, MatchArmLeadingPipe, StyleEdition};
use crate::expr::{ use crate::expr::{
format_expr, is_empty_block, is_simple_block, is_unsafe_block, prefer_next_line, rewrite_cond, format_expr, is_empty_block, is_simple_block, is_unsafe_block, prefer_next_line, rewrite_cond,
ExprType, RhsTactics, ExprType, RhsTactics,
@ -112,9 +112,10 @@ pub(crate) fn rewrite_match(
let inner_attrs_str = if inner_attrs.is_empty() { let inner_attrs_str = if inner_attrs.is_empty() {
String::new() String::new()
} else { } else {
let shape = match context.config.version() { let shape = if context.config.style_edition() <= StyleEdition::Edition2021 {
Version::One => shape, shape
_ => shape.block_indent(context.config.tab_spaces()), } else {
shape.block_indent(context.config.tab_spaces())
}; };
inner_attrs inner_attrs
.rewrite_result(context, shape) .rewrite_result(context, shape)
@ -437,7 +438,7 @@ fn rewrite_match_body(
let arrow_snippet = context.snippet(arrow_span).trim(); let arrow_snippet = context.snippet(arrow_span).trim();
// search for the arrow starting from the end of the snippet since there may be a match // search for the arrow starting from the end of the snippet since there may be a match
// expression within the guard // expression within the guard
let arrow_index = if context.config.version() == Version::One { let arrow_index = if context.config.style_edition() <= StyleEdition::Edition2021 {
arrow_snippet.rfind("=>").unwrap() arrow_snippet.rfind("=>").unwrap()
} else { } else {
arrow_snippet.find_last_uncommented("=>").unwrap() arrow_snippet.find_last_uncommented("=>").unwrap()
@ -475,7 +476,7 @@ fn rewrite_match_body(
} else { } else {
"" ""
}; };
let semicolon = if context.config.version() == Version::One { let semicolon = if context.config.style_edition() <= StyleEdition::Edition2021 {
"" ""
} else { } else {
if semicolon_for_expr(context, body) { if semicolon_for_expr(context, body) {

View File

@ -3,7 +3,7 @@
use crate::comment::{is_last_comment_block, rewrite_comment, CodeCharKind, CommentCodeSlices}; use crate::comment::{is_last_comment_block, rewrite_comment, CodeCharKind, CommentCodeSlices};
use crate::config::file_lines::FileLines; use crate::config::file_lines::FileLines;
use crate::config::FileName; use crate::config::FileName;
use crate::config::Version; use crate::config::StyleEdition;
use crate::coverage::transform_missing_snippet; use crate::coverage::transform_missing_snippet;
use crate::shape::{Indent, Shape}; use crate::shape::{Indent, Shape};
use crate::source_map::LineRangeUtils; use crate::source_map::LineRangeUtils;
@ -246,7 +246,9 @@ fn process_comment(
let indent_str = self.block_indent.to_string(self.config); let indent_str = self.block_indent.to_string(self.config);
self.push_str(&indent_str); self.push_str(&indent_str);
self.block_indent self.block_indent
} else if self.config.version() == Version::Two && !snippet.starts_with('\n') { } else if self.config.style_edition() >= StyleEdition::Edition2024
&& !snippet.starts_with('\n')
{
// The comment appears on the same line as the previous formatted code. // The comment appears on the same line as the previous formatted code.
// Assuming that comment is logically associated with that code, we want to keep it on // Assuming that comment is logically associated with that code, we want to keep it on
// the same level and avoid mixing it with possible other comment. // the same level and avoid mixing it with possible other comment.

View File

@ -8,7 +8,7 @@
use rustc_span::Span; use rustc_span::Span;
use crate::closures; use crate::closures;
use crate::config::Version; use crate::config::StyleEdition;
use crate::config::{lists::*, Config}; use crate::config::{lists::*, Config};
use crate::expr::{ use crate::expr::{
can_be_overflowed_expr, is_every_expr_simple, is_method_call, is_nested_call, is_simple_expr, can_be_overflowed_expr, is_every_expr_simple, is_method_call, is_nested_call, is_simple_expr,
@ -200,8 +200,8 @@ fn special_cases(&self, config: &Config) -> impl Iterator<Item = &(&'static str,
OverflowableItem::NestedMetaItem(..) => SPECIAL_CASE_ATTR, OverflowableItem::NestedMetaItem(..) => SPECIAL_CASE_ATTR,
_ => &[], _ => &[],
}; };
let additional_cases = match (self, config.version()) { let additional_cases = match (self, config.style_edition()) {
(OverflowableItem::MacroArg(..), Version::Two) => SPECIAL_CASE_MACROS_V2, (OverflowableItem::MacroArg(..), StyleEdition::Edition2024) => SPECIAL_CASE_MACROS_V2,
_ => &[], _ => &[],
}; };
base_cases.iter().chain(additional_cases) base_cases.iter().chain(additional_cases)
@ -494,7 +494,7 @@ fn try_overflow_last_item(&self, list_items: &mut Vec<ListItem>) -> DefinitiveLi
Some(OverflowableItem::MacroArg(MacroArg::Expr(expr))) Some(OverflowableItem::MacroArg(MacroArg::Expr(expr)))
if !combine_arg_with_callee if !combine_arg_with_callee
&& is_method_call(expr) && is_method_call(expr)
&& self.context.config.version() == Version::Two => && self.context.config.style_edition() >= StyleEdition::Edition2024 =>
{ {
self.context.force_one_line_chain.replace(true); self.context.force_one_line_chain.replace(true);
} }
@ -690,7 +690,8 @@ fn wrap_items(&self, items_str: &str, shape: Shape, is_extendable: bool) -> Stri
); );
result.push_str(self.ident); result.push_str(self.ident);
result.push_str(prefix); result.push_str(prefix);
let force_single_line = if self.context.config.version() == Version::Two { let force_single_line = if self.context.config.style_edition() >= StyleEdition::Edition2024
{
!self.context.use_block_indent() || (is_extendable && extend_width <= shape.width) !self.context.use_block_indent() || (is_extendable && extend_width <= shape.width)
} else { } else {
// 2 = `()` // 2 = `()`

View File

@ -4,7 +4,7 @@
use crate::comment::{combine_strs_with_missing_comments, FindUncommented}; use crate::comment::{combine_strs_with_missing_comments, FindUncommented};
use crate::config::lists::*; use crate::config::lists::*;
use crate::config::Version; use crate::config::StyleEdition;
use crate::expr::{can_be_overflowed_expr, rewrite_unary_prefix, wrap_struct_field}; use crate::expr::{can_be_overflowed_expr, rewrite_unary_prefix, wrap_struct_field};
use crate::lists::{ use crate::lists::{
definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape, definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape,
@ -280,7 +280,9 @@ fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String>
rewrite_tuple_pat(pat_vec, Some(path_str), self.span, context, shape).ok() rewrite_tuple_pat(pat_vec, Some(path_str), self.span, context, shape).ok()
} }
PatKind::Lit(ref expr) => expr.rewrite(context, shape), PatKind::Lit(ref expr) => expr.rewrite(context, shape),
PatKind::Slice(ref slice_pat) if context.config.version() == Version::One => { PatKind::Slice(ref slice_pat)
if context.config.style_edition() <= StyleEdition::Edition2021 =>
{
let rw: Vec<String> = slice_pat let rw: Vec<String> = slice_pat
.iter() .iter()
.map(|p| { .map(|p| {

View File

@ -2,7 +2,7 @@
use rustc_span::Span; use rustc_span::Span;
use crate::comment::recover_comment_removed; use crate::comment::recover_comment_removed;
use crate::config::Version; use crate::config::StyleEdition;
use crate::expr::{format_expr, is_simple_block, ExprType}; use crate::expr::{format_expr, is_simple_block, ExprType};
use crate::rewrite::{Rewrite, RewriteContext}; use crate::rewrite::{Rewrite, RewriteContext};
use crate::shape::Shape; use crate::shape::Shape;
@ -90,7 +90,8 @@ fn is_last_expr(&self) -> bool {
impl<'a> Rewrite for Stmt<'a> { impl<'a> Rewrite for Stmt<'a> {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> { fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
let expr_type = if context.config.version() == Version::Two && self.is_last_expr() { let expr_type =
if context.config.style_edition() >= StyleEdition::Edition2024 && self.is_last_expr() {
ExprType::SubExpression ExprType::SubExpression
} else { } else {
ExprType::Statement ExprType::Statement

View File

@ -6,7 +6,7 @@
use crate::comment::{combine_strs_with_missing_comments, contains_comment}; use crate::comment::{combine_strs_with_missing_comments, contains_comment};
use crate::config::lists::*; use crate::config::lists::*;
use crate::config::{IndentStyle, TypeDensity, Version}; use crate::config::{IndentStyle, StyleEdition, TypeDensity};
use crate::expr::{ use crate::expr::{
format_expr, rewrite_assign_rhs, rewrite_call, rewrite_tuple, rewrite_unary_prefix, ExprType, format_expr, rewrite_assign_rhs, rewrite_call, rewrite_tuple, rewrite_unary_prefix, ExprType,
RhsAssignKind, RhsAssignKind,
@ -888,7 +888,7 @@ fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteR
// FIXME: we drop any comments here, even though it's a silly place to put // FIXME: we drop any comments here, even though it's a silly place to put
// comments. // comments.
ast::TyKind::Paren(ref ty) => { ast::TyKind::Paren(ref ty) => {
if context.config.version() == Version::One if context.config.style_edition() <= StyleEdition::Edition2021
|| context.config.indent_style() == IndentStyle::Visual || context.config.indent_style() == IndentStyle::Visual
{ {
let budget = shape let budget = shape
@ -966,7 +966,7 @@ fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteR
if it.is_empty() { if it.is_empty() {
return Ok("impl".to_owned()); return Ok("impl".to_owned());
} }
let rw = if context.config.version() == Version::One { let rw = if context.config.style_edition() <= StyleEdition::Edition2021 {
it.rewrite_result(context, shape) it.rewrite_result(context, shape)
} else { } else {
join_bounds(context, shape, it, false) join_bounds(context, shape, it, false)
@ -1212,16 +1212,16 @@ fn join_bounds_inner(
// and either there is more than one item; // and either there is more than one item;
// or the single item is of type `Trait`, // or the single item is of type `Trait`,
// and any of the internal arrays contains more than one item; // and any of the internal arrays contains more than one item;
let retry_with_force_newline = match context.config.version() { let retry_with_force_newline = match context.config.style_edition() {
Version::One => { style_edition @ _ if style_edition <= StyleEdition::Edition2021 => {
!force_newline !force_newline
&& items.len() > 1 && items.len() > 1
&& (result.0.contains('\n') || result.0.len() > shape.width) && (result.0.contains('\n') || result.0.len() > shape.width)
} }
Version::Two if force_newline => false, _ if force_newline => false,
Version::Two if (!result.0.contains('\n') && result.0.len() <= shape.width) => false, _ if (!result.0.contains('\n') && result.0.len() <= shape.width) => false,
Version::Two if items.len() > 1 => true, _ if items.len() > 1 => true,
Version::Two => is_item_with_multi_items_array(&items[0]), _ => is_item_with_multi_items_array(&items[0]),
}; };
if retry_with_force_newline { if retry_with_force_newline {

View File

@ -10,7 +10,7 @@
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
use crate::comment::{filter_normal_code, CharClasses, FullCodeCharKind, LineClasses}; use crate::comment::{filter_normal_code, CharClasses, FullCodeCharKind, LineClasses};
use crate::config::{Config, Version}; use crate::config::{Config, StyleEdition};
use crate::rewrite::RewriteContext; use crate::rewrite::RewriteContext;
use crate::shape::{Indent, Shape}; use crate::shape::{Indent, Shape};
@ -604,7 +604,7 @@ pub(crate) fn trim_left_preserve_layout(
// just InString{Commented} in order to allow the start of a string to be indented // just InString{Commented} in order to allow the start of a string to be indented
let new_veto_trim_value = (kind == FullCodeCharKind::InString let new_veto_trim_value = (kind == FullCodeCharKind::InString
|| (config.version() == Version::Two || (config.style_edition() >= StyleEdition::Edition2024
&& kind == FullCodeCharKind::InStringCommented)) && kind == FullCodeCharKind::InStringCommented))
&& !line.ends_with('\\'); && !line.ends_with('\\');
let line = if veto_trim || new_veto_trim_value { let line = if veto_trim || new_veto_trim_value {
@ -620,7 +620,7 @@ pub(crate) fn trim_left_preserve_layout(
// such lines should not be taken into account when computing the minimum. // such lines should not be taken into account when computing the minimum.
match kind { match kind {
FullCodeCharKind::InStringCommented | FullCodeCharKind::EndStringCommented FullCodeCharKind::InStringCommented | FullCodeCharKind::EndStringCommented
if config.version() == Version::Two => if config.style_edition() >= StyleEdition::Edition2024 =>
{ {
None None
} }
@ -664,7 +664,7 @@ pub(crate) fn indent_next_line(kind: FullCodeCharKind, line: &str, config: &Conf
// formatting the code block, therefore the string's indentation needs // formatting the code block, therefore the string's indentation needs
// to be adjusted for the code surrounding the code block. // to be adjusted for the code surrounding the code block.
config.format_strings() && line.ends_with('\\') config.format_strings() && line.ends_with('\\')
} else if config.version() == Version::Two { } else if config.style_edition() >= StyleEdition::Edition2024 {
!kind.is_commented_string() !kind.is_commented_string()
} else { } else {
true true

View File

@ -7,8 +7,7 @@
use crate::attr::*; use crate::attr::*;
use crate::comment::{contains_comment, rewrite_comment, CodeCharKind, CommentCodeSlices}; use crate::comment::{contains_comment, rewrite_comment, CodeCharKind, CommentCodeSlices};
use crate::config::Version; use crate::config::{BraceStyle, Config, MacroSelector, StyleEdition};
use crate::config::{BraceStyle, Config, MacroSelector};
use crate::coverage::transform_missing_snippet; use crate::coverage::transform_missing_snippet;
use crate::items::{ use crate::items::{
format_impl, format_trait, format_trait_alias, is_mod_decl, is_use_item, rewrite_extern_crate, format_impl, format_trait, format_trait_alias, is_mod_decl, is_use_item, rewrite_extern_crate,
@ -290,7 +289,9 @@ fn close_block(&mut self, span: Span, unindent_comment: bool) {
let mut comment_shape = let mut comment_shape =
Shape::indented(self.block_indent, config).comment(config); Shape::indented(self.block_indent, config).comment(config);
if self.config.version() == Version::Two && comment_on_same_line { if self.config.style_edition() >= StyleEdition::Edition2024
&& comment_on_same_line
{
self.push_str(" "); self.push_str(" ");
// put the first line of the comment on the same line as the // put the first line of the comment on the same line as the
// block's last line // block's last line