]> git.lizzy.rs Git - rust.git/blob - src/imports.rs
Cargo update and clippy (#2643)
[rust.git] / src / imports.rs
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.
4 //
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.
10
11 use std::cmp::Ordering;
12
13 use config::lists::*;
14 use syntax::ast::{self, UseTreeKind};
15 use syntax::codemap::{self, BytePos, Span, DUMMY_SP};
16
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};
21 use shape::Shape;
22 use spanned::Spanned;
23 use utils::mk_sp;
24 use visitor::FmtVisitor;
25
26 use std::borrow::Cow;
27 use std::fmt;
28
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
33 }
34
35 impl<'a> FmtVisitor<'a> {
36     pub fn format_import(&mut self, item: &ast::Item, tree: &ast::UseTree) {
37         let span = item.span;
38         let shape = self.shape();
39         let rw = UseTree::from_ast(
40             &self.get_context(),
41             tree,
42             None,
43             Some(item.vis.clone()),
44             Some(item.span.lo()),
45             Some(item.attrs.clone()),
46         ).rewrite_top_level(&self.get_context(), shape);
47         match rw {
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') {
56                     self.buffer.pop();
57                     self.line_number -= 1;
58                 }
59                 self.last_pos = source!(self, span).hi();
60             }
61             Some(ref s) => {
62                 self.format_missing_with_indent(source!(self, span).lo());
63                 self.push_str(s);
64                 self.last_pos = source!(self, span).hi();
65             }
66             None => {
67                 self.format_missing_with_indent(source!(self, span).lo());
68                 self.format_missing(source!(self, span).hi());
69             }
70         }
71     }
72 }
73
74 // Ordering of imports
75
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.
81 //
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
85 // practice).
86
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
90 // sorting.
91
92 // FIXME we do a lot of allocation to make our own representation.
93 #[derive(Clone, Eq, PartialEq)]
94 pub enum UseSegment {
95     Ident(String, Option<String>),
96     Slf(Option<String>),
97     Super(Option<String>),
98     Glob,
99     List(Vec<UseTree>),
100 }
101
102 #[derive(Clone)]
103 pub struct UseTree {
104     pub path: Vec<UseSegment>,
105     pub span: Span,
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>>,
112 }
113
114 impl PartialEq for UseTree {
115     fn eq(&self, other: &UseTree) -> bool {
116         self.path == other.path
117     }
118 }
119 impl Eq for UseTree {}
120
121 impl UseSegment {
122     // Clone a version of self with any top-level alias removed.
123     fn remove_alias(&self) -> UseSegment {
124         match *self {
125             UseSegment::Ident(ref s, _) => UseSegment::Ident(s.clone(), None),
126             UseSegment::Slf(_) => UseSegment::Slf(None),
127             UseSegment::Super(_) => UseSegment::Super(None),
128             _ => self.clone(),
129         }
130     }
131
132     fn from_path_segment(path_seg: &ast::PathSegment) -> Option<UseSegment> {
133         let name = path_seg.ident.name.as_str();
134         if name == "{{root}}" {
135             return None;
136         }
137         Some(if name == "self" {
138             UseSegment::Slf(None)
139         } else if name == "super" {
140             UseSegment::Super(None)
141         } else {
142             UseSegment::Ident((*name).to_owned(), None)
143         })
144     }
145 }
146
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);
152             continue;
153         }
154
155         for flattened in use_tree.flatten() {
156             merge_use_trees_inner(&mut result, flattened);
157         }
158     }
159     result
160 }
161
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);
166             return;
167         }
168     }
169
170     trees.push(use_tree);
171 }
172
173 impl fmt::Debug for UseTree {
174     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
175         fmt::Display::fmt(self, f)
176     }
177 }
178
179 impl fmt::Debug for UseSegment {
180     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
181         fmt::Display::fmt(self, f)
182     }
183 }
184
185 impl fmt::Display for UseSegment {
186     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
187         match *self {
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) => {
193                 write!(f, "{{")?;
194                 for (i, item) in list.iter().enumerate() {
195                     let is_last = i == list.len() - 1;
196                     write!(f, "{}", item)?;
197                     if !is_last {
198                         write!(f, ", ")?;
199                     }
200                 }
201                 write!(f, "}}")
202             }
203         }
204     }
205 }
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)?;
211             if !is_last {
212                 write!(f, "::")?;
213             }
214         }
215         write!(f, "")
216     }
217 }
218
219 impl UseTree {
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));
227             }
228         }
229
230         let vis = self.visibility
231             .as_ref()
232             .map_or(Cow::from(""), |vis| ::utils::format_visibility(&vis));
233         result.push_str(&self.rewrite(context, shape.offset_left(vis.len())?)
234             .map(|s| {
235                 if s.is_empty() {
236                     s.to_owned()
237                 } else {
238                     format!("{}use {};", vis, s)
239                 }
240             })?);
241         Some(result)
242     }
243
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 {
250         UseTree {
251             path,
252             span,
253             list_item: None,
254             visibility: None,
255             attrs: None,
256         }
257     }
258
259     pub fn from_ast_with_normalization(
260         context: &RewriteContext,
261         item: &ast::Item,
262     ) -> Option<UseTree> {
263         match item.node {
264             ast::ItemKind::Use(ref use_tree) => Some(
265                 UseTree::from_ast(
266                     context,
267                     use_tree,
268                     None,
269                     Some(item.vis.clone()),
270                     Some(item.span().lo()),
271                     if item.attrs.is_empty() {
272                         None
273                     } else {
274                         Some(item.attrs.clone())
275                     },
276                 ).normalize(),
277             ),
278             _ => None,
279         }
280     }
281
282     fn from_ast(
283         context: &RewriteContext,
284         a: &ast::UseTree,
285         list_item: Option<ListItem>,
286         visibility: Option<ast::Visibility>,
287         opt_lo: Option<BytePos>,
288         attrs: Option<Vec<ast::Attribute>>,
289     ) -> UseTree {
290         let span = if let Some(lo) = opt_lo {
291             mk_sp(lo, a.span.hi())
292         } else {
293             a.span
294         };
295         let mut result = UseTree {
296             path: vec![],
297             span,
298             list_item,
299             visibility,
300             attrs,
301         };
302         for p in &a.prefix.segments {
303             if let Some(use_segment) = UseSegment::from_path_segment(p) {
304                 result.path.push(use_segment);
305             }
306         }
307         match a.kind {
308             UseTreeKind::Glob => {
309                 result.path.push(UseSegment::Glob);
310             }
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),
317                     "}",
318                     ",",
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, "{"),
323                     a.span.hi(),
324                     false,
325                 ).collect();
326                 result.path.push(UseSegment::List(
327                     list.iter()
328                         .zip(items.into_iter())
329                         .map(|(t, list_item)| {
330                             Self::from_ast(context, &t.0, Some(list_item), None, None, None)
331                         })
332                         .collect(),
333                 ));
334             }
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) {
339                         None
340                     } else {
341                         Some(ident.to_string())
342                     }
343                 });
344
345                 let segment = if &name == "self" {
346                     UseSegment::Slf(alias)
347                 } else if &name == "super" {
348                     UseSegment::Super(alias)
349                 } else {
350                     UseSegment::Ident(name, alias)
351                 };
352
353                 // `name` is already in result.
354                 result.path.pop();
355                 result.path.push(segment);
356             }
357         }
358         result
359     }
360
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;
367
368         // Remove foo::{} or self without attributes.
369         match last {
370             _ if self.attrs.is_some() => (),
371             UseSegment::List(ref list) if list.is_empty() => {
372                 self.path = vec![];
373                 return self;
374             }
375             UseSegment::Slf(None) if self.path.is_empty() && self.visibility.is_some() => {
376                 self.path = vec![];
377                 return self;
378             }
379             _ => (),
380         }
381
382         // Normalise foo::self -> foo.
383         if let UseSegment::Slf(None) = last {
384             if !self.path.is_empty() {
385                 return self;
386             }
387         }
388
389         // Normalise foo::self as bar -> foo as bar.
390         if let UseSegment::Slf(_) = last {
391             match self.path.last() {
392                 None => {}
393                 Some(UseSegment::Ident(_, None)) => {
394                     aliased_self = true;
395                 }
396                 _ => unreachable!(),
397             }
398         }
399
400         let mut done = false;
401         if aliased_self {
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);
407                         done = true;
408                     }
409                 }
410                 _ => unreachable!(),
411             }
412         }
413
414         if done {
415             return self;
416         }
417
418         // Normalise foo::{bar} -> foo::bar
419         if let UseSegment::List(ref list) = last {
420             if list.len() == 1 {
421                 normalize_sole_list = true;
422             }
423         }
424
425         if normalize_sole_list {
426             match last {
427                 UseSegment::List(list) => {
428                     for seg in &list[0].path {
429                         self.path.push(seg.clone());
430                     }
431                     return self.normalize();
432                 }
433                 _ => unreachable!(),
434             }
435         }
436
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<_>>();
442             list.sort();
443             last = UseSegment::List(list);
444         }
445
446         self.path.push(last);
447         self
448     }
449
450     fn has_comment(&self) -> bool {
451         self.list_item.as_ref().map_or(false, ListItem::has_comment)
452     }
453
454     fn same_visibility(&self, other: &UseTree) -> bool {
455         match (&self.visibility, &other.visibility) {
456             (
457                 Some(codemap::Spanned {
458                     node: ast::VisibilityKind::Inherited,
459                     ..
460                 }),
461                 None,
462             )
463             | (
464                 None,
465                 Some(codemap::Spanned {
466                     node: ast::VisibilityKind::Inherited,
467                     ..
468                 }),
469             )
470             | (None, None) => true,
471             (
472                 Some(codemap::Spanned { node: lnode, .. }),
473                 Some(codemap::Spanned { node: rnode, .. }),
474             ) => lnode == rnode,
475             _ => false,
476         }
477     }
478
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)
482         {
483             false
484         } else {
485             self.path[0] == other.path[0]
486         }
487     }
488
489     fn flatten(self) -> Vec<UseTree> {
490         if self.path.is_empty() {
491             return vec![self];
492         }
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 {
502                             path: new_path,
503                             span: self.span,
504                             list_item: None,
505                             visibility: self.visibility.clone(),
506                             attrs: None,
507                         });
508                     }
509                 }
510
511                 result
512             }
513             _ => vec![self],
514         }
515     }
516
517     fn merge(&mut self, other: UseTree) {
518         let mut new_path = vec![];
519         for (mut a, b) in self.path
520             .clone()
521             .iter_mut()
522             .zip(other.path.clone().into_iter())
523         {
524             if *a == b {
525                 new_path.push(b);
526             } else {
527                 break;
528             }
529         }
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);
533         }
534         self.path = new_path;
535     }
536 }
537
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() {
542         return None;
543     }
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),
548         ]));
549     }
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),
554         ]));
555     }
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));
558         list.sort();
559         return Some(UseSegment::List(list.clone()));
560     }
561     let mut list = vec![
562         UseTree::from_path(a_rest.to_vec(), DUMMY_SP),
563         UseTree::from_path(b_rest.to_vec(), DUMMY_SP),
564     ];
565     list.sort();
566     Some(UseSegment::List(list))
567 }
568
569 impl PartialOrd for UseSegment {
570     fn partial_cmp(&self, other: &UseSegment) -> Option<Ordering> {
571         Some(self.cmp(other))
572     }
573 }
574 impl PartialOrd for UseTree {
575     fn partial_cmp(&self, other: &UseTree) -> Option<Ordering> {
576         Some(self.cmp(other))
577     }
578 }
579 impl Ord for UseSegment {
580     fn cmp(&self, other: &UseSegment) -> Ordering {
581         use self::UseSegment::*;
582
583         fn is_upper_snake_case(s: &str) -> bool {
584             s.chars().all(|c| c.is_uppercase() || c == '_')
585         }
586
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;
594                 }
595                 if ia.starts_with(char::is_lowercase) && ib.starts_with(char::is_uppercase) {
596                     return Ordering::Less;
597                 }
598                 if is_upper_snake_case(ia) && !is_upper_snake_case(ib) {
599                     return Ordering::Greater;
600                 }
601                 if !is_upper_snake_case(ia) && is_upper_snake_case(ib) {
602                     return Ordering::Less;
603                 }
604                 let ident_ord = ia.cmp(ib);
605                 if ident_ord != Ordering::Equal {
606                     return ident_ord;
607                 }
608                 if aa.is_none() && ab.is_some() {
609                     return Ordering::Less;
610                 }
611                 if aa.is_some() && ab.is_none() {
612                     return Ordering::Greater;
613                 }
614                 aa.cmp(ab)
615             }
616             (&List(ref a), &List(ref b)) => {
617                 for (a, b) in a.iter().zip(b.iter()) {
618                     let ord = a.cmp(b);
619                     if ord != Ordering::Equal {
620                         return ord;
621                     }
622                 }
623
624                 a.len().cmp(&b.len())
625             }
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,
634         }
635     }
636 }
637 impl Ord for UseTree {
638     fn cmp(&self, other: &UseTree) -> Ordering {
639         for (a, b) in self.path.iter().zip(other.path.iter()) {
640             let ord = a.cmp(b);
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
645             {
646                 return ord;
647             }
648         }
649
650         self.path.len().cmp(&other.path.len())
651     }
652 }
653
654 fn rewrite_nested_use_tree(
655     context: &RewriteContext,
656     use_tree_list: &[UseTree],
657     shape: Shape,
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)
664             .sub_width(1)?,
665         IndentStyle::Visual => shape.visual_indent(0),
666     };
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);
671         } else {
672             list_items.push(ListItem::from_str(use_tree.rewrite(context, nested_shape)?));
673         }
674     }
675     let has_nested_list = use_tree_list.iter().any(|use_segment| {
676         use_segment
677             .path
678             .last()
679             .map_or(false, |last_segment| match last_segment {
680                 UseSegment::List(..) => true,
681                 _ => false,
682             })
683     });
684     let (tactic, remaining_width) = if has_nested_list {
685         (DefinitiveListTactic::Vertical, 0)
686     } else {
687         let remaining_width = shape.width.checked_sub(2).unwrap_or(0);
688         let tactic = definitive_tactic(
689             &list_items,
690             context.config.imports_layout(),
691             Separator::Comma,
692             remaining_width,
693         );
694         (tactic, remaining_width)
695     };
696
697     let ends_with_newline = context.config.imports_indent() == IndentStyle::Block
698         && tactic != DefinitiveListTactic::Horizontal;
699     let fmt = ListFormatting {
700         tactic,
701         separator: ",",
702         trailing_separator: if ends_with_newline {
703             context.config.trailing_comma()
704         } else {
705             SeparatorTactic::Never
706         },
707         separator_place: SeparatorPlace::Back,
708         shape: nested_shape,
709         ends_with_newline,
710         preserve_newline: true,
711         config: context.config,
712     };
713
714     let list_str = write_list(&list_items, &fmt)?;
715
716     let result = if (list_str.contains('\n') || list_str.len() > remaining_width)
717         && context.config.imports_indent() == IndentStyle::Block
718     {
719         format!(
720             "{{\n{}{}\n{}}}",
721             nested_shape.indent.to_string(context.config),
722             list_str,
723             shape.indent.to_string(context.config)
724         )
725     } else {
726         format!("{{{}}}", list_str)
727     };
728
729     Some(result)
730 }
731
732 impl Rewrite for UseSegment {
733     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
734         Some(match *self {
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(
743                 context,
744                 use_tree_list,
745                 // 1 = "{" and "}"
746                 shape.offset_left(1)?.sub_width(1)?,
747             )?,
748         })
749     }
750 }
751
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("::");
762                 // 2 = "::"
763                 shape = shape.offset_left(2 + segment_str.len())?;
764             }
765         }
766         Some(result)
767     }
768 }
769
770 #[cfg(test)]
771 mod test {
772     use super::*;
773     use syntax::codemap::DUMMY_SP;
774
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;
779         use std::mem::swap;
780         use std::str::Chars;
781
782         struct Parser<'a> {
783             input: Peekable<Chars<'a>>,
784         }
785
786         impl<'a> Parser<'a> {
787             fn bump(&mut self) {
788                 self.input.next().unwrap();
789             }
790             fn eat(&mut self, c: char) {
791                 assert!(self.input.next().unwrap() == c);
792             }
793             fn push_segment(
794                 result: &mut Vec<UseSegment>,
795                 buf: &mut String,
796                 alias_buf: &mut Option<String>,
797             ) {
798                 if !buf.is_empty() {
799                     let mut alias = None;
800                     swap(alias_buf, &mut alias);
801                     if buf == "self" {
802                         result.push(UseSegment::Slf(alias));
803                         *buf = String::new();
804                         *alias_buf = None;
805                     } else if buf == "super" {
806                         result.push(UseSegment::Super(alias));
807                         *buf = String::new();
808                         *alias_buf = None;
809                     } else {
810                         let mut name = String::new();
811                         swap(buf, &mut name);
812                         result.push(UseSegment::Ident(name, alias));
813                     }
814                 }
815             }
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() {
821                     match c {
822                         '{' => {
823                             assert!(buf.is_empty());
824                             self.bump();
825                             result.push(UseSegment::List(self.parse_list()));
826                             self.eat('}');
827                         }
828                         '*' => {
829                             assert!(buf.is_empty());
830                             self.bump();
831                             result.push(UseSegment::Glob);
832                         }
833                         ':' => {
834                             self.bump();
835                             self.eat(':');
836                             Self::push_segment(&mut result, &mut buf, &mut alias_buf);
837                         }
838                         '}' | ',' => {
839                             Self::push_segment(&mut result, &mut buf, &mut alias_buf);
840                             return UseTree {
841                                 path: result,
842                                 span: DUMMY_SP,
843                                 list_item: None,
844                                 visibility: None,
845                                 attrs: None,
846                             };
847                         }
848                         ' ' => {
849                             self.bump();
850                             self.eat('a');
851                             self.eat('s');
852                             self.eat(' ');
853                             alias_buf = Some(String::new());
854                         }
855                         c => {
856                             self.bump();
857                             if let Some(ref mut buf) = alias_buf {
858                                 buf.push(c);
859                             } else {
860                                 buf.push(c);
861                             }
862                         }
863                     }
864                 }
865                 Self::push_segment(&mut result, &mut buf, &mut alias_buf);
866                 UseTree {
867                     path: result,
868                     span: DUMMY_SP,
869                     list_item: None,
870                     visibility: None,
871                     attrs: None,
872                 }
873             }
874
875             fn parse_list(&mut self) -> Vec<UseTree> {
876                 let mut result = vec![];
877                 loop {
878                     match self.input.peek().unwrap() {
879                         ',' | ' ' => self.bump(),
880                         '}' => {
881                             return result;
882                         }
883                         _ => result.push(self.parse_in_list()),
884                     }
885                 }
886             }
887         }
888
889         let mut parser = Parser {
890             input: s.chars().peekable(),
891         };
892         parser.parse_in_list()
893     }
894
895     macro parse_use_trees($($s:expr),* $(,)*) {
896         vec![
897             $(parse_use_tree($s),)*
898         ]
899     }
900
901     #[test]
902     fn test_use_tree_merge() {
903         macro test_merge([$($input:expr),* $(,)*], [$($output:expr),* $(,)*]) {
904             assert_eq!(
905                 merge_use_trees(parse_use_trees!($($input,)*)),
906                 parse_use_trees!($($output,)*),
907             );
908         }
909
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}}"]);
914         test_merge!(
915             ["a::{b::{self, c}, d::e}", "a::d::f"],
916             ["a::{b::{self, c}, d::{e, f}}"]
917         );
918         test_merge!(
919             ["a::d::f", "a::{b::{self, c}, d::e}"],
920             ["a::{b::{self, c}, d::{e, f}}"]
921         );
922         test_merge!(
923             ["a::{c, d, b}", "a::{d, e, b, a, f}", "a::{f, g, c}"],
924             ["a::{a, b, c, d, e, f, g}"]
925         );
926     }
927
928     #[test]
929     fn test_use_tree_flatten() {
930         assert_eq!(
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",)
933         );
934
935         assert_eq!(
936             parse_use_tree("a::b::{c::{d, e, f}, g, h::{i, j, k}}").flatten(),
937             parse_use_trees![
938                 "a::b::c::d",
939                 "a::b::c::e",
940                 "a::b::c::f",
941                 "a::b::g",
942                 "a::b::h::i",
943                 "a::b::h::j",
944                 "a::b::h::k",
945             ]
946         );
947     }
948
949     #[test]
950     fn test_use_tree_normalize() {
951         assert_eq!(parse_use_tree("a::self").normalize(), parse_use_tree("a"));
952         assert_eq!(
953             parse_use_tree("a::self as foo").normalize(),
954             parse_use_tree("a as foo")
955         );
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"));
958         assert_eq!(
959             parse_use_tree("a::{b, c::self}").normalize(),
960             parse_use_tree("a::{b, c}")
961         );
962         assert_eq!(
963             parse_use_tree("a::{b as bar, c::self}").normalize(),
964             parse_use_tree("a::{b as bar, c}")
965         );
966     }
967
968     #[test]
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());
975
976         assert!(
977             parse_use_tree("aaaaaaaaaaaaaaa::{bb, cc, dddddddd}").normalize()
978                 < parse_use_tree("aaaaaaaaaaaaaaa::{bb, cc, ddddddddd}").normalize()
979         );
980         assert!(
981             parse_use_tree("serde::de::{Deserialize}").normalize()
982                 < parse_use_tree("serde_json").normalize()
983         );
984         assert!(parse_use_tree("a::b::c").normalize() < parse_use_tree("a::b::*").normalize());
985         assert!(
986             parse_use_tree("foo::{Bar, Baz}").normalize()
987                 < parse_use_tree("{Bar, Baz}").normalize()
988         );
989
990         assert!(
991             parse_use_tree("foo::{self as bar}").normalize()
992                 < parse_use_tree("foo::{qux as bar}").normalize()
993         );
994         assert!(
995             parse_use_tree("foo::{qux as bar}").normalize()
996                 < parse_use_tree("foo::{baz, qux as bar}").normalize()
997         );
998         assert!(
999             parse_use_tree("foo::{self as bar, baz}").normalize()
1000                 < parse_use_tree("foo::{baz, qux as bar}").normalize()
1001         );
1002
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());
1005
1006         assert!(
1007             parse_use_tree("std::cmp::{d, c, b, a}").normalize()
1008                 < parse_use_tree("std::cmp::{b, e, g, f}").normalize()
1009         );
1010     }
1011 }