@@ -11,29 +11,40 @@ use std::cmp::Ordering;
11
11
use rustc_ast:: { ast, attr} ;
12
12
use rustc_span:: { Span , symbol:: sym} ;
13
13
14
+ use crate :: StyleEdition ;
14
15
use crate :: config:: { Config , GroupImportsTactic } ;
15
16
use crate :: imports:: { UseSegmentKind , UseTree , normalize_use_trees_with_granularity} ;
16
17
use crate :: items:: { is_mod_decl, rewrite_extern_crate, rewrite_mod} ;
17
18
use crate :: lists:: { ListFormatting , ListItem , itemize_list, write_list} ;
18
19
use crate :: rewrite:: { RewriteContext , RewriteErrorExt } ;
19
20
use crate :: shape:: Shape ;
21
+ use crate :: sort:: version_sort;
20
22
use crate :: source_map:: LineRangeUtils ;
21
23
use crate :: spanned:: Spanned ;
22
24
use crate :: utils:: { contains_skip, mk_sp} ;
23
25
use crate :: visitor:: FmtVisitor ;
24
26
25
27
/// Choose the ordering between the given two items.
26
- fn compare_items ( a : & ast:: Item , b : & ast:: Item ) -> Ordering {
28
+ fn compare_items ( a : & ast:: Item , b : & ast:: Item , context : & RewriteContext < ' _ > ) -> Ordering {
29
+ let style_edition = context. config . style_edition ( ) ;
27
30
match ( & a. kind , & b. kind ) {
28
31
( & ast:: ItemKind :: Mod ( ..) , & ast:: ItemKind :: Mod ( ..) ) => {
29
- a. ident . as_str ( ) . cmp ( b. ident . as_str ( ) )
32
+ if style_edition <= StyleEdition :: Edition2021 {
33
+ a. ident . as_str ( ) . cmp ( b. ident . as_str ( ) )
34
+ } else {
35
+ version_sort ( a. ident . as_str ( ) , b. ident . as_str ( ) )
36
+ }
30
37
}
31
38
( & ast:: ItemKind :: ExternCrate ( ref a_name) , & ast:: ItemKind :: ExternCrate ( ref b_name) ) => {
32
39
// `extern crate foo as bar;`
33
40
// ^^^ Comparing this.
34
41
let a_orig_name = a_name. unwrap_or ( a. ident . name ) ;
35
42
let b_orig_name = b_name. unwrap_or ( b. ident . name ) ;
36
- let result = a_orig_name. as_str ( ) . cmp ( b_orig_name. as_str ( ) ) ;
43
+ let result = if style_edition <= StyleEdition :: Edition2021 {
44
+ a_orig_name. as_str ( ) . cmp ( b_orig_name. as_str ( ) )
45
+ } else {
46
+ version_sort ( a_orig_name. as_str ( ) , b_orig_name. as_str ( ) )
47
+ } ;
37
48
if result != Ordering :: Equal {
38
49
return result;
39
50
}
@@ -44,7 +55,10 @@ fn compare_items(a: &ast::Item, b: &ast::Item) -> Ordering {
44
55
( Some ( ..) , None ) => Ordering :: Greater ,
45
56
( None , Some ( ..) ) => Ordering :: Less ,
46
57
( None , None ) => Ordering :: Equal ,
47
- ( Some ( ..) , Some ( ..) ) => a. ident . as_str ( ) . cmp ( b. ident . as_str ( ) ) ,
58
+ ( Some ( ..) , Some ( ..) ) if style_edition <= StyleEdition :: Edition2021 => {
59
+ a. ident . as_str ( ) . cmp ( b. ident . as_str ( ) )
60
+ }
61
+ ( Some ( ..) , Some ( ..) ) => version_sort ( a. ident . as_str ( ) , b. ident . as_str ( ) ) ,
48
62
}
49
63
}
50
64
_ => unreachable ! ( ) ,
@@ -165,7 +179,7 @@ fn rewrite_reorderable_or_regroupable_items(
165
179
) ;
166
180
167
181
let mut item_pair_vec: Vec < _ > = list_items. zip ( reorderable_items. iter ( ) ) . collect ( ) ;
168
- item_pair_vec. sort_by ( |a, b| compare_items ( a. 1 , b. 1 ) ) ;
182
+ item_pair_vec. sort_by ( |a, b| compare_items ( a. 1 , b. 1 , context ) ) ;
169
183
let item_vec: Vec < _ > = item_pair_vec. into_iter ( ) . map ( |pair| pair. 0 ) . collect ( ) ;
170
184
171
185
wrap_reorderable_items ( context, & item_vec, shape)
0 commit comments