@@ -11,29 +11,40 @@ use std::cmp::Ordering;
1111use rustc_ast:: { ast, attr} ;
1212use rustc_span:: { Span , symbol:: sym} ;
1313
14+ use crate :: StyleEdition ;
1415use crate :: config:: { Config , GroupImportsTactic } ;
1516use crate :: imports:: { UseSegmentKind , UseTree , normalize_use_trees_with_granularity} ;
1617use crate :: items:: { is_mod_decl, rewrite_extern_crate, rewrite_mod} ;
1718use crate :: lists:: { ListFormatting , ListItem , itemize_list, write_list} ;
1819use crate :: rewrite:: { RewriteContext , RewriteErrorExt } ;
1920use crate :: shape:: Shape ;
21+ use crate :: sort:: version_sort;
2022use crate :: source_map:: LineRangeUtils ;
2123use crate :: spanned:: Spanned ;
2224use crate :: utils:: { contains_skip, mk_sp} ;
2325use crate :: visitor:: FmtVisitor ;
2426
2527/// 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 ( ) ;
2730 match ( & a. kind , & b. kind ) {
2831 ( & 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+ }
3037 }
3138 ( & ast:: ItemKind :: ExternCrate ( ref a_name) , & ast:: ItemKind :: ExternCrate ( ref b_name) ) => {
3239 // `extern crate foo as bar;`
3340 // ^^^ Comparing this.
3441 let a_orig_name = a_name. unwrap_or ( a. ident . name ) ;
3542 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+ } ;
3748 if result != Ordering :: Equal {
3849 return result;
3950 }
@@ -44,7 +55,10 @@ fn compare_items(a: &ast::Item, b: &ast::Item) -> Ordering {
4455 ( Some ( ..) , None ) => Ordering :: Greater ,
4556 ( None , Some ( ..) ) => Ordering :: Less ,
4657 ( 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 ( ) ) ,
4862 }
4963 }
5064 _ => unreachable ! ( ) ,
@@ -165,7 +179,7 @@ fn rewrite_reorderable_or_regroupable_items(
165179 ) ;
166180
167181 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 ) ) ;
169183 let item_vec: Vec < _ > = item_pair_vec. into_iter ( ) . map ( |pair| pair. 0 ) . collect ( ) ;
170184
171185 wrap_reorderable_items ( context, & item_vec, shape)
0 commit comments