]> git.lizzy.rs Git - rust.git/blob - src/imports.rs
Merge pull request #2806 from topecongiro/rustc-ap-syntax
[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 comment::combine_strs_with_missing_comments;
19 use config::IndentStyle;
20 use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator};
21 use rewrite::{Rewrite, RewriteContext};
22 use shape::Shape;
23 use spanned::Spanned;
24 use utils::mk_sp;
25 use visitor::FmtVisitor;
26
27 use std::borrow::Cow;
28 use std::fmt;
29
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
34 }
35
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(
41             &self.get_context(),
42             tree,
43             None,
44             Some(item.vis.clone()),
45             Some(item.span.lo()),
46             Some(item.attrs.clone()),
47         ).rewrite_top_level(&self.get_context(), shape);
48         match rw {
49             Some(ref s) if s.is_empty() => {
50                 // Format up to last newline
51                 let prev_span = mk_sp(self.last_pos, source!(self, span).lo());
52                 let trimmed_snippet = self.snippet(prev_span).trim_right();
53                 let span_end = self.last_pos + BytePos(trimmed_snippet.len() as u32);
54                 self.format_missing(span_end);
55                 // We have an excessive newline from the removed import.
56                 if self.buffer.ends_with('\n') {
57                     self.buffer.pop();
58                     self.line_number -= 1;
59                 }
60                 self.last_pos = source!(self, span).hi();
61             }
62             Some(ref s) => {
63                 self.format_missing_with_indent(source!(self, span).lo());
64                 self.push_str(s);
65                 self.last_pos = source!(self, span).hi();
66             }
67             None => {
68                 self.format_missing_with_indent(source!(self, span).lo());
69                 self.format_missing(source!(self, span).hi());
70             }
71         }
72     }
73 }
74
75 // Ordering of imports
76
77 // We order imports by translating to our own representation and then sorting.
78 // The Rust AST data structures are really bad for this. Rustfmt applies a bunch
79 // of normalisations to imports and since we want to sort based on the result
80 // of these (and to maintain idempotence) we must apply the same normalisations
81 // to the data structures for sorting.
82 //
83 // We sort `self` and `super` before other imports, then identifier imports,
84 // then glob imports, then lists of imports. We do not take aliases into account
85 // when ordering unless the imports are identical except for the alias (rare in
86 // practice).
87
88 // FIXME(#2531) - we should unify the comparison code here with the formatting
89 // code elsewhere since we are essentially string-ifying twice. Furthermore, by
90 // parsing to our own format on comparison, we repeat a lot of work when
91 // sorting.
92
93 // FIXME we do a lot of allocation to make our own representation.
94 #[derive(Clone, Eq, PartialEq)]
95 pub enum UseSegment {
96     Ident(String, Option<String>),
97     Slf(Option<String>),
98     Super(Option<String>),
99     Glob,
100     List(Vec<UseTree>),
101 }
102
103 #[derive(Clone)]
104 pub struct UseTree {
105     pub path: Vec<UseSegment>,
106     pub span: Span,
107     // Comment information within nested use tree.
108     pub list_item: Option<ListItem>,
109     // Additional fields for top level use items.
110     // Should we have another struct for top-level use items rather than reusing this?
111     visibility: Option<ast::Visibility>,
112     attrs: Option<Vec<ast::Attribute>>,
113 }
114
115 impl PartialEq for UseTree {
116     fn eq(&self, other: &UseTree) -> bool {
117         self.path == other.path
118     }
119 }
120 impl Eq for UseTree {}
121
122 impl Spanned for UseTree {
123     fn span(&self) -> Span {
124         let lo = if let Some(ref attrs) = self.attrs {
125             attrs.iter().next().map_or(self.span.lo(), |a| a.span.lo())
126         } else {
127             self.span.lo()
128         };
129         mk_sp(lo, self.span.hi())
130     }
131 }
132
133 impl UseSegment {
134     // Clone a version of self with any top-level alias removed.
135     fn remove_alias(&self) -> UseSegment {
136         match *self {
137             UseSegment::Ident(ref s, _) => UseSegment::Ident(s.clone(), None),
138             UseSegment::Slf(_) => UseSegment::Slf(None),
139             UseSegment::Super(_) => UseSegment::Super(None),
140             _ => self.clone(),
141         }
142     }
143
144     fn from_path_segment(path_seg: &ast::PathSegment) -> Option<UseSegment> {
145         let name = path_seg.ident.name.as_str();
146         if name == "{{root}}" {
147             return None;
148         }
149         Some(if name == "self" {
150             UseSegment::Slf(None)
151         } else if name == "super" {
152             UseSegment::Super(None)
153         } else {
154             UseSegment::Ident((*name).to_owned(), None)
155         })
156     }
157 }
158
159 pub fn merge_use_trees(use_trees: Vec<UseTree>) -> Vec<UseTree> {
160     let mut result = Vec::with_capacity(use_trees.len());
161     for use_tree in use_trees {
162         if use_tree.has_comment() || use_tree.attrs.is_some() {
163             result.push(use_tree);
164             continue;
165         }
166
167         for flattened in use_tree.flatten() {
168             merge_use_trees_inner(&mut result, flattened);
169         }
170     }
171     result
172 }
173
174 fn merge_use_trees_inner(trees: &mut Vec<UseTree>, use_tree: UseTree) {
175     for tree in trees.iter_mut() {
176         if tree.share_prefix(&use_tree) {
177             tree.merge(use_tree);
178             return;
179         }
180     }
181
182     trees.push(use_tree);
183 }
184
185 impl fmt::Debug for UseTree {
186     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
187         fmt::Display::fmt(self, f)
188     }
189 }
190
191 impl fmt::Debug for UseSegment {
192     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
193         fmt::Display::fmt(self, f)
194     }
195 }
196
197 impl fmt::Display for UseSegment {
198     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
199         match *self {
200             UseSegment::Glob => write!(f, "*"),
201             UseSegment::Ident(ref s, _) => write!(f, "{}", s),
202             UseSegment::Slf(..) => write!(f, "self"),
203             UseSegment::Super(..) => write!(f, "super"),
204             UseSegment::List(ref list) => {
205                 write!(f, "{{")?;
206                 for (i, item) in list.iter().enumerate() {
207                     let is_last = i == list.len() - 1;
208                     write!(f, "{}", item)?;
209                     if !is_last {
210                         write!(f, ", ")?;
211                     }
212                 }
213                 write!(f, "}}")
214             }
215         }
216     }
217 }
218 impl fmt::Display for UseTree {
219     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
220         for (i, segment) in self.path.iter().enumerate() {
221             let is_last = i == self.path.len() - 1;
222             write!(f, "{}", segment)?;
223             if !is_last {
224                 write!(f, "::")?;
225             }
226         }
227         write!(f, "")
228     }
229 }
230
231 impl UseTree {
232     // Rewrite use tree with `use ` and a trailing `;`.
233     pub fn rewrite_top_level(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
234         let vis = self
235             .visibility
236             .as_ref()
237             .map_or(Cow::from(""), |vis| ::utils::format_visibility(&vis));
238         let use_str = self
239             .rewrite(context, shape.offset_left(vis.len())?)
240             .map(|s| {
241                 if s.is_empty() {
242                     s.to_owned()
243                 } else {
244                     format!("{}use {};", vis, s)
245                 }
246             })?;
247         if let Some(ref attrs) = self.attrs {
248             let attr_str = attrs.rewrite(context, shape)?;
249             let lo = attrs.last().as_ref()?.span().hi();
250             let hi = self.span.lo();
251             let span = mk_sp(lo, hi);
252             combine_strs_with_missing_comments(context, &attr_str, &use_str, span, shape, false)
253         } else {
254             Some(use_str)
255         }
256     }
257
258     // FIXME: Use correct span?
259     // The given span is essentially incorrect, since we are reconstructing
260     // use statements. This should not be a problem, though, since we have
261     // already tried to extract comment and observed that there are no comment
262     // around the given use item, and the span will not be used afterward.
263     fn from_path(path: Vec<UseSegment>, span: Span) -> UseTree {
264         UseTree {
265             path,
266             span,
267             list_item: None,
268             visibility: None,
269             attrs: None,
270         }
271     }
272
273     pub fn from_ast_with_normalization(
274         context: &RewriteContext,
275         item: &ast::Item,
276     ) -> Option<UseTree> {
277         match item.node {
278             ast::ItemKind::Use(ref use_tree) => Some(
279                 UseTree::from_ast(
280                     context,
281                     use_tree,
282                     None,
283                     Some(item.vis.clone()),
284                     Some(item.span.lo()),
285                     if item.attrs.is_empty() {
286                         None
287                     } else {
288                         Some(item.attrs.clone())
289                     },
290                 ).normalize(),
291             ),
292             _ => None,
293         }
294     }
295
296     fn from_ast(
297         context: &RewriteContext,
298         a: &ast::UseTree,
299         list_item: Option<ListItem>,
300         visibility: Option<ast::Visibility>,
301         opt_lo: Option<BytePos>,
302         attrs: Option<Vec<ast::Attribute>>,
303     ) -> UseTree {
304         let span = if let Some(lo) = opt_lo {
305             mk_sp(lo, a.span.hi())
306         } else {
307             a.span
308         };
309         let mut result = UseTree {
310             path: vec![],
311             span,
312             list_item,
313             visibility,
314             attrs,
315         };
316         for p in &a.prefix.segments {
317             if let Some(use_segment) = UseSegment::from_path_segment(p) {
318                 result.path.push(use_segment);
319             }
320         }
321         match a.kind {
322             UseTreeKind::Glob => {
323                 result.path.push(UseSegment::Glob);
324             }
325             UseTreeKind::Nested(ref list) => {
326                 // Extract comments between nested use items.
327                 // This needs to be done before sorting use items.
328                 let items: Vec<_> = itemize_list(
329                     context.snippet_provider,
330                     list.iter().map(|(tree, _)| tree),
331                     "}",
332                     ",",
333                     |tree| tree.span.lo(),
334                     |tree| tree.span.hi(),
335                     |_| Some("".to_owned()), // We only need comments for now.
336                     context.snippet_provider.span_after(a.span, "{"),
337                     a.span.hi(),
338                     false,
339                 ).collect();
340                 result.path.push(UseSegment::List(
341                     list.iter()
342                         .zip(items.into_iter())
343                         .map(|(t, list_item)| {
344                             Self::from_ast(context, &t.0, Some(list_item), None, None, None)
345                         })
346                         .collect(),
347                 ));
348             }
349             UseTreeKind::Simple(ref rename, ..) => {
350                 let mut name = (*path_to_imported_ident(&a.prefix).name.as_str()).to_owned();
351                 let alias = rename.and_then(|ident| {
352                     if ident == path_to_imported_ident(&a.prefix) {
353                         None
354                     } else {
355                         Some(ident.to_string())
356                     }
357                 });
358
359                 let segment = if &name == "self" {
360                     UseSegment::Slf(alias)
361                 } else if &name == "super" {
362                     UseSegment::Super(alias)
363                 } else {
364                     UseSegment::Ident(name, alias)
365                 };
366
367                 // `name` is already in result.
368                 result.path.pop();
369                 result.path.push(segment);
370             }
371         }
372         result
373     }
374
375     // Do the adjustments that rustfmt does elsewhere to use paths.
376     pub fn normalize(mut self) -> UseTree {
377         let mut last = self.path.pop().expect("Empty use tree?");
378         // Hack around borrow checker.
379         let mut normalize_sole_list = false;
380         let mut aliased_self = false;
381
382         // Remove foo::{} or self without attributes.
383         match last {
384             _ if self.attrs.is_some() => (),
385             UseSegment::List(ref list) if list.is_empty() => {
386                 self.path = vec![];
387                 return self;
388             }
389             UseSegment::Slf(None) if self.path.is_empty() && self.visibility.is_some() => {
390                 self.path = vec![];
391                 return self;
392             }
393             _ => (),
394         }
395
396         // Normalise foo::self -> foo.
397         if let UseSegment::Slf(None) = last {
398             if !self.path.is_empty() {
399                 return self;
400             }
401         }
402
403         // Normalise foo::self as bar -> foo as bar.
404         if let UseSegment::Slf(_) = last {
405             match self.path.last() {
406                 None => {}
407                 Some(UseSegment::Ident(_, None)) => {
408                     aliased_self = true;
409                 }
410                 _ => unreachable!(),
411             }
412         }
413
414         let mut done = false;
415         if aliased_self {
416             match self.path.last_mut() {
417                 Some(UseSegment::Ident(_, ref mut old_rename)) => {
418                     assert!(old_rename.is_none());
419                     if let UseSegment::Slf(Some(rename)) = last.clone() {
420                         *old_rename = Some(rename);
421                         done = true;
422                     }
423                 }
424                 _ => unreachable!(),
425             }
426         }
427
428         if done {
429             return self;
430         }
431
432         // Normalise foo::{bar} -> foo::bar
433         if let UseSegment::List(ref list) = last {
434             if list.len() == 1 {
435                 normalize_sole_list = true;
436             }
437         }
438
439         if normalize_sole_list {
440             match last {
441                 UseSegment::List(list) => {
442                     for seg in &list[0].path {
443                         self.path.push(seg.clone());
444                     }
445                     return self.normalize();
446                 }
447                 _ => unreachable!(),
448             }
449         }
450
451         // Recursively normalize elements of a list use (including sorting the list).
452         if let UseSegment::List(list) = last {
453             let mut list = list
454                 .into_iter()
455                 .map(|ut| ut.normalize())
456                 .collect::<Vec<_>>();
457             list.sort();
458             last = UseSegment::List(list);
459         }
460
461         self.path.push(last);
462         self
463     }
464
465     fn has_comment(&self) -> bool {
466         self.list_item.as_ref().map_or(false, ListItem::has_comment)
467     }
468
469     fn same_visibility(&self, other: &UseTree) -> bool {
470         match (&self.visibility, &other.visibility) {
471             (
472                 Some(codemap::Spanned {
473                     node: ast::VisibilityKind::Inherited,
474                     ..
475                 }),
476                 None,
477             )
478             | (
479                 None,
480                 Some(codemap::Spanned {
481                     node: ast::VisibilityKind::Inherited,
482                     ..
483                 }),
484             )
485             | (None, None) => true,
486             (
487                 Some(codemap::Spanned { node: lnode, .. }),
488                 Some(codemap::Spanned { node: rnode, .. }),
489             ) => lnode == rnode,
490             _ => false,
491         }
492     }
493
494     fn share_prefix(&self, other: &UseTree) -> bool {
495         if self.path.is_empty()
496             || other.path.is_empty()
497             || self.attrs.is_some()
498             || !self.same_visibility(other)
499         {
500             false
501         } else {
502             self.path[0] == other.path[0]
503         }
504     }
505
506     fn flatten(self) -> Vec<UseTree> {
507         if self.path.is_empty() {
508             return vec![self];
509         }
510         match self.path.clone().last().unwrap() {
511             UseSegment::List(list) => {
512                 let prefix = &self.path[..self.path.len() - 1];
513                 let mut result = vec![];
514                 for nested_use_tree in list {
515                     for mut flattend in &mut nested_use_tree.clone().flatten() {
516                         let mut new_path = prefix.to_vec();
517                         new_path.append(&mut flattend.path);
518                         result.push(UseTree {
519                             path: new_path,
520                             span: self.span,
521                             list_item: None,
522                             visibility: self.visibility.clone(),
523                             attrs: None,
524                         });
525                     }
526                 }
527
528                 result
529             }
530             _ => vec![self],
531         }
532     }
533
534     fn merge(&mut self, other: UseTree) {
535         let mut new_path = vec![];
536         for (mut a, b) in self
537             .path
538             .clone()
539             .iter_mut()
540             .zip(other.path.clone().into_iter())
541         {
542             if *a == b {
543                 new_path.push(b);
544             } else {
545                 break;
546             }
547         }
548         if let Some(merged) = merge_rest(&self.path, &other.path, new_path.len()) {
549             new_path.push(merged);
550             self.span = self.span.to(other.span);
551         }
552         self.path = new_path;
553     }
554 }
555
556 fn merge_rest(a: &[UseSegment], b: &[UseSegment], len: usize) -> Option<UseSegment> {
557     let a_rest = &a[len..];
558     let b_rest = &b[len..];
559     if a_rest.is_empty() && b_rest.is_empty() {
560         return None;
561     }
562     if a_rest.is_empty() {
563         return Some(UseSegment::List(vec![
564             UseTree::from_path(vec![UseSegment::Slf(None)], DUMMY_SP),
565             UseTree::from_path(b_rest.to_vec(), DUMMY_SP),
566         ]));
567     }
568     if b_rest.is_empty() {
569         return Some(UseSegment::List(vec![
570             UseTree::from_path(vec![UseSegment::Slf(None)], DUMMY_SP),
571             UseTree::from_path(a_rest.to_vec(), DUMMY_SP),
572         ]));
573     }
574     if let UseSegment::List(mut list) = a_rest[0].clone() {
575         merge_use_trees_inner(&mut list, UseTree::from_path(b_rest.to_vec(), DUMMY_SP));
576         list.sort();
577         return Some(UseSegment::List(list.clone()));
578     }
579     let mut list = vec![
580         UseTree::from_path(a_rest.to_vec(), DUMMY_SP),
581         UseTree::from_path(b_rest.to_vec(), DUMMY_SP),
582     ];
583     list.sort();
584     Some(UseSegment::List(list))
585 }
586
587 impl PartialOrd for UseSegment {
588     fn partial_cmp(&self, other: &UseSegment) -> Option<Ordering> {
589         Some(self.cmp(other))
590     }
591 }
592 impl PartialOrd for UseTree {
593     fn partial_cmp(&self, other: &UseTree) -> Option<Ordering> {
594         Some(self.cmp(other))
595     }
596 }
597 impl Ord for UseSegment {
598     fn cmp(&self, other: &UseSegment) -> Ordering {
599         use self::UseSegment::*;
600
601         fn is_upper_snake_case(s: &str) -> bool {
602             s.chars().all(|c| c.is_uppercase() || c == '_')
603         }
604
605         match (self, other) {
606             (&Slf(ref a), &Slf(ref b)) | (&Super(ref a), &Super(ref b)) => a.cmp(b),
607             (&Glob, &Glob) => Ordering::Equal,
608             (&Ident(ref ia, ref aa), &Ident(ref ib, ref ab)) => {
609                 // snake_case < CamelCase < UPPER_SNAKE_CASE
610                 if ia.starts_with(char::is_uppercase) && ib.starts_with(char::is_lowercase) {
611                     return Ordering::Greater;
612                 }
613                 if ia.starts_with(char::is_lowercase) && ib.starts_with(char::is_uppercase) {
614                     return Ordering::Less;
615                 }
616                 if is_upper_snake_case(ia) && !is_upper_snake_case(ib) {
617                     return Ordering::Greater;
618                 }
619                 if !is_upper_snake_case(ia) && is_upper_snake_case(ib) {
620                     return Ordering::Less;
621                 }
622                 let ident_ord = ia.cmp(ib);
623                 if ident_ord != Ordering::Equal {
624                     return ident_ord;
625                 }
626                 if aa.is_none() && ab.is_some() {
627                     return Ordering::Less;
628                 }
629                 if aa.is_some() && ab.is_none() {
630                     return Ordering::Greater;
631                 }
632                 aa.cmp(ab)
633             }
634             (&List(ref a), &List(ref b)) => {
635                 for (a, b) in a.iter().zip(b.iter()) {
636                     let ord = a.cmp(b);
637                     if ord != Ordering::Equal {
638                         return ord;
639                     }
640                 }
641
642                 a.len().cmp(&b.len())
643             }
644             (&Slf(_), _) => Ordering::Less,
645             (_, &Slf(_)) => Ordering::Greater,
646             (&Super(_), _) => Ordering::Less,
647             (_, &Super(_)) => Ordering::Greater,
648             (&Ident(..), _) => Ordering::Less,
649             (_, &Ident(..)) => Ordering::Greater,
650             (&Glob, _) => Ordering::Less,
651             (_, &Glob) => Ordering::Greater,
652         }
653     }
654 }
655 impl Ord for UseTree {
656     fn cmp(&self, other: &UseTree) -> Ordering {
657         for (a, b) in self.path.iter().zip(other.path.iter()) {
658             let ord = a.cmp(b);
659             // The comparison without aliases is a hack to avoid situations like
660             // comparing `a::b` to `a as c` - where the latter should be ordered
661             // first since it is shorter.
662             if ord != Ordering::Equal && a.remove_alias().cmp(&b.remove_alias()) != Ordering::Equal
663             {
664                 return ord;
665             }
666         }
667
668         self.path.len().cmp(&other.path.len())
669     }
670 }
671
672 fn rewrite_nested_use_tree(
673     context: &RewriteContext,
674     use_tree_list: &[UseTree],
675     shape: Shape,
676 ) -> Option<String> {
677     let mut list_items = Vec::with_capacity(use_tree_list.len());
678     let nested_shape = match context.config.imports_indent() {
679         IndentStyle::Block => shape
680             .block_indent(context.config.tab_spaces())
681             .with_max_width(context.config)
682             .sub_width(1)?,
683         IndentStyle::Visual => shape.visual_indent(0),
684     };
685     for use_tree in use_tree_list {
686         if let Some(mut list_item) = use_tree.list_item.clone() {
687             list_item.item = use_tree.rewrite(context, nested_shape);
688             list_items.push(list_item);
689         } else {
690             list_items.push(ListItem::from_str(use_tree.rewrite(context, nested_shape)?));
691         }
692     }
693     let has_nested_list = use_tree_list.iter().any(|use_segment| {
694         use_segment
695             .path
696             .last()
697             .map_or(false, |last_segment| match last_segment {
698                 UseSegment::List(..) => true,
699                 _ => false,
700             })
701     });
702
703     let remaining_width = if has_nested_list {
704         0
705     } else {
706         shape.width.saturating_sub(2)
707     };
708
709     let tactic = definitive_tactic(
710         &list_items,
711         context.config.imports_layout(),
712         Separator::Comma,
713         remaining_width,
714     );
715
716     let ends_with_newline = context.config.imports_indent() == IndentStyle::Block
717         && tactic != DefinitiveListTactic::Horizontal;
718     let fmt = ListFormatting {
719         tactic,
720         separator: ",",
721         trailing_separator: if ends_with_newline {
722             context.config.trailing_comma()
723         } else {
724             SeparatorTactic::Never
725         },
726         separator_place: SeparatorPlace::Back,
727         shape: nested_shape,
728         ends_with_newline,
729         preserve_newline: true,
730         nested: has_nested_list,
731         config: context.config,
732     };
733
734     let list_str = write_list(&list_items, &fmt)?;
735
736     let result = if (list_str.contains('\n') || list_str.len() > remaining_width)
737         && context.config.imports_indent() == IndentStyle::Block
738     {
739         format!(
740             "{{\n{}{}\n{}}}",
741             nested_shape.indent.to_string(context.config),
742             list_str,
743             shape.indent.to_string(context.config)
744         )
745     } else {
746         format!("{{{}}}", list_str)
747     };
748
749     Some(result)
750 }
751
752 impl Rewrite for UseSegment {
753     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
754         Some(match *self {
755             UseSegment::Ident(ref ident, Some(ref rename)) => format!("{} as {}", ident, rename),
756             UseSegment::Ident(ref ident, None) => ident.clone(),
757             UseSegment::Slf(Some(ref rename)) => format!("self as {}", rename),
758             UseSegment::Slf(None) => "self".to_owned(),
759             UseSegment::Super(Some(ref rename)) => format!("super as {}", rename),
760             UseSegment::Super(None) => "super".to_owned(),
761             UseSegment::Glob => "*".to_owned(),
762             UseSegment::List(ref use_tree_list) => rewrite_nested_use_tree(
763                 context,
764                 use_tree_list,
765                 // 1 = "{" and "}"
766                 shape.offset_left(1)?.sub_width(1)?,
767             )?,
768         })
769     }
770 }
771
772 impl Rewrite for UseTree {
773     // This does NOT format attributes and visibility or add a trailing `;`.
774     fn rewrite(&self, context: &RewriteContext, mut shape: Shape) -> Option<String> {
775         let mut result = String::with_capacity(256);
776         let mut iter = self.path.iter().peekable();
777         while let Some(ref segment) = iter.next() {
778             let segment_str = segment.rewrite(context, shape)?;
779             result.push_str(&segment_str);
780             if iter.peek().is_some() {
781                 result.push_str("::");
782                 // 2 = "::"
783                 shape = shape.offset_left(2 + segment_str.len())?;
784             }
785         }
786         Some(result)
787     }
788 }
789
790 #[cfg(test)]
791 mod test {
792     use super::*;
793     use syntax::codemap::DUMMY_SP;
794
795     // Parse the path part of an import. This parser is not robust and is only
796     // suitable for use in a test harness.
797     fn parse_use_tree(s: &str) -> UseTree {
798         use std::iter::Peekable;
799         use std::mem::swap;
800         use std::str::Chars;
801
802         struct Parser<'a> {
803             input: Peekable<Chars<'a>>,
804         }
805
806         impl<'a> Parser<'a> {
807             fn bump(&mut self) {
808                 self.input.next().unwrap();
809             }
810
811             fn eat(&mut self, c: char) {
812                 assert!(self.input.next().unwrap() == c);
813             }
814
815             fn push_segment(
816                 result: &mut Vec<UseSegment>,
817                 buf: &mut String,
818                 alias_buf: &mut Option<String>,
819             ) {
820                 if !buf.is_empty() {
821                     let mut alias = None;
822                     swap(alias_buf, &mut alias);
823                     if buf == "self" {
824                         result.push(UseSegment::Slf(alias));
825                         *buf = String::new();
826                         *alias_buf = None;
827                     } else if buf == "super" {
828                         result.push(UseSegment::Super(alias));
829                         *buf = String::new();
830                         *alias_buf = None;
831                     } else {
832                         let mut name = String::new();
833                         swap(buf, &mut name);
834                         result.push(UseSegment::Ident(name, alias));
835                     }
836                 }
837             }
838
839             fn parse_in_list(&mut self) -> UseTree {
840                 let mut result = vec![];
841                 let mut buf = String::new();
842                 let mut alias_buf = None;
843                 while let Some(&c) = self.input.peek() {
844                     match c {
845                         '{' => {
846                             assert!(buf.is_empty());
847                             self.bump();
848                             result.push(UseSegment::List(self.parse_list()));
849                             self.eat('}');
850                         }
851                         '*' => {
852                             assert!(buf.is_empty());
853                             self.bump();
854                             result.push(UseSegment::Glob);
855                         }
856                         ':' => {
857                             self.bump();
858                             self.eat(':');
859                             Self::push_segment(&mut result, &mut buf, &mut alias_buf);
860                         }
861                         '}' | ',' => {
862                             Self::push_segment(&mut result, &mut buf, &mut alias_buf);
863                             return UseTree {
864                                 path: result,
865                                 span: DUMMY_SP,
866                                 list_item: None,
867                                 visibility: None,
868                                 attrs: None,
869                             };
870                         }
871                         ' ' => {
872                             self.bump();
873                             self.eat('a');
874                             self.eat('s');
875                             self.eat(' ');
876                             alias_buf = Some(String::new());
877                         }
878                         c => {
879                             self.bump();
880                             if let Some(ref mut buf) = alias_buf {
881                                 buf.push(c);
882                             } else {
883                                 buf.push(c);
884                             }
885                         }
886                     }
887                 }
888                 Self::push_segment(&mut result, &mut buf, &mut alias_buf);
889                 UseTree {
890                     path: result,
891                     span: DUMMY_SP,
892                     list_item: None,
893                     visibility: None,
894                     attrs: None,
895                 }
896             }
897
898             fn parse_list(&mut self) -> Vec<UseTree> {
899                 let mut result = vec![];
900                 loop {
901                     match self.input.peek().unwrap() {
902                         ',' | ' ' => self.bump(),
903                         '}' => {
904                             return result;
905                         }
906                         _ => result.push(self.parse_in_list()),
907                     }
908                 }
909             }
910         }
911
912         let mut parser = Parser {
913             input: s.chars().peekable(),
914         };
915         parser.parse_in_list()
916     }
917
918     macro parse_use_trees($($s:expr),* $(,)*) {
919         vec![
920             $(parse_use_tree($s),)*
921         ]
922     }
923
924     #[test]
925     fn test_use_tree_merge() {
926         macro test_merge([$($input:expr),* $(,)*], [$($output:expr),* $(,)*]) {
927             assert_eq!(
928                 merge_use_trees(parse_use_trees!($($input,)*)),
929                 parse_use_trees!($($output,)*),
930             );
931         }
932
933         test_merge!(["a::b::{c, d}", "a::b::{e, f}"], ["a::b::{c, d, e, f}"]);
934         test_merge!(["a::b::c", "a::b"], ["a::b::{self, c}"]);
935         test_merge!(["a::b", "a::b"], ["a::b"]);
936         test_merge!(["a", "a::b", "a::b::c"], ["a::{self, b::{self, c}}"]);
937         test_merge!(
938             ["a::{b::{self, c}, d::e}", "a::d::f"],
939             ["a::{b::{self, c}, d::{e, f}}"]
940         );
941         test_merge!(
942             ["a::d::f", "a::{b::{self, c}, d::e}"],
943             ["a::{b::{self, c}, d::{e, f}}"]
944         );
945         test_merge!(
946             ["a::{c, d, b}", "a::{d, e, b, a, f}", "a::{f, g, c}"],
947             ["a::{a, b, c, d, e, f, g}"]
948         );
949     }
950
951     #[test]
952     fn test_use_tree_flatten() {
953         assert_eq!(
954             parse_use_tree("a::b::{c, d, e, f}").flatten(),
955             parse_use_trees!("a::b::c", "a::b::d", "a::b::e", "a::b::f",)
956         );
957
958         assert_eq!(
959             parse_use_tree("a::b::{c::{d, e, f}, g, h::{i, j, k}}").flatten(),
960             parse_use_trees![
961                 "a::b::c::d",
962                 "a::b::c::e",
963                 "a::b::c::f",
964                 "a::b::g",
965                 "a::b::h::i",
966                 "a::b::h::j",
967                 "a::b::h::k",
968             ]
969         );
970     }
971
972     #[test]
973     fn test_use_tree_normalize() {
974         assert_eq!(parse_use_tree("a::self").normalize(), parse_use_tree("a"));
975         assert_eq!(
976             parse_use_tree("a::self as foo").normalize(),
977             parse_use_tree("a as foo")
978         );
979         assert_eq!(parse_use_tree("a::{self}").normalize(), parse_use_tree("a"));
980         assert_eq!(parse_use_tree("a::{b}").normalize(), parse_use_tree("a::b"));
981         assert_eq!(
982             parse_use_tree("a::{b, c::self}").normalize(),
983             parse_use_tree("a::{b, c}")
984         );
985         assert_eq!(
986             parse_use_tree("a::{b as bar, c::self}").normalize(),
987             parse_use_tree("a::{b as bar, c}")
988         );
989     }
990
991     #[test]
992     fn test_use_tree_ord() {
993         assert!(parse_use_tree("a").normalize() < parse_use_tree("aa").normalize());
994         assert!(parse_use_tree("a").normalize() < parse_use_tree("a::a").normalize());
995         assert!(parse_use_tree("a").normalize() < parse_use_tree("*").normalize());
996         assert!(parse_use_tree("a").normalize() < parse_use_tree("{a, b}").normalize());
997         assert!(parse_use_tree("*").normalize() < parse_use_tree("{a, b}").normalize());
998
999         assert!(
1000             parse_use_tree("aaaaaaaaaaaaaaa::{bb, cc, dddddddd}").normalize()
1001                 < parse_use_tree("aaaaaaaaaaaaaaa::{bb, cc, ddddddddd}").normalize()
1002         );
1003         assert!(
1004             parse_use_tree("serde::de::{Deserialize}").normalize()
1005                 < parse_use_tree("serde_json").normalize()
1006         );
1007         assert!(parse_use_tree("a::b::c").normalize() < parse_use_tree("a::b::*").normalize());
1008         assert!(
1009             parse_use_tree("foo::{Bar, Baz}").normalize()
1010                 < parse_use_tree("{Bar, Baz}").normalize()
1011         );
1012
1013         assert!(
1014             parse_use_tree("foo::{self as bar}").normalize()
1015                 < parse_use_tree("foo::{qux as bar}").normalize()
1016         );
1017         assert!(
1018             parse_use_tree("foo::{qux as bar}").normalize()
1019                 < parse_use_tree("foo::{baz, qux as bar}").normalize()
1020         );
1021         assert!(
1022             parse_use_tree("foo::{self as bar, baz}").normalize()
1023                 < parse_use_tree("foo::{baz, qux as bar}").normalize()
1024         );
1025
1026         assert!(parse_use_tree("foo").normalize() < parse_use_tree("Foo").normalize());
1027         assert!(parse_use_tree("foo").normalize() < parse_use_tree("foo::Bar").normalize());
1028
1029         assert!(
1030             parse_use_tree("std::cmp::{d, c, b, a}").normalize()
1031                 < parse_use_tree("std::cmp::{b, e, g, f}").normalize()
1032         );
1033     }
1034 }