]> git.lizzy.rs Git - rust.git/blobdiff - src/imports.rs
Use `AttrVec` for `Arm`, `FieldDef`, and `Variant`
[rust.git] / src / imports.rs
index 9a02ee64fb7dc23d335a7b48ed118bd5b4a6ac22..0f635fe1ccb3584210a21debfaa5f423384f556a 100644 (file)
@@ -1,39 +1,35 @@
-// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
+use std::borrow::Cow;
 use std::cmp::Ordering;
+use std::fmt;
 
-use config::lists::*;
-use syntax::ast::{self, UseTreeKind};
-use syntax::codemap::{BytePos, Span};
+use rustc_ast::ast::{self, UseTreeKind};
+use rustc_span::{
+    symbol::{self, sym},
+    BytePos, Span, DUMMY_SP,
+};
 
-use codemap::SpanUtils;
-use config::IndentStyle;
-use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator};
-use rewrite::{Rewrite, RewriteContext};
-use shape::Shape;
-use spanned::Spanned;
-use utils::mk_sp;
-use visitor::FmtVisitor;
+use crate::comment::combine_strs_with_missing_comments;
+use crate::config::lists::*;
+use crate::config::{Edition, IndentStyle};
+use crate::lists::{
+    definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator,
+};
+use crate::rewrite::{Rewrite, RewriteContext};
+use crate::shape::Shape;
+use crate::source_map::SpanUtils;
+use crate::spanned::Spanned;
+use crate::utils::{is_same_visibility, mk_sp, rewrite_ident};
+use crate::visitor::FmtVisitor;
 
