]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/render/cache.rs
cdfd6b3073adfc9e230a5449e4a02055186fbf31
[rust.git] / src / librustdoc / html / render / cache.rs
1 use crate::clean::{self, AttributesExt, GetDefId};
2 use crate::fold::DocFolder;
3 use rustc::middle::privacy::AccessLevels;
4 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
5 use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX};
6 use rustc_span::source_map::FileName;
7 use rustc_span::symbol::sym;
8 use std::collections::BTreeMap;
9 use std::mem;
10 use std::path::{Path, PathBuf};
11
12 use serde::Serialize;
13
14 use super::{plain_summary_line, shorten, Impl, IndexItem, IndexItemFunctionType, ItemType};
15 use super::{RenderInfo, Type};
16
17 /// Indicates where an external crate can be found.
18 pub enum ExternalLocation {
19     /// Remote URL root of the external crate
20     Remote(String),
21     /// This external crate can be found in the local doc/ folder
22     Local,
23     /// The external crate could not be found.
24     Unknown,
25 }
26
27 /// This cache is used to store information about the `clean::Crate` being
28 /// rendered in order to provide more useful documentation. This contains
29 /// information like all implementors of a trait, all traits a type implements,
30 /// documentation for all known traits, etc.
31 ///
32 /// This structure purposefully does not implement `Clone` because it's intended
33 /// to be a fairly large and expensive structure to clone. Instead this adheres
34 /// to `Send` so it may be stored in a `Arc` instance and shared among the various
35 /// rendering threads.
36 #[derive(Default)]
37 crate struct Cache {
38     /// Maps a type ID to all known implementations for that type. This is only
39     /// recognized for intra-crate `ResolvedPath` types, and is used to print
40     /// out extra documentation on the page of an enum/struct.
41     ///
42     /// The values of the map are a list of implementations and documentation
43     /// found on that implementation.
44     pub impls: FxHashMap<DefId, Vec<Impl>>,
45
46     /// Maintains a mapping of local crate `NodeId`s to the fully qualified name
47     /// and "short type description" of that node. This is used when generating
48     /// URLs when a type is being linked to. External paths are not located in
49     /// this map because the `External` type itself has all the information
50     /// necessary.
51     pub paths: FxHashMap<DefId, (Vec<String>, ItemType)>,
52
53     /// Similar to `paths`, but only holds external paths. This is only used for
54     /// generating explicit hyperlinks to other crates.
55     pub external_paths: FxHashMap<DefId, (Vec<String>, ItemType)>,
56
57     /// Maps local `DefId`s of exported types to fully qualified paths.
58     /// Unlike 'paths', this mapping ignores any renames that occur
59     /// due to 'use' statements.
60     ///
61     /// This map is used when writing out the special 'implementors'
62     /// javascript file. By using the exact path that the type
63     /// is declared with, we ensure that each path will be identical
64     /// to the path used if the corresponding type is inlined. By
65     /// doing this, we can detect duplicate impls on a trait page, and only display
66     /// the impl for the inlined type.
67     pub exact_paths: FxHashMap<DefId, Vec<String>>,
68
69     /// This map contains information about all known traits of this crate.
70     /// Implementations of a crate should inherit the documentation of the
71     /// parent trait if no extra documentation is specified, and default methods
72     /// should show up in documentation about trait implementations.
73     pub traits: FxHashMap<DefId, clean::Trait>,
74
75     /// When rendering traits, it's often useful to be able to list all
76     /// implementors of the trait, and this mapping is exactly, that: a mapping
77     /// of trait ids to the list of known implementors of the trait
78     pub implementors: FxHashMap<DefId, Vec<Impl>>,
79
80     /// Cache of where external crate documentation can be found.
81     pub extern_locations: FxHashMap<CrateNum, (String, PathBuf, ExternalLocation)>,
82
83     /// Cache of where documentation for primitives can be found.
84     pub primitive_locations: FxHashMap<clean::PrimitiveType, DefId>,
85
86     // Note that external items for which `doc(hidden)` applies to are shown as
87     // non-reachable while local items aren't. This is because we're reusing
88     // the access levels from the privacy check pass.
89     pub access_levels: AccessLevels<DefId>,
90
91     /// The version of the crate being documented, if given from the `--crate-version` flag.
92     pub crate_version: Option<String>,
93
94     // Private fields only used when initially crawling a crate to build a cache
95     stack: Vec<String>,
96     parent_stack: Vec<DefId>,
97     parent_is_trait_impl: bool,
98     search_index: Vec<IndexItem>,
99     stripped_mod: bool,
100     pub deref_trait_did: Option<DefId>,
101     pub deref_mut_trait_did: Option<DefId>,
102     pub owned_box_did: Option<DefId>,
103     masked_crates: FxHashSet<CrateNum>,
104
105     // In rare case where a structure is defined in one module but implemented
106     // in another, if the implementing module is parsed before defining module,
107     // then the fully qualified name of the structure isn't presented in `paths`
108     // yet when its implementation methods are being indexed. Caches such methods
109     // and their parent id here and indexes them at the end of crate parsing.
110     orphan_impl_items: Vec<(DefId, clean::Item)>,
111
112     // Similarly to `orphan_impl_items`, sometimes trait impls are picked up
113     // even though the trait itself is not exported. This can happen if a trait
114     // was defined in function/expression scope, since the impl will be picked
115     // up by `collect-trait-impls` but the trait won't be scraped out in the HIR
116     // crawl. In order to prevent crashes when looking for spotlight traits or
117     // when gathering trait documentation on a type, hold impls here while
118     // folding and add them to the cache later on if we find the trait.
119     orphan_trait_impls: Vec<(DefId, FxHashSet<DefId>, Impl)>,
120
121     /// Aliases added through `#[doc(alias = "...")]`. Since a few items can have the same alias,
122     /// we need the alias element to have an array of items.
123     pub(super) aliases: FxHashMap<String, Vec<IndexItem>>,
124 }
125
126 impl Cache {
127     pub fn from_krate(
128         renderinfo: RenderInfo,
129         extern_html_root_urls: &BTreeMap<String, String>,
130         dst: &Path,
131         mut krate: clean::Crate,
132     ) -> (clean::Crate, String, Cache) {
133         // Crawl the crate to build various caches used for the output
134         let RenderInfo {
135             inlined: _,
136             external_paths,
137             exact_paths,
138             access_levels,
139             deref_trait_did,
140             deref_mut_trait_did,
141             owned_box_did,
142         } = renderinfo;
143
144         let external_paths =
145             external_paths.into_iter().map(|(k, (v, t))| (k, (v, ItemType::from(t)))).collect();
146
147         let mut cache = Cache {
148             impls: Default::default(),
149             external_paths,
150             exact_paths,
151             paths: Default::default(),
152             implementors: Default::default(),
153             stack: Vec::new(),
154             parent_stack: Vec::new(),
155             search_index: Vec::new(),
156             parent_is_trait_impl: false,
157             extern_locations: Default::default(),
158             primitive_locations: Default::default(),
159             stripped_mod: false,
160             access_levels,
161             crate_version: krate.version.take(),
162             orphan_impl_items: Vec::new(),
163             orphan_trait_impls: Vec::new(),
164             traits: krate.external_traits.replace(Default::default()),
165             deref_trait_did,
166             deref_mut_trait_did,
167             owned_box_did,
168             masked_crates: mem::take(&mut krate.masked_crates),
169             aliases: Default::default(),
170         };
171
172         // Cache where all our extern crates are located
173         for &(n, ref e) in &krate.externs {
174             let src_root = match e.src {
175                 FileName::Real(ref p) => match p.parent() {
176                     Some(p) => p.to_path_buf(),
177                     None => PathBuf::new(),
178                 },
179                 _ => PathBuf::new(),
180             };
181             let extern_url = extern_html_root_urls.get(&e.name).map(|u| &**u);
182             cache
183                 .extern_locations
184                 .insert(n, (e.name.clone(), src_root, extern_location(e, extern_url, &dst)));
185
186             let did = DefId { krate: n, index: CRATE_DEF_INDEX };
187             cache.external_paths.insert(did, (vec![e.name.to_string()], ItemType::Module));
188         }
189
190         // Cache where all known primitives have their documentation located.
191         //
192         // Favor linking to as local extern as possible, so iterate all crates in
193         // reverse topological order.
194         for &(_, ref e) in krate.externs.iter().rev() {
195             for &(def_id, prim, _) in &e.primitives {
196                 cache.primitive_locations.insert(prim, def_id);
197             }
198         }
199         for &(def_id, prim, _) in &krate.primitives {
200             cache.primitive_locations.insert(prim, def_id);
201         }
202
203         cache.stack.push(krate.name.clone());
204         krate = cache.fold_crate(krate);
205
206         for (trait_did, dids, impl_) in cache.orphan_trait_impls.drain(..) {
207             if cache.traits.contains_key(&trait_did) {
208                 for did in dids {
209                     cache.impls.entry(did).or_insert(vec![]).push(impl_.clone());
210                 }
211             }
212         }
213
214         // Build our search index
215         let index = build_index(&krate, &mut cache);
216
217         (krate, index, cache)
218     }
219 }
220
221 impl DocFolder for Cache {
222     fn fold_item(&mut self, item: clean::Item) -> Option<clean::Item> {
223         if item.def_id.is_local() {
224             debug!("folding {} \"{:?}\", id {:?}", item.type_(), item.name, item.def_id);
225         }
226
227         // If this is a stripped module,
228         // we don't want it or its children in the search index.
229         let orig_stripped_mod = match item.inner {
230             clean::StrippedItem(box clean::ModuleItem(..)) => {
231                 mem::replace(&mut self.stripped_mod, true)
232             }
233             _ => self.stripped_mod,
234         };
235
236         // If the impl is from a masked crate or references something from a
237         // masked crate then remove it completely.
238         if let clean::ImplItem(ref i) = item.inner {
239             if self.masked_crates.contains(&item.def_id.krate)
240                 || i.trait_.def_id().map_or(false, |d| self.masked_crates.contains(&d.krate))
241                 || i.for_.def_id().map_or(false, |d| self.masked_crates.contains(&d.krate))
242             {
243                 return None;
244             }
245         }
246
247         // Propagate a trait method's documentation to all implementors of the
248         // trait.
249         if let clean::TraitItem(ref t) = item.inner {
250             self.traits.entry(item.def_id).or_insert_with(|| t.clone());
251         }
252
253         // Collect all the implementors of traits.
254         if let clean::ImplItem(ref i) = item.inner {
255             if let Some(did) = i.trait_.def_id() {
256                 if i.blanket_impl.is_none() {
257                     self.implementors
258                         .entry(did)
259                         .or_default()
260                         .push(Impl { impl_item: item.clone() });
261                 }
262             }
263         }
264
265         // Index this method for searching later on.
266         if let Some(ref s) = item.name {
267             let (parent, is_inherent_impl_item) = match item.inner {
268                 clean::StrippedItem(..) => ((None, None), false),
269                 clean::AssocConstItem(..) | clean::TypedefItem(_, true)
270                     if self.parent_is_trait_impl =>
271                 {
272                     // skip associated items in trait impls
273                     ((None, None), false)
274                 }
275                 clean::AssocTypeItem(..)
276                 | clean::TyMethodItem(..)
277                 | clean::StructFieldItem(..)
278                 | clean::VariantItem(..) => (
279                     (
280                         Some(*self.parent_stack.last().expect("parent_stack is empty")),
281                         Some(&self.stack[..self.stack.len() - 1]),
282                     ),
283                     false,
284                 ),
285                 clean::MethodItem(..) | clean::AssocConstItem(..) => {
286                     if self.parent_stack.is_empty() {
287                         ((None, None), false)
288                     } else {
289                         let last = self.parent_stack.last().expect("parent_stack is empty 2");
290                         let did = *last;
291                         let path = match self.paths.get(&did) {
292                             // The current stack not necessarily has correlation
293                             // for where the type was defined. On the other
294                             // hand, `paths` always has the right
295                             // information if present.
296                             Some(&(ref fqp, ItemType::Trait))
297                             | Some(&(ref fqp, ItemType::Struct))
298                             | Some(&(ref fqp, ItemType::Union))
299                             | Some(&(ref fqp, ItemType::Enum)) => Some(&fqp[..fqp.len() - 1]),
300                             Some(..) => Some(&*self.stack),
301                             None => None,
302                         };
303                         ((Some(*last), path), true)
304                     }
305                 }
306                 _ => ((None, Some(&*self.stack)), false),
307             };
308
309             match parent {
310                 (parent, Some(path)) if is_inherent_impl_item || (!self.stripped_mod) => {
311                     debug_assert!(!item.is_stripped());
312
313                     // A crate has a module at its root, containing all items,
314                     // which should not be indexed. The crate-item itself is
315                     // inserted later on when serializing the search-index.
316                     if item.def_id.index != CRATE_DEF_INDEX {
317                         self.search_index.push(IndexItem {
318                             ty: item.type_(),
319                             name: s.to_string(),
320                             path: path.join("::"),
321                             desc: shorten(plain_summary_line(item.doc_value())),
322                             parent,
323                             parent_idx: None,
324                             search_type: get_index_search_type(&item),
325                         });
326                     }
327                 }
328                 (Some(parent), None) if is_inherent_impl_item => {
329                     // We have a parent, but we don't know where they're
330                     // defined yet. Wait for later to index this item.
331                     self.orphan_impl_items.push((parent, item.clone()));
332                 }
333                 _ => {}
334             }
335         }
336
337         // Keep track of the fully qualified path for this item.
338         let pushed = match item.name {
339             Some(ref n) if !n.is_empty() => {
340                 self.stack.push(n.to_string());
341                 true
342             }
343             _ => false,
344         };
345
346         match item.inner {
347             clean::StructItem(..)
348             | clean::EnumItem(..)
349             | clean::TypedefItem(..)
350             | clean::TraitItem(..)
351             | clean::FunctionItem(..)
352             | clean::ModuleItem(..)
353             | clean::ForeignFunctionItem(..)
354             | clean::ForeignStaticItem(..)
355             | clean::ConstantItem(..)
356             | clean::StaticItem(..)
357             | clean::UnionItem(..)
358             | clean::ForeignTypeItem
359             | clean::MacroItem(..)
360             | clean::ProcMacroItem(..)
361                 if !self.stripped_mod =>
362             {
363                 // Re-exported items mean that the same id can show up twice
364                 // in the rustdoc ast that we're looking at. We know,
365                 // however, that a re-exported item doesn't show up in the
366                 // `public_items` map, so we can skip inserting into the
367                 // paths map if there was already an entry present and we're
368                 // not a public item.
369                 if !self.paths.contains_key(&item.def_id)
370                     || self.access_levels.is_public(item.def_id)
371                 {
372                     self.paths.insert(item.def_id, (self.stack.clone(), item.type_()));
373                 }
374                 self.add_aliases(&item);
375             }
376             // Link variants to their parent enum because pages aren't emitted
377             // for each variant.
378             clean::VariantItem(..) if !self.stripped_mod => {
379                 let mut stack = self.stack.clone();
380                 stack.pop();
381                 self.paths.insert(item.def_id, (stack, ItemType::Enum));
382             }
383
384             clean::PrimitiveItem(..) => {
385                 self.add_aliases(&item);
386                 self.paths.insert(item.def_id, (self.stack.clone(), item.type_()));
387             }
388
389             _ => {}
390         }
391
392         // Maintain the parent stack
393         let orig_parent_is_trait_impl = self.parent_is_trait_impl;
394         let parent_pushed = match item.inner {
395             clean::TraitItem(..)
396             | clean::EnumItem(..)
397             | clean::ForeignTypeItem
398             | clean::StructItem(..)
399             | clean::UnionItem(..) => {
400                 self.parent_stack.push(item.def_id);
401                 self.parent_is_trait_impl = false;
402                 true
403             }
404             clean::ImplItem(ref i) => {
405                 self.parent_is_trait_impl = i.trait_.is_some();
406                 match i.for_ {
407                     clean::ResolvedPath { did, .. } => {
408                         self.parent_stack.push(did);
409                         true
410                     }
411                     ref t => {
412                         let prim_did = t
413                             .primitive_type()
414                             .and_then(|t| self.primitive_locations.get(&t).cloned());
415                         match prim_did {
416                             Some(did) => {
417                                 self.parent_stack.push(did);
418                                 true
419                             }
420                             None => false,
421                         }
422                     }
423                 }
424             }
425             _ => false,
426         };
427
428         // Once we've recursively found all the generics, hoard off all the
429         // implementations elsewhere.
430         let ret = self.fold_item_recur(item).and_then(|item| {
431             if let clean::Item { inner: clean::ImplItem(_), .. } = item {
432                 // Figure out the id of this impl. This may map to a
433                 // primitive rather than always to a struct/enum.
434                 // Note: matching twice to restrict the lifetime of the `i` borrow.
435                 let mut dids = FxHashSet::default();
436                 if let clean::Item { inner: clean::ImplItem(ref i), .. } = item {
437                     match i.for_ {
438                         clean::ResolvedPath { did, .. }
439                         | clean::BorrowedRef {
440                             type_: box clean::ResolvedPath { did, .. }, ..
441                         } => {
442                             dids.insert(did);
443                         }
444                         ref t => {
445                             let did = t
446                                 .primitive_type()
447                                 .and_then(|t| self.primitive_locations.get(&t).cloned());
448
449                             if let Some(did) = did {
450                                 dids.insert(did);
451                             }
452                         }
453                     }
454
455                     if let Some(generics) = i.trait_.as_ref().and_then(|t| t.generics()) {
456                         for bound in generics {
457                             if let Some(did) = bound.def_id() {
458                                 dids.insert(did);
459                             }
460                         }
461                     }
462                 } else {
463                     unreachable!()
464                 };
465                 let impl_item = Impl { impl_item: item };
466                 if impl_item.trait_did().map_or(true, |d| self.traits.contains_key(&d)) {
467                     for did in dids {
468                         self.impls.entry(did).or_insert(vec![]).push(impl_item.clone());
469                     }
470                 } else {
471                     let trait_did = impl_item.trait_did().expect("no trait did");
472                     self.orphan_trait_impls.push((trait_did, dids, impl_item));
473                 }
474                 None
475             } else {
476                 Some(item)
477             }
478         });
479
480         if pushed {
481             self.stack.pop().expect("stack already empty");
482         }
483         if parent_pushed {
484             self.parent_stack.pop().expect("parent stack already empty");
485         }
486         self.stripped_mod = orig_stripped_mod;
487         self.parent_is_trait_impl = orig_parent_is_trait_impl;
488         ret
489     }
490 }
491
492 impl Cache {
493     fn add_aliases(&mut self, item: &clean::Item) {
494         if item.def_id.index == CRATE_DEF_INDEX {
495             return;
496         }
497         if let Some(ref item_name) = item.name {
498             let path = self
499                 .paths
500                 .get(&item.def_id)
501                 .map(|p| p.0[..p.0.len() - 1].join("::"))
502                 .unwrap_or("std".to_owned());
503             for alias in item
504                 .attrs
505                 .lists(sym::doc)
506                 .filter(|a| a.check_name(sym::alias))
507                 .filter_map(|a| a.value_str().map(|s| s.to_string().replace("\"", "")))
508                 .filter(|v| !v.is_empty())
509                 .collect::<FxHashSet<_>>()
510                 .into_iter()
511             {
512                 self.aliases.entry(alias).or_insert(Vec::with_capacity(1)).push(IndexItem {
513                     ty: item.type_(),
514                     name: item_name.to_string(),
515                     path: path.clone(),
516                     desc: shorten(plain_summary_line(item.doc_value())),
517                     parent: None,
518                     parent_idx: None,
519                     search_type: get_index_search_type(&item),
520                 });
521             }
522         }
523     }
524 }
525
526 /// Attempts to find where an external crate is located, given that we're
527 /// rendering in to the specified source destination.
528 fn extern_location(
529     e: &clean::ExternalCrate,
530     extern_url: Option<&str>,
531     dst: &Path,
532 ) -> ExternalLocation {
533     use ExternalLocation::*;
534     // See if there's documentation generated into the local directory
535     let local_location = dst.join(&e.name);
536     if local_location.is_dir() {
537         return Local;
538     }
539
540     if let Some(url) = extern_url {
541         let mut url = url.to_string();
542         if !url.ends_with("/") {
543             url.push('/');
544         }
545         return Remote(url);
546     }
547
548     // Failing that, see if there's an attribute specifying where to find this
549     // external crate
550     e.attrs
551         .lists(sym::doc)
552         .filter(|a| a.check_name(sym::html_root_url))
553         .filter_map(|a| a.value_str())
554         .map(|url| {
555             let mut url = url.to_string();
556             if !url.ends_with("/") {
557                 url.push('/')
558             }
559             Remote(url)
560         })
561         .next()
562         .unwrap_or(Unknown) // Well, at least we tried.
563 }
564
565 /// Builds the search index from the collected metadata
566 fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
567     let mut nodeid_to_pathid = FxHashMap::default();
568     let mut crate_items = Vec::with_capacity(cache.search_index.len());
569     let mut crate_paths = vec![];
570
571     let Cache { ref mut search_index, ref orphan_impl_items, ref paths, .. } = *cache;
572
573     // Attach all orphan items to the type's definition if the type
574     // has since been learned.
575     for &(did, ref item) in orphan_impl_items {
576         if let Some(&(ref fqp, _)) = paths.get(&did) {
577             if item.name.is_none() { // this is most likely from a typedef
578                 continue;
579             }
580             search_index.push(IndexItem {
581                 ty: item.type_(),
582                 name: item.name.clone().unwrap(),
583                 path: fqp[..fqp.len() - 1].join("::"),
584                 desc: shorten(plain_summary_line(item.doc_value())),
585                 parent: Some(did),
586                 parent_idx: None,
587                 search_type: get_index_search_type(&item),
588             });
589         }
590     }
591
592     // Reduce `NodeId` in paths into smaller sequential numbers,
593     // and prune the paths that do not appear in the index.
594     let mut lastpath = String::new();
595     let mut lastpathid = 0usize;
596
597     for item in search_index {
598         item.parent_idx = match item.parent {
599             Some(nodeid) => {
600                 Some(if nodeid_to_pathid.contains_key(&nodeid) {
601                     *nodeid_to_pathid.get(&nodeid).expect("no pathid")
602                 } else {
603                     let pathid = lastpathid;
604                     nodeid_to_pathid.insert(nodeid, pathid);
605                     lastpathid += 1;
606
607                     if let Some(&(ref fqp, short)) = paths.get(&nodeid) {
608                         crate_paths.push((short, fqp.last().expect("no fqp").clone()));
609                     } else {
610                         continue
611                     }
612                     pathid
613                 })
614             }
615             None => None,
616         };
617
618         // Omit the parent path if it is same to that of the prior item.
619         if lastpath == item.path {
620             item.path.clear();
621         } else {
622             lastpath = item.path.clone();
623         }
624         crate_items.push(&*item);
625     }
626
627     let crate_doc = krate
628         .module
629         .as_ref()
630         .map(|module| shorten(plain_summary_line(module.doc_value())))
631         .unwrap_or(String::new());
632
633     #[derive(Serialize)]
634     struct CrateData<'a> {
635         doc: String,
636         #[serde(rename = "i")]
637         items: Vec<&'a IndexItem>,
638         #[serde(rename = "p")]
639         paths: Vec<(ItemType, String)>,
640     }
641
642     // Collect the index into a string
643     format!(
644         r#"searchIndex["{}"] = {};"#,
645         krate.name,
646         serde_json::to_string(&CrateData {
647             doc: crate_doc,
648             items: crate_items,
649             paths: crate_paths,
650         })
651         .expect("failed serde conversion")
652     )
653 }
654
655 fn get_index_search_type(item: &clean::Item) -> Option<IndexItemFunctionType> {
656     let (all_types, ret_types) = match item.inner {
657         clean::FunctionItem(ref f) => (&f.all_types, &f.ret_types),
658         clean::MethodItem(ref m) => (&m.all_types, &m.ret_types),
659         clean::TyMethodItem(ref m) => (&m.all_types, &m.ret_types),
660         _ => return None,
661     };
662
663     let inputs =
664         all_types.iter().map(|arg| get_index_type(&arg)).filter(|a| a.name.is_some()).collect();
665     let output = ret_types
666         .iter()
667         .map(|arg| get_index_type(&arg))
668         .filter(|a| a.name.is_some())
669         .collect::<Vec<_>>();
670     let output = if output.is_empty() { None } else { Some(output) };
671
672     Some(IndexItemFunctionType { inputs, output })
673 }
674
675 fn get_index_type(clean_type: &clean::Type) -> Type {
676     let t = Type {
677         name: get_index_type_name(clean_type, true).map(|s| s.to_ascii_lowercase()),
678         generics: get_generics(clean_type),
679     };
680     t
681 }
682
683 fn get_index_type_name(clean_type: &clean::Type, accept_generic: bool) -> Option<String> {
684     match *clean_type {
685         clean::ResolvedPath { ref path, .. } => {
686             let segments = &path.segments;
687             let path_segment = segments.into_iter().last().unwrap_or_else(|| panic!(
688                 "get_index_type_name(clean_type: {:?}, accept_generic: {:?}) had length zero path",
689                 clean_type, accept_generic
690             ));
691             Some(path_segment.name.clone())
692         }
693         clean::Generic(ref s) if accept_generic => Some(s.clone()),
694         clean::Primitive(ref p) => Some(format!("{:?}", p)),
695         clean::BorrowedRef { ref type_, .. } => get_index_type_name(type_, accept_generic),
696         // FIXME: add all from clean::Type.
697         _ => None,
698     }
699 }
700
701 fn get_generics(clean_type: &clean::Type) -> Option<Vec<String>> {
702     clean_type.generics().and_then(|types| {
703         let r = types
704             .iter()
705             .filter_map(|t| get_index_type_name(t, false))
706             .map(|s| s.to_ascii_lowercase())
707             .collect::<Vec<_>>();
708         if r.is_empty() { None } else { Some(r) }
709     })
710 }