Skip to content

Commit 5438b84

Browse files
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.
1 parent 53ef93c commit 5438b84

15 files changed

+138
-92
lines changed

Diff for: src/chains.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use rustc_ast::{ast, ptr};
6262
use rustc_span::{symbol, BytePos, Span};
6363

6464
use crate::comment::{rewrite_comment, CharClasses, FullCodeCharKind, RichChar};
65-
use crate::config::{IndentStyle, Version};
65+
use crate::config::{IndentStyle, StyleEdition};
6666
use crate::expr::rewrite_call;
6767
use crate::lists::extract_pre_comment;
6868
use crate::macros::convert_try_mac;
@@ -284,7 +284,7 @@ impl Rewrite for ChainItem {
284284
ChainItemKind::StructField(ident) => format!(".{}", rewrite_ident(context, ident)),
285285
ChainItemKind::TupleField(ident, nested) => format!(
286286
"{}.{}",
287-
if nested && context.config.version() == Version::One {
287+
if nested && context.config.style_edition() <= StyleEdition::Edition2021 {
288288
" "
289289
} else {
290290
""

Diff for: src/closures.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use thin_vec::thin_vec;
44

55
use crate::attr::get_attrs_from_stmt;
66
use crate::config::lists::*;
7-
use crate::config::Version;
7+
use crate::config::StyleEdition;
88
use crate::expr::{block_contains_comment, is_simple_block, is_unsafe_block, rewrite_cond};
99
use crate::items::{span_hi_for_param, span_lo_for_param};
1010
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
457457
if context.inside_macro() {
458458
false
459459
} else {
460-
is_block_closure_forced_inner(expr, context.config.version())
460+
is_block_closure_forced_inner(expr, context.config.style_edition())
461461
}
462462
}
463463

464-
fn is_block_closure_forced_inner(expr: &ast::Expr, version: Version) -> bool {
464+
fn is_block_closure_forced_inner(expr: &ast::Expr, style_edition: StyleEdition) -> bool {
465465
match expr.kind {
466466
ast::ExprKind::If(..) | ast::ExprKind::While(..) | ast::ExprKind::ForLoop { .. } => true,
467-
ast::ExprKind::Loop(..) if version == Version::Two => true,
467+
ast::ExprKind::Loop(..) if style_edition >= StyleEdition::Edition2024 => true,
468468
ast::ExprKind::AddrOf(_, _, ref expr)
469469
| ast::ExprKind::Try(ref expr)
470470
| ast::ExprKind::Unary(_, ref expr)
471-
| ast::ExprKind::Cast(ref expr, _) => is_block_closure_forced_inner(expr, version),
471+
| ast::ExprKind::Cast(ref expr, _) => is_block_closure_forced_inner(expr, style_edition),
472472
_ => false,
473473
}
474474
}

Diff for: src/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::comment::{
1313
rewrite_missing_comment, CharClasses, FindUncommented,
1414
};
1515
use crate::config::lists::*;
16-
use crate::config::{Config, ControlBraceStyle, HexLiteralCase, IndentStyle, Version};
16+
use crate::config::{Config, ControlBraceStyle, HexLiteralCase, IndentStyle, StyleEdition};
1717
use crate::lists::{
1818
definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape,
1919
struct_lit_tactic, write_list, ListFormatting, Separator,
@@ -1276,7 +1276,7 @@ fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) ->
12761276
.lines()
12771277
.dropping_back(1)
12781278
.all(|line| line.ends_with('\\'))
1279-
&& context.config.version() == Version::Two
1279+
&& context.config.style_edition() >= StyleEdition::Edition2024
12801280
{
12811281
return Some(string_lit.to_owned());
12821282
} else {

Diff for: src/imports.rs

+68-32
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_span::{
1515
use crate::comment::combine_strs_with_missing_comments;
1616
use crate::config::lists::*;
1717
use crate::config::ImportGranularity;
18-
use crate::config::{Edition, IndentStyle, Version};
18+
use crate::config::{Edition, IndentStyle, StyleEdition};
1919
use crate::lists::{
2020
definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator,
2121
};
@@ -104,7 +104,7 @@ pub(crate) enum UseSegmentKind {
104104
#[derive(Clone, Eq, PartialEq)]
105105
pub(crate) struct UseSegment {
106106
pub(crate) kind: UseSegmentKind,
107-
pub(crate) version: Version,
107+
pub(crate) style_edition: StyleEdition,
108108
}
109109

110110
#[derive(Clone)]
@@ -149,7 +149,7 @@ impl UseSegment {
149149
};
150150
UseSegment {
151151
kind,
152-
version: self.version,
152+
style_edition: self.style_edition,
153153
}
154154
}
155155

@@ -197,7 +197,7 @@ impl UseSegment {
197197

198198
Some(UseSegment {
199199
kind,
200-
version: context.config.version(),
200+
style_edition: context.config.style_edition(),
201201
})
202202
}
203203

@@ -444,18 +444,21 @@ impl UseTree {
444444
}
445445
}
446446

447-
let version = context.config.version();
447+
let style_edition = context.config.style_edition();
448448

449449
match a.kind {
450450
UseTreeKind::Glob => {
451451
// in case of a global path and the glob starts at the root, e.g., "::*"
452452
if a.prefix.segments.len() == 1 && leading_modsep {
453453
let kind = UseSegmentKind::Ident("".to_owned(), None);
454-
result.path.push(UseSegment { kind, version });
454+
result.path.push(UseSegment {
455+
kind,
456+
style_edition,
457+
});
455458
}
456459
result.path.push(UseSegment {
457460
kind: UseSegmentKind::Glob,
458-
version,
461+
style_edition,
459462
});
460463
}
461464
UseTreeKind::Nested {
@@ -480,7 +483,10 @@ impl UseTree {
480483
// e.g., "::{foo, bar}"
481484
if a.prefix.segments.len() == 1 && leading_modsep {
482485
let kind = UseSegmentKind::Ident("".to_owned(), None);
483-
result.path.push(UseSegment { kind, version });
486+
result.path.push(UseSegment {
487+
kind,
488+
style_edition,
489+
});
484490
}
485491
let kind = UseSegmentKind::List(
486492
list.iter()
@@ -490,7 +496,10 @@ impl UseTree {
490496
})
491497
.collect(),
492498
);
493-
result.path.push(UseSegment { kind, version });
499+
result.path.push(UseSegment {
500+
kind,
501+
style_edition,
502+
});
494503
}
495504
UseTreeKind::Simple(ref rename) => {
496505
// If the path has leading double colons and is composed of only 2 segments, then we
@@ -519,7 +528,10 @@ impl UseTree {
519528
_ => UseSegmentKind::Ident(name, alias),
520529
};
521530

522-
let segment = UseSegment { kind, version };
531+
let segment = UseSegment {
532+
kind,
533+
style_edition,
534+
};
523535

524536
// `name` is already in result.
525537
result.path.pop();
@@ -614,7 +626,7 @@ impl UseTree {
614626
list.sort();
615627
last = UseSegment {
616628
kind: UseSegmentKind::List(list),
617-
version: last.version,
629+
style_edition: last.style_edition,
618630
};
619631
}
620632

@@ -732,9 +744,12 @@ impl UseTree {
732744
}) = self.path.last()
733745
{
734746
let self_segment = self.path.pop().unwrap();
735-
let version = self_segment.version;
747+
let style_edition = self_segment.style_edition;
736748
let kind = UseSegmentKind::List(vec![UseTree::from_path(vec![self_segment], DUMMY_SP)]);
737-
self.path.push(UseSegment { kind, version });
749+
self.path.push(UseSegment {
750+
kind,
751+
style_edition,
752+
});
738753
}
739754
self
740755
}
@@ -750,7 +765,7 @@ fn merge_rest(
750765
return None;
751766
}
752767
if a.len() != len && b.len() != len {
753-
let version = a[len].version;
768+
let style_edition = a[len].style_edition;
754769
if let UseSegmentKind::List(ref list) = a[len].kind {
755770
let mut list = list.clone();
756771
merge_use_trees_inner(
@@ -760,7 +775,10 @@ fn merge_rest(
760775
);
761776
let mut new_path = b[..len].to_vec();
762777
let kind = UseSegmentKind::List(list);
763-
new_path.push(UseSegment { kind, version });
778+
new_path.push(UseSegment {
779+
kind,
780+
style_edition,
781+
});
764782
return Some(new_path);
765783
}
766784
} else if len == 1 {
@@ -770,9 +788,12 @@ fn merge_rest(
770788
(&b[0], &a[1..])
771789
};
772790
let kind = UseSegmentKind::Slf(common.get_alias().map(ToString::to_string));
773-
let version = a[0].version;
791+
let style_edition = a[0].style_edition;
774792
let mut list = vec![UseTree::from_path(
775-
vec![UseSegment { kind, version }],
793+
vec![UseSegment {
794+
kind,
795+
style_edition,
796+
}],
776797
DUMMY_SP,
777798
)];
778799
match rest {
@@ -788,7 +809,7 @@ fn merge_rest(
788809
b[0].clone(),
789810
UseSegment {
790811
kind: UseSegmentKind::List(list),
791-
version,
812+
style_edition,
792813
},
793814
]);
794815
} else {
@@ -801,8 +822,11 @@ fn merge_rest(
801822
list.sort();
802823
let mut new_path = b[..len].to_vec();
803824
let kind = UseSegmentKind::List(list);
804-
let version = a[0].version;
805-
new_path.push(UseSegment { kind, version });
825+
let style_edition = a[0].style_edition;
826+
new_path.push(UseSegment {
827+
kind,
828+
style_edition,
829+
});
806830
Some(new_path)
807831
}
808832

@@ -892,7 +916,7 @@ impl Ord for UseSegment {
892916
| (Super(ref a), Super(ref b))
893917
| (Crate(ref a), Crate(ref b)) => match (a, b) {
894918
(Some(sa), Some(sb)) => {
895-
if self.version == Version::Two {
919+
if self.style_edition >= StyleEdition::Edition2024 {
896920
sa.trim_start_matches("r#").cmp(sb.trim_start_matches("r#"))
897921
} else {
898922
a.cmp(b)
@@ -902,7 +926,7 @@ impl Ord for UseSegment {
902926
},
903927
(Glob, Glob) => Ordering::Equal,
904928
(Ident(ref pia, ref aa), Ident(ref pib, ref ab)) => {
905-
let (ia, ib) = if self.version == Version::Two {
929+
let (ia, ib) = if self.style_edition >= StyleEdition::Edition2024 {
906930
(pia.trim_start_matches("r#"), pib.trim_start_matches("r#"))
907931
} else {
908932
(pia.as_str(), pib.as_str())
@@ -928,7 +952,7 @@ impl Ord for UseSegment {
928952
(None, Some(_)) => Ordering::Less,
929953
(Some(_), None) => Ordering::Greater,
930954
(Some(aas), Some(abs)) => {
931-
if self.version == Version::Two {
955+
if self.style_edition >= StyleEdition::Edition2024 {
932956
aas.trim_start_matches("r#")
933957
.cmp(abs.trim_start_matches("r#"))
934958
} else {
@@ -1114,7 +1138,7 @@ mod test {
11141138

11151139
struct Parser<'a> {
11161140
input: Peekable<Chars<'a>>,
1117-
version: Version,
1141+
style_edition: StyleEdition,
11181142
}
11191143

11201144
impl<'a> Parser<'a> {
@@ -1132,35 +1156,47 @@ mod test {
11321156
buf: &mut String,
11331157
alias_buf: &mut Option<String>,
11341158
) {
1135-
let version = self.version;
1159+
let style_edition = self.style_edition;
11361160
if !buf.is_empty() {
11371161
let mut alias = None;
11381162
swap(alias_buf, &mut alias);
11391163

11401164
match buf.as_ref() {
11411165
"self" => {
11421166
let kind = UseSegmentKind::Slf(alias);
1143-
result.push(UseSegment { kind, version });
1167+
result.push(UseSegment {
1168+
kind,
1169+
style_edition,
1170+
});
11441171
*buf = String::new();
11451172
*alias_buf = None;
11461173
}
11471174
"super" => {
11481175
let kind = UseSegmentKind::Super(alias);
1149-
result.push(UseSegment { kind, version });
1176+
result.push(UseSegment {
1177+
kind,
1178+
style_edition,
1179+
});
11501180
*buf = String::new();
11511181
*alias_buf = None;
11521182
}
11531183
"crate" => {
11541184
let kind = UseSegmentKind::Crate(alias);
1155-
result.push(UseSegment { kind, version });
1185+
result.push(UseSegment {
1186+
kind,
1187+
style_edition,
1188+
});
11561189
*buf = String::new();
11571190
*alias_buf = None;
11581191
}
11591192
_ => {
11601193
let mut name = String::new();
11611194
swap(buf, &mut name);
11621195
let kind = UseSegmentKind::Ident(name, alias);
1163-
result.push(UseSegment { kind, version });
1196+
result.push(UseSegment {
1197+
kind,
1198+
style_edition,
1199+
});
11641200
}
11651201
}
11661202
}
@@ -1178,7 +1214,7 @@ mod test {
11781214
let kind = UseSegmentKind::List(self.parse_list());
11791215
result.push(UseSegment {
11801216
kind,
1181-
version: self.version,
1217+
style_edition: self.style_edition,
11821218
});
11831219
self.eat('}');
11841220
}
@@ -1188,7 +1224,7 @@ mod test {
11881224
let kind = UseSegmentKind::Glob;
11891225
result.push(UseSegment {
11901226
kind,
1191-
version: self.version,
1227+
style_edition: self.style_edition,
11921228
});
11931229
}
11941230
':' => {
@@ -1249,7 +1285,7 @@ mod test {
12491285

12501286
let mut parser = Parser {
12511287
input: s.chars().peekable(),
1252-
version: Version::One,
1288+
style_edition: StyleEdition::Edition2015,
12531289
};
12541290
parser.parse_in_list()
12551291
}

0 commit comments

Comments
 (0)