diff --git a/src/imports.rs b/src/imports.rs index dac748af9b1..b940dcc9de7 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -105,13 +105,32 @@ fn compare_use_trees(a: &ast::UseTree, b: &ast::UseTree, nested: bool) -> Orderi } } -fn compare_use_items(context: &RewriteContext, a: &ast::Item, b: &ast::Item) -> Option { +fn compare_use_items(a: &ast::Item, b: &ast::Item) -> Option { match (&a.node, &b.node) { (&ast::ItemKind::Use(ref a_tree), &ast::ItemKind::Use(ref b_tree)) => { Some(compare_use_trees(a_tree, b_tree, false)) } - (&ast::ItemKind::ExternCrate(..), &ast::ItemKind::ExternCrate(..)) => { - Some(context.snippet(a.span).cmp(context.snippet(b.span))) + (&ast::ItemKind::ExternCrate(ref a_name), &ast::ItemKind::ExternCrate(ref b_name)) => { + // `extern crate foo as bar;` + // ^^^ Comparing this. + let a_orig_name = + a_name.map_or_else(|| a.ident.name.as_str(), |symbol| symbol.as_str()); + let b_orig_name = + b_name.map_or_else(|| b.ident.name.as_str(), |symbol| symbol.as_str()); + let result = a_orig_name.cmp(&b_orig_name); + if result != Ordering::Equal { + return Some(result); + } + + // `extern crate foo as bar;` + // ^^^ Comparing this. + let result = match (a_name, b_name) { + (Some(..), None) => Ordering::Greater, + (None, Some(..)) => Ordering::Less, + (None, None) => Ordering::Equal, + (Some(..), Some(..)) => a.ident.name.cmp(&b.ident.name), + }; + Some(result) } _ => None, } @@ -257,7 +276,7 @@ fn rewrite_imports( false, ); let mut item_pair_vec: Vec<_> = items.zip(use_items.iter()).collect(); - item_pair_vec.sort_by(|a, b| compare_use_items(context, a.1, b.1).unwrap()); + item_pair_vec.sort_by(|a, b| compare_use_items(a.1, b.1).unwrap()); let item_vec: Vec<_> = item_pair_vec.into_iter().map(|pair| pair.0).collect(); let fmt = ListFormatting { diff --git a/src/lib.rs b/src/lib.rs index 111345a24b6..f9c9626a1f5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#![feature(match_default_bindings)] #![feature(rustc_private)] #![feature(type_ascription)] diff --git a/tests/source/extern.rs b/tests/source/extern.rs index bc82bcd61db..187e698607a 100644 --- a/tests/source/extern.rs +++ b/tests/source/extern.rs @@ -10,6 +10,10 @@ extern crate chrono; extern crate foo; extern crate bar; +// #2315 +extern crate proc_macro2; +extern crate proc_macro; + extern "C" { fn c_func(x: *mut *mut libc::c_void); diff --git a/tests/target/extern.rs b/tests/target/extern.rs index 1d49b76dc42..b0aa51127d5 100644 --- a/tests/target/extern.rs +++ b/tests/target/extern.rs @@ -1,7 +1,7 @@ // rustfmt-normalize_comments: true -extern crate foo as bar; extern crate foo; +extern crate foo as bar; extern crate chrono; extern crate dotenv; @@ -10,6 +10,10 @@ extern crate futures; extern crate bar; extern crate foo; +// #2315 +extern crate proc_macro; +extern crate proc_macro2; + extern "C" { fn c_func(x: *mut *mut libc::c_void);