-use std::borrow::Cow;
-
-/// Returns a name imported by a `use` declaration. e.g. returns `Ordering`
-/// for `std::cmp::Ordering` and `self` for `std::cmp::self`.
-pub fn path_to_imported_ident(path: &ast::Path) -> ast::Ident {
+/// Returns a name imported by a `use` declaration.
+/// E.g., returns `Ordering` for `std::cmp::Ordering` and `self` for `std::cmp::self`.
+pub(crate) fn path_to_imported_ident(path: &ast::Path) -> symbol::Ident {
     path.segments.last().unwrap().ident
 }
 
 impl<'a> FmtVisitor<'a> {
-    pub fn format_import(&mut self, item: &ast::Item, tree: &ast::UseTree) {
-        let span = item.span;
+    pub(crate) fn format_import(&mut self, item: &ast::Item, tree: &ast::UseTree) {
+        let span = item.span();
         let shape = self.shape();
         let rw = UseTree::from_ast(
             &self.get_context(),
@@ -42,12 +38,13 @@ pub fn format_import(&mut self, item: &ast::Item, tree: &ast::UseTree) {
             Some(item.vis.clone()),
             Some(item.span.lo()),
             Some(item.attrs.clone()),
-        ).rewrite_top_level(&self.get_context(), shape);
+        )
+        .rewrite_top_level(&self.get_context(), shape);
         match rw {
             Some(ref s) if s.is_empty() => {
                 // Format up to last newline
                 let prev_span = mk_sp(self.last_pos, source!(self, span).lo());
-                let trimmed_snippet = self.snippet(prev_span).trim_right();
+                let trimmed_snippet = self.snippet(prev_span).trim_end();
                 let span_end = self.last_pos + BytePos(trimmed_snippet.len() as u32);
                 self.format_missing(span_end);
                 // We have an excessive newline from the removed import.
@@ -83,27 +80,28 @@ pub fn format_import(&mut self, item: &ast::Item, tree: &ast::UseTree) {
 // when ordering unless the imports are identical except for the alias (rare in
 // practice).
 
-// FIXME(#2531) - we should unify the comparison code here with the formatting
+// FIXME(#2531): we should unify the comparison code here with the formatting
 // code elsewhere since we are essentially string-ifying twice. Furthermore, by
 // parsing to our own format on comparison, we repeat a lot of work when
 // sorting.
 
 // FIXME we do a lot of allocation to make our own representation.
-#[derive(Debug, Clone, Eq, PartialEq)]
-pub enum UseSegment {
+#[derive(Clone, Eq, PartialEq)]
+pub(crate) enum UseSegment {
     Ident(String, Option<String>),
     Slf(Option<String>),
     Super(Option<String>),
+    Crate(Option<String>),
     Glob,
     List(Vec<UseTree>),
 }
 
-#[derive(Debug, Clone)]
-pub struct UseTree {
-    pub path: Vec<UseSegment>,
-    pub span: Span,
+#[derive(Clone)]
+pub(crate) struct UseTree {
+    pub(crate) path: Vec<UseSegment>,
+    pub(crate) span: Span,
     // Comment information within nested use tree.
-    list_item: Option<ListItem>,
+    pub(crate) list_item: Option<ListItem>,
     // Additional fields for top level use items.
     // Should we have another struct for top-level use items rather than reusing this?
     visibility: Option<ast::Visibility>,
@@ -117,6 +115,17 @@ fn eq(&self, other: &UseTree) -> bool {
 }
 impl Eq for UseTree {}
 
+impl Spanned for UseTree {
+    fn span(&self) -> Span {
+        let lo = if let Some(ref attrs) = self.attrs {
+            attrs.iter().next().map_or(self.span.lo(), |a| a.span.lo())
+        } else {
+            self.span.lo()
+        };
+        mk_sp(lo, self.span.hi())
+    }
+}
+
 impl UseSegment {
     // Clone a version of self with any top-level alias removed.
     fn remove_alias(&self) -> UseSegment {
@@ -124,75 +133,205 @@ fn remove_alias(&self) -> UseSegment {
             UseSegment::Ident(ref s, _) => UseSegment::Ident(s.clone(), None),
             UseSegment::Slf(_) => UseSegment::Slf(None),
             UseSegment::Super(_) => UseSegment::Super(None),
+            UseSegment::Crate(_) => UseSegment::Crate(None),
             _ => self.clone(),
         }
     }
 
-    fn from_path_segment(path_seg: &ast::PathSegment) -> Option<UseSegment> {
-        let name = path_seg.ident.name.as_str();
-        if name == "{{root}}" {
+    fn from_path_segment(
+        context: &RewriteContext<'_>,
+        path_seg: &ast::PathSegment,
+        modsep: bool,
+    ) -> Option<UseSegment> {
+        let name = rewrite_ident(context, path_seg.ident);
+        if name.is_empty() || name == "{{root}}" {
             return None;
         }
-        Some(if name == "self" {
-            UseSegment::Slf(None)
-        } else if name == "super" {
-            UseSegment::Super(None)
-        } else {
-            UseSegment::Ident((*name).to_owned(), None)
+        Some(match name {
+            "self" => UseSegment::Slf(None),
+            "super" => UseSegment::Super(None),
+            "crate" => UseSegment::Crate(None),
+            _ => {
+                let mod_sep = if modsep { "::" } else { "" };
+                UseSegment::Ident(format!("{}{}", mod_sep, name), None)
+            }
         })
     }
 }
 
-impl UseTree {
-    // Rewrite use tree with `use ` and a trailing `;`.
-    pub fn rewrite_top_level(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
-        let mut result = String::with_capacity(256);
-        if let Some(ref attrs) = self.attrs {
-            result.push_str(&attrs.rewrite(context, shape)?);
-            if !result.is_empty() {
-                result.push_str(&shape.indent.to_string_with_newline(context.config));
+pub(crate) fn merge_use_trees(use_trees: Vec<UseTree>, merge_by: SharedPrefix) -> Vec<UseTree> {
+    let mut result = Vec::with_capacity(use_trees.len());
+    for use_tree in use_trees {
+        if use_tree.has_comment() || use_tree.attrs.is_some() {
+            result.push(use_tree);
+            continue;
+        }
+
+        for flattened in use_tree.flatten() {
+            if let Some(tree) = result
+                .iter_mut()
+                .find(|tree| tree.share_prefix(&flattened, merge_by))
+            {
+                tree.merge(&flattened, merge_by);
+            } else {
+                result.push(flattened);
+            }
+        }
+    }
+    result
+}
+
+pub(crate) fn flatten_use_trees(use_trees: Vec<UseTree>) -> Vec<UseTree> {
+    use_trees
+        .into_iter()
+        .flat_map(UseTree::flatten)
+        .map(|mut tree| {
+            // If a path ends in `::self`, rewrite it to `::{self}`.
+            if let Some(UseSegment::Slf(..)) = tree.path.last() {
+                let self_segment = tree.path.pop().unwrap();
+                tree.path.push(UseSegment::List(vec![UseTree::from_path(
+                    vec![self_segment],
+                    DUMMY_SP,
+                )]));
+            }
+            tree
+        })
+        .collect()
+}
+
+impl fmt::Debug for UseTree {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        fmt::Display::fmt(self, f)
+    }
+}
+
+impl fmt::Debug for UseSegment {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        fmt::Display::fmt(self, f)
+    }
+}
+
+impl fmt::Display for UseSegment {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        match *self {
+            UseSegment::Glob => write!(f, "*"),
+            UseSegment::Ident(ref s, _) => write!(f, "{}", s),
+            UseSegment::Slf(..) => write!(f, "self"),
+            UseSegment::Super(..) => write!(f, "super"),
+            UseSegment::Crate(..) => write!(f, "crate"),
+            UseSegment::List(ref list) => {
+                write!(f, "{{")?;
+                for (i, item) in list.iter().enumerate() {
+                    if i != 0 {
+                        write!(f, ", ")?;
+                    }
+                    write!(f, "{}", item)?;
+                }
+                write!(f, "}}")
             }
         }
+    }
+}
+impl fmt::Display for UseTree {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        for (i, segment) in self.path.iter().enumerate() {
+            if i != 0 {
+                write!(f, "::")?;
+            }
+            write!(f, "{}", segment)?;
+        }
+        Ok(())
+    }
+}
 
-        let vis = self.visibility
-            .as_ref()
-            .map_or(Cow::from(""), |vis| ::utils::format_visibility(&vis));
-        result.push_str(&self.rewrite(context, shape.offset_left(vis.len())?)
+impl UseTree {
+    // Rewrite use tree with `use ` and a trailing `;`.
+    pub(crate) fn rewrite_top_level(
+        &self,
+        context: &RewriteContext<'_>,
+        shape: Shape,
+    ) -> Option<String> {
+        let vis = self.visibility.as_ref().map_or(Cow::from(""), |vis| {
+            crate::utils::format_visibility(context, &vis)
+        });
+        let use_str = self
+            .rewrite(context, shape.offset_left(vis.len())?)
             .map(|s| {
                 if s.is_empty() {
-                    s.to_owned()
+                    s
                 } else {
                     format!("{}use {};", vis, s)
                 }
-            })?);
-        Some(result)
+            })?;
+        match self.attrs {
+            Some(ref attrs) if !attrs.is_empty() => {
+                let attr_str = attrs.rewrite(context, shape)?;
+                let lo = attrs.last().as_ref()?.span.hi();
+                let hi = self.span.lo();
+                let span = mk_sp(lo, hi);
+
+                let allow_extend = if attrs.len() == 1 {
+                    let line_len = attr_str.len() + 1 + use_str.len();
+                    !attrs.first().unwrap().is_doc_comment()
+                        && context.config.inline_attribute_width() >= line_len
+                } else {
+                    false
+                };
+
+                combine_strs_with_missing_comments(
+                    context,
+                    &attr_str,
+                    &use_str,
+                    span,
+                    shape,
+                    allow_extend,
+                )
+            }
+            _ => Some(use_str),
+        }
     }
 
-    pub fn from_ast_with_normalization(
-        context: &RewriteContext,
+    // FIXME: Use correct span?
+    // The given span is essentially incorrect, since we are reconstructing
+    // use-statements. This should not be a problem, though, since we have
+    // already tried to extract comment and observed that there are no comment
+    // around the given use item, and the span will not be used afterward.
+    fn from_path(path: Vec<UseSegment>, span: Span) -> UseTree {
+        UseTree {
+            path,
+            span,
+            list_item: None,
+            visibility: None,
+            attrs: None,
+        }
+    }
+
+    pub(crate) fn from_ast_with_normalization(
+        context: &RewriteContext<'_>,
         item: &ast::Item,
     ) -> Option<UseTree> {
-        match item.node {
+        match item.kind {
             ast::ItemKind::Use(ref use_tree) => Some(
                 UseTree::from_ast(
                     context,
                     use_tree,
                     None,
                     Some(item.vis.clone()),
-                    Some(item.span().lo()),
+                    Some(item.span.lo()),
                     if item.attrs.is_empty() {
                         None
                     } else {
                         Some(item.attrs.clone())
                     },
-                ).normalize(context.config.reorder_imported_names()),
+                )
+                .normalize(),
             ),
             _ => None,
         }
     }
 
     fn from_ast(
-        context: &RewriteContext,
+        context: &RewriteContext<'_>,
         a: &ast::UseTree,
         list_item: Option<ListItem>,
         visibility: Option<ast::Visibility>,
@@ -211,13 +350,25 @@ fn from_ast(
             visibility,
             attrs,
         };
+
+        let leading_modsep =
+            context.config.edition() >= Edition::Edition2018 && a.prefix.is_global();
+
+        let mut modsep = leading_modsep;
+
         for p in &a.prefix.segments {
-            if let Some(use_segment) = UseSegment::from_path_segment(p) {
+            if let Some(use_segment) = UseSegment::from_path_segment(context, p, modsep) {
                 result.path.push(use_segment);
+                modsep = false;
             }
         }
+
         match a.kind {
             UseTreeKind::Glob => {
+                // in case of a global path and the glob starts at the root, e.g., "::*"
+                if a.prefix.segments.len() == 1 && leading_modsep {
+                    result.path.push(UseSegment::Ident("".to_owned(), None));
+                }
                 result.path.push(UseSegment::Glob);
             }
             UseTreeKind::Nested(ref list) => {
@@ -234,7 +385,13 @@ fn from_ast(
                     context.snippet_provider.span_after(a.span, "{"),
                     a.span.hi(),
                     false,
-                ).collect();
+                )
+                .collect();
+                // in case of a global path and the nested list starts at the root,
+                // e.g., "::{foo, bar}"
+                if a.prefix.segments.len() == 1 && leading_modsep {
+                    result.path.push(UseSegment::Ident("".to_owned(), None));
+                }
                 result.path.push(UseSegment::List(
                     list.iter()
                         .zip(items.into_iter())
@@ -244,22 +401,31 @@ fn from_ast(
                         .collect(),
                 ));
             }
-            UseTreeKind::Simple(ref rename) => {
-                let mut name = (*path_to_imported_ident(&a.prefix).name.as_str()).to_owned();
+            UseTreeKind::Simple(ref rename, ..) => {
+                // If the path has leading double colons and is composed of only 2 segments, then we
+                // bypass the call to path_to_imported_ident which would get only the ident and
+                // lose the path root, e.g., `that` in `::that`.
+                // The span of `a.prefix` contains the leading colons.
+                let name = if a.prefix.segments.len() == 2 && leading_modsep {
+                    context.snippet(a.prefix.span).to_owned()
+                } else {
+                    rewrite_ident(context, path_to_imported_ident(&a.prefix)).to_owned()
+                };
                 let alias = rename.and_then(|ident| {
-                    if ident == path_to_imported_ident(&a.prefix) {
+                    if ident.name == sym::underscore_imports {
+                        // for impl-only-use
+                        Some("_".to_owned())
+                    } else if ident == path_to_imported_ident(&a.prefix) {
                         None
                     } else {
-                        Some(ident.to_string())
+                        Some(rewrite_ident(context, ident).to_owned())
                     }
                 });
-
-                let segment = if &name == "self" {
-                    UseSegment::Slf(alias)
-                } else if &name == "super" {
-                    UseSegment::Super(alias)
-                } else {
-                    UseSegment::Ident(name, alias)
+                let segment = match name.as_ref() {
+                    "self" => UseSegment::Slf(alias),
+                    "super" => UseSegment::Super(alias),
+                    "crate" => UseSegment::Crate(alias),
+                    _ => UseSegment::Ident(name, alias),
                 };
 
                 // `name` is already in result.
@@ -271,7 +437,7 @@ fn from_ast(
     }
 
     // Do the adjustments that rustfmt does elsewhere to use paths.
-    pub fn normalize(mut self, do_sort: bool) -> UseTree {
+    pub(crate) fn normalize(mut self) -> UseTree {
         let mut last = self.path.pop().expect("Empty use tree?");
         // Hack around borrow checker.
         let mut normalize_sole_list = false;
@@ -293,7 +459,7 @@ pub fn normalize(mut self, do_sort: bool) -> UseTree {
 
         // Normalise foo::self -> foo.
         if let UseSegment::Slf(None) = last {
-            if self.path.len() > 0 {
+            if !self.path.is_empty() {
                 return self;
             }
         }
@@ -301,11 +467,10 @@ pub fn normalize(mut self, do_sort: bool) -> UseTree {
         // Normalise foo::self as bar -> foo as bar.
         if let UseSegment::Slf(_) = last {
             match self.path.last() {
-                None => {}
                 Some(UseSegment::Ident(_, None)) => {
                     aliased_self = true;
                 }
-                _ => unreachable!(),
+                _ => {}
             }
         }
 
@@ -329,7 +494,7 @@ pub fn normalize(mut self, do_sort: bool) -> UseTree {
 
         // Normalise foo::{bar} -> foo::bar
         if let UseSegment::List(ref list) = last {
-            if list.len() == 1 {
+            if list.len() == 1 && list[0].to_string() != "self" {
                 normalize_sole_list = true;
             }
         }
@@ -340,7 +505,7 @@ pub fn normalize(mut self, do_sort: bool) -> UseTree {
                     for seg in &list[0].path {
                         self.path.push(seg.clone());
                     }
-                    return self.normalize(do_sort);
+                    return self.normalize();
                 }
                 _ => unreachable!(),
             }
@@ -348,18 +513,169 @@ pub fn normalize(mut self, do_sort: bool) -> UseTree {
 
         // Recursively normalize elements of a list use (including sorting the list).
         if let UseSegment::List(list) = last {
-            let mut list = list.into_iter()
-                .map(|ut| ut.normalize(do_sort))
-                .collect::<Vec<_>>();
-            if do_sort {
-                list.sort();
-            }
+            let mut list = list.into_iter().map(UseTree::normalize).collect::<Vec<_>>();
+            list.sort();
             last = UseSegment::List(list);
         }
 
         self.path.push(last);
         self
     }
+
+    fn has_comment(&self) -> bool {
+        self.list_item.as_ref().map_or(false, ListItem::has_comment)
+    }
+
+    fn same_visibility(&self, other: &UseTree) -> bool {
+        match (&self.visibility, &other.visibility) {
+            (
+                Some(ast::Visibility {
+                    kind: ast::VisibilityKind::Inherited,
+                    ..
+                }),
+                None,
+            )
+            | (
+                None,
+                Some(ast::Visibility {
+                    kind: ast::VisibilityKind::Inherited,
+                    ..
+                }),
+            )
+            | (None, None) => true,
+            (Some(ref a), Some(ref b)) => is_same_visibility(a, b),
+            _ => false,
+        }
+    }
+
+    fn share_prefix(&self, other: &UseTree, shared_prefix: SharedPrefix) -> bool {
+        if self.path.is_empty()
+            || other.path.is_empty()
+            || self.attrs.is_some()
+            || !self.same_visibility(other)
+        {
+            false
+        } else {
+            match shared_prefix {
+                SharedPrefix::Crate => self.path[0] == other.path[0],
+                SharedPrefix::Module => {
+                    self.path[..self.path.len() - 1] == other.path[..other.path.len() - 1]
+                }
+            }
+        }
+    }
+
+    fn flatten(self) -> Vec<UseTree> {
+        if self.path.is_empty() {
+            return vec![self];
+        }
+        match self.path.clone().last().unwrap() {
+            UseSegment::List(list) => {
+                if list.len() == 1 && list[0].path.len() == 1 {
+                    match list[0].path[0] {
+                        UseSegment::Slf(..) => return vec![self],
+                        _ => (),
+                    };
+                }
+                let prefix = &self.path[..self.path.len() - 1];
+                let mut result = vec![];
+                for nested_use_tree in list {
+                    for flattend in &mut nested_use_tree.clone().flatten() {
+                        let mut new_path = prefix.to_vec();
+                        new_path.append(&mut flattend.path);
+                        result.push(UseTree {
+                            path: new_path,
+                            span: self.span,
+                            list_item: None,
+                            visibility: self.visibility.clone(),
+                            attrs: None,
+                        });
+                    }
+                }
+
+                result
+            }
+            _ => vec![self],
+        }
+    }
+
+    fn merge(&mut self, other: &UseTree, merge_by: SharedPrefix) {
+        let mut prefix = 0;
+        for (a, b) in self.path.iter().zip(other.path.iter()) {
+            if *a == *b {
+                prefix += 1;
+            } else {
+                break;
+            }
+        }
+        if let Some(new_path) = merge_rest(&self.path, &other.path, prefix, merge_by) {
+            self.path = new_path;
+            self.span = self.span.to(other.span);
+        }
+    }
+}
+
+fn merge_rest(
+    a: &[UseSegment],
+    b: &[UseSegment],
+    mut len: usize,
+    merge_by: SharedPrefix,
+) -> Option<Vec<UseSegment>> {
+    if a.len() == len && b.len() == len {
+        return None;
+    }
+    if a.len() != len && b.len() != len {
+        if let UseSegment::List(ref list) = a[len] {
+            let mut list = list.clone();
+            merge_use_trees_inner(
+                &mut list,
+                UseTree::from_path(b[len..].to_vec(), DUMMY_SP),
+                merge_by,
+            );
+            let mut new_path = b[..len].to_vec();
+            new_path.push(UseSegment::List(list));
+            return Some(new_path);
+        }
+    } else if len == 1 {
+        let rest = if a.len() == len { &b[1..] } else { &a[1..] };
+        return Some(vec![
+            b[0].clone(),
+            UseSegment::List(vec![
+                UseTree::from_path(vec![UseSegment::Slf(None)], DUMMY_SP),
+                UseTree::from_path(rest.to_vec(), DUMMY_SP),
+            ]),
+        ]);
+    } else {
+        len -= 1;
+    }
+    let mut list = vec![
+        UseTree::from_path(a[len..].to_vec(), DUMMY_SP),
+        UseTree::from_path(b[len..].to_vec(), DUMMY_SP),
+    ];
+    list.sort();
+    let mut new_path = b[..len].to_vec();
+    new_path.push(UseSegment::List(list));
+    Some(new_path)
+}
+
+fn merge_use_trees_inner(trees: &mut Vec<UseTree>, use_tree: UseTree, merge_by: SharedPrefix) {
+    let similar_trees = trees
+        .iter_mut()
+        .filter(|tree| tree.share_prefix(&use_tree, merge_by));
+    if use_tree.path.len() == 1 && merge_by == SharedPrefix::Crate {
+        if let Some(tree) = similar_trees.min_by_key(|tree| tree.path.len()) {
+            if tree.path.len() == 1 {
+                return;
+            }
+        }
+    } else if let Some(tree) = similar_trees.max_by_key(|tree| tree.path.len()) {
+        if tree.path.len() > 1 {
+            tree.merge(&use_tree, merge_by);
+            return;
+        }
+    }
+    trees.push(use_tree);
+    trees.sort();
 }
 
 impl PartialOrd for UseSegment {
@@ -377,11 +693,14 @@ fn cmp(&self, other: &UseSegment) -> Ordering {
         use self::UseSegment::*;
 
         fn is_upper_snake_case(s: &str) -> bool {
-            s.chars().all(|c| c.is_uppercase() || c == '_')
+            s.chars()
+                .all(|c| c.is_uppercase() || c == '_' || c.is_numeric())
         }
 
         match (self, other) {
-            (&Slf(ref a), &Slf(ref b)) | (&Super(ref a), &Super(ref b)) => a.cmp(b),
+            (&Slf(ref a), &Slf(ref b))
+            | (&Super(ref a), &Super(ref b))
+            | (&Crate(ref a), &Crate(ref b)) => a.cmp(b),
             (&Glob, &Glob) => Ordering::Equal,
             (&Ident(ref ia, ref aa), &Ident(ref ib, ref ab)) => {
                 // snake_case < CamelCase < UPPER_SNAKE_CASE
@@ -423,6 +742,8 @@ fn is_upper_snake_case(s: &str) -> bool {
             (_, &Slf(_)) => Ordering::Greater,
             (&Super(_), _) => Ordering::Less,
             (_, &Super(_)) => Ordering::Greater,
+            (&Crate(_), _) => Ordering::Less,
+            (_, &Crate(_)) => Ordering::Greater,
             (&Ident(..), _) => Ordering::Less,
             (_, &Ident(..)) => Ordering::Greater,
             (&Glob, _) => Ordering::Less,
@@ -448,7 +769,7 @@ fn cmp(&self, other: &UseTree) -> Ordering {
 }
 
 fn rewrite_nested_use_tree(
-    context: &RewriteContext,
+    context: &RewriteContext<'_>,
     use_tree_list: &[UseTree],
     shape: Shape,
 ) -> Option<String> {
@@ -461,11 +782,14 @@ fn rewrite_nested_use_tree(
         IndentStyle::Visual => shape.visual_indent(0),
     };
     for use_tree in use_tree_list {
-        let mut list_item = use_tree.list_item.clone()?;
-        list_item.item = use_tree.rewrite(context, nested_shape);
-        list_items.push(list_item);
+        if let Some(mut list_item) = use_tree.list_item.clone() {
+            list_item.item = use_tree.rewrite(context, nested_shape);
+            list_items.push(list_item);
+        } else {
+            list_items.push(ListItem::from_str(use_tree.rewrite(context, nested_shape)?));
+        }
     }
-    let (tactic, remaining_width) = if use_tree_list.iter().any(|use_segment| {
+    let has_nested_list = use_tree_list.iter().any(|use_segment| {
         use_segment
             .path
             .last()
@@ -473,34 +797,34 @@ fn rewrite_nested_use_tree(
                 UseSegment::List(..) => true,
                 _ => false,
             })
-    }) {
-        (DefinitiveListTactic::Vertical, 0)
+    });
+
+    let remaining_width = if has_nested_list {
+        0
     } else {
-        let remaining_width = shape.width.checked_sub(2).unwrap_or(0);
-        let tactic = definitive_tactic(
-            &list_items,
-            context.config.imports_layout(),
-            Separator::Comma,
-            remaining_width,
-        );
-        (tactic, remaining_width)
+        shape.width.saturating_sub(2)
     };
+
+    let tactic = definitive_tactic(
+        &list_items,
+        context.config.imports_layout(),
+        Separator::Comma,
+        remaining_width,
+    );
+
     let ends_with_newline = context.config.imports_indent() == IndentStyle::Block
         && tactic != DefinitiveListTactic::Horizontal;
-    let fmt = ListFormatting {
-        tactic,
-        separator: ",",
-        trailing_separator: if ends_with_newline {
-            context.config.trailing_comma()
-        } else {
-            SeparatorTactic::Never
-        },
-        separator_place: SeparatorPlace::Back,
-        shape: nested_shape,
-        ends_with_newline,
-        preserve_newline: true,
-        config: context.config,
+    let trailing_separator = if ends_with_newline {
+        context.config.trailing_comma()
+    } else {
+        SeparatorTactic::Never
     };
+    let fmt = ListFormatting::new(nested_shape, context.config)
+        .tactic(tactic)
+        .trailing_separator(trailing_separator)
+        .ends_with_newline(ends_with_newline)
+        .preserve_newline(true)
+        .nested(has_nested_list);
 
     let list_str = write_list(&list_items, &fmt)?;
 
@@ -521,14 +845,16 @@ fn rewrite_nested_use_tree(
 }
 
 impl Rewrite for UseSegment {
-    fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
-        Some(match *self {
+    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
+        Some(match self {
             UseSegment::Ident(ref ident, Some(ref rename)) => format!("{} as {}", ident, rename),
             UseSegment::Ident(ref ident, None) => ident.clone(),
             UseSegment::Slf(Some(ref rename)) => format!("self as {}", rename),
             UseSegment::Slf(None) => "self".to_owned(),
             UseSegment::Super(Some(ref rename)) => format!("super as {}", rename),
             UseSegment::Super(None) => "super".to_owned(),
+            UseSegment::Crate(Some(ref rename)) => format!("crate as {}", rename),
+            UseSegment::Crate(None) => "crate".to_owned(),
             UseSegment::Glob => "*".to_owned(),
             UseSegment::List(ref use_tree_list) => rewrite_nested_use_tree(
                 context,
@@ -542,7 +868,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
 
 impl Rewrite for UseTree {
     // This does NOT format attributes and visibility or add a trailing `;`.
-    fn rewrite(&self, context: &RewriteContext, mut shape: Shape) -> Option<String> {
+    fn rewrite(&self, context: &RewriteContext<'_>, mut shape: Shape) -> Option<String> {
         let mut result = String::with_capacity(256);
         let mut iter = self.path.iter().peekable();
         while let Some(ref segment) = iter.next() {
@@ -558,10 +884,16 @@ fn rewrite(&self, context: &RewriteContext, mut shape: Shape) -> Option<String>
     }
 }
 
+#[derive(Copy, Clone, Debug, PartialEq, Eq)]
+pub(crate) enum SharedPrefix {
+    Crate,
+    Module,
+}
+
 #[cfg(test)]
 mod test {
     use super::*;
-    use syntax::codemap::DUMMY_SP;
+    use rustc_span::DUMMY_SP;
 
     // Parse the path part of an import. This parser is not robust and is only
     // suitable for use in a test harness.
@@ -578,9 +910,11 @@ impl<'a> Parser<'a> {
             fn bump(&mut self) {
                 self.input.next().unwrap();
             }
+
             fn eat(&mut self, c: char) {
                 assert!(self.input.next().unwrap() == c);
             }
+
             fn push_segment(
                 result: &mut Vec<UseSegment>,
                 buf: &mut String,
@@ -589,21 +923,32 @@ fn push_segment(
                 if !buf.is_empty() {
                     let mut alias = None;
                     swap(alias_buf, &mut alias);
-                    if buf == "self" {
-                        result.push(UseSegment::Slf(alias));
-                        *buf = String::new();
-                        *alias_buf = None;
-                    } else if buf == "super" {
-                        result.push(UseSegment::Super(alias));
-                        *buf = String::new();
-                        *alias_buf = None;
-                    } else {
-                        let mut name = String::new();
-                        swap(buf, &mut name);
-                        result.push(UseSegment::Ident(name, alias));
+
+                    match buf.as_ref() {
+                        "self" => {
+                            result.push(UseSegment::Slf(alias));
+                            *buf = String::new();
+                            *alias_buf = None;
+                        }
+                        "super" => {
+                            result.push(UseSegment::Super(alias));
+                            *buf = String::new();
+                            *alias_buf = None;
+                        }
+                        "crate" => {
+                            result.push(UseSegment::Crate(alias));
+                            *buf = String::new();
+                            *alias_buf = None;
+                        }
+                        _ => {
+                            let mut name = String::new();
+                            swap(buf, &mut name);
+                            result.push(UseSegment::Ident(name, alias));
+                        }
                     }
                 }
             }
+
             fn parse_in_list(&mut self) -> UseTree {
                 let mut result = vec![];
                 let mut buf = String::new();
@@ -683,77 +1028,183 @@ fn parse_list(&mut self) -> Vec<UseTree> {
         parser.parse_in_list()
     }
 
+    macro_rules! parse_use_trees {
+        ($($s:expr),* $(,)*) => {
+            vec![
+                $(parse_use_tree($s),)*
+            ]
+        }
+    }
+
+    macro_rules! test_merge {
+        ($by:ident, [$($input:expr),* $(,)*], [$($output:expr),* $(,)*]) => {
+            assert_eq!(
+                merge_use_trees(parse_use_trees!($($input,)*), SharedPrefix::$by),
+                parse_use_trees!($($output,)*),
+            );
+        }
+    }
+
     #[test]
-    fn test_use_tree_normalize() {
+    fn test_use_tree_merge_crate() {
+        test_merge!(
+            Crate,
+            ["a::b::{c, d}", "a::b::{e, f}"],
+            ["a::b::{c, d, e, f}"]
+        );
+        test_merge!(Crate, ["a::b::c", "a::b"], ["a::{b, b::c}"]);
+        test_merge!(Crate, ["a::b", "a::b"], ["a::b"]);
+        test_merge!(Crate, ["a", "a::b", "a::b::c"], ["a::{self, b, b::c}"]);
+        test_merge!(
+            Crate,
+            ["a", "a::b", "a::b::c", "a::b::c::d"],
+            ["a::{self, b, b::{c, c::d}}"]
+        );
+        test_merge!(
+            Crate,
+            ["a", "a::b", "a::b::c", "a::b"],
+            ["a::{self, b, b::c}"]
+        );
+        test_merge!(
+            Crate,
+            ["a::{b::{self, c}, d::e}", "a::d::f"],
+            ["a::{b::{self, c}, d::{e, f}}"]
+        );
+        test_merge!(
+            Crate,
+            ["a::d::f", "a::{b::{self, c}, d::e}"],
+            ["a::{b::{self, c}, d::{e, f}}"]
+        );
+        test_merge!(
+            Crate,
+            ["a::{c, d, b}", "a::{d, e, b, a, f}", "a::{f, g, c}"],
+            ["a::{a, b, c, d, e, f, g}"]
+        );
+        test_merge!(
+            Crate,
+            ["a::{self}", "b::{self as foo}"],
+            ["a::{self}", "b::{self as foo}"]
+        );
+    }
+
+    #[test]
+    fn test_use_tree_merge_module() {
+        test_merge!(
+            Module,
+            ["foo::b", "foo::{a, c, d::e}"],
+            ["foo::{a, b, c}", "foo::d::e"]
+        );
+
+        test_merge!(
+            Module,
+            ["foo::{a::b, a::c, d::e, d::f}"],
+            ["foo::a::{b, c}", "foo::d::{e, f}"]
+        );
+    }
+
+    #[test]
+    fn test_flatten_use_trees() {
         assert_eq!(
-            parse_use_tree("a::self").normalize(true),
-            parse_use_tree("a")
+            flatten_use_trees(parse_use_trees!["foo::{a::{b, c}, d::e}"]),
+            parse_use_trees!["foo::a::b", "foo::a::c", "foo::d::e"]
         );
+
         assert_eq!(
-            parse_use_tree("a::self as foo").normalize(true),
-            parse_use_tree("a as foo")
+            flatten_use_trees(parse_use_trees!["foo::{self, a, b::{c, d}, e::*}"]),
+            parse_use_trees![
+                "foo::{self}",
+                "foo::a",
+                "foo::b::c",
+                "foo::b::d",
+                "foo::e::*"
+            ]
+        );
+    }
+
+    #[test]
+    fn test_use_tree_flatten() {
+        assert_eq!(
+            parse_use_tree("a::b::{c, d, e, f}").flatten(),
+            parse_use_trees!("a::b::c", "a::b::d", "a::b::e", "a::b::f",)
         );
+
+        assert_eq!(
+            parse_use_tree("a::b::{c::{d, e, f}, g, h::{i, j, k}}").flatten(),
+            parse_use_trees![
+                "a::b::c::d",
+                "a::b::c::e",
+                "a::b::c::f",
+                "a::b::g",
+                "a::b::h::i",
+                "a::b::h::j",
+                "a::b::h::k",
+            ]
+        );
+    }
+
+    #[test]
+    fn test_use_tree_normalize() {
+        assert_eq!(parse_use_tree("a::self").normalize(), parse_use_tree("a"));
         assert_eq!(
-            parse_use_tree("a::{self}").normalize(true),
-            parse_use_tree("a")
+            parse_use_tree("a::self as foo").normalize(),
+            parse_use_tree("a as foo")
         );
         assert_eq!(
-            parse_use_tree("a::{b}").normalize(true),
-            parse_use_tree("a::b")
+            parse_use_tree("a::{self}").normalize(),
+            parse_use_tree("a::{self}")
         );
+        assert_eq!(parse_use_tree("a::{b}").normalize(), parse_use_tree("a::b"));
         assert_eq!(
-            parse_use_tree("a::{b, c::self}").normalize(true),
+            parse_use_tree("a::{b, c::self}").normalize(),
             parse_use_tree("a::{b, c}")
         );
         assert_eq!(
-            parse_use_tree("a::{b as bar, c::self}").normalize(true),
+            parse_use_tree("a::{b as bar, c::self}").normalize(),
             parse_use_tree("a::{b as bar, c}")
         );
     }
 
     #[test]
     fn test_use_tree_ord() {
-        assert!(parse_use_tree("a").normalize(true) < parse_use_tree("aa").normalize(true));
-        assert!(parse_use_tree("a").normalize(true) < parse_use_tree("a::a").normalize(true));
-        assert!(parse_use_tree("a").normalize(true) < parse_use_tree("*").normalize(true));
-        assert!(parse_use_tree("a").normalize(true) < parse_use_tree("{a, b}").normalize(true));
-        assert!(parse_use_tree("*").normalize(true) < parse_use_tree("{a, b}").normalize(true));
+        assert!(parse_use_tree("a").normalize() < parse_use_tree("aa").normalize());
+        assert!(parse_use_tree("a").normalize() < parse_use_tree("a::a").normalize());
+        assert!(parse_use_tree("a").normalize() < parse_use_tree("*").normalize());
+        assert!(parse_use_tree("a").normalize() < parse_use_tree("{a, b}").normalize());
+        assert!(parse_use_tree("*").normalize() < parse_use_tree("{a, b}").normalize());
 
         assert!(
-            parse_use_tree("aaaaaaaaaaaaaaa::{bb, cc, dddddddd}").normalize(true)
-                < parse_use_tree("aaaaaaaaaaaaaaa::{bb, cc, ddddddddd}").normalize(true)
-        );
-        assert!(
-            parse_use_tree("serde::de::{Deserialize}").normalize(true)
-                < parse_use_tree("serde_json").normalize(true)
+            parse_use_tree("aaaaaaaaaaaaaaa::{bb, cc, dddddddd}").normalize()
+                < parse_use_tree("aaaaaaaaaaaaaaa::{bb, cc, ddddddddd}").normalize()
         );
         assert!(
-            parse_use_tree("a::b::c").normalize(true) < parse_use_tree("a::b::*").normalize(true)
+            parse_use_tree("serde::de::{Deserialize}").normalize()
+                < parse_use_tree("serde_json").normalize()
         );
+        assert!(parse_use_tree("a::b::c").normalize() < parse_use_tree("a::b::*").normalize());
         assert!(
-            parse_use_tree("foo::{Bar, Baz}").normalize(true)
-                < parse_use_tree("{Bar, Baz}").normalize(true)
+            parse_use_tree("foo::{Bar, Baz}").normalize()
+                < parse_use_tree("{Bar, Baz}").normalize()
         );
 
         assert!(
-            parse_use_tree("foo::{self as bar}").normalize(true)
-                < parse_use_tree("foo::{qux as bar}").normalize(true)
+            parse_use_tree("foo::{qux as bar}").normalize()
+                < parse_use_tree("foo::{self as bar}").normalize()
         );
         assert!(
-            parse_use_tree("foo::{qux as bar}").normalize(true)
-                < parse_use_tree("foo::{baz, qux as bar}").normalize(true)
+            parse_use_tree("foo::{qux as bar}").normalize()
+                < parse_use_tree("foo::{baz, qux as bar}").normalize()
         );
         assert!(
-            parse_use_tree("foo::{self as bar, baz}").normalize(true)
-                < parse_use_tree("foo::{baz, qux as bar}").normalize(true)
+            parse_use_tree("foo::{self as bar, baz}").normalize()
+                < parse_use_tree("foo::{baz, qux as bar}").normalize()
         );
 
-        assert!(parse_use_tree("foo").normalize(true) < parse_use_tree("Foo").normalize(true));
-        assert!(parse_use_tree("foo").normalize(true) < parse_use_tree("foo::Bar").normalize(true));
+        assert!(parse_use_tree("foo").normalize() < parse_use_tree("Foo").normalize());
+        assert!(parse_use_tree("foo").normalize() < parse_use_tree("foo::Bar").normalize());
 
         assert!(
-            parse_use_tree("std::cmp::{d, c, b, a}").normalize(true)
-                < parse_use_tree("std::cmp::{b, e, g, f}").normalize(true)
+            parse_use_tree("std::cmp::{d, c, b, a}").normalize()
+                < parse_use_tree("std::cmp::{b, e, g, f}").normalize()
         );
     }
 }