]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/format.rs
Fix invalid linker position
[rust.git] / src / librustdoc / html / format.rs
1 // Copyright 2013-2014 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 //! HTML formatting module
12 //!
13 //! This module contains a large number of `fmt::Display` implementations for
14 //! various types in `rustdoc::clean`. These implementations all currently
15 //! assume that HTML output is desired, although it may be possible to redesign
16 //! them in the future to instead emit any format desired.
17
18 use std::fmt;
19 use std::iter::repeat;
20
21 use rustc::hir::def_id::DefId;
22 use syntax::abi::Abi;
23 use rustc::hir;
24
25 use clean::{self, PrimitiveType};
26 use core::DocAccessLevels;
27 use html::item_type::ItemType;
28 use html::render;
29 use html::render::{cache, CURRENT_LOCATION_KEY};
30
31 /// Helper to render an optional visibility with a space after it (if the
32 /// visibility is preset)
33 #[derive(Copy, Clone)]
34 pub struct VisSpace<'a>(pub &'a Option<clean::Visibility>);
35 /// Similarly to VisSpace, this structure is used to render a function style with a
36 /// space after it.
37 #[derive(Copy, Clone)]
38 pub struct UnsafetySpace(pub hir::Unsafety);
39 /// Similarly to VisSpace, this structure is used to render a function constness
40 /// with a space after it.
41 #[derive(Copy, Clone)]
42 pub struct ConstnessSpace(pub hir::Constness);
43 /// Similar to VisSpace, but used for mutability
44 #[derive(Copy, Clone)]
45 pub struct MutableSpace(pub clean::Mutability);
46 /// Similar to VisSpace, but used for mutability
47 #[derive(Copy, Clone)]
48 pub struct RawMutableSpace(pub clean::Mutability);
49 /// Wrapper struct for emitting type parameter bounds.
50 pub struct TyParamBounds<'a>(pub &'a [clean::TyParamBound]);
51 /// Wrapper struct for emitting a comma-separated list of items
52 pub struct CommaSep<'a, T: 'a>(pub &'a [T]);
53 pub struct AbiSpace(pub Abi);
54
55 /// Wrapper struct for properly emitting a method declaration.
56 pub struct Method<'a> {
57     /// The declaration to emit.
58     pub decl: &'a clean::FnDecl,
59     /// The length of the function's "name", used to determine line-wrapping.
60     pub name_len: usize,
61     /// The number of spaces to indent each successive line with, if line-wrapping is necessary.
62     pub indent: usize,
63 }
64
65 /// Wrapper struct for emitting a where clause from Generics.
66 pub struct WhereClause<'a>{
67     /// The Generics from which to emit a where clause.
68     pub gens: &'a clean::Generics,
69     /// The number of spaces to indent each line with.
70     pub indent: usize,
71     /// Whether the where clause needs to add a comma and newline after the last bound.
72     pub end_newline: bool,
73 }
74
75 pub struct HRef<'a> {
76     pub did: DefId,
77     pub text: &'a str,
78 }
79
80 impl<'a> VisSpace<'a> {
81     pub fn get(self) -> &'a Option<clean::Visibility> {
82         let VisSpace(v) = self; v
83     }
84 }
85
86 impl UnsafetySpace {
87     pub fn get(&self) -> hir::Unsafety {
88         let UnsafetySpace(v) = *self; v
89     }
90 }
91
92 impl ConstnessSpace {
93     pub fn get(&self) -> hir::Constness {
94         let ConstnessSpace(v) = *self; v
95     }
96 }
97
98 impl<'a, T: fmt::Display> fmt::Display for CommaSep<'a, T> {
99     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
100         for (i, item) in self.0.iter().enumerate() {
101             if i != 0 { write!(f, ", ")?; }
102             fmt::Display::fmt(item, f)?;
103         }
104         Ok(())
105     }
106 }
107
108 impl<'a> fmt::Display for TyParamBounds<'a> {
109     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
110         let &TyParamBounds(bounds) = self;
111         for (i, bound) in bounds.iter().enumerate() {
112             if i > 0 {
113                 f.write_str(" + ")?;
114             }
115             fmt::Display::fmt(bound, f)?;
116         }
117         Ok(())
118     }
119 }
120
121 impl fmt::Display for clean::Generics {
122     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
123         if self.lifetimes.is_empty() && self.type_params.is_empty() { return Ok(()) }
124         if f.alternate() {
125             f.write_str("<")?;
126         } else {
127             f.write_str("&lt;")?;
128         }
129
130         for (i, life) in self.lifetimes.iter().enumerate() {
131             if i > 0 {
132                 f.write_str(", ")?;
133             }
134             write!(f, "{}", *life)?;
135         }
136
137         if !self.type_params.is_empty() {
138             if !self.lifetimes.is_empty() {
139                 f.write_str(", ")?;
140             }
141             for (i, tp) in self.type_params.iter().enumerate() {
142                 if i > 0 {
143                     f.write_str(", ")?
144                 }
145                 f.write_str(&tp.name)?;
146
147                 if !tp.bounds.is_empty() {
148                     if f.alternate() {
149                         write!(f, ": {:#}", TyParamBounds(&tp.bounds))?;
150                     } else {
151                         write!(f, ":&nbsp;{}", TyParamBounds(&tp.bounds))?;
152                     }
153                 }
154
155                 if let Some(ref ty) = tp.default {
156                     if f.alternate() {
157                         write!(f, " = {:#}", ty)?;
158                     } else {
159                         write!(f, "&nbsp;=&nbsp;{}", ty)?;
160                     }
161                 };
162             }
163         }
164         if f.alternate() {
165             f.write_str(">")?;
166         } else {
167             f.write_str("&gt;")?;
168         }
169         Ok(())
170     }
171 }
172
173 impl<'a> fmt::Display for WhereClause<'a> {
174     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
175         let &WhereClause { gens, indent, end_newline } = self;
176         if gens.where_predicates.is_empty() {
177             return Ok(());
178         }
179         let mut clause = String::new();
180         if f.alternate() {
181             clause.push_str(" where");
182         } else {
183             if end_newline {
184                 clause.push_str(" <span class=\"where fmt-newline\">where");
185             } else {
186                 clause.push_str(" <span class=\"where\">where");
187             }
188         }
189         for (i, pred) in gens.where_predicates.iter().enumerate() {
190             if f.alternate() {
191                 clause.push(' ');
192             } else {
193                 clause.push_str("<br>");
194             }
195
196             match pred {
197                 &clean::WherePredicate::BoundPredicate { ref ty, ref bounds } => {
198                     let bounds = bounds;
199                     if f.alternate() {
200                         clause.push_str(&format!("{:#}: {:#}", ty, TyParamBounds(bounds)));
201                     } else {
202                         clause.push_str(&format!("{}: {}", ty, TyParamBounds(bounds)));
203                     }
204                 }
205                 &clean::WherePredicate::RegionPredicate { ref lifetime,
206                                                           ref bounds } => {
207                     clause.push_str(&format!("{}: ", lifetime));
208                     for (i, lifetime) in bounds.iter().enumerate() {
209                         if i > 0 {
210                             clause.push_str(" + ");
211                         }
212
213                         clause.push_str(&format!("{}", lifetime));
214                     }
215                 }
216                 &clean::WherePredicate::EqPredicate { ref lhs, ref rhs } => {
217                     if f.alternate() {
218                         clause.push_str(&format!("{:#} == {:#}", lhs, rhs));
219                     } else {
220                         clause.push_str(&format!("{} == {}", lhs, rhs));
221                     }
222                 }
223             }
224
225             if i < gens.where_predicates.len() - 1 || end_newline {
226                 clause.push(',');
227             }
228         }
229
230         if end_newline {
231             // add a space so stripping <br> tags and breaking spaces still renders properly
232             if f.alternate() {
233                 clause.push(' ');
234             } else {
235                 clause.push_str("&nbsp;");
236             }
237         }
238
239         if !f.alternate() {
240             clause.push_str("</span>");
241             let padding = repeat("&nbsp;").take(indent + 4).collect::<String>();
242             clause = clause.replace("<br>", &format!("<br>{}", padding));
243             clause.insert_str(0, &repeat("&nbsp;").take(indent.saturating_sub(1))
244                                                   .collect::<String>());
245             if !end_newline {
246                 clause.insert_str(0, "<br>");
247             }
248         }
249         write!(f, "{}", clause)
250     }
251 }
252
253 impl fmt::Display for clean::Lifetime {
254     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
255         f.write_str(self.get_ref())?;
256         Ok(())
257     }
258 }
259
260 impl fmt::Display for clean::PolyTrait {
261     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
262         if !self.lifetimes.is_empty() {
263             if f.alternate() {
264                 f.write_str("for<")?;
265             } else {
266                 f.write_str("for&lt;")?;
267             }
268             for (i, lt) in self.lifetimes.iter().enumerate() {
269                 if i > 0 {
270                     f.write_str(", ")?;
271                 }
272                 write!(f, "{}", lt)?;
273             }
274             if f.alternate() {
275                 f.write_str("> ")?;
276             } else {
277                 f.write_str("&gt; ")?;
278             }
279         }
280         if f.alternate() {
281             write!(f, "{:#}", self.trait_)
282         } else {
283             write!(f, "{}", self.trait_)
284         }
285     }
286 }
287
288 impl fmt::Display for clean::TyParamBound {
289     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
290         match *self {
291             clean::RegionBound(ref lt) => {
292                 write!(f, "{}", *lt)
293             }
294             clean::TraitBound(ref ty, modifier) => {
295                 let modifier_str = match modifier {
296                     hir::TraitBoundModifier::None => "",
297                     hir::TraitBoundModifier::Maybe => "?",
298                 };
299                 if f.alternate() {
300                     write!(f, "{}{:#}", modifier_str, *ty)
301                 } else {
302                     write!(f, "{}{}", modifier_str, *ty)
303                 }
304             }
305         }
306     }
307 }
308
309 impl fmt::Display for clean::PathParameters {
310     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
311         match *self {
312             clean::PathParameters::AngleBracketed {
313                 ref lifetimes, ref types, ref bindings
314             } => {
315                 if !lifetimes.is_empty() || !types.is_empty() || !bindings.is_empty() {
316                     if f.alternate() {
317                         f.write_str("<")?;
318                     } else {
319                         f.write_str("&lt;")?;
320                     }
321                     let mut comma = false;
322                     for lifetime in lifetimes {
323                         if comma {
324                             f.write_str(", ")?;
325                         }
326                         comma = true;
327                         write!(f, "{}", *lifetime)?;
328                     }
329                     for ty in types {
330                         if comma {
331                             f.write_str(", ")?;
332                         }
333                         comma = true;
334                         if f.alternate() {
335                             write!(f, "{:#}", *ty)?;
336                         } else {
337                             write!(f, "{}", *ty)?;
338                         }
339                     }
340                     for binding in bindings {
341                         if comma {
342                             f.write_str(", ")?;
343                         }
344                         comma = true;
345                         if f.alternate() {
346                             write!(f, "{:#}", *binding)?;
347                         } else {
348                             write!(f, "{}", *binding)?;
349                         }
350                     }
351                     if f.alternate() {
352                         f.write_str(">")?;
353                     } else {
354                         f.write_str("&gt;")?;
355                     }
356                 }
357             }
358             clean::PathParameters::Parenthesized { ref inputs, ref output } => {
359                 f.write_str("(")?;
360                 let mut comma = false;
361                 for ty in inputs {
362                     if comma {
363                         f.write_str(", ")?;
364                     }
365                     comma = true;
366                     if f.alternate() {
367                         write!(f, "{:#}", *ty)?;
368                     } else {
369                         write!(f, "{}", *ty)?;
370                     }
371                 }
372                 f.write_str(")")?;
373                 if let Some(ref ty) = *output {
374                     if f.alternate() {
375                         write!(f, " -> {:#}", ty)?;
376                     } else {
377                         write!(f, " -&gt; {}", ty)?;
378                     }
379                 }
380             }
381         }
382         Ok(())
383     }
384 }
385
386 impl fmt::Display for clean::PathSegment {
387     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
388         f.write_str(&self.name)?;
389         if f.alternate() {
390             write!(f, "{:#}", self.params)
391         } else {
392             write!(f, "{}", self.params)
393         }
394     }
395 }
396
397 impl fmt::Display for clean::Path {
398     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
399         if self.global {
400             f.write_str("::")?
401         }
402
403         for (i, seg) in self.segments.iter().enumerate() {
404             if i > 0 {
405                 f.write_str("::")?
406             }
407             if f.alternate() {
408                 write!(f, "{:#}", seg)?;
409             } else {
410                 write!(f, "{}", seg)?;
411             }
412         }
413         Ok(())
414     }
415 }
416
417 pub fn href(did: DefId) -> Option<(String, ItemType, Vec<String>)> {
418     let cache = cache();
419     if !did.is_local() && !cache.access_levels.is_doc_reachable(did) {
420         return None
421     }
422
423     let loc = CURRENT_LOCATION_KEY.with(|l| l.borrow().clone());
424     let (fqp, shortty, mut url) = match cache.paths.get(&did) {
425         Some(&(ref fqp, shortty)) => {
426             (fqp, shortty, repeat("../").take(loc.len()).collect())
427         }
428         None => match cache.external_paths.get(&did) {
429             Some(&(ref fqp, shortty)) => {
430                 (fqp, shortty, match cache.extern_locations[&did.krate] {
431                     (.., render::Remote(ref s)) => s.to_string(),
432                     (.., render::Local) => repeat("../").take(loc.len()).collect(),
433                     (.., render::Unknown) => return None,
434                 })
435             }
436             None => return None,
437         }
438     };
439     for component in &fqp[..fqp.len() - 1] {
440         url.push_str(component);
441         url.push_str("/");
442     }
443     match shortty {
444         ItemType::Module => {
445             url.push_str(fqp.last().unwrap());
446             url.push_str("/index.html");
447         }
448         _ => {
449             url.push_str(shortty.css_class());
450             url.push_str(".");
451             url.push_str(fqp.last().unwrap());
452             url.push_str(".html");
453         }
454     }
455     Some((url, shortty, fqp.to_vec()))
456 }
457
458 /// Used when rendering a `ResolvedPath` structure. This invokes the `path`
459 /// rendering function with the necessary arguments for linking to a local path.
460 fn resolved_path(w: &mut fmt::Formatter, did: DefId, path: &clean::Path,
461                  print_all: bool, use_absolute: bool) -> fmt::Result {
462     let last = path.segments.last().unwrap();
463     let rel_root = match &*path.segments[0].name {
464         "self" => Some("./".to_string()),
465         _ => None,
466     };
467
468     if print_all {
469         let amt = path.segments.len() - 1;
470         match rel_root {
471             Some(mut root) => {
472                 for seg in &path.segments[..amt] {
473                     if "super" == seg.name || "self" == seg.name || w.alternate() {
474                         write!(w, "{}::", seg.name)?;
475                     } else {
476                         root.push_str(&seg.name);
477                         root.push_str("/");
478                         write!(w, "<a class=\"mod\" href=\"{}index.html\">{}</a>::",
479                                root,
480                                seg.name)?;
481                     }
482                 }
483             }
484             None => {
485                 for seg in &path.segments[..amt] {
486                     write!(w, "{}::", seg.name)?;
487                 }
488             }
489         }
490     }
491     if w.alternate() {
492         write!(w, "{:#}{:#}", HRef::new(did, &last.name), last.params)?;
493     } else {
494         let path = if use_absolute {
495             match href(did) {
496                 Some((_, _, fqp)) => {
497                     format!("{}::{}",
498                             fqp[..fqp.len() - 1].join("::"),
499                             HRef::new(did, fqp.last().unwrap()))
500                 }
501                 None => format!("{}", HRef::new(did, &last.name)),
502             }
503         } else {
504             format!("{}", HRef::new(did, &last.name))
505         };
506         write!(w, "{}{}", path, last.params)?;
507     }
508     Ok(())
509 }
510
511 fn primitive_link(f: &mut fmt::Formatter,
512                   prim: clean::PrimitiveType,
513                   name: &str) -> fmt::Result {
514     let m = cache();
515     let mut needs_termination = false;
516     if !f.alternate() {
517         match m.primitive_locations.get(&prim) {
518             Some(&def_id) if def_id.is_local() => {
519                 let len = CURRENT_LOCATION_KEY.with(|s| s.borrow().len());
520                 let len = if len == 0 {0} else {len - 1};
521                 write!(f, "<a class=\"primitive\" href=\"{}primitive.{}.html\">",
522                        repeat("../").take(len).collect::<String>(),
523                        prim.to_url_str())?;
524                 needs_termination = true;
525             }
526             Some(&def_id) => {
527                 let loc = match m.extern_locations[&def_id.krate] {
528                     (ref cname, _, render::Remote(ref s)) => {
529                         Some((cname, s.to_string()))
530                     }
531                     (ref cname, _, render::Local) => {
532                         let len = CURRENT_LOCATION_KEY.with(|s| s.borrow().len());
533                         Some((cname, repeat("../").take(len).collect::<String>()))
534                     }
535                     (.., render::Unknown) => None,
536                 };
537                 if let Some((cname, root)) = loc {
538                     write!(f, "<a class=\"primitive\" href=\"{}{}/primitive.{}.html\">",
539                            root,
540                            cname,
541                            prim.to_url_str())?;
542                     needs_termination = true;
543                 }
544             }
545             None => {}
546         }
547     }
548     write!(f, "{}", name)?;
549     if needs_termination {
550         write!(f, "</a>")?;
551     }
552     Ok(())
553 }
554
555 /// Helper to render type parameters
556 fn tybounds(w: &mut fmt::Formatter,
557             typarams: &Option<Vec<clean::TyParamBound>>) -> fmt::Result {
558     match *typarams {
559         Some(ref params) => {
560             for param in params {
561                 write!(w, " + ")?;
562                 fmt::Display::fmt(param, w)?;
563             }
564             Ok(())
565         }
566         None => Ok(())
567     }
568 }
569
570 impl<'a> HRef<'a> {
571     pub fn new(did: DefId, text: &'a str) -> HRef<'a> {
572         HRef { did: did, text: text }
573     }
574 }
575
576 impl<'a> fmt::Display for HRef<'a> {
577     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
578         match href(self.did) {
579             Some((url, shortty, fqp)) => if !f.alternate() {
580                 write!(f, "<a class=\"{}\" href=\"{}\" title=\"{} {}\">{}</a>",
581                        shortty, url, shortty, fqp.join("::"), self.text)
582             } else {
583                 write!(f, "{}", self.text)
584             },
585             _ => write!(f, "{}", self.text),
586         }
587     }
588 }
589
590 fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt::Result {
591     match *t {
592         clean::Generic(ref name) => {
593             f.write_str(name)
594         }
595         clean::ResolvedPath{ did, ref typarams, ref path, is_generic } => {
596             // Paths like T::Output and Self::Output should be rendered with all segments
597             resolved_path(f, did, path, is_generic, use_absolute)?;
598             tybounds(f, typarams)
599         }
600         clean::Infer => write!(f, "_"),
601         clean::Primitive(prim) => primitive_link(f, prim, prim.as_str()),
602         clean::BareFunction(ref decl) => {
603             if f.alternate() {
604                 write!(f, "{}{}fn{:#}{:#}",
605                        UnsafetySpace(decl.unsafety),
606                        AbiSpace(decl.abi),
607                        decl.generics,
608                        decl.decl)
609             } else {
610                 write!(f, "{}{}", UnsafetySpace(decl.unsafety), AbiSpace(decl.abi))?;
611                 primitive_link(f, PrimitiveType::Fn, "fn")?;
612                 write!(f, "{}{}", decl.generics, decl.decl)
613             }
614         }
615         clean::Tuple(ref typs) => {
616             match &typs[..] {
617                 &[] => primitive_link(f, PrimitiveType::Tuple, "()"),
618                 &[ref one] => {
619                     primitive_link(f, PrimitiveType::Tuple, "(")?;
620                     //carry f.alternate() into this display w/o branching manually
621                     fmt::Display::fmt(one, f)?;
622                     primitive_link(f, PrimitiveType::Tuple, ",)")
623                 }
624                 many => {
625                     primitive_link(f, PrimitiveType::Tuple, "(")?;
626                     fmt::Display::fmt(&CommaSep(&many), f)?;
627                     primitive_link(f, PrimitiveType::Tuple, ")")
628                 }
629             }
630         }
631         clean::Slice(ref t) => {
632             primitive_link(f, PrimitiveType::Slice, "[")?;
633             fmt::Display::fmt(t, f)?;
634             primitive_link(f, PrimitiveType::Slice, "]")
635         }
636         clean::Array(ref t, n) => {
637             primitive_link(f, PrimitiveType::Array, "[")?;
638             fmt::Display::fmt(t, f)?;
639             primitive_link(f, PrimitiveType::Array, &format!("; {}]", n))
640         }
641         clean::Never => f.write_str("!"),
642         clean::RawPointer(m, ref t) => {
643             match **t {
644                 clean::Generic(_) | clean::ResolvedPath {is_generic: true, ..} => {
645                     if f.alternate() {
646                         primitive_link(f, clean::PrimitiveType::RawPointer,
647                                        &format!("*{}{:#}", RawMutableSpace(m), t))
648                     } else {
649                         primitive_link(f, clean::PrimitiveType::RawPointer,
650                                        &format!("*{}{}", RawMutableSpace(m), t))
651                     }
652                 }
653                 _ => {
654                     primitive_link(f, clean::PrimitiveType::RawPointer,
655                                    &format!("*{}", RawMutableSpace(m)))?;
656                     fmt::Display::fmt(t, f)
657                 }
658             }
659         }
660         clean::BorrowedRef{ lifetime: ref l, mutability, type_: ref ty} => {
661             let lt = match *l {
662                 Some(ref l) => format!("{} ", *l),
663                 _ => "".to_string(),
664             };
665             let m = MutableSpace(mutability);
666             let amp = if f.alternate() {
667                 "&".to_string()
668             } else {
669                 "&amp;".to_string()
670             };
671             match **ty {
672                 clean::Slice(ref bt) => { // BorrowedRef{ ... Slice(T) } is &[T]
673                     match **bt {
674                         clean::Generic(_) => {
675                             if f.alternate() {
676                                 primitive_link(f, PrimitiveType::Slice,
677                                     &format!("{}{}{}[{:#}]", amp, lt, m, **bt))
678                             } else {
679                                 primitive_link(f, PrimitiveType::Slice,
680                                     &format!("{}{}{}[{}]", amp, lt, m, **bt))
681                             }
682                         }
683                         _ => {
684                             primitive_link(f, PrimitiveType::Slice,
685                                            &format!("{}{}{}[", amp, lt, m))?;
686                             if f.alternate() {
687                                 write!(f, "{:#}", **bt)?;
688                             } else {
689                                 write!(f, "{}", **bt)?;
690                             }
691                             primitive_link(f, PrimitiveType::Slice, "]")
692                         }
693                     }
694                 }
695                 clean::ResolvedPath { typarams: Some(ref v), .. } if !v.is_empty() => {
696                     write!(f, "{}{}{}(", amp, lt, m)?;
697                     fmt_type(&ty, f, use_absolute)?;
698                     write!(f, ")")
699                 }
700                 clean::Generic(..) => {
701                     primitive_link(f, PrimitiveType::Reference,
702                                    &format!("{}{}{}", amp, lt, m))?;
703                     fmt_type(&ty, f, use_absolute)
704                 }
705                 _ => {
706                     write!(f, "{}{}{}", amp, lt, m)?;
707                     fmt_type(&ty, f, use_absolute)
708                 }
709             }
710         }
711         clean::ImplTrait(ref bounds) => {
712             write!(f, "impl ")?;
713             for (i, bound) in bounds.iter().enumerate() {
714                 if i != 0 {
715                     write!(f, " + ")?;
716                 }
717                 if f.alternate() {
718                     write!(f, "{:#}", *bound)?;
719                 } else {
720                     write!(f, "{}", *bound)?;
721                 }
722             }
723             Ok(())
724         }
725         clean::QPath { ref name, ref self_type, ref trait_ } => {
726             let should_show_cast = match *trait_ {
727                 box clean::ResolvedPath { ref path, .. } => {
728                     !path.segments.is_empty() && !self_type.is_self_type()
729                 }
730                 _ => true,
731             };
732             if f.alternate() {
733                 if should_show_cast {
734                     write!(f, "<{:#} as {:#}>::", self_type, trait_)?
735                 } else {
736                     write!(f, "{:#}::", self_type)?
737                 }
738             } else {
739                 if should_show_cast {
740                     write!(f, "&lt;{} as {}&gt;::", self_type, trait_)?
741                 } else {
742                     write!(f, "{}::", self_type)?
743                 }
744             };
745             match *trait_ {
746                 // It's pretty unsightly to look at `<A as B>::C` in output, and
747                 // we've got hyperlinking on our side, so try to avoid longer
748                 // notation as much as possible by making `C` a hyperlink to trait
749                 // `B` to disambiguate.
750                 //
751                 // FIXME: this is still a lossy conversion and there should probably
752                 //        be a better way of representing this in general? Most of
753                 //        the ugliness comes from inlining across crates where
754                 //        everything comes in as a fully resolved QPath (hard to
755                 //        look at).
756                 box clean::ResolvedPath { did, ref typarams, .. } => {
757                     match href(did) {
758                         Some((ref url, _, ref path)) if !f.alternate() => {
759                             write!(f,
760                                    "<a class=\"type\" href=\"{url}#{shortty}.{name}\" \
761                                    title=\"type {path}::{name}\">{name}</a>",
762                                    url = url,
763                                    shortty = ItemType::AssociatedType,
764                                    name = name,
765                                    path = path.join("::"))?;
766                         }
767                         _ => write!(f, "{}", name)?,
768                     }
769
770                     // FIXME: `typarams` are not rendered, and this seems bad?
771                     drop(typarams);
772                     Ok(())
773                 }
774                 _ => {
775                     write!(f, "{}", name)
776                 }
777             }
778         }
779         clean::Unique(..) => {
780             panic!("should have been cleaned")
781         }
782     }
783 }
784
785 impl fmt::Display for clean::Type {
786     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
787         fmt_type(self, f, false)
788     }
789 }
790
791 fn fmt_impl(i: &clean::Impl,
792             f: &mut fmt::Formatter,
793             link_trait: bool,
794             use_absolute: bool) -> fmt::Result {
795     if f.alternate() {
796         write!(f, "impl{:#} ", i.generics)?;
797     } else {
798         write!(f, "impl{} ", i.generics)?;
799     }
800
801     if let Some(ref ty) = i.trait_ {
802         if i.polarity == Some(clean::ImplPolarity::Negative) {
803             write!(f, "!")?;
804         }
805
806         if link_trait {
807             fmt::Display::fmt(ty, f)?;
808         } else {
809             match *ty {
810                 clean::ResolvedPath { typarams: None, ref path, is_generic: false, .. } => {
811                     let last = path.segments.last().unwrap();
812                     fmt::Display::fmt(&last.name, f)?;
813                     fmt::Display::fmt(&last.params, f)?;
814                 }
815                 _ => unreachable!(),
816             }
817         }
818         write!(f, " for ")?;
819     }
820
821     fmt_type(&i.for_, f, use_absolute)?;
822
823     fmt::Display::fmt(&WhereClause { gens: &i.generics, indent: 0, end_newline: true }, f)?;
824     Ok(())
825 }
826
827 impl fmt::Display for clean::Impl {
828     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
829         fmt_impl(self, f, true, false)
830     }
831 }
832
833 // The difference from above is that trait is not hyperlinked.
834 pub fn fmt_impl_for_trait_page(i: &clean::Impl,
835                                f: &mut fmt::Formatter,
836                                use_absolute: bool) -> fmt::Result {
837     fmt_impl(i, f, false, use_absolute)
838 }
839
840 impl fmt::Display for clean::Arguments {
841     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
842         for (i, input) in self.values.iter().enumerate() {
843             if !input.name.is_empty() {
844                 write!(f, "{}: ", input.name)?;
845             }
846             if f.alternate() {
847                 write!(f, "{:#}", input.type_)?;
848             } else {
849                 write!(f, "{}", input.type_)?;
850             }
851             if i + 1 < self.values.len() { write!(f, ", ")?; }
852         }
853         Ok(())
854     }
855 }
856
857 impl fmt::Display for clean::FunctionRetTy {
858     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
859         match *self {
860             clean::Return(clean::Tuple(ref tys)) if tys.is_empty() => Ok(()),
861             clean::Return(ref ty) if f.alternate() => write!(f, " -> {:#}", ty),
862             clean::Return(ref ty) => write!(f, " -&gt; {}", ty),
863             clean::DefaultReturn => Ok(()),
864         }
865     }
866 }
867
868 impl fmt::Display for clean::FnDecl {
869     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
870         if self.variadic {
871             if f.alternate() {
872                 write!(f, "({args:#}, ...){arrow:#}", args = self.inputs, arrow = self.output)
873             } else {
874                 write!(f, "({args}, ...){arrow}", args = self.inputs, arrow = self.output)
875             }
876         } else {
877             if f.alternate() {
878                 write!(f, "({args:#}){arrow:#}", args = self.inputs, arrow = self.output)
879             } else {
880                 write!(f, "({args}){arrow}", args = self.inputs, arrow = self.output)
881             }
882         }
883     }
884 }
885
886 impl<'a> fmt::Display for Method<'a> {
887     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
888         let &Method { decl, name_len, indent } = self;
889         let amp = if f.alternate() { "&" } else { "&amp;" };
890         let mut args = String::new();
891         let mut args_plain = String::new();
892         for (i, input) in decl.inputs.values.iter().enumerate() {
893             if i == 0 {
894                 args.push_str("<br>");
895             }
896
897             if let Some(selfty) = input.to_self() {
898                 match selfty {
899                     clean::SelfValue => {
900                         args.push_str("self");
901                         args_plain.push_str("self");
902                     }
903                     clean::SelfBorrowed(Some(ref lt), mtbl) => {
904                         args.push_str(&format!("{}{} {}self", amp, *lt, MutableSpace(mtbl)));
905                         args_plain.push_str(&format!("&{} {}self", *lt, MutableSpace(mtbl)));
906                     }
907                     clean::SelfBorrowed(None, mtbl) => {
908                         args.push_str(&format!("{}{}self", amp, MutableSpace(mtbl)));
909                         args_plain.push_str(&format!("&{}self", MutableSpace(mtbl)));
910                     }
911                     clean::SelfExplicit(ref typ) => {
912                         if f.alternate() {
913                             args.push_str(&format!("self: {:#}", *typ));
914                         } else {
915                             args.push_str(&format!("self: {}", *typ));
916                         }
917                         args_plain.push_str(&format!("self: {:#}", *typ));
918                     }
919                 }
920             } else {
921                 if i > 0 {
922                     args.push_str(" <br>");
923                     args_plain.push_str(" ");
924                 }
925                 if !input.name.is_empty() {
926                     args.push_str(&format!("{}: ", input.name));
927                     args_plain.push_str(&format!("{}: ", input.name));
928                 }
929
930                 if f.alternate() {
931                     args.push_str(&format!("{:#}", input.type_));
932                 } else {
933                     args.push_str(&format!("{}", input.type_));
934                 }
935                 args_plain.push_str(&format!("{:#}", input.type_));
936             }
937             if i + 1 < decl.inputs.values.len() {
938                 args.push(',');
939                 args_plain.push(',');
940             }
941         }
942
943         if decl.variadic {
944             args.push_str(",<br> ...");
945             args_plain.push_str(", ...");
946         }
947
948         let arrow_plain = format!("{:#}", decl.output);
949         let arrow = if f.alternate() {
950             format!("{:#}", decl.output)
951         } else {
952             format!("{}", decl.output)
953         };
954
955         let pad = repeat(" ").take(name_len).collect::<String>();
956         let plain = format!("{pad}({args}){arrow}",
957                         pad = pad,
958                         args = args_plain,
959                         arrow = arrow_plain);
960
961         let output = if plain.len() > 80 {
962             let full_pad = format!("<br>{}", repeat("&nbsp;").take(indent + 4).collect::<String>());
963             let close_pad = format!("<br>{}", repeat("&nbsp;").take(indent).collect::<String>());
964             format!("({args}{close}){arrow}",
965                     args = args.replace("<br>", &full_pad),
966                     close = close_pad,
967                     arrow = arrow)
968         } else {
969             format!("({args}){arrow}", args = args.replace("<br>", ""), arrow = arrow)
970         };
971
972         if f.alternate() {
973             write!(f, "{}", output.replace("<br>", "\n"))
974         } else {
975             write!(f, "{}", output)
976         }
977     }
978 }
979
980 impl<'a> fmt::Display for VisSpace<'a> {
981     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
982         match *self.get() {
983             Some(clean::Public) => write!(f, "pub "),
984             Some(clean::Inherited) | None => Ok(())
985         }
986     }
987 }
988
989 impl fmt::Display for UnsafetySpace {
990     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
991         match self.get() {
992             hir::Unsafety::Unsafe => write!(f, "unsafe "),
993             hir::Unsafety::Normal => Ok(())
994         }
995     }
996 }
997
998 impl fmt::Display for ConstnessSpace {
999     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1000         match self.get() {
1001             hir::Constness::Const => write!(f, "const "),
1002             hir::Constness::NotConst => Ok(())
1003         }
1004     }
1005 }
1006
1007 impl fmt::Display for clean::Import {
1008     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1009         match *self {
1010             clean::Import::Simple(ref name, ref src) => {
1011                 if *name == src.path.last_name() {
1012                     write!(f, "use {};", *src)
1013                 } else {
1014                     write!(f, "use {} as {};", *src, *name)
1015                 }
1016             }
1017             clean::Import::Glob(ref src) => {
1018                 if src.path.segments.is_empty() {
1019                     write!(f, "use *;")
1020                 } else {
1021                     write!(f, "use {}::*;", *src)
1022                 }
1023             }
1024         }
1025     }
1026 }
1027
1028 impl fmt::Display for clean::ImportSource {
1029     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1030         match self.did {
1031             Some(did) => resolved_path(f, did, &self.path, true, false),
1032             _ => {
1033                 for (i, seg) in self.path.segments.iter().enumerate() {
1034                     if i > 0 {
1035                         write!(f, "::")?
1036                     }
1037                     write!(f, "{}", seg.name)?;
1038                 }
1039                 Ok(())
1040             }
1041         }
1042     }
1043 }
1044
1045 impl fmt::Display for clean::TypeBinding {
1046     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1047         if f.alternate() {
1048             write!(f, "{} = {:#}", self.name, self.ty)
1049         } else {
1050             write!(f, "{} = {}", self.name, self.ty)
1051         }
1052     }
1053 }
1054
1055 impl fmt::Display for MutableSpace {
1056     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1057         match *self {
1058             MutableSpace(clean::Immutable) => Ok(()),
1059             MutableSpace(clean::Mutable) => write!(f, "mut "),
1060         }
1061     }
1062 }
1063
1064 impl fmt::Display for RawMutableSpace {
1065     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1066         match *self {
1067             RawMutableSpace(clean::Immutable) => write!(f, "const "),
1068             RawMutableSpace(clean::Mutable) => write!(f, "mut "),
1069         }
1070     }
1071 }
1072
1073 impl fmt::Display for AbiSpace {
1074     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1075         let quot = if f.alternate() { "\"" } else { "&quot;" };
1076         match self.0 {
1077             Abi::Rust => Ok(()),
1078             abi => write!(f, "extern {0}{1}{0} ", quot, abi.name()),
1079         }
1080     }
1081 }