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::source_map::{self, BytePos, Span, DUMMY_SP};
17 use comment::combine_strs_with_missing_comments;
18 use config::{Edition, IndentStyle};
19 use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator};
20 use rewrite::{Rewrite, RewriteContext};
22 use source_map::SpanUtils;
24 use utils::{is_same_visibility, mk_sp, rewrite_ident};
25 use visitor::FmtVisitor;
30 /// Returns a name imported by a `use` declaration. e.g. returns `Ordering`
31 /// for `std::cmp::Ordering` and `self` for `std::cmp::self`.
32 pub fn path_to_imported_ident(path: &ast::Path) -> ast::Ident {
33 path.segments.last().unwrap().ident
36 impl<'a> FmtVisitor<'a> {
37 pub fn format_import(&mut self, item: &ast::Item, tree: &ast::UseTree) {
38 let span = item.span();
39 let shape = self.shape();
40 let rw = UseTree::from_ast(
44 Some(item.vis.clone()),
46 Some(item.attrs.clone()),
48 .rewrite_top_level(&self.get_context(), shape);
50 Some(ref s) if s.is_empty() => {
51 // Format up to last newline
52 let prev_span = mk_sp(self.last_pos, source!(self, span).lo());
53 let trimmed_snippet = self.snippet(prev_span).trim_right();
54 let span_end = self.last_pos + BytePos(trimmed_snippet.len() as u32);
55 self.format_missing(span_end);
56 // We have an excessive newline from the removed import.
57 if self.buffer.ends_with('\n') {
59 self.line_number -= 1;
61 self.last_pos = source!(self, span).hi();
64 self.format_missing_with_indent(source!(self, span).lo());
66 self.last_pos = source!(self, span).hi();
69 self.format_missing_with_indent(source!(self, span).lo());
70 self.format_missing(source!(self, span).hi());
76 // Ordering of imports
78 // We order imports by translating to our own representation and then sorting.
79 // The Rust AST data structures are really bad for this. Rustfmt applies a bunch
80 // of normalisations to imports and since we want to sort based on the result
81 // of these (and to maintain idempotence) we must apply the same normalisations
82 // to the data structures for sorting.
84 // We sort `self` and `super` before other imports, then identifier imports,
85 // then glob imports, then lists of imports. We do not take aliases into account
86 // when ordering unless the imports are identical except for the alias (rare in
89 // FIXME(#2531) - we should unify the comparison code here with the formatting
90 // code elsewhere since we are essentially string-ifying twice. Furthermore, by
91 // parsing to our own format on comparison, we repeat a lot of work when
94 // FIXME we do a lot of allocation to make our own representation.
95 #[derive(Clone, Eq, PartialEq)]
97 Ident(String, Option<String>),
99 Super(Option<String>),
106 pub path: Vec<UseSegment>,
108 // Comment information within nested use tree.
109 pub list_item: Option<ListItem>,
110 // Additional fields for top level use items.
111 // Should we have another struct for top-level use items rather than reusing this?
112 visibility: Option<ast::Visibility>,
113 attrs: Option<Vec<ast::Attribute>>,
116 impl PartialEq for UseTree {
117 fn eq(&self, other: &UseTree) -> bool {
118 self.path == other.path
121 impl Eq for UseTree {}
123 impl Spanned for UseTree {
124 fn span(&self) -> Span {
125 let lo = if let Some(ref attrs) = self.attrs {
126 attrs.iter().next().map_or(self.span.lo(), |a| a.span.lo())
130 mk_sp(lo, self.span.hi())
135 // Clone a version of self with any top-level alias removed.
136 fn remove_alias(&self) -> UseSegment {
138 UseSegment::Ident(ref s, _) => UseSegment::Ident(s.clone(), None),
139 UseSegment::Slf(_) => UseSegment::Slf(None),
140 UseSegment::Super(_) => UseSegment::Super(None),
145 fn from_path_segment(
146 context: &RewriteContext,
147 path_seg: &ast::PathSegment,
149 ) -> Option<UseSegment> {
150 let name = rewrite_ident(context, path_seg.ident);
151 if name.is_empty() || name == "{{root}}" {
155 "self" => UseSegment::Slf(None),
156 "super" => UseSegment::Super(None),
158 let mod_sep = if modsep { "::" } else { "" };
159 UseSegment::Ident(format!("{}{}", mod_sep, name), None)
165 pub fn merge_use_trees(use_trees: Vec<UseTree>) -> Vec<UseTree> {
166 let mut result = Vec::with_capacity(use_trees.len());
167 for use_tree in use_trees {
168 if use_tree.has_comment() || use_tree.attrs.is_some() {
169 result.push(use_tree);
173 for flattened in use_tree.flatten() {
174 merge_use_trees_inner(&mut result, flattened);
180 fn merge_use_trees_inner(trees: &mut Vec<UseTree>, use_tree: UseTree) {
181 for tree in trees.iter_mut() {
182 if tree.share_prefix(&use_tree) {
183 tree.merge(&use_tree);
188 trees.push(use_tree);
191 impl fmt::Debug for UseTree {
192 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
193 fmt::Display::fmt(self, f)
197 impl fmt::Debug for UseSegment {
198 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
199 fmt::Display::fmt(self, f)
203 impl fmt::Display for UseSegment {
204 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
206 UseSegment::Glob => write!(f, "*"),
207 UseSegment::Ident(ref s, _) => write!(f, "{}", s),
208 UseSegment::Slf(..) => write!(f, "self"),
209 UseSegment::Super(..) => write!(f, "super"),
210 UseSegment::List(ref list) => {
212 for (i, item) in list.iter().enumerate() {
213 let is_last = i == list.len() - 1;
214 write!(f, "{}", item)?;
224 impl fmt::Display for UseTree {
225 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
226 for (i, segment) in self.path.iter().enumerate() {
227 let is_last = i == self.path.len() - 1;
228 write!(f, "{}", segment)?;
238 // Rewrite use tree with `use ` and a trailing `;`.
239 pub fn rewrite_top_level(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
240 let vis = self.visibility.as_ref().map_or(Cow::from(""), |vis| {
241 ::utils::format_visibility(context, &vis)
244 .rewrite(context, shape.offset_left(vis.len())?)
249 format!("{}use {};", vis, s)
252 if let Some(ref attrs) = self.attrs {
253 let attr_str = attrs.rewrite(context, shape)?;
254 let lo = attrs.last().as_ref()?.span().hi();
255 let hi = self.span.lo();
256 let span = mk_sp(lo, hi);
257 combine_strs_with_missing_comments(context, &attr_str, &use_str, span, shape, false)
263 // FIXME: Use correct span?
264 // The given span is essentially incorrect, since we are reconstructing
265 // use statements. This should not be a problem, though, since we have
266 // already tried to extract comment and observed that there are no comment
267 // around the given use item, and the span will not be used afterward.
268 fn from_path(path: Vec<UseSegment>, span: Span) -> UseTree {
278 pub fn from_ast_with_normalization(
279 context: &RewriteContext,
281 ) -> Option<UseTree> {
283 ast::ItemKind::Use(ref use_tree) => Some(
288 Some(item.vis.clone()),
289 Some(item.span.lo()),
290 if item.attrs.is_empty() {
293 Some(item.attrs.clone())
303 context: &RewriteContext,
305 list_item: Option<ListItem>,
306 visibility: Option<ast::Visibility>,
307 opt_lo: Option<BytePos>,
308 attrs: Option<Vec<ast::Attribute>>,
310 let span = if let Some(lo) = opt_lo {
311 mk_sp(lo, a.span.hi())
315 let mut result = UseTree {
323 let leading_modsep = context.config.edition() == Edition::Edition2018
324 && a.prefix.to_string().len() > 2
325 && a.prefix.to_string().starts_with("::");
327 let mut modsep = leading_modsep;
329 for p in &a.prefix.segments {
330 if let Some(use_segment) = UseSegment::from_path_segment(context, p, modsep) {
331 result.path.push(use_segment);
337 UseTreeKind::Glob => {
338 result.path.push(UseSegment::Glob);
340 UseTreeKind::Nested(ref list) => {
341 // Extract comments between nested use items.
342 // This needs to be done before sorting use items.
343 let items: Vec<_> = itemize_list(
344 context.snippet_provider,
345 list.iter().map(|(tree, _)| tree),
348 |tree| tree.span.lo(),
349 |tree| tree.span.hi(),
350 |_| Some("".to_owned()), // We only need comments for now.
351 context.snippet_provider.span_after(a.span, "{"),
356 result.path.push(UseSegment::List(
358 .zip(items.into_iter())
359 .map(|(t, list_item)| {
360 Self::from_ast(context, &t.0, Some(list_item), None, None, None)
365 UseTreeKind::Simple(ref rename, ..) => {
366 let name = rewrite_ident(context, path_to_imported_ident(&a.prefix)).to_owned();
367 let alias = rename.and_then(|ident| {
368 if ident.name == "_" {
371 } else if ident == path_to_imported_ident(&a.prefix) {
374 Some(rewrite_ident(context, ident).to_owned())
377 let segment = match name.as_ref() {
378 "self" => UseSegment::Slf(alias),
379 "super" => UseSegment::Super(alias),
380 _ => UseSegment::Ident(name, alias),
383 // `name` is already in result.
385 result.path.push(segment);
391 // Do the adjustments that rustfmt does elsewhere to use paths.
392 pub fn normalize(mut self) -> UseTree {
393 let mut last = self.path.pop().expect("Empty use tree?");
394 // Hack around borrow checker.
395 let mut normalize_sole_list = false;
396 let mut aliased_self = false;
398 // Remove foo::{} or self without attributes.
400 _ if self.attrs.is_some() => (),
401 UseSegment::List(ref list) if list.is_empty() => {
405 UseSegment::Slf(None) if self.path.is_empty() && self.visibility.is_some() => {
412 // Normalise foo::self -> foo.
413 if let UseSegment::Slf(None) = last {
414 if !self.path.is_empty() {
419 // Normalise foo::self as bar -> foo as bar.
420 if let UseSegment::Slf(_) = last {
421 match self.path.last() {
423 Some(UseSegment::Ident(_, None)) => {
430 let mut done = false;
432 match self.path.last_mut() {
433 Some(UseSegment::Ident(_, ref mut old_rename)) => {
434 assert!(old_rename.is_none());
435 if let UseSegment::Slf(Some(rename)) = last.clone() {
436 *old_rename = Some(rename);
448 // Normalise foo::{bar} -> foo::bar
449 if let UseSegment::List(ref list) = last {
451 normalize_sole_list = true;
455 if normalize_sole_list {
457 UseSegment::List(list) => {
458 for seg in &list[0].path {
459 self.path.push(seg.clone());
461 return self.normalize();
467 // Recursively normalize elements of a list use (including sorting the list).
468 if let UseSegment::List(list) = last {
471 .map(|ut| ut.normalize())
472 .collect::<Vec<_>>();
474 last = UseSegment::List(list);
477 self.path.push(last);
481 fn has_comment(&self) -> bool {
482 self.list_item.as_ref().map_or(false, ListItem::has_comment)
485 fn same_visibility(&self, other: &UseTree) -> bool {
486 match (&self.visibility, &other.visibility) {
488 Some(source_map::Spanned {
489 node: ast::VisibilityKind::Inherited,
496 Some(source_map::Spanned {
497 node: ast::VisibilityKind::Inherited,
501 | (None, None) => true,
502 (Some(ref a), Some(ref b)) => is_same_visibility(a, b),
507 fn share_prefix(&self, other: &UseTree) -> bool {
508 if self.path.is_empty()
509 || other.path.is_empty()
510 || self.attrs.is_some()
511 || !self.same_visibility(other)
515 self.path[0] == other.path[0]
519 fn flatten(self) -> Vec<UseTree> {
520 if self.path.is_empty() {
523 match self.path.clone().last().unwrap() {
524 UseSegment::List(list) => {
525 let prefix = &self.path[..self.path.len() - 1];
526 let mut result = vec![];
527 for nested_use_tree in list {
528 for flattend in &mut nested_use_tree.clone().flatten() {
529 let mut new_path = prefix.to_vec();
530 new_path.append(&mut flattend.path);
531 result.push(UseTree {
535 visibility: self.visibility.clone(),
547 fn merge(&mut self, other: &UseTree) {
548 let mut new_path = vec![];
553 .zip(other.path.clone().into_iter())
561 if let Some(merged) = merge_rest(&self.path, &other.path, new_path.len()) {
562 new_path.push(merged);
563 self.span = self.span.to(other.span);
565 self.path = new_path;
569 fn merge_rest(a: &[UseSegment], b: &[UseSegment], len: usize) -> Option<UseSegment> {
570 let a_rest = &a[len..];
571 let b_rest = &b[len..];
572 if a_rest.is_empty() && b_rest.is_empty() {
575 if a_rest.is_empty() {
576 return Some(UseSegment::List(vec![
577 UseTree::from_path(vec![UseSegment::Slf(None)], DUMMY_SP),
578 UseTree::from_path(b_rest.to_vec(), DUMMY_SP),
581 if b_rest.is_empty() {
582 return Some(UseSegment::List(vec![
583 UseTree::from_path(vec![UseSegment::Slf(None)], DUMMY_SP),
584 UseTree::from_path(a_rest.to_vec(), DUMMY_SP),
587 if let UseSegment::List(mut list) = a_rest[0].clone() {
588 merge_use_trees_inner(&mut list, UseTree::from_path(b_rest.to_vec(), DUMMY_SP));
590 return Some(UseSegment::List(list.clone()));
593 UseTree::from_path(a_rest.to_vec(), DUMMY_SP),
594 UseTree::from_path(b_rest.to_vec(), DUMMY_SP),
597 Some(UseSegment::List(list))
600 impl PartialOrd for UseSegment {
601 fn partial_cmp(&self, other: &UseSegment) -> Option<Ordering> {
602 Some(self.cmp(other))
605 impl PartialOrd for UseTree {
606 fn partial_cmp(&self, other: &UseTree) -> Option<Ordering> {
607 Some(self.cmp(other))
610 impl Ord for UseSegment {
611 fn cmp(&self, other: &UseSegment) -> Ordering {
612 use self::UseSegment::*;
614 fn is_upper_snake_case(s: &str) -> bool {
616 .all(|c| c.is_uppercase() || c == '_' || c.is_numeric())
619 match (self, other) {
620 (&Slf(ref a), &Slf(ref b)) | (&Super(ref a), &Super(ref b)) => a.cmp(b),
621 (&Glob, &Glob) => Ordering::Equal,
622 (&Ident(ref ia, ref aa), &Ident(ref ib, ref ab)) => {
623 // snake_case < CamelCase < UPPER_SNAKE_CASE
624 if ia.starts_with(char::is_uppercase) && ib.starts_with(char::is_lowercase) {
625 return Ordering::Greater;
627 if ia.starts_with(char::is_lowercase) && ib.starts_with(char::is_uppercase) {
628 return Ordering::Less;
630 if is_upper_snake_case(ia) && !is_upper_snake_case(ib) {
631 return Ordering::Greater;
633 if !is_upper_snake_case(ia) && is_upper_snake_case(ib) {
634 return Ordering::Less;
636 let ident_ord = ia.cmp(ib);
637 if ident_ord != Ordering::Equal {
640 if aa.is_none() && ab.is_some() {
641 return Ordering::Less;
643 if aa.is_some() && ab.is_none() {
644 return Ordering::Greater;
648 (&List(ref a), &List(ref b)) => {
649 for (a, b) in a.iter().zip(b.iter()) {
651 if ord != Ordering::Equal {
656 a.len().cmp(&b.len())
658 (&Slf(_), _) => Ordering::Less,
659 (_, &Slf(_)) => Ordering::Greater,
660 (&Super(_), _) => Ordering::Less,
661 (_, &Super(_)) => Ordering::Greater,
662 (&Ident(..), _) => Ordering::Less,
663 (_, &Ident(..)) => Ordering::Greater,
664 (&Glob, _) => Ordering::Less,
665 (_, &Glob) => Ordering::Greater,
669 impl Ord for UseTree {
670 fn cmp(&self, other: &UseTree) -> Ordering {
671 for (a, b) in self.path.iter().zip(other.path.iter()) {
673 // The comparison without aliases is a hack to avoid situations like
674 // comparing `a::b` to `a as c` - where the latter should be ordered
675 // first since it is shorter.
676 if ord != Ordering::Equal && a.remove_alias().cmp(&b.remove_alias()) != Ordering::Equal
682 self.path.len().cmp(&other.path.len())
686 fn rewrite_nested_use_tree(
687 context: &RewriteContext,
688 use_tree_list: &[UseTree],
690 ) -> Option<String> {
691 let mut list_items = Vec::with_capacity(use_tree_list.len());
692 let nested_shape = match context.config.imports_indent() {
693 IndentStyle::Block => shape
694 .block_indent(context.config.tab_spaces())
695 .with_max_width(context.config)
697 IndentStyle::Visual => shape.visual_indent(0),
699 for use_tree in use_tree_list {
700 if let Some(mut list_item) = use_tree.list_item.clone() {
701 list_item.item = use_tree.rewrite(context, nested_shape);
702 list_items.push(list_item);
704 list_items.push(ListItem::from_str(use_tree.rewrite(context, nested_shape)?));
707 let has_nested_list = use_tree_list.iter().any(|use_segment| {
711 .map_or(false, |last_segment| match last_segment {
712 UseSegment::List(..) => true,
717 let remaining_width = if has_nested_list {
720 shape.width.saturating_sub(2)
723 let tactic = definitive_tactic(
725 context.config.imports_layout(),
730 let ends_with_newline = context.config.imports_indent() == IndentStyle::Block
731 && tactic != DefinitiveListTactic::Horizontal;
732 let trailing_separator = if ends_with_newline {
733 context.config.trailing_comma()
735 SeparatorTactic::Never
737 let fmt = ListFormatting::new(nested_shape, context.config)
739 .trailing_separator(trailing_separator)
740 .ends_with_newline(ends_with_newline)
741 .preserve_newline(true)
742 .nested(has_nested_list);
744 let list_str = write_list(&list_items, &fmt)?;
746 let result = if (list_str.contains('\n') || list_str.len() > remaining_width)
747 && context.config.imports_indent() == IndentStyle::Block
751 nested_shape.indent.to_string(context.config),
753 shape.indent.to_string(context.config)
756 format!("{{{}}}", list_str)
762 impl Rewrite for UseSegment {
763 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
765 UseSegment::Ident(ref ident, Some(ref rename)) => format!("{} as {}", ident, rename),
766 UseSegment::Ident(ref ident, None) => ident.clone(),
767 UseSegment::Slf(Some(ref rename)) => format!("self as {}", rename),
768 UseSegment::Slf(None) => "self".to_owned(),
769 UseSegment::Super(Some(ref rename)) => format!("super as {}", rename),
770 UseSegment::Super(None) => "super".to_owned(),
771 UseSegment::Glob => "*".to_owned(),
772 UseSegment::List(ref use_tree_list) => rewrite_nested_use_tree(
776 shape.offset_left(1)?.sub_width(1)?,
782 impl Rewrite for UseTree {
783 // This does NOT format attributes and visibility or add a trailing `;`.
784 fn rewrite(&self, context: &RewriteContext, mut shape: Shape) -> Option<String> {
785 let mut result = String::with_capacity(256);
786 let mut iter = self.path.iter().peekable();
787 while let Some(ref segment) = iter.next() {
788 let segment_str = segment.rewrite(context, shape)?;
789 result.push_str(&segment_str);
790 if iter.peek().is_some() {
791 result.push_str("::");
793 shape = shape.offset_left(2 + segment_str.len())?;
803 use syntax::source_map::DUMMY_SP;
805 // Parse the path part of an import. This parser is not robust and is only
806 // suitable for use in a test harness.
807 fn parse_use_tree(s: &str) -> UseTree {
808 use std::iter::Peekable;
813 input: Peekable<Chars<'a>>,
816 impl<'a> Parser<'a> {
818 self.input.next().unwrap();
821 fn eat(&mut self, c: char) {
822 assert!(self.input.next().unwrap() == c);
826 result: &mut Vec<UseSegment>,
828 alias_buf: &mut Option<String>,
831 let mut alias = None;
832 swap(alias_buf, &mut alias);
834 result.push(UseSegment::Slf(alias));
835 *buf = String::new();
837 } else if buf == "super" {
838 result.push(UseSegment::Super(alias));
839 *buf = String::new();
842 let mut name = String::new();
843 swap(buf, &mut name);
844 result.push(UseSegment::Ident(name, alias));
849 fn parse_in_list(&mut self) -> UseTree {
850 let mut result = vec![];
851 let mut buf = String::new();
852 let mut alias_buf = None;
853 while let Some(&c) = self.input.peek() {
856 assert!(buf.is_empty());
858 result.push(UseSegment::List(self.parse_list()));
862 assert!(buf.is_empty());
864 result.push(UseSegment::Glob);
869 Self::push_segment(&mut result, &mut buf, &mut alias_buf);
872 Self::push_segment(&mut result, &mut buf, &mut alias_buf);
886 alias_buf = Some(String::new());
890 if let Some(ref mut buf) = alias_buf {
898 Self::push_segment(&mut result, &mut buf, &mut alias_buf);
908 fn parse_list(&mut self) -> Vec<UseTree> {
909 let mut result = vec![];
911 match self.input.peek().unwrap() {
912 ',' | ' ' => self.bump(),
916 _ => result.push(self.parse_in_list()),
922 let mut parser = Parser {
923 input: s.chars().peekable(),
925 parser.parse_in_list()
928 macro parse_use_trees($($s:expr),* $(,)*) {
930 $(parse_use_tree($s),)*
935 fn test_use_tree_merge() {
936 macro test_merge([$($input:expr),* $(,)*], [$($output:expr),* $(,)*]) {
938 merge_use_trees(parse_use_trees!($($input,)*)),
939 parse_use_trees!($($output,)*),
943 test_merge!(["a::b::{c, d}", "a::b::{e, f}"], ["a::b::{c, d, e, f}"]);
944 test_merge!(["a::b::c", "a::b"], ["a::b::{self, c}"]);
945 test_merge!(["a::b", "a::b"], ["a::b"]);
946 test_merge!(["a", "a::b", "a::b::c"], ["a::{self, b::{self, c}}"]);
948 ["a::{b::{self, c}, d::e}", "a::d::f"],
949 ["a::{b::{self, c}, d::{e, f}}"]
952 ["a::d::f", "a::{b::{self, c}, d::e}"],
953 ["a::{b::{self, c}, d::{e, f}}"]
956 ["a::{c, d, b}", "a::{d, e, b, a, f}", "a::{f, g, c}"],
957 ["a::{a, b, c, d, e, f, g}"]
962 fn test_use_tree_flatten() {
964 parse_use_tree("a::b::{c, d, e, f}").flatten(),
965 parse_use_trees!("a::b::c", "a::b::d", "a::b::e", "a::b::f",)
969 parse_use_tree("a::b::{c::{d, e, f}, g, h::{i, j, k}}").flatten(),
983 fn test_use_tree_normalize() {
984 assert_eq!(parse_use_tree("a::self").normalize(), parse_use_tree("a"));
986 parse_use_tree("a::self as foo").normalize(),
987 parse_use_tree("a as foo")
989 assert_eq!(parse_use_tree("a::{self}").normalize(), parse_use_tree("a"));
990 assert_eq!(parse_use_tree("a::{b}").normalize(), parse_use_tree("a::b"));
992 parse_use_tree("a::{b, c::self}").normalize(),
993 parse_use_tree("a::{b, c}")
996 parse_use_tree("a::{b as bar, c::self}").normalize(),
997 parse_use_tree("a::{b as bar, c}")
1002 fn test_use_tree_ord() {
1003 assert!(parse_use_tree("a").normalize() < parse_use_tree("aa").normalize());
1004 assert!(parse_use_tree("a").normalize() < parse_use_tree("a::a").normalize());
1005 assert!(parse_use_tree("a").normalize() < parse_use_tree("*").normalize());
1006 assert!(parse_use_tree("a").normalize() < parse_use_tree("{a, b}").normalize());
1007 assert!(parse_use_tree("*").normalize() < parse_use_tree("{a, b}").normalize());
1010 parse_use_tree("aaaaaaaaaaaaaaa::{bb, cc, dddddddd}").normalize()
1011 < parse_use_tree("aaaaaaaaaaaaaaa::{bb, cc, ddddddddd}").normalize()
1014 parse_use_tree("serde::de::{Deserialize}").normalize()
1015 < parse_use_tree("serde_json").normalize()
1017 assert!(parse_use_tree("a::b::c").normalize() < parse_use_tree("a::b::*").normalize());
1019 parse_use_tree("foo::{Bar, Baz}").normalize()
1020 < parse_use_tree("{Bar, Baz}").normalize()
1024 parse_use_tree("foo::{self as bar}").normalize()
1025 < parse_use_tree("foo::{qux as bar}").normalize()
1028 parse_use_tree("foo::{qux as bar}").normalize()
1029 < parse_use_tree("foo::{baz, qux as bar}").normalize()
1032 parse_use_tree("foo::{self as bar, baz}").normalize()
1033 < parse_use_tree("foo::{baz, qux as bar}").normalize()
1036 assert!(parse_use_tree("foo").normalize() < parse_use_tree("Foo").normalize());
1037 assert!(parse_use_tree("foo").normalize() < parse_use_tree("foo::Bar").normalize());
1040 parse_use_tree("std::cmp::{d, c, b, a}").normalize()
1041 < parse_use_tree("std::cmp::{b, e, g, f}").normalize()