1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use std::cmp::Ordering;
14 use syntax::ast::{self, UseTreeKind};
15 use syntax::codemap::{self, BytePos, Span, DUMMY_SP};
17 use codemap::SpanUtils;
18 use config::IndentStyle;
19 use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator};
20 use rewrite::{Rewrite, RewriteContext};
24 use visitor::FmtVisitor;
29 /// Returns a name imported by a `use` declaration. e.g. returns `Ordering`
30 /// for `std::cmp::Ordering` and `self` for `std::cmp::self`.
31 pub fn path_to_imported_ident(path: &ast::Path) -> ast::Ident {
32 path.segments.last().unwrap().ident
35 impl<'a> FmtVisitor<'a> {
36 pub fn format_import(&mut self, item: &ast::Item, tree: &ast::UseTree) {
38 let shape = self.shape();
39 let rw = UseTree::from_ast(
43 Some(item.vis.clone()),
45 Some(item.attrs.clone()),
46 ).rewrite_top_level(&self.get_context(), shape);
48 Some(ref s) if s.is_empty() => {
49 // Format up to last newline
50 let prev_span = mk_sp(self.last_pos, source!(self, span).lo());
51 let trimmed_snippet = self.snippet(prev_span).trim_right();
52 let span_end = self.last_pos + BytePos(trimmed_snippet.len() as u32);
53 self.format_missing(span_end);
54 // We have an excessive newline from the removed import.
55 if self.buffer.ends_with('\n') {
57 self.line_number -= 1;
59 self.last_pos = source!(self, span).hi();
62 self.format_missing_with_indent(source!(self, span).lo());
64 self.last_pos = source!(self, span).hi();
67 self.format_missing_with_indent(source!(self, span).lo());
68 self.format_missing(source!(self, span).hi());
74 // Ordering of imports
76 // We order imports by translating to our own representation and then sorting.
77 // The Rust AST data structures are really bad for this. Rustfmt applies a bunch
78 // of normalisations to imports and since we want to sort based on the result
79 // of these (and to maintain idempotence) we must apply the same normalisations
80 // to the data structures for sorting.
82 // We sort `self` and `super` before other imports, then identifier imports,
83 // then glob imports, then lists of imports. We do not take aliases into account
84 // when ordering unless the imports are identical except for the alias (rare in
87 // FIXME(#2531) - we should unify the comparison code here with the formatting
88 // code elsewhere since we are essentially string-ifying twice. Furthermore, by
89 // parsing to our own format on comparison, we repeat a lot of work when
92 // FIXME we do a lot of allocation to make our own representation.
93 #[derive(Clone, Eq, PartialEq)]
95 Ident(String, Option<String>),
97 Super(Option<String>),
104 pub path: Vec<UseSegment>,
106 // Comment information within nested use tree.
107 pub list_item: Option<ListItem>,
108 // Additional fields for top level use items.
109 // Should we have another struct for top-level use items rather than reusing this?
110 visibility: Option<ast::Visibility>,
111 attrs: Option<Vec<ast::Attribute>>,
114 impl PartialEq for UseTree {
115 fn eq(&self, other: &UseTree) -> bool {
116 self.path == other.path
119 impl Eq for UseTree {}
122 // Clone a version of self with any top-level alias removed.
123 fn remove_alias(&self) -> UseSegment {
125 UseSegment::Ident(ref s, _) => UseSegment::Ident(s.clone(), None),
126 UseSegment::Slf(_) => UseSegment::Slf(None),
127 UseSegment::Super(_) => UseSegment::Super(None),
132 fn from_path_segment(path_seg: &ast::PathSegment) -> Option<UseSegment> {
133 let name = path_seg.ident.name.as_str();
134 if name == "{{root}}" {
137 Some(if name == "self" {
138 UseSegment::Slf(None)
139 } else if name == "super" {
140 UseSegment::Super(None)
142 UseSegment::Ident((*name).to_owned(), None)
147 pub fn merge_use_trees(use_trees: Vec<UseTree>) -> Vec<UseTree> {
148 let mut result = Vec::with_capacity(use_trees.len());
149 for use_tree in use_trees {
150 if use_tree.has_comment() || use_tree.attrs.is_some() {
151 result.push(use_tree);
155 for flattened in use_tree.flatten() {
156 merge_use_trees_inner(&mut result, flattened);
162 fn merge_use_trees_inner(trees: &mut Vec<UseTree>, use_tree: UseTree) {
163 for tree in trees.iter_mut() {
164 if tree.share_prefix(&use_tree) {
165 tree.merge(use_tree);
170 trees.push(use_tree);
173 impl fmt::Debug for UseTree {
174 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
175 fmt::Display::fmt(self, f)
179 impl fmt::Debug for UseSegment {
180 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
181 fmt::Display::fmt(self, f)
185 impl fmt::Display for UseSegment {
186 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
188 UseSegment::Glob => write!(f, "*"),
189 UseSegment::Ident(ref s, _) => write!(f, "{}", s),
190 UseSegment::Slf(..) => write!(f, "self"),
191 UseSegment::Super(..) => write!(f, "super"),
192 UseSegment::List(ref list) => {
194 for (i, item) in list.iter().enumerate() {
195 let is_last = i == list.len() - 1;
196 write!(f, "{}", item)?;
206 impl fmt::Display for UseTree {
207 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
208 for (i, segment) in self.path.iter().enumerate() {
209 let is_last = i == self.path.len() - 1;
210 write!(f, "{}", segment)?;
220 // Rewrite use tree with `use ` and a trailing `;`.
221 pub fn rewrite_top_level(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
222 let mut result = String::with_capacity(256);
223 if let Some(ref attrs) = self.attrs {
224 result.push_str(&attrs.rewrite(context, shape)?);
225 if !result.is_empty() {
226 result.push_str(&shape.indent.to_string_with_newline(context.config));
230 let vis = self.visibility
232 .map_or(Cow::from(""), |vis| ::utils::format_visibility(&vis));
233 result.push_str(&self.rewrite(context, shape.offset_left(vis.len())?)
238 format!("{}use {};", vis, s)
244 // FIXME: Use correct span?
245 // The given span is essentially incorrect, since we are reconstructing
246 // use statements. This should not be a problem, though, since we have
247 // already tried to extract comment and observed that there are no comment
248 // around the given use item, and the span will not be used afterward.
249 fn from_path(path: Vec<UseSegment>, span: Span) -> UseTree {
259 pub fn from_ast_with_normalization(
260 context: &RewriteContext,
262 ) -> Option<UseTree> {
264 ast::ItemKind::Use(ref use_tree) => Some(
269 Some(item.vis.clone()),
270 Some(item.span().lo()),
271 if item.attrs.is_empty() {
274 Some(item.attrs.clone())
283 context: &RewriteContext,
285 list_item: Option<ListItem>,
286 visibility: Option<ast::Visibility>,
287 opt_lo: Option<BytePos>,
288 attrs: Option<Vec<ast::Attribute>>,
290 let span = if let Some(lo) = opt_lo {
291 mk_sp(lo, a.span.hi())
295 let mut result = UseTree {
302 for p in &a.prefix.segments {
303 if let Some(use_segment) = UseSegment::from_path_segment(p) {
304 result.path.push(use_segment);
308 UseTreeKind::Glob => {
309 result.path.push(UseSegment::Glob);
311 UseTreeKind::Nested(ref list) => {
312 // Extract comments between nested use items.
313 // This needs to be done before sorting use items.
314 let items: Vec<_> = itemize_list(
315 context.snippet_provider,
316 list.iter().map(|(tree, _)| tree),
319 |tree| tree.span.lo(),
320 |tree| tree.span.hi(),
321 |_| Some("".to_owned()), // We only need comments for now.
322 context.snippet_provider.span_after(a.span, "{"),
326 result.path.push(UseSegment::List(
328 .zip(items.into_iter())
329 .map(|(t, list_item)| {
330 Self::from_ast(context, &t.0, Some(list_item), None, None, None)
335 UseTreeKind::Simple(ref rename) => {
336 let mut name = (*path_to_imported_ident(&a.prefix).name.as_str()).to_owned();
337 let alias = rename.and_then(|ident| {
338 if ident == path_to_imported_ident(&a.prefix) {
341 Some(ident.to_string())
345 let segment = if &name == "self" {
346 UseSegment::Slf(alias)
347 } else if &name == "super" {
348 UseSegment::Super(alias)
350 UseSegment::Ident(name, alias)
353 // `name` is already in result.
355 result.path.push(segment);
361 // Do the adjustments that rustfmt does elsewhere to use paths.
362 pub fn normalize(mut self) -> UseTree {
363 let mut last = self.path.pop().expect("Empty use tree?");
364 // Hack around borrow checker.
365 let mut normalize_sole_list = false;
366 let mut aliased_self = false;
368 // Remove foo::{} or self without attributes.
370 _ if self.attrs.is_some() => (),
371 UseSegment::List(ref list) if list.is_empty() => {
375 UseSegment::Slf(None) if self.path.is_empty() && self.visibility.is_some() => {
382 // Normalise foo::self -> foo.
383 if let UseSegment::Slf(None) = last {
384 if !self.path.is_empty() {
389 // Normalise foo::self as bar -> foo as bar.
390 if let UseSegment::Slf(_) = last {
391 match self.path.last() {
393 Some(UseSegment::Ident(_, None)) => {
400 let mut done = false;
402 match self.path.last_mut() {
403 Some(UseSegment::Ident(_, ref mut old_rename)) => {
404 assert!(old_rename.is_none());
405 if let UseSegment::Slf(Some(rename)) = last.clone() {
406 *old_rename = Some(rename);
418 // Normalise foo::{bar} -> foo::bar
419 if let UseSegment::List(ref list) = last {
421 normalize_sole_list = true;
425 if normalize_sole_list {
427 UseSegment::List(list) => {
428 for seg in &list[0].path {
429 self.path.push(seg.clone());
431 return self.normalize();
437 // Recursively normalize elements of a list use (including sorting the list).
438 if let UseSegment::List(list) = last {
439 let mut list = list.into_iter()
440 .map(|ut| ut.normalize())
441 .collect::<Vec<_>>();
443 last = UseSegment::List(list);
446 self.path.push(last);
450 fn has_comment(&self) -> bool {
451 self.list_item.as_ref().map_or(false, ListItem::has_comment)
454 fn same_visibility(&self, other: &UseTree) -> bool {
455 match (&self.visibility, &other.visibility) {
457 Some(codemap::Spanned {
458 node: ast::VisibilityKind::Inherited,
465 Some(codemap::Spanned {
466 node: ast::VisibilityKind::Inherited,
470 | (None, None) => true,
472 Some(codemap::Spanned { node: lnode, .. }),
473 Some(codemap::Spanned { node: rnode, .. }),
479 fn share_prefix(&self, other: &UseTree) -> bool {
480 if self.path.is_empty() || other.path.is_empty() || self.attrs.is_some()
481 || !self.same_visibility(other)
485 self.path[0] == other.path[0]
489 fn flatten(self) -> Vec<UseTree> {
490 if self.path.is_empty() {
493 match self.path.clone().last().unwrap() {
494 UseSegment::List(list) => {
495 let prefix = &self.path[..self.path.len() - 1];
496 let mut result = vec![];
497 for nested_use_tree in list {
498 for mut flattend in &mut nested_use_tree.clone().flatten() {
499 let mut new_path = prefix.to_vec();
500 new_path.append(&mut flattend.path);
501 result.push(UseTree {
505 visibility: self.visibility.clone(),
517 fn merge(&mut self, other: UseTree) {
518 let mut new_path = vec![];
519 for (mut a, b) in self.path
522 .zip(other.path.clone().into_iter())
530 if let Some(merged) = merge_rest(&self.path, &other.path, new_path.len()) {
531 new_path.push(merged);
532 self.span = self.span.to(other.span);
534 self.path = new_path;
538 fn merge_rest(a: &[UseSegment], b: &[UseSegment], len: usize) -> Option<UseSegment> {
539 let a_rest = &a[len..];
540 let b_rest = &b[len..];
541 if a_rest.is_empty() && b_rest.is_empty() {
544 if a_rest.is_empty() {
545 return Some(UseSegment::List(vec![
546 UseTree::from_path(vec![UseSegment::Slf(None)], DUMMY_SP),
547 UseTree::from_path(b_rest.to_vec(), DUMMY_SP),
550 if b_rest.is_empty() {
551 return Some(UseSegment::List(vec![
552 UseTree::from_path(vec![UseSegment::Slf(None)], DUMMY_SP),
553 UseTree::from_path(a_rest.to_vec(), DUMMY_SP),
556 if let UseSegment::List(mut list) = a_rest[0].clone() {
557 merge_use_trees_inner(&mut list, UseTree::from_path(b_rest.to_vec(), DUMMY_SP));
559 return Some(UseSegment::List(list.clone()));
562 UseTree::from_path(a_rest.to_vec(), DUMMY_SP),
563 UseTree::from_path(b_rest.to_vec(), DUMMY_SP),
566 Some(UseSegment::List(list))
569 impl PartialOrd for UseSegment {
570 fn partial_cmp(&self, other: &UseSegment) -> Option<Ordering> {
571 Some(self.cmp(other))
574 impl PartialOrd for UseTree {
575 fn partial_cmp(&self, other: &UseTree) -> Option<Ordering> {
576 Some(self.cmp(other))
579 impl Ord for UseSegment {
580 fn cmp(&self, other: &UseSegment) -> Ordering {
581 use self::UseSegment::*;
583 fn is_upper_snake_case(s: &str) -> bool {
584 s.chars().all(|c| c.is_uppercase() || c == '_')
587 match (self, other) {
588 (&Slf(ref a), &Slf(ref b)) | (&Super(ref a), &Super(ref b)) => a.cmp(b),
589 (&Glob, &Glob) => Ordering::Equal,
590 (&Ident(ref ia, ref aa), &Ident(ref ib, ref ab)) => {
591 // snake_case < CamelCase < UPPER_SNAKE_CASE
592 if ia.starts_with(char::is_uppercase) && ib.starts_with(char::is_lowercase) {
593 return Ordering::Greater;
595 if ia.starts_with(char::is_lowercase) && ib.starts_with(char::is_uppercase) {
596 return Ordering::Less;
598 if is_upper_snake_case(ia) && !is_upper_snake_case(ib) {
599 return Ordering::Greater;
601 if !is_upper_snake_case(ia) && is_upper_snake_case(ib) {
602 return Ordering::Less;
604 let ident_ord = ia.cmp(ib);
605 if ident_ord != Ordering::Equal {
608 if aa.is_none() && ab.is_some() {
609 return Ordering::Less;
611 if aa.is_some() && ab.is_none() {
612 return Ordering::Greater;
616 (&List(ref a), &List(ref b)) => {
617 for (a, b) in a.iter().zip(b.iter()) {
619 if ord != Ordering::Equal {
624 a.len().cmp(&b.len())
626 (&Slf(_), _) => Ordering::Less,
627 (_, &Slf(_)) => Ordering::Greater,
628 (&Super(_), _) => Ordering::Less,
629 (_, &Super(_)) => Ordering::Greater,
630 (&Ident(..), _) => Ordering::Less,
631 (_, &Ident(..)) => Ordering::Greater,
632 (&Glob, _) => Ordering::Less,
633 (_, &Glob) => Ordering::Greater,
637 impl Ord for UseTree {
638 fn cmp(&self, other: &UseTree) -> Ordering {
639 for (a, b) in self.path.iter().zip(other.path.iter()) {
641 // The comparison without aliases is a hack to avoid situations like
642 // comparing `a::b` to `a as c` - where the latter should be ordered
643 // first since it is shorter.
644 if ord != Ordering::Equal && a.remove_alias().cmp(&b.remove_alias()) != Ordering::Equal
650 self.path.len().cmp(&other.path.len())
654 fn rewrite_nested_use_tree(
655 context: &RewriteContext,
656 use_tree_list: &[UseTree],
658 ) -> Option<String> {
659 let mut list_items = Vec::with_capacity(use_tree_list.len());
660 let nested_shape = match context.config.imports_indent() {
661 IndentStyle::Block => shape
662 .block_indent(context.config.tab_spaces())
663 .with_max_width(context.config)
665 IndentStyle::Visual => shape.visual_indent(0),
667 for use_tree in use_tree_list {
668 if let Some(mut list_item) = use_tree.list_item.clone() {
669 list_item.item = use_tree.rewrite(context, nested_shape);
670 list_items.push(list_item);
672 list_items.push(ListItem::from_str(use_tree.rewrite(context, nested_shape)?));
675 let has_nested_list = use_tree_list.iter().any(|use_segment| {
679 .map_or(false, |last_segment| match last_segment {
680 UseSegment::List(..) => true,
684 let (tactic, remaining_width) = if has_nested_list {
685 (DefinitiveListTactic::Vertical, 0)
687 let remaining_width = shape.width.checked_sub(2).unwrap_or(0);
688 let tactic = definitive_tactic(
690 context.config.imports_layout(),
694 (tactic, remaining_width)
697 let ends_with_newline = context.config.imports_indent() == IndentStyle::Block
698 && tactic != DefinitiveListTactic::Horizontal;
699 let fmt = ListFormatting {
702 trailing_separator: if ends_with_newline {
703 context.config.trailing_comma()
705 SeparatorTactic::Never
707 separator_place: SeparatorPlace::Back,
710 preserve_newline: true,
711 config: context.config,
714 let list_str = write_list(&list_items, &fmt)?;
716 let result = if (list_str.contains('\n') || list_str.len() > remaining_width)
717 && context.config.imports_indent() == IndentStyle::Block
721 nested_shape.indent.to_string(context.config),
723 shape.indent.to_string(context.config)
726 format!("{{{}}}", list_str)
732 impl Rewrite for UseSegment {
733 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
735 UseSegment::Ident(ref ident, Some(ref rename)) => format!("{} as {}", ident, rename),
736 UseSegment::Ident(ref ident, None) => ident.clone(),
737 UseSegment::Slf(Some(ref rename)) => format!("self as {}", rename),
738 UseSegment::Slf(None) => "self".to_owned(),
739 UseSegment::Super(Some(ref rename)) => format!("super as {}", rename),
740 UseSegment::Super(None) => "super".to_owned(),
741 UseSegment::Glob => "*".to_owned(),
742 UseSegment::List(ref use_tree_list) => rewrite_nested_use_tree(
746 shape.offset_left(1)?.sub_width(1)?,
752 impl Rewrite for UseTree {
753 // This does NOT format attributes and visibility or add a trailing `;`.
754 fn rewrite(&self, context: &RewriteContext, mut shape: Shape) -> Option<String> {
755 let mut result = String::with_capacity(256);
756 let mut iter = self.path.iter().peekable();
757 while let Some(ref segment) = iter.next() {
758 let segment_str = segment.rewrite(context, shape)?;
759 result.push_str(&segment_str);
760 if iter.peek().is_some() {
761 result.push_str("::");
763 shape = shape.offset_left(2 + segment_str.len())?;
773 use syntax::codemap::DUMMY_SP;
775 // Parse the path part of an import. This parser is not robust and is only
776 // suitable for use in a test harness.
777 fn parse_use_tree(s: &str) -> UseTree {
778 use std::iter::Peekable;
783 input: Peekable<Chars<'a>>,
786 impl<'a> Parser<'a> {
788 self.input.next().unwrap();
790 fn eat(&mut self, c: char) {
791 assert!(self.input.next().unwrap() == c);
794 result: &mut Vec<UseSegment>,
796 alias_buf: &mut Option<String>,
799 let mut alias = None;
800 swap(alias_buf, &mut alias);
802 result.push(UseSegment::Slf(alias));
803 *buf = String::new();
805 } else if buf == "super" {
806 result.push(UseSegment::Super(alias));
807 *buf = String::new();
810 let mut name = String::new();
811 swap(buf, &mut name);
812 result.push(UseSegment::Ident(name, alias));
816 fn parse_in_list(&mut self) -> UseTree {
817 let mut result = vec![];
818 let mut buf = String::new();
819 let mut alias_buf = None;
820 while let Some(&c) = self.input.peek() {
823 assert!(buf.is_empty());
825 result.push(UseSegment::List(self.parse_list()));
829 assert!(buf.is_empty());
831 result.push(UseSegment::Glob);
836 Self::push_segment(&mut result, &mut buf, &mut alias_buf);
839 Self::push_segment(&mut result, &mut buf, &mut alias_buf);
853 alias_buf = Some(String::new());
857 if let Some(ref mut buf) = alias_buf {
865 Self::push_segment(&mut result, &mut buf, &mut alias_buf);
875 fn parse_list(&mut self) -> Vec<UseTree> {
876 let mut result = vec![];
878 match self.input.peek().unwrap() {
879 ',' | ' ' => self.bump(),
883 _ => result.push(self.parse_in_list()),
889 let mut parser = Parser {
890 input: s.chars().peekable(),
892 parser.parse_in_list()
895 macro parse_use_trees($($s:expr),* $(,)*) {
897 $(parse_use_tree($s),)*
902 fn test_use_tree_merge() {
903 macro test_merge([$($input:expr),* $(,)*], [$($output:expr),* $(,)*]) {
905 merge_use_trees(parse_use_trees!($($input,)*)),
906 parse_use_trees!($($output,)*),
910 test_merge!(["a::b::{c, d}", "a::b::{e, f}"], ["a::b::{c, d, e, f}"]);
911 test_merge!(["a::b::c", "a::b"], ["a::b::{self, c}"]);
912 test_merge!(["a::b", "a::b"], ["a::b"]);
913 test_merge!(["a", "a::b", "a::b::c"], ["a::{self, b::{self, c}}"]);
915 ["a::{b::{self, c}, d::e}", "a::d::f"],
916 ["a::{b::{self, c}, d::{e, f}}"]
919 ["a::d::f", "a::{b::{self, c}, d::e}"],
920 ["a::{b::{self, c}, d::{e, f}}"]
923 ["a::{c, d, b}", "a::{d, e, b, a, f}", "a::{f, g, c}"],
924 ["a::{a, b, c, d, e, f, g}"]
929 fn test_use_tree_flatten() {
931 parse_use_tree("a::b::{c, d, e, f}").flatten(),
932 parse_use_trees!("a::b::c", "a::b::d", "a::b::e", "a::b::f",)
936 parse_use_tree("a::b::{c::{d, e, f}, g, h::{i, j, k}}").flatten(),
950 fn test_use_tree_normalize() {
951 assert_eq!(parse_use_tree("a::self").normalize(), parse_use_tree("a"));
953 parse_use_tree("a::self as foo").normalize(),
954 parse_use_tree("a as foo")
956 assert_eq!(parse_use_tree("a::{self}").normalize(), parse_use_tree("a"));
957 assert_eq!(parse_use_tree("a::{b}").normalize(), parse_use_tree("a::b"));
959 parse_use_tree("a::{b, c::self}").normalize(),
960 parse_use_tree("a::{b, c}")
963 parse_use_tree("a::{b as bar, c::self}").normalize(),
964 parse_use_tree("a::{b as bar, c}")
969 fn test_use_tree_ord() {
970 assert!(parse_use_tree("a").normalize() < parse_use_tree("aa").normalize());
971 assert!(parse_use_tree("a").normalize() < parse_use_tree("a::a").normalize());
972 assert!(parse_use_tree("a").normalize() < parse_use_tree("*").normalize());
973 assert!(parse_use_tree("a").normalize() < parse_use_tree("{a, b}").normalize());
974 assert!(parse_use_tree("*").normalize() < parse_use_tree("{a, b}").normalize());
977 parse_use_tree("aaaaaaaaaaaaaaa::{bb, cc, dddddddd}").normalize()
978 < parse_use_tree("aaaaaaaaaaaaaaa::{bb, cc, ddddddddd}").normalize()
981 parse_use_tree("serde::de::{Deserialize}").normalize()
982 < parse_use_tree("serde_json").normalize()
984 assert!(parse_use_tree("a::b::c").normalize() < parse_use_tree("a::b::*").normalize());
986 parse_use_tree("foo::{Bar, Baz}").normalize()
987 < parse_use_tree("{Bar, Baz}").normalize()
991 parse_use_tree("foo::{self as bar}").normalize()
992 < parse_use_tree("foo::{qux as bar}").normalize()
995 parse_use_tree("foo::{qux as bar}").normalize()
996 < parse_use_tree("foo::{baz, qux as bar}").normalize()
999 parse_use_tree("foo::{self as bar, baz}").normalize()
1000 < parse_use_tree("foo::{baz, qux as bar}").normalize()
1003 assert!(parse_use_tree("foo").normalize() < parse_use_tree("Foo").normalize());
1004 assert!(parse_use_tree("foo").normalize() < parse_use_tree("foo::Bar").normalize());
1007 parse_use_tree("std::cmp::{d, c, b, a}").normalize()
1008 < parse_use_tree("std::cmp::{b, e, g, f}").normalize()