1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! This module contains the "cleaned" pieces of the AST, and the functions
14 pub use self::Type::*;
15 pub use self::Mutability::*;
16 pub use self::ItemEnum::*;
17 pub use self::SelfTy::*;
18 pub use self::FunctionRetTy::*;
19 pub use self::Visibility::{Public, Inherited};
21 use rustc_target::spec::abi::Abi;
23 use syntax::ast::{self, AttrStyle, NodeId, Ident};
25 use syntax::codemap::{dummy_spanned, Spanned};
26 use syntax::feature_gate::UnstableFeatures;
28 use syntax::symbol::keywords::{self, Keyword};
29 use syntax::symbol::{Symbol, InternedString};
30 use syntax_pos::{self, DUMMY_SP, Pos, FileName};
32 use rustc::mir::interpret::ConstValue;
33 use rustc::middle::privacy::AccessLevels;
34 use rustc::middle::resolve_lifetime as rl;
35 use rustc::ty::fold::TypeFolder;
36 use rustc::middle::lang_items;
37 use rustc::mir::interpret::GlobalId;
38 use rustc::hir::{self, GenericArg, HirVec};
39 use rustc::hir::def::{self, Def, CtorKind};
40 use rustc::hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};
41 use rustc::hir::def_id::DefIndexAddressSpace;
42 use rustc::ty::subst::Substs;
43 use rustc::ty::{self, TyCtxt, Region, RegionVid, Ty, AdtKind};
44 use rustc::middle::stability;
45 use rustc::util::nodemap::{FxHashMap, FxHashSet};
46 use rustc_typeck::hir_ty_to_ty;
47 use rustc::infer::region_constraints::{RegionConstraintData, Constraint};
48 use rustc::lint as lint;
50 use std::collections::hash_map::Entry;
52 use std::hash::{Hash, Hasher};
53 use std::default::Default;
54 use std::{mem, slice, vec};
55 use std::iter::{FromIterator, once};
56 use rustc_data_structures::sync::Lrc;
58 use std::str::FromStr;
59 use std::cell::RefCell;
64 use core::{self, DocContext};
67 use html::render::{cache, ExternalLocation};
68 use html::item_type::ItemType;
69 use html::markdown::markdown_links;
77 use self::auto_trait::AutoTraitFinder;
79 thread_local!(static MAX_DEF_ID: RefCell<FxHashMap<CrateNum, DefId>> = RefCell::new(FxHashMap()));
81 const FN_OUTPUT_NAME: &'static str = "Output";
83 // extract the stability index for a node from tcx, if possible
84 fn get_stability(cx: &DocContext, def_id: DefId) -> Option<Stability> {
85 cx.tcx.lookup_stability(def_id).clean(cx)
88 fn get_deprecation(cx: &DocContext, def_id: DefId) -> Option<Deprecation> {
89 cx.tcx.lookup_deprecation(def_id).clean(cx)
93 fn clean(&self, cx: &DocContext) -> T;
96 impl<T: Clean<U>, U> Clean<Vec<U>> for [T] {
97 fn clean(&self, cx: &DocContext) -> Vec<U> {
98 self.iter().map(|x| x.clean(cx)).collect()
102 impl<T: Clean<U>, U> Clean<U> for P<T> {
103 fn clean(&self, cx: &DocContext) -> U {
108 impl<T: Clean<U>, U> Clean<U> for Rc<T> {
109 fn clean(&self, cx: &DocContext) -> U {
114 impl<T: Clean<U>, U> Clean<Option<U>> for Option<T> {
115 fn clean(&self, cx: &DocContext) -> Option<U> {
116 self.as_ref().map(|v| v.clean(cx))
120 impl<T, U> Clean<U> for ty::Binder<T> where T: Clean<U> {
121 fn clean(&self, cx: &DocContext) -> U {
122 self.skip_binder().clean(cx)
126 impl<T: Clean<U>, U> Clean<Vec<U>> for P<[T]> {
127 fn clean(&self, cx: &DocContext) -> Vec<U> {
128 self.iter().map(|x| x.clean(cx)).collect()
132 #[derive(Clone, Debug)]
135 pub version: Option<String>,
137 pub module: Option<Item>,
138 pub externs: Vec<(CrateNum, ExternalCrate)>,
139 pub primitives: Vec<(DefId, PrimitiveType, Attributes)>,
140 pub access_levels: Arc<AccessLevels<DefId>>,
141 // These are later on moved into `CACHEKEY`, leaving the map empty.
142 // Only here so that they can be filtered through the rustdoc passes.
143 pub external_traits: FxHashMap<DefId, Trait>,
144 pub masked_crates: FxHashSet<CrateNum>,
147 impl<'a, 'tcx, 'rcx> Clean<Crate> for visit_ast::RustdocVisitor<'a, 'tcx, 'rcx> {
148 fn clean(&self, cx: &DocContext) -> Crate {
149 use ::visit_lib::LibEmbargoVisitor;
152 let mut r = cx.renderinfo.borrow_mut();
153 r.deref_trait_did = cx.tcx.lang_items().deref_trait();
154 r.deref_mut_trait_did = cx.tcx.lang_items().deref_mut_trait();
155 r.owned_box_did = cx.tcx.lang_items().owned_box();
158 let mut externs = Vec::new();
159 for &cnum in cx.tcx.crates().iter() {
160 externs.push((cnum, cnum.clean(cx)));
161 // Analyze doc-reachability for extern items
162 LibEmbargoVisitor::new(cx).visit_lib(cnum);
164 externs.sort_by(|&(a, _), &(b, _)| a.cmp(&b));
166 // Clean the crate, translating the entire libsyntax AST to one that is
167 // understood by rustdoc.
168 let mut module = self.module.clean(cx);
169 let mut masked_crates = FxHashSet();
172 ModuleItem(ref module) => {
173 for it in &module.items {
174 if it.is_extern_crate() && it.attrs.has_doc_flag("masked") {
175 masked_crates.insert(it.def_id.krate);
182 let ExternalCrate { name, src, primitives, keywords, .. } = LOCAL_CRATE.clean(cx);
184 let m = match module.inner {
185 ModuleItem(ref mut m) => m,
188 m.items.extend(primitives.iter().map(|&(def_id, prim, ref attrs)| {
190 source: Span::empty(),
191 name: Some(prim.to_url_str().to_string()),
192 attrs: attrs.clone(),
193 visibility: Some(Public),
194 stability: get_stability(cx, def_id),
195 deprecation: get_deprecation(cx, def_id),
197 inner: PrimitiveItem(prim),
200 m.items.extend(keywords.into_iter().map(|(def_id, kw, attrs)| {
202 source: Span::empty(),
203 name: Some(kw.clone()),
205 visibility: Some(Public),
206 stability: get_stability(cx, def_id),
207 deprecation: get_deprecation(cx, def_id),
209 inner: KeywordItem(kw),
214 let mut access_levels = cx.access_levels.borrow_mut();
215 let mut external_traits = cx.external_traits.borrow_mut();
221 module: Some(module),
224 access_levels: Arc::new(mem::replace(&mut access_levels, Default::default())),
225 external_traits: mem::replace(&mut external_traits, Default::default()),
231 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
232 pub struct ExternalCrate {
235 pub attrs: Attributes,
236 pub primitives: Vec<(DefId, PrimitiveType, Attributes)>,
237 pub keywords: Vec<(DefId, String, Attributes)>,
240 impl Clean<ExternalCrate> for CrateNum {
241 fn clean(&self, cx: &DocContext) -> ExternalCrate {
242 let root = DefId { krate: *self, index: CRATE_DEF_INDEX };
243 let krate_span = cx.tcx.def_span(root);
244 let krate_src = cx.sess().codemap().span_to_filename(krate_span);
246 // Collect all inner modules which are tagged as implementations of
249 // Note that this loop only searches the top-level items of the crate,
250 // and this is intentional. If we were to search the entire crate for an
251 // item tagged with `#[doc(primitive)]` then we would also have to
252 // search the entirety of external modules for items tagged
253 // `#[doc(primitive)]`, which is a pretty inefficient process (decoding
254 // all that metadata unconditionally).
256 // In order to keep the metadata load under control, the
257 // `#[doc(primitive)]` feature is explicitly designed to only allow the
258 // primitive tags to show up as the top level items in a crate.
260 // Also note that this does not attempt to deal with modules tagged
261 // duplicately for the same primitive. This is handled later on when
262 // rendering by delegating everything to a hash map.
263 let as_primitive = |def: Def| {
264 if let Def::Mod(def_id) = def {
265 let attrs = cx.tcx.get_attrs(def_id).clean(cx);
267 for attr in attrs.lists("doc") {
268 if let Some(v) = attr.value_str() {
269 if attr.check_name("primitive") {
270 prim = PrimitiveType::from_str(&v.as_str());
274 // FIXME: should warn on unknown primitives?
278 return prim.map(|p| (def_id, p, attrs));
282 let primitives = if root.is_local() {
283 cx.tcx.hir.krate().module.item_ids.iter().filter_map(|&id| {
284 let item = cx.tcx.hir.expect_item(id.id);
287 as_primitive(Def::Mod(cx.tcx.hir.local_def_id(id.id)))
289 hir::ItemUse(ref path, hir::UseKind::Single)
290 if item.vis.node.is_pub() => {
291 as_primitive(path.def).map(|(_, prim, attrs)| {
292 // Pretend the primitive is local.
293 (cx.tcx.hir.local_def_id(id.id), prim, attrs)
300 cx.tcx.item_children(root).iter().map(|item| item.def)
301 .filter_map(as_primitive).collect()
304 let as_keyword = |def: Def| {
305 if let Def::Mod(def_id) = def {
306 let attrs = cx.tcx.get_attrs(def_id).clean(cx);
307 let mut keyword = None;
308 for attr in attrs.lists("doc") {
309 if let Some(v) = attr.value_str() {
310 if attr.check_name("keyword") {
311 keyword = Keyword::from_str(&v.as_str()).ok()
312 .map(|x| x.name().to_string());
313 if keyword.is_some() {
316 // FIXME: should warn on unknown keywords?
320 return keyword.map(|p| (def_id, p, attrs));
324 let keywords = if root.is_local() {
325 cx.tcx.hir.krate().module.item_ids.iter().filter_map(|&id| {
326 let item = cx.tcx.hir.expect_item(id.id);
329 as_keyword(Def::Mod(cx.tcx.hir.local_def_id(id.id)))
331 hir::ItemUse(ref path, hir::UseKind::Single)
332 if item.vis.node.is_pub() => {
333 as_keyword(path.def).map(|(_, prim, attrs)| {
334 (cx.tcx.hir.local_def_id(id.id), prim, attrs)
341 cx.tcx.item_children(root).iter().map(|item| item.def)
342 .filter_map(as_keyword).collect()
346 name: cx.tcx.crate_name(*self).to_string(),
348 attrs: cx.tcx.get_attrs(root).clean(cx),
355 /// Anything with a source location and set of attributes and, optionally, a
356 /// name. That is, anything that can be documented. This doesn't correspond
357 /// directly to the AST's concept of an item; it's a strict superset.
358 #[derive(Clone, RustcEncodable, RustcDecodable)]
362 /// Not everything has a name. E.g., impls
363 pub name: Option<String>,
364 pub attrs: Attributes,
366 pub visibility: Option<Visibility>,
368 pub stability: Option<Stability>,
369 pub deprecation: Option<Deprecation>,
372 impl fmt::Debug for Item {
373 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
375 let fake = MAX_DEF_ID.with(|m| m.borrow().get(&self.def_id.krate)
376 .map(|id| self.def_id >= *id).unwrap_or(false));
377 let def_id: &fmt::Debug = if fake { &"**FAKE**" } else { &self.def_id };
379 fmt.debug_struct("Item")
380 .field("source", &self.source)
381 .field("name", &self.name)
382 .field("attrs", &self.attrs)
383 .field("inner", &self.inner)
384 .field("visibility", &self.visibility)
385 .field("def_id", def_id)
386 .field("stability", &self.stability)
387 .field("deprecation", &self.deprecation)
393 /// Finds the `doc` attribute as a NameValue and returns the corresponding
395 pub fn doc_value<'a>(&'a self) -> Option<&'a str> {
396 self.attrs.doc_value()
398 /// Finds all `doc` attributes as NameValues and returns their corresponding values, joined
400 pub fn collapsed_doc_value(&self) -> Option<String> {
401 self.attrs.collapsed_doc_value()
404 pub fn links(&self) -> Vec<(String, String)> {
405 self.attrs.links(&self.def_id.krate)
408 pub fn is_crate(&self) -> bool {
410 StrippedItem(box ModuleItem(Module { is_crate: true, ..})) |
411 ModuleItem(Module { is_crate: true, ..}) => true,
415 pub fn is_mod(&self) -> bool {
416 self.type_() == ItemType::Module
418 pub fn is_trait(&self) -> bool {
419 self.type_() == ItemType::Trait
421 pub fn is_struct(&self) -> bool {
422 self.type_() == ItemType::Struct
424 pub fn is_enum(&self) -> bool {
425 self.type_() == ItemType::Enum
427 pub fn is_fn(&self) -> bool {
428 self.type_() == ItemType::Function
430 pub fn is_associated_type(&self) -> bool {
431 self.type_() == ItemType::AssociatedType
433 pub fn is_associated_const(&self) -> bool {
434 self.type_() == ItemType::AssociatedConst
436 pub fn is_method(&self) -> bool {
437 self.type_() == ItemType::Method
439 pub fn is_ty_method(&self) -> bool {
440 self.type_() == ItemType::TyMethod
442 pub fn is_typedef(&self) -> bool {
443 self.type_() == ItemType::Typedef
445 pub fn is_primitive(&self) -> bool {
446 self.type_() == ItemType::Primitive
448 pub fn is_union(&self) -> bool {
449 self.type_() == ItemType::Union
451 pub fn is_import(&self) -> bool {
452 self.type_() == ItemType::Import
454 pub fn is_extern_crate(&self) -> bool {
455 self.type_() == ItemType::ExternCrate
457 pub fn is_keyword(&self) -> bool {
458 self.type_() == ItemType::Keyword
461 pub fn is_stripped(&self) -> bool {
462 match self.inner { StrippedItem(..) => true, _ => false }
464 pub fn has_stripped_fields(&self) -> Option<bool> {
466 StructItem(ref _struct) => Some(_struct.fields_stripped),
467 UnionItem(ref union) => Some(union.fields_stripped),
468 VariantItem(Variant { kind: VariantKind::Struct(ref vstruct)} ) => {
469 Some(vstruct.fields_stripped)
475 pub fn stability_class(&self) -> Option<String> {
476 self.stability.as_ref().and_then(|ref s| {
477 let mut classes = Vec::with_capacity(2);
479 if s.level == stability::Unstable {
480 classes.push("unstable");
483 if !s.deprecated_since.is_empty() {
484 classes.push("deprecated");
487 if classes.len() != 0 {
488 Some(classes.join(" "))
495 pub fn stable_since(&self) -> Option<&str> {
496 self.stability.as_ref().map(|s| &s.since[..])
499 /// Returns a documentation-level item type from the item.
500 pub fn type_(&self) -> ItemType {
505 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
507 ExternCrateItem(String, Option<String>),
512 FunctionItem(Function),
514 TypedefItem(Typedef, bool /* is associated type */),
516 ConstantItem(Constant),
519 /// A method signature only. Used for required methods in traits (ie,
520 /// non-default-methods).
521 TyMethodItem(TyMethod),
522 /// A method with a body.
524 StructFieldItem(Type),
525 VariantItem(Variant),
526 /// `fn`s from an extern block
527 ForeignFunctionItem(Function),
528 /// `static`s from an extern block
529 ForeignStaticItem(Static),
530 /// `type`s from an extern block
533 PrimitiveItem(PrimitiveType),
534 AssociatedConstItem(Type, Option<String>),
535 AssociatedTypeItem(Vec<GenericBound>, Option<Type>),
536 /// An item that has been stripped by a rustdoc pass
537 StrippedItem(Box<ItemEnum>),
542 pub fn generics(&self) -> Option<&Generics> {
544 ItemEnum::StructItem(ref s) => &s.generics,
545 ItemEnum::EnumItem(ref e) => &e.generics,
546 ItemEnum::FunctionItem(ref f) => &f.generics,
547 ItemEnum::TypedefItem(ref t, _) => &t.generics,
548 ItemEnum::TraitItem(ref t) => &t.generics,
549 ItemEnum::ImplItem(ref i) => &i.generics,
550 ItemEnum::TyMethodItem(ref i) => &i.generics,
551 ItemEnum::MethodItem(ref i) => &i.generics,
552 ItemEnum::ForeignFunctionItem(ref f) => &f.generics,
558 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
560 pub items: Vec<Item>,
564 impl Clean<Item> for doctree::Module {
565 fn clean(&self, cx: &DocContext) -> Item {
566 let name = if self.name.is_some() {
567 self.name.unwrap().clean(cx)
572 // maintain a stack of mod ids, for doc comment path resolution
573 // but we also need to resolve the module's own docs based on whether its docs were written
574 // inside or outside the module, so check for that
575 let attrs = if self.attrs.iter()
576 .filter(|a| a.check_name("doc"))
578 .map_or(true, |a| a.style == AttrStyle::Inner) {
579 // inner doc comment, use the module's own scope for resolution
580 cx.mod_ids.borrow_mut().push(self.id);
583 // outer doc comment, use its parent's scope
584 let attrs = self.attrs.clean(cx);
585 cx.mod_ids.borrow_mut().push(self.id);
589 let mut items: Vec<Item> = vec![];
590 items.extend(self.extern_crates.iter().map(|x| x.clean(cx)));
591 items.extend(self.imports.iter().flat_map(|x| x.clean(cx)));
592 items.extend(self.structs.iter().flat_map(|x| x.clean(cx)));
593 items.extend(self.unions.iter().flat_map(|x| x.clean(cx)));
594 items.extend(self.enums.iter().flat_map(|x| x.clean(cx)));
595 items.extend(self.fns.iter().map(|x| x.clean(cx)));
596 items.extend(self.foreigns.iter().flat_map(|x| x.clean(cx)));
597 items.extend(self.mods.iter().map(|x| x.clean(cx)));
598 items.extend(self.typedefs.iter().map(|x| x.clean(cx)));
599 items.extend(self.statics.iter().map(|x| x.clean(cx)));
600 items.extend(self.constants.iter().map(|x| x.clean(cx)));
601 items.extend(self.traits.iter().map(|x| x.clean(cx)));
602 items.extend(self.impls.iter().flat_map(|x| x.clean(cx)));
603 items.extend(self.macros.iter().map(|x| x.clean(cx)));
605 cx.mod_ids.borrow_mut().pop();
607 // determine if we should display the inner contents or
608 // the outer `mod` item for the source code.
610 let cm = cx.sess().codemap();
611 let outer = cm.lookup_char_pos(self.where_outer.lo());
612 let inner = cm.lookup_char_pos(self.where_inner.lo());
613 if outer.file.start_pos == inner.file.start_pos {
617 // mod foo; (and a separate FileMap for the contents)
625 source: whence.clean(cx),
626 visibility: self.vis.clean(cx),
627 stability: self.stab.clean(cx),
628 deprecation: self.depr.clean(cx),
629 def_id: cx.tcx.hir.local_def_id(self.id),
630 inner: ModuleItem(Module {
631 is_crate: self.is_crate,
638 pub struct ListAttributesIter<'a> {
639 attrs: slice::Iter<'a, ast::Attribute>,
640 current_list: vec::IntoIter<ast::NestedMetaItem>,
644 impl<'a> Iterator for ListAttributesIter<'a> {
645 type Item = ast::NestedMetaItem;
647 fn next(&mut self) -> Option<Self::Item> {
648 if let Some(nested) = self.current_list.next() {
652 for attr in &mut self.attrs {
653 if let Some(list) = attr.meta_item_list() {
654 if attr.check_name(self.name) {
655 self.current_list = list.into_iter();
656 if let Some(nested) = self.current_list.next() {
666 fn size_hint(&self) -> (usize, Option<usize>) {
667 let lower = self.current_list.len();
672 pub trait AttributesExt {
673 /// Finds an attribute as List and returns the list of attributes nested inside.
674 fn lists<'a>(&'a self, name: &'a str) -> ListAttributesIter<'a>;
677 impl AttributesExt for [ast::Attribute] {
678 fn lists<'a>(&'a self, name: &'a str) -> ListAttributesIter<'a> {
681 current_list: Vec::new().into_iter(),
687 pub trait NestedAttributesExt {
688 /// Returns whether the attribute list contains a specific `Word`
689 fn has_word(self, word: &str) -> bool;
692 impl<I: IntoIterator<Item=ast::NestedMetaItem>> NestedAttributesExt for I {
693 fn has_word(self, word: &str) -> bool {
694 self.into_iter().any(|attr| attr.is_word() && attr.check_name(word))
698 /// A portion of documentation, extracted from a `#[doc]` attribute.
700 /// Each variant contains the line number within the complete doc-comment where the fragment
701 /// starts, as well as the Span where the corresponding doc comment or attribute is located.
703 /// Included files are kept separate from inline doc comments so that proper line-number
704 /// information can be given when a doctest fails. Sugared doc comments and "raw" doc comments are
705 /// kept separate because of issue #42760.
706 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
707 pub enum DocFragment {
708 // FIXME #44229 (misdreavus): sugared and raw doc comments can be brought back together once
709 // hoedown is completely removed from rustdoc.
710 /// A doc fragment created from a `///` or `//!` doc comment.
711 SugaredDoc(usize, syntax_pos::Span, String),
712 /// A doc fragment created from a "raw" `#[doc=""]` attribute.
713 RawDoc(usize, syntax_pos::Span, String),
714 /// A doc fragment created from a `#[doc(include="filename")]` attribute. Contains both the
715 /// given filename and the file contents.
716 Include(usize, syntax_pos::Span, String, String),
720 pub fn as_str(&self) -> &str {
722 DocFragment::SugaredDoc(_, _, ref s) => &s[..],
723 DocFragment::RawDoc(_, _, ref s) => &s[..],
724 DocFragment::Include(_, _, _, ref s) => &s[..],
728 pub fn span(&self) -> syntax_pos::Span {
730 DocFragment::SugaredDoc(_, span, _) |
731 DocFragment::RawDoc(_, span, _) |
732 DocFragment::Include(_, span, _, _) => span,
737 impl<'a> FromIterator<&'a DocFragment> for String {
738 fn from_iter<T>(iter: T) -> Self
740 T: IntoIterator<Item = &'a DocFragment>
742 iter.into_iter().fold(String::new(), |mut acc, frag| {
747 DocFragment::SugaredDoc(_, _, ref docs)
748 | DocFragment::RawDoc(_, _, ref docs)
749 | DocFragment::Include(_, _, _, ref docs) =>
758 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, Default)]
759 pub struct Attributes {
760 pub doc_strings: Vec<DocFragment>,
761 pub other_attrs: Vec<ast::Attribute>,
762 pub cfg: Option<Arc<Cfg>>,
763 pub span: Option<syntax_pos::Span>,
764 /// map from Rust paths to resolved defs and potential URL fragments
765 pub links: Vec<(String, Option<DefId>, Option<String>)>,
769 /// Extracts the content from an attribute `#[doc(cfg(content))]`.
770 fn extract_cfg(mi: &ast::MetaItem) -> Option<&ast::MetaItem> {
771 use syntax::ast::NestedMetaItemKind::MetaItem;
773 if let ast::MetaItemKind::List(ref nmis) = mi.node {
775 if let MetaItem(ref cfg_mi) = nmis[0].node {
776 if cfg_mi.check_name("cfg") {
777 if let ast::MetaItemKind::List(ref cfg_nmis) = cfg_mi.node {
778 if cfg_nmis.len() == 1 {
779 if let MetaItem(ref content_mi) = cfg_nmis[0].node {
780 return Some(content_mi);
792 /// Reads a `MetaItem` from within an attribute, looks for whether it is a
793 /// `#[doc(include="file")]`, and returns the filename and contents of the file as loaded from
795 fn extract_include(mi: &ast::MetaItem)
796 -> Option<(String, String)>
798 mi.meta_item_list().and_then(|list| {
800 if meta.check_name("include") {
801 // the actual compiled `#[doc(include="filename")]` gets expanded to
802 // `#[doc(include(file="filename", contents="file contents")]` so we need to
803 // look for that instead
804 return meta.meta_item_list().and_then(|list| {
805 let mut filename: Option<String> = None;
806 let mut contents: Option<String> = None;
809 if it.check_name("file") {
810 if let Some(name) = it.value_str() {
811 filename = Some(name.to_string());
813 } else if it.check_name("contents") {
814 if let Some(docs) = it.value_str() {
815 contents = Some(docs.to_string());
820 if let (Some(filename), Some(contents)) = (filename, contents) {
821 Some((filename, contents))
833 pub fn has_doc_flag(&self, flag: &str) -> bool {
834 for attr in &self.other_attrs {
835 if !attr.check_name("doc") { continue; }
837 if let Some(items) = attr.meta_item_list() {
838 if items.iter().filter_map(|i| i.meta_item()).any(|it| it.check_name(flag)) {
847 pub fn from_ast(diagnostic: &::errors::Handler,
848 attrs: &[ast::Attribute]) -> Attributes {
849 let mut doc_strings = vec![];
851 let mut cfg = Cfg::True;
852 let mut doc_line = 0;
854 let other_attrs = attrs.iter().filter_map(|attr| {
855 attr.with_desugared_doc(|attr| {
856 if attr.check_name("doc") {
857 if let Some(mi) = attr.meta() {
858 if let Some(value) = mi.value_str() {
859 // Extracted #[doc = "..."]
860 let value = value.to_string();
862 doc_line += value.lines().count();
864 if attr.is_sugared_doc {
865 doc_strings.push(DocFragment::SugaredDoc(line, attr.span, value));
867 doc_strings.push(DocFragment::RawDoc(line, attr.span, value));
871 sp = Some(attr.span);
874 } else if let Some(cfg_mi) = Attributes::extract_cfg(&mi) {
875 // Extracted #[doc(cfg(...))]
876 match Cfg::parse(cfg_mi) {
877 Ok(new_cfg) => cfg &= new_cfg,
878 Err(e) => diagnostic.span_err(e.span, e.msg),
881 } else if let Some((filename, contents)) = Attributes::extract_include(&mi)
884 doc_line += contents.lines().count();
885 doc_strings.push(DocFragment::Include(line,
896 // treat #[target_feature(enable = "feat")] attributes as if they were
897 // #[doc(cfg(target_feature = "feat"))] attributes as well
898 for attr in attrs.lists("target_feature") {
899 if attr.check_name("enable") {
900 if let Some(feat) = attr.value_str() {
901 let meta = attr::mk_name_value_item_str(Ident::from_str("target_feature"),
902 dummy_spanned(feat));
903 if let Ok(feat_cfg) = Cfg::parse(&meta) {
913 cfg: if cfg == Cfg::True { None } else { Some(Arc::new(cfg)) },
919 /// Finds the `doc` attribute as a NameValue and returns the corresponding
921 pub fn doc_value<'a>(&'a self) -> Option<&'a str> {
922 self.doc_strings.first().map(|s| s.as_str())
925 /// Finds all `doc` attributes as NameValues and returns their corresponding values, joined
927 pub fn collapsed_doc_value(&self) -> Option<String> {
928 if !self.doc_strings.is_empty() {
929 Some(self.doc_strings.iter().collect())
935 /// Get links as a vector
937 /// Cache must be populated before call
938 pub fn links(&self, krate: &CrateNum) -> Vec<(String, String)> {
939 use html::format::href;
940 self.links.iter().filter_map(|&(ref s, did, ref fragment)| {
943 if let Some((mut href, ..)) = href(did) {
944 if let Some(ref fragment) = *fragment {
946 href.push_str(fragment);
948 Some((s.clone(), href))
954 if let Some(ref fragment) = *fragment {
956 let url = match cache.extern_locations.get(krate) {
957 Some(&(_, ref src, ExternalLocation::Local)) =>
958 src.to_str().expect("invalid file path"),
959 Some(&(_, _, ExternalLocation::Remote(ref s))) => s,
960 Some(&(_, _, ExternalLocation::Unknown)) | None =>
961 "https://doc.rust-lang.org/nightly",
963 // This is a primitive so the url is done "by hand".
965 format!("{}{}std/primitive.{}.html",
967 if !url.ends_with('/') { "/" } else { "" },
970 panic!("This isn't a primitive?!");
978 impl PartialEq for Attributes {
979 fn eq(&self, rhs: &Self) -> bool {
980 self.doc_strings == rhs.doc_strings &&
981 self.cfg == rhs.cfg &&
982 self.span == rhs.span &&
983 self.links == rhs.links &&
984 self.other_attrs.iter().map(|attr| attr.id).eq(rhs.other_attrs.iter().map(|attr| attr.id))
988 impl Eq for Attributes {}
990 impl Hash for Attributes {
991 fn hash<H: Hasher>(&self, hasher: &mut H) {
992 self.doc_strings.hash(hasher);
993 self.cfg.hash(hasher);
994 self.span.hash(hasher);
995 self.links.hash(hasher);
996 for attr in &self.other_attrs {
997 attr.id.hash(hasher);
1002 impl AttributesExt for Attributes {
1003 fn lists<'a>(&'a self, name: &'a str) -> ListAttributesIter<'a> {
1004 self.other_attrs.lists(name)
1008 /// Given a def, returns its name and disambiguator
1009 /// for a value namespace
1011 /// Returns None for things which cannot be ambiguous since
1012 /// they exist in both namespaces (structs and modules)
1013 fn value_ns_kind(def: Def, path_str: &str) -> Option<(&'static str, String)> {
1015 // structs, variants, and mods exist in both namespaces. skip them
1016 Def::StructCtor(..) | Def::Mod(..) | Def::Variant(..) | Def::VariantCtor(..) => None,
1018 => Some(("function", format!("{}()", path_str))),
1020 => Some(("method", format!("{}()", path_str))),
1022 => Some(("const", format!("const@{}", path_str))),
1024 => Some(("static", format!("static@{}", path_str))),
1025 _ => Some(("value", format!("value@{}", path_str))),
1029 /// Given a def, returns its name, the article to be used, and a disambiguator
1030 /// for the type namespace
1031 fn type_ns_kind(def: Def, path_str: &str) -> (&'static str, &'static str, String) {
1032 let (kind, article) = match def {
1033 // we can still have non-tuple structs
1034 Def::Struct(..) => ("struct", "a"),
1035 Def::Enum(..) => ("enum", "an"),
1036 Def::Trait(..) => ("trait", "a"),
1037 Def::Union(..) => ("union", "a"),
1040 (kind, article, format!("{}@{}", kind, path_str))
1043 fn span_of_attrs(attrs: &Attributes) -> syntax_pos::Span {
1044 if attrs.doc_strings.is_empty() {
1047 let start = attrs.doc_strings[0].span();
1048 let end = attrs.doc_strings.last().unwrap().span();
1052 fn ambiguity_error(cx: &DocContext, attrs: &Attributes,
1054 article1: &str, kind1: &str, disambig1: &str,
1055 article2: &str, kind2: &str, disambig2: &str) {
1056 let sp = span_of_attrs(attrs);
1058 .struct_span_warn(sp,
1059 &format!("`{}` is both {} {} and {} {}",
1060 path_str, article1, kind1,
1062 .help(&format!("try `{}` if you want to select the {}, \
1063 or `{}` if you want to \
1065 disambig1, kind1, disambig2,
1070 /// Given an enum variant's def, return the def of its enum and the associated fragment
1071 fn handle_variant(cx: &DocContext, def: Def) -> Result<(Def, Option<String>), ()> {
1072 use rustc::ty::DefIdTree;
1074 let parent = if let Some(parent) = cx.tcx.parent(def.def_id()) {
1079 let parent_def = Def::Enum(parent);
1080 let variant = cx.tcx.expect_variant_def(def);
1081 Ok((parent_def, Some(format!("{}.v", variant.name))))
1084 const PRIMITIVES: &[(&str, Def)] = &[
1085 ("u8", Def::PrimTy(hir::PrimTy::TyUint(syntax::ast::UintTy::U8))),
1086 ("u16", Def::PrimTy(hir::PrimTy::TyUint(syntax::ast::UintTy::U16))),
1087 ("u32", Def::PrimTy(hir::PrimTy::TyUint(syntax::ast::UintTy::U32))),
1088 ("u64", Def::PrimTy(hir::PrimTy::TyUint(syntax::ast::UintTy::U64))),
1089 ("u128", Def::PrimTy(hir::PrimTy::TyUint(syntax::ast::UintTy::U128))),
1090 ("usize", Def::PrimTy(hir::PrimTy::TyUint(syntax::ast::UintTy::Usize))),
1091 ("i8", Def::PrimTy(hir::PrimTy::TyInt(syntax::ast::IntTy::I8))),
1092 ("i16", Def::PrimTy(hir::PrimTy::TyInt(syntax::ast::IntTy::I16))),
1093 ("i32", Def::PrimTy(hir::PrimTy::TyInt(syntax::ast::IntTy::I32))),
1094 ("i64", Def::PrimTy(hir::PrimTy::TyInt(syntax::ast::IntTy::I64))),
1095 ("i128", Def::PrimTy(hir::PrimTy::TyInt(syntax::ast::IntTy::I128))),
1096 ("isize", Def::PrimTy(hir::PrimTy::TyInt(syntax::ast::IntTy::Isize))),
1097 ("f32", Def::PrimTy(hir::PrimTy::TyFloat(syntax::ast::FloatTy::F32))),
1098 ("f64", Def::PrimTy(hir::PrimTy::TyFloat(syntax::ast::FloatTy::F64))),
1099 ("str", Def::PrimTy(hir::PrimTy::TyStr)),
1100 ("bool", Def::PrimTy(hir::PrimTy::TyBool)),
1101 ("char", Def::PrimTy(hir::PrimTy::TyChar)),
1104 fn is_primitive(path_str: &str, is_val: bool) -> Option<Def> {
1108 PRIMITIVES.iter().find(|x| x.0 == path_str).map(|x| x.1)
1112 /// Resolve a given string as a path, along with whether or not it is
1113 /// in the value namespace. Also returns an optional URL fragment in the case
1114 /// of variants and methods
1115 fn resolve(cx: &DocContext, path_str: &str, is_val: bool) -> Result<(Def, Option<String>), ()> {
1116 // In case we're in a module, try to resolve the relative
1118 if let Some(id) = cx.mod_ids.borrow().last() {
1119 let result = cx.resolver.borrow_mut()
1122 resolver.resolve_str_path_error(DUMMY_SP,
1126 if let Ok(result) = result {
1127 // In case this is a trait item, skip the
1128 // early return and try looking for the trait
1129 let value = match result.def {
1130 Def::Method(_) | Def::AssociatedConst(_) => true,
1131 Def::AssociatedTy(_) => false,
1132 Def::Variant(_) => return handle_variant(cx, result.def),
1133 // not a trait item, just return what we found
1134 _ => return Ok((result.def, None))
1137 if value != is_val {
1140 } else if let Some(prim) = is_primitive(path_str, is_val) {
1141 return Ok((prim, Some(path_str.to_owned())))
1143 // If resolution failed, it may still be a method
1144 // because methods are not handled by the resolver
1145 // If so, bail when we're not looking for a value
1151 // Try looking for methods and associated items
1152 let mut split = path_str.rsplitn(2, "::");
1153 let mut item_name = if let Some(first) = split.next() {
1159 let mut path = if let Some(second) = split.next() {
1165 let ty = cx.resolver.borrow_mut()
1168 resolver.resolve_str_path_error(DUMMY_SP, &path, false)
1171 Def::Struct(did) | Def::Union(did) | Def::Enum(did) | Def::TyAlias(did) => {
1172 let item = cx.tcx.inherent_impls(did).iter()
1173 .flat_map(|imp| cx.tcx.associated_items(*imp))
1174 .find(|item| item.ident.name == item_name);
1175 if let Some(item) = item {
1176 let out = match item.kind {
1177 ty::AssociatedKind::Method if is_val => "method",
1178 ty::AssociatedKind::Const if is_val => "associatedconstant",
1181 Ok((ty.def, Some(format!("{}.{}", out, item_name))))
1183 let is_enum = match ty.def {
1184 Def::Enum(_) => true,
1187 let elem = if is_enum {
1188 cx.tcx.adt_def(did).all_fields().find(|item| item.ident.name == item_name)
1194 .find(|item| item.ident.name == item_name)
1196 if let Some(item) = elem {
1198 Some(format!("{}.{}",
1199 if is_enum { "variant" } else { "structfield" },
1206 Def::Trait(did) => {
1207 let item = cx.tcx.associated_item_def_ids(did).iter()
1208 .map(|item| cx.tcx.associated_item(*item))
1209 .find(|item| item.ident.name == item_name);
1210 if let Some(item) = item {
1211 let kind = match item.kind {
1212 ty::AssociatedKind::Const if is_val => "associatedconstant",
1213 ty::AssociatedKind::Type if !is_val => "associatedtype",
1214 ty::AssociatedKind::Method if is_val => {
1215 if item.defaultness.has_value() {
1224 Ok((ty.def, Some(format!("{}.{}", kind, item_name))))
1236 /// Resolve a string as a macro
1237 fn macro_resolve(cx: &DocContext, path_str: &str) -> Option<Def> {
1238 use syntax::ext::base::{MacroKind, SyntaxExtension};
1239 use syntax::ext::hygiene::Mark;
1240 let segment = ast::PathSegment::from_ident(Ident::from_str(path_str));
1241 let path = ast::Path { segments: vec![segment], span: DUMMY_SP };
1242 let mut resolver = cx.resolver.borrow_mut();
1243 let mark = Mark::root();
1245 .resolve_macro_to_def_inner(mark, &path, MacroKind::Bang, false);
1246 if let Ok(def) = res {
1247 if let SyntaxExtension::DeclMacro { .. } = *resolver.get_macro(def) {
1252 } else if let Some(def) = resolver.all_macros.get(&Symbol::intern(path_str)) {
1261 /// can be either value or type, not a macro
1265 /// values, functions, consts, statics, everything in the value namespace
1267 /// types, traits, everything in the type namespace
1271 fn resolution_failure(
1276 link_range: Option<Range<usize>>,
1278 let sp = span_of_attrs(attrs);
1279 let msg = format!("`[{}]` cannot be resolved, ignoring it...", path_str);
1281 let code_dox = sp.to_src(cx);
1283 let doc_comment_padding = 3;
1284 let mut diag = if let Some(link_range) = link_range {
1285 // blah blah blah\nblah\nblah [blah] blah blah\nblah blah
1288 // last_new_line_offset
1291 if dox.lines().count() == code_dox.lines().count() {
1292 let line_offset = dox[..link_range.start].lines().count();
1293 // The span starts in the `///`, so we don't have to account for the leading whitespace
1294 let code_dox_len = if line_offset <= 1 {
1298 doc_comment_padding +
1299 // Each subsequent leading whitespace and `///`
1300 code_dox.lines().skip(1).take(line_offset - 1).fold(0, |sum, line| {
1301 sum + doc_comment_padding + line.len() - line.trim().len()
1305 // Extract the specific span
1306 let sp = sp.from_inner_byte_pos(
1307 link_range.start + code_dox_len,
1308 link_range.end + code_dox_len,
1311 diag = cx.tcx.struct_span_lint_node(lint::builtin::INTRA_DOC_LINK_RESOLUTION_FAILURE,
1315 diag.span_label(sp, "cannot be resolved, ignoring");
1317 diag = cx.tcx.struct_span_lint_node(lint::builtin::INTRA_DOC_LINK_RESOLUTION_FAILURE,
1322 let last_new_line_offset = dox[..link_range.start].rfind('\n').map_or(0, |n| n + 1);
1323 let line = dox[last_new_line_offset..].lines().next().unwrap_or("");
1325 // Print the line containing the `link_range` and manually mark it with '^'s
1327 "the link appears in this line:\n\n{line}\n\
1328 {indicator: <before$}{indicator:^<found$}",
1331 before=link_range.start - last_new_line_offset,
1332 found=link_range.len(),
1337 cx.tcx.struct_span_lint_node(lint::builtin::INTRA_DOC_LINK_RESOLUTION_FAILURE,
1342 diag.help("to escape `[` and `]` characters, just add '\\' before them like \
1347 impl Clean<Attributes> for [ast::Attribute] {
1348 fn clean(&self, cx: &DocContext) -> Attributes {
1349 let mut attrs = Attributes::from_ast(cx.sess().diagnostic(), self);
1351 if UnstableFeatures::from_environment().is_nightly_build() {
1352 let dox = attrs.collapsed_doc_value().unwrap_or_else(String::new);
1353 for (ori_link, link_range) in markdown_links(&dox) {
1354 // bail early for real links
1355 if ori_link.contains('/') {
1358 let link = ori_link.replace("`", "");
1359 let (def, fragment) = {
1360 let mut kind = PathKind::Unknown;
1361 let path_str = if let Some(prefix) =
1362 ["struct@", "enum@", "type@",
1363 "trait@", "union@"].iter()
1364 .find(|p| link.starts_with(**p)) {
1365 kind = PathKind::Type;
1366 link.trim_left_matches(prefix)
1367 } else if let Some(prefix) =
1368 ["const@", "static@",
1369 "value@", "function@", "mod@",
1370 "fn@", "module@", "method@"]
1371 .iter().find(|p| link.starts_with(**p)) {
1372 kind = PathKind::Value;
1373 link.trim_left_matches(prefix)
1374 } else if link.ends_with("()") {
1375 kind = PathKind::Value;
1376 link.trim_right_matches("()")
1377 } else if link.starts_with("macro@") {
1378 kind = PathKind::Macro;
1379 link.trim_left_matches("macro@")
1380 } else if link.ends_with('!') {
1381 kind = PathKind::Macro;
1382 link.trim_right_matches('!')
1387 if path_str.contains(|ch: char| !(ch.is_alphanumeric() ||
1388 ch == ':' || ch == '_')) {
1393 PathKind::Value => {
1394 if let Ok(def) = resolve(cx, path_str, true) {
1397 resolution_failure(cx, &attrs, path_str, &dox, link_range);
1398 // this could just be a normal link or a broken link
1399 // we could potentially check if something is
1400 // "intra-doc-link-like" and warn in that case
1405 if let Ok(def) = resolve(cx, path_str, false) {
1408 resolution_failure(cx, &attrs, path_str, &dox, link_range);
1409 // this could just be a normal link
1413 PathKind::Unknown => {
1415 if let Some(macro_def) = macro_resolve(cx, path_str) {
1416 if let Ok(type_def) = resolve(cx, path_str, false) {
1417 let (type_kind, article, type_disambig)
1418 = type_ns_kind(type_def.0, path_str);
1419 ambiguity_error(cx, &attrs, path_str,
1420 article, type_kind, &type_disambig,
1421 "a", "macro", &format!("macro@{}", path_str));
1423 } else if let Ok(value_def) = resolve(cx, path_str, true) {
1424 let (value_kind, value_disambig)
1425 = value_ns_kind(value_def.0, path_str)
1426 .expect("struct and mod cases should have been \
1427 caught in previous branch");
1428 ambiguity_error(cx, &attrs, path_str,
1429 "a", value_kind, &value_disambig,
1430 "a", "macro", &format!("macro@{}", path_str));
1433 } else if let Ok(type_def) = resolve(cx, path_str, false) {
1434 // It is imperative we search for not-a-value first
1435 // Otherwise we will find struct ctors for when we are looking
1436 // for structs, and the link won't work.
1437 // if there is something in both namespaces
1438 if let Ok(value_def) = resolve(cx, path_str, true) {
1439 let kind = value_ns_kind(value_def.0, path_str);
1440 if let Some((value_kind, value_disambig)) = kind {
1441 let (type_kind, article, type_disambig)
1442 = type_ns_kind(type_def.0, path_str);
1443 ambiguity_error(cx, &attrs, path_str,
1444 article, type_kind, &type_disambig,
1445 "a", value_kind, &value_disambig);
1450 } else if let Ok(value_def) = resolve(cx, path_str, true) {
1453 resolution_failure(cx, &attrs, path_str, &dox, link_range);
1454 // this could just be a normal link
1458 PathKind::Macro => {
1459 if let Some(def) = macro_resolve(cx, path_str) {
1462 resolution_failure(cx, &attrs, path_str, &dox, link_range);
1469 if let Def::PrimTy(_) = def {
1470 attrs.links.push((ori_link, None, fragment));
1472 let id = register_def(cx, def);
1473 attrs.links.push((ori_link, Some(id), fragment));
1477 cx.sess().abort_if_errors();
1484 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1485 pub enum GenericBound {
1486 TraitBound(PolyTrait, hir::TraitBoundModifier),
1491 fn maybe_sized(cx: &DocContext) -> GenericBound {
1492 let did = cx.tcx.require_lang_item(lang_items::SizedTraitLangItem);
1493 let empty = cx.tcx.intern_substs(&[]);
1494 let path = external_path(cx, &cx.tcx.item_name(did).as_str(),
1495 Some(did), false, vec![], empty);
1496 inline::record_extern_fqn(cx, did, TypeKind::Trait);
1497 GenericBound::TraitBound(PolyTrait {
1498 trait_: ResolvedPath {
1504 generic_params: Vec::new(),
1505 }, hir::TraitBoundModifier::Maybe)
1508 fn is_sized_bound(&self, cx: &DocContext) -> bool {
1509 use rustc::hir::TraitBoundModifier as TBM;
1510 if let GenericBound::TraitBound(PolyTrait { ref trait_, .. }, TBM::None) = *self {
1511 if trait_.def_id() == cx.tcx.lang_items().sized_trait() {
1518 fn get_poly_trait(&self) -> Option<PolyTrait> {
1519 if let GenericBound::TraitBound(ref p, _) = *self {
1520 return Some(p.clone())
1525 fn get_trait_type(&self) -> Option<Type> {
1527 if let GenericBound::TraitBound(PolyTrait { ref trait_, .. }, _) = *self {
1528 return Some(trait_.clone());
1534 impl Clean<GenericBound> for hir::GenericBound {
1535 fn clean(&self, cx: &DocContext) -> GenericBound {
1537 hir::GenericBound::Outlives(lt) => GenericBound::Outlives(lt.clean(cx)),
1538 hir::GenericBound::Trait(ref t, modifier) => {
1539 GenericBound::TraitBound(t.clean(cx), modifier)
1545 fn external_generic_args(cx: &DocContext, trait_did: Option<DefId>, has_self: bool,
1546 bindings: Vec<TypeBinding>, substs: &Substs) -> GenericArgs {
1547 let lifetimes = substs.regions().filter_map(|v| v.clean(cx)).collect();
1548 let types = substs.types().skip(has_self as usize).collect::<Vec<_>>();
1551 // Attempt to sugar an external path like Fn<(A, B,), C> to Fn(A, B) -> C
1552 Some(did) if cx.tcx.lang_items().fn_trait_kind(did).is_some() => {
1553 assert_eq!(types.len(), 1);
1554 let inputs = match types[0].sty {
1555 ty::TyTuple(ref tys) => tys.iter().map(|t| t.clean(cx)).collect(),
1557 return GenericArgs::AngleBracketed {
1559 types: types.clean(cx),
1565 // FIXME(#20299) return type comes from a projection now
1566 // match types[1].sty {
1567 // ty::TyTuple(ref v) if v.is_empty() => None, // -> ()
1568 // _ => Some(types[1].clean(cx))
1570 GenericArgs::Parenthesized {
1576 GenericArgs::AngleBracketed {
1578 types: types.clean(cx),
1585 // trait_did should be set to a trait's DefId if called on a TraitRef, in order to sugar
1586 // from Fn<(A, B,), C> to Fn(A, B) -> C
1587 fn external_path(cx: &DocContext, name: &str, trait_did: Option<DefId>, has_self: bool,
1588 bindings: Vec<TypeBinding>, substs: &Substs) -> Path {
1592 segments: vec![PathSegment {
1593 name: name.to_string(),
1594 args: external_generic_args(cx, trait_did, has_self, bindings, substs)
1599 impl<'a, 'tcx> Clean<GenericBound> for (&'a ty::TraitRef<'tcx>, Vec<TypeBinding>) {
1600 fn clean(&self, cx: &DocContext) -> GenericBound {
1601 let (trait_ref, ref bounds) = *self;
1602 inline::record_extern_fqn(cx, trait_ref.def_id, TypeKind::Trait);
1603 let path = external_path(cx, &cx.tcx.item_name(trait_ref.def_id).as_str(),
1604 Some(trait_ref.def_id), true, bounds.clone(), trait_ref.substs);
1606 debug!("ty::TraitRef\n subst: {:?}\n", trait_ref.substs);
1608 // collect any late bound regions
1609 let mut late_bounds = vec![];
1610 for ty_s in trait_ref.input_types().skip(1) {
1611 if let ty::TyTuple(ts) = ty_s.sty {
1613 if let ty::TyRef(ref reg, _, _) = ty_s.sty {
1614 if let &ty::RegionKind::ReLateBound(..) = *reg {
1615 debug!(" hit an ReLateBound {:?}", reg);
1616 if let Some(Lifetime(name)) = reg.clean(cx) {
1617 late_bounds.push(GenericParamDef {
1619 kind: GenericParamDefKind::Lifetime,
1628 GenericBound::TraitBound(
1630 trait_: ResolvedPath {
1633 did: trait_ref.def_id,
1636 generic_params: late_bounds,
1638 hir::TraitBoundModifier::None
1643 impl<'tcx> Clean<GenericBound> for ty::TraitRef<'tcx> {
1644 fn clean(&self, cx: &DocContext) -> GenericBound {
1645 (self, vec![]).clean(cx)
1649 impl<'tcx> Clean<Option<Vec<GenericBound>>> for Substs<'tcx> {
1650 fn clean(&self, cx: &DocContext) -> Option<Vec<GenericBound>> {
1651 let mut v = Vec::new();
1652 v.extend(self.regions().filter_map(|r| r.clean(cx)).map(GenericBound::Outlives));
1653 v.extend(self.types().map(|t| GenericBound::TraitBound(PolyTrait {
1654 trait_: t.clean(cx),
1655 generic_params: Vec::new(),
1656 }, hir::TraitBoundModifier::None)));
1657 if !v.is_empty() {Some(v)} else {None}
1661 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1662 pub struct Lifetime(String);
1665 pub fn get_ref<'a>(&'a self) -> &'a str {
1666 let Lifetime(ref s) = *self;
1671 pub fn statik() -> Lifetime {
1672 Lifetime("'static".to_string())
1676 impl Clean<Lifetime> for hir::Lifetime {
1677 fn clean(&self, cx: &DocContext) -> Lifetime {
1678 if self.id != ast::DUMMY_NODE_ID {
1679 let hir_id = cx.tcx.hir.node_to_hir_id(self.id);
1680 let def = cx.tcx.named_region(hir_id);
1682 Some(rl::Region::EarlyBound(_, node_id, _)) |
1683 Some(rl::Region::LateBound(_, node_id, _)) |
1684 Some(rl::Region::Free(_, node_id)) => {
1685 if let Some(lt) = cx.lt_substs.borrow().get(&node_id).cloned() {
1692 Lifetime(self.name.ident().to_string())
1696 impl Clean<Lifetime> for hir::GenericParam {
1697 fn clean(&self, _: &DocContext) -> Lifetime {
1699 hir::GenericParamKind::Lifetime { .. } => {
1700 if self.bounds.len() > 0 {
1701 let mut bounds = self.bounds.iter().map(|bound| match bound {
1702 hir::GenericBound::Outlives(lt) => lt,
1705 let name = bounds.next().unwrap().name.ident();
1706 let mut s = format!("{}: {}", self.name.ident(), name);
1707 for bound in bounds {
1708 s.push_str(&format!(" + {}", bound.name.ident()));
1712 Lifetime(self.name.ident().to_string())
1720 impl<'tcx> Clean<Lifetime> for ty::GenericParamDef {
1721 fn clean(&self, _cx: &DocContext) -> Lifetime {
1722 Lifetime(self.name.to_string())
1726 impl Clean<Option<Lifetime>> for ty::RegionKind {
1727 fn clean(&self, cx: &DocContext) -> Option<Lifetime> {
1729 ty::ReStatic => Some(Lifetime::statik()),
1730 ty::ReLateBound(_, ty::BrNamed(_, name)) => Some(Lifetime(name.to_string())),
1731 ty::ReEarlyBound(ref data) => Some(Lifetime(data.name.clean(cx))),
1733 ty::ReLateBound(..) |
1737 ty::ReSkolemized(..) |
1739 ty::ReClosureBound(_) |
1740 ty::ReCanonical(_) |
1741 ty::ReErased => None
1746 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1747 pub enum WherePredicate {
1748 BoundPredicate { ty: Type, bounds: Vec<GenericBound> },
1749 RegionPredicate { lifetime: Lifetime, bounds: Vec<GenericBound> },
1750 EqPredicate { lhs: Type, rhs: Type },
1753 impl Clean<WherePredicate> for hir::WherePredicate {
1754 fn clean(&self, cx: &DocContext) -> WherePredicate {
1756 hir::WherePredicate::BoundPredicate(ref wbp) => {
1757 WherePredicate::BoundPredicate {
1758 ty: wbp.bounded_ty.clean(cx),
1759 bounds: wbp.bounds.clean(cx)
1763 hir::WherePredicate::RegionPredicate(ref wrp) => {
1764 WherePredicate::RegionPredicate {
1765 lifetime: wrp.lifetime.clean(cx),
1766 bounds: wrp.bounds.clean(cx)
1770 hir::WherePredicate::EqPredicate(ref wrp) => {
1771 WherePredicate::EqPredicate {
1772 lhs: wrp.lhs_ty.clean(cx),
1773 rhs: wrp.rhs_ty.clean(cx)
1780 impl<'a> Clean<WherePredicate> for ty::Predicate<'a> {
1781 fn clean(&self, cx: &DocContext) -> WherePredicate {
1782 use rustc::ty::Predicate;
1785 Predicate::Trait(ref pred) => pred.clean(cx),
1786 Predicate::Subtype(ref pred) => pred.clean(cx),
1787 Predicate::RegionOutlives(ref pred) => pred.clean(cx),
1788 Predicate::TypeOutlives(ref pred) => pred.clean(cx),
1789 Predicate::Projection(ref pred) => pred.clean(cx),
1790 Predicate::WellFormed(_) => panic!("not user writable"),
1791 Predicate::ObjectSafe(_) => panic!("not user writable"),
1792 Predicate::ClosureKind(..) => panic!("not user writable"),
1793 Predicate::ConstEvaluatable(..) => panic!("not user writable"),
1798 impl<'a> Clean<WherePredicate> for ty::TraitPredicate<'a> {
1799 fn clean(&self, cx: &DocContext) -> WherePredicate {
1800 WherePredicate::BoundPredicate {
1801 ty: self.trait_ref.self_ty().clean(cx),
1802 bounds: vec![self.trait_ref.clean(cx)]
1807 impl<'tcx> Clean<WherePredicate> for ty::SubtypePredicate<'tcx> {
1808 fn clean(&self, _cx: &DocContext) -> WherePredicate {
1809 panic!("subtype predicates are an internal rustc artifact \
1810 and should not be seen by rustdoc")
1814 impl<'tcx> Clean<WherePredicate> for ty::OutlivesPredicate<ty::Region<'tcx>, ty::Region<'tcx>> {
1815 fn clean(&self, cx: &DocContext) -> WherePredicate {
1816 let ty::OutlivesPredicate(ref a, ref b) = *self;
1817 WherePredicate::RegionPredicate {
1818 lifetime: a.clean(cx).unwrap(),
1819 bounds: vec![GenericBound::Outlives(b.clean(cx).unwrap())]
1824 impl<'tcx> Clean<WherePredicate> for ty::OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>> {
1825 fn clean(&self, cx: &DocContext) -> WherePredicate {
1826 let ty::OutlivesPredicate(ref ty, ref lt) = *self;
1828 WherePredicate::BoundPredicate {
1830 bounds: vec![GenericBound::Outlives(lt.clean(cx).unwrap())]
1835 impl<'tcx> Clean<WherePredicate> for ty::ProjectionPredicate<'tcx> {
1836 fn clean(&self, cx: &DocContext) -> WherePredicate {
1837 WherePredicate::EqPredicate {
1838 lhs: self.projection_ty.clean(cx),
1839 rhs: self.ty.clean(cx)
1844 impl<'tcx> Clean<Type> for ty::ProjectionTy<'tcx> {
1845 fn clean(&self, cx: &DocContext) -> Type {
1846 let trait_ = match self.trait_ref(cx.tcx).clean(cx) {
1847 GenericBound::TraitBound(t, _) => t.trait_,
1848 GenericBound::Outlives(_) => panic!("cleaning a trait got a lifetime"),
1851 name: cx.tcx.associated_item(self.item_def_id).ident.name.clean(cx),
1852 self_type: box self.self_ty().clean(cx),
1858 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug, Hash)]
1859 pub enum GenericParamDefKind {
1863 bounds: Vec<GenericBound>,
1864 default: Option<Type>,
1865 synthetic: Option<hir::SyntheticTyParamKind>,
1869 impl Eq for GenericParamDefKind {}
1871 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1872 pub struct GenericParamDef {
1875 pub kind: GenericParamDefKind,
1878 impl GenericParamDef {
1879 pub fn is_synthetic_type_param(&self) -> bool {
1881 GenericParamDefKind::Lifetime => false,
1882 GenericParamDefKind::Type { ref synthetic, .. } => synthetic.is_some(),
1887 impl<'tcx> Clean<GenericParamDef> for ty::GenericParamDef {
1888 fn clean(&self, cx: &DocContext) -> GenericParamDef {
1889 let (name, kind) = match self.kind {
1890 ty::GenericParamDefKind::Lifetime => {
1891 (self.name.to_string(), GenericParamDefKind::Lifetime)
1893 ty::GenericParamDefKind::Type { has_default, .. } => {
1894 cx.renderinfo.borrow_mut().external_typarams
1895 .insert(self.def_id, self.name.clean(cx));
1896 let default = if has_default {
1897 Some(cx.tcx.type_of(self.def_id).clean(cx))
1901 (self.name.clean(cx), GenericParamDefKind::Type {
1903 bounds: vec![], // These are filled in from the where-clauses.
1917 impl Clean<GenericParamDef> for hir::GenericParam {
1918 fn clean(&self, cx: &DocContext) -> GenericParamDef {
1919 let (name, kind) = match self.kind {
1920 hir::GenericParamKind::Lifetime { .. } => {
1921 let name = if self.bounds.len() > 0 {
1922 let mut bounds = self.bounds.iter().map(|bound| match bound {
1923 hir::GenericBound::Outlives(lt) => lt,
1926 let name = bounds.next().unwrap().name.ident();
1927 let mut s = format!("{}: {}", self.name.ident(), name);
1928 for bound in bounds {
1929 s.push_str(&format!(" + {}", bound.name.ident()));
1933 self.name.ident().to_string()
1935 (name, GenericParamDefKind::Lifetime)
1937 hir::GenericParamKind::Type { ref default, synthetic, .. } => {
1938 (self.name.ident().name.clean(cx), GenericParamDefKind::Type {
1939 did: cx.tcx.hir.local_def_id(self.id),
1940 bounds: self.bounds.clean(cx),
1941 default: default.clean(cx),
1942 synthetic: synthetic,
1954 // maybe use a Generic enum and use Vec<Generic>?
1955 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Default, Hash)]
1956 pub struct Generics {
1957 pub params: Vec<GenericParamDef>,
1958 pub where_predicates: Vec<WherePredicate>,
1961 impl Clean<Generics> for hir::Generics {
1962 fn clean(&self, cx: &DocContext) -> Generics {
1963 // Synthetic type-parameters are inserted after normal ones.
1964 // In order for normal parameters to be able to refer to synthetic ones,
1965 // scans them first.
1966 fn is_impl_trait(param: &hir::GenericParam) -> bool {
1968 hir::GenericParamKind::Type { synthetic, .. } => {
1969 synthetic == Some(hir::SyntheticTyParamKind::ImplTrait)
1974 let impl_trait_params = self.params
1976 .filter(|param| is_impl_trait(param))
1978 let param: GenericParamDef = param.clean(cx);
1980 GenericParamDefKind::Lifetime => unreachable!(),
1981 GenericParamDefKind::Type { did, ref bounds, .. } => {
1982 cx.impl_trait_bounds.borrow_mut().insert(did, bounds.clone());
1987 .collect::<Vec<_>>();
1989 let mut params = Vec::with_capacity(self.params.len());
1990 for p in self.params.iter().filter(|p| !is_impl_trait(p)) {
1991 let p = p.clean(cx);
1994 params.extend(impl_trait_params);
1996 let mut generics = Generics {
1998 where_predicates: self.where_clause.predicates.clean(cx),
2001 // Some duplicates are generated for ?Sized bounds between type params and where
2002 // predicates. The point in here is to move the bounds definitions from type params
2003 // to where predicates when such cases occur.
2004 for where_pred in &mut generics.where_predicates {
2006 WherePredicate::BoundPredicate { ty: Generic(ref name), ref mut bounds } => {
2007 if bounds.is_empty() {
2008 for param in &mut generics.params {
2010 GenericParamDefKind::Lifetime => {}
2011 GenericParamDefKind::Type { bounds: ref mut ty_bounds, .. } => {
2012 if ¶m.name == name {
2013 mem::swap(bounds, ty_bounds);
2028 impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics,
2029 &'a ty::GenericPredicates<'tcx>) {
2030 fn clean(&self, cx: &DocContext) -> Generics {
2031 use self::WherePredicate as WP;
2033 let (gens, preds) = *self;
2035 // Bounds in the type_params and lifetimes fields are repeated in the
2036 // predicates field (see rustc_typeck::collect::ty_generics), so remove
2038 let stripped_typarams = gens.params.iter().filter_map(|param| match param.kind {
2039 ty::GenericParamDefKind::Lifetime => None,
2040 ty::GenericParamDefKind::Type { .. } => {
2041 if param.name == keywords::SelfType.name().as_str() {
2042 assert_eq!(param.index, 0);
2045 Some(param.clean(cx))
2047 }).collect::<Vec<GenericParamDef>>();
2049 let mut where_predicates = preds.predicates.to_vec().clean(cx);
2051 // Type parameters and have a Sized bound by default unless removed with
2052 // ?Sized. Scan through the predicates and mark any type parameter with
2053 // a Sized bound, removing the bounds as we find them.
2055 // Note that associated types also have a sized bound by default, but we
2056 // don't actually know the set of associated types right here so that's
2057 // handled in cleaning associated types
2058 let mut sized_params = FxHashSet();
2059 where_predicates.retain(|pred| {
2061 WP::BoundPredicate { ty: Generic(ref g), ref bounds } => {
2062 if bounds.iter().any(|b| b.is_sized_bound(cx)) {
2063 sized_params.insert(g.clone());
2073 // Run through the type parameters again and insert a ?Sized
2074 // unbound for any we didn't find to be Sized.
2075 for tp in &stripped_typarams {
2076 if !sized_params.contains(&tp.name) {
2077 where_predicates.push(WP::BoundPredicate {
2078 ty: Type::Generic(tp.name.clone()),
2079 bounds: vec![GenericBound::maybe_sized(cx)],
2084 // It would be nice to collect all of the bounds on a type and recombine
2085 // them if possible, to avoid e.g. `where T: Foo, T: Bar, T: Sized, T: 'a`
2086 // and instead see `where T: Foo + Bar + Sized + 'a`
2091 .flat_map(|param| match param.kind {
2092 ty::GenericParamDefKind::Lifetime => Some(param.clean(cx)),
2093 ty::GenericParamDefKind::Type { .. } => None,
2094 }).chain(simplify::ty_params(stripped_typarams).into_iter())
2096 where_predicates: simplify::where_clauses(cx, where_predicates),
2101 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2103 pub generics: Generics,
2105 pub header: hir::FnHeader,
2108 impl<'a> Clean<Method> for (&'a hir::MethodSig, &'a hir::Generics, hir::BodyId) {
2109 fn clean(&self, cx: &DocContext) -> Method {
2110 let (generics, decl) = enter_impl_trait(cx, || {
2111 (self.1.clean(cx), (&*self.0.decl, self.2).clean(cx))
2116 header: self.0.header,
2121 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2122 pub struct TyMethod {
2123 pub header: hir::FnHeader,
2125 pub generics: Generics,
2128 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2129 pub struct Function {
2131 pub generics: Generics,
2132 pub header: hir::FnHeader,
2135 impl Clean<Item> for doctree::Function {
2136 fn clean(&self, cx: &DocContext) -> Item {
2137 let (generics, decl) = enter_impl_trait(cx, || {
2138 (self.generics.clean(cx), (&self.decl, self.body).clean(cx))
2141 name: Some(self.name.clean(cx)),
2142 attrs: self.attrs.clean(cx),
2143 source: self.whence.clean(cx),
2144 visibility: self.vis.clean(cx),
2145 stability: self.stab.clean(cx),
2146 deprecation: self.depr.clean(cx),
2147 def_id: cx.tcx.hir.local_def_id(self.id),
2148 inner: FunctionItem(Function {
2151 header: self.header,
2157 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
2159 pub inputs: Arguments,
2160 pub output: FunctionRetTy,
2162 pub attrs: Attributes,
2166 pub fn has_self(&self) -> bool {
2167 self.inputs.values.len() > 0 && self.inputs.values[0].name == "self"
2170 pub fn self_type(&self) -> Option<SelfTy> {
2171 self.inputs.values.get(0).and_then(|v| v.to_self())
2175 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
2176 pub struct Arguments {
2177 pub values: Vec<Argument>,
2180 impl<'a> Clean<Arguments> for (&'a [hir::Ty], &'a [ast::Ident]) {
2181 fn clean(&self, cx: &DocContext) -> Arguments {
2183 values: self.0.iter().enumerate().map(|(i, ty)| {
2184 let mut name = self.1.get(i).map(|ident| ident.to_string())
2185 .unwrap_or(String::new());
2186 if name.is_empty() {
2187 name = "_".to_string();
2191 type_: ty.clean(cx),
2198 impl<'a> Clean<Arguments> for (&'a [hir::Ty], hir::BodyId) {
2199 fn clean(&self, cx: &DocContext) -> Arguments {
2200 let body = cx.tcx.hir.body(self.1);
2203 values: self.0.iter().enumerate().map(|(i, ty)| {
2205 name: name_from_pat(&body.arguments[i].pat),
2206 type_: ty.clean(cx),
2213 impl<'a, A: Copy> Clean<FnDecl> for (&'a hir::FnDecl, A)
2214 where (&'a [hir::Ty], A): Clean<Arguments>
2216 fn clean(&self, cx: &DocContext) -> FnDecl {
2218 inputs: (&self.0.inputs[..], self.1).clean(cx),
2219 output: self.0.output.clean(cx),
2220 variadic: self.0.variadic,
2221 attrs: Attributes::default()
2226 impl<'a, 'tcx> Clean<FnDecl> for (DefId, ty::PolyFnSig<'tcx>) {
2227 fn clean(&self, cx: &DocContext) -> FnDecl {
2228 let (did, sig) = *self;
2229 let mut names = if cx.tcx.hir.as_local_node_id(did).is_some() {
2232 cx.tcx.fn_arg_names(did).into_iter()
2236 output: Return(sig.skip_binder().output().clean(cx)),
2237 attrs: Attributes::default(),
2238 variadic: sig.skip_binder().variadic,
2240 values: sig.skip_binder().inputs().iter().map(|t| {
2243 name: names.next().map_or("".to_string(), |name| name.to_string()),
2251 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
2252 pub struct Argument {
2257 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug)]
2260 SelfBorrowed(Option<Lifetime>, Mutability),
2265 pub fn to_self(&self) -> Option<SelfTy> {
2266 if self.name != "self" {
2269 if self.type_.is_self_type() {
2270 return Some(SelfValue);
2273 BorrowedRef{ref lifetime, mutability, ref type_} if type_.is_self_type() => {
2274 Some(SelfBorrowed(lifetime.clone(), mutability))
2276 _ => Some(SelfExplicit(self.type_.clone()))
2281 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
2282 pub enum FunctionRetTy {
2287 impl Clean<FunctionRetTy> for hir::FunctionRetTy {
2288 fn clean(&self, cx: &DocContext) -> FunctionRetTy {
2290 hir::Return(ref typ) => Return(typ.clean(cx)),
2291 hir::DefaultReturn(..) => DefaultReturn,
2296 impl GetDefId for FunctionRetTy {
2297 fn def_id(&self) -> Option<DefId> {
2299 Return(ref ty) => ty.def_id(),
2300 DefaultReturn => None,
2305 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2308 pub unsafety: hir::Unsafety,
2309 pub items: Vec<Item>,
2310 pub generics: Generics,
2311 pub bounds: Vec<GenericBound>,
2312 pub is_spotlight: bool,
2316 impl Clean<Item> for doctree::Trait {
2317 fn clean(&self, cx: &DocContext) -> Item {
2318 let attrs = self.attrs.clean(cx);
2319 let is_spotlight = attrs.has_doc_flag("spotlight");
2321 name: Some(self.name.clean(cx)),
2323 source: self.whence.clean(cx),
2324 def_id: cx.tcx.hir.local_def_id(self.id),
2325 visibility: self.vis.clean(cx),
2326 stability: self.stab.clean(cx),
2327 deprecation: self.depr.clean(cx),
2328 inner: TraitItem(Trait {
2329 auto: self.is_auto.clean(cx),
2330 unsafety: self.unsafety,
2331 items: self.items.clean(cx),
2332 generics: self.generics.clean(cx),
2333 bounds: self.bounds.clean(cx),
2334 is_spotlight: is_spotlight,
2335 is_auto: self.is_auto.clean(cx),
2341 impl Clean<bool> for hir::IsAuto {
2342 fn clean(&self, _: &DocContext) -> bool {
2344 hir::IsAuto::Yes => true,
2345 hir::IsAuto::No => false,
2350 impl Clean<Type> for hir::TraitRef {
2351 fn clean(&self, cx: &DocContext) -> Type {
2352 resolve_type(cx, self.path.clean(cx), self.ref_id)
2356 impl Clean<PolyTrait> for hir::PolyTraitRef {
2357 fn clean(&self, cx: &DocContext) -> PolyTrait {
2359 trait_: self.trait_ref.clean(cx),
2360 generic_params: self.bound_generic_params.clean(cx)
2365 impl Clean<Item> for hir::TraitItem {
2366 fn clean(&self, cx: &DocContext) -> Item {
2367 let inner = match self.node {
2368 hir::TraitItemKind::Const(ref ty, default) => {
2369 AssociatedConstItem(ty.clean(cx),
2370 default.map(|e| print_const_expr(cx, e)))
2372 hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Provided(body)) => {
2373 MethodItem((sig, &self.generics, body).clean(cx))
2375 hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Required(ref names)) => {
2376 let (generics, decl) = enter_impl_trait(cx, || {
2377 (self.generics.clean(cx), (&*sig.decl, &names[..]).clean(cx))
2379 TyMethodItem(TyMethod {
2385 hir::TraitItemKind::Type(ref bounds, ref default) => {
2386 AssociatedTypeItem(bounds.clean(cx), default.clean(cx))
2390 name: Some(self.ident.name.clean(cx)),
2391 attrs: self.attrs.clean(cx),
2392 source: self.span.clean(cx),
2393 def_id: cx.tcx.hir.local_def_id(self.id),
2395 stability: get_stability(cx, cx.tcx.hir.local_def_id(self.id)),
2396 deprecation: get_deprecation(cx, cx.tcx.hir.local_def_id(self.id)),
2402 impl Clean<Item> for hir::ImplItem {
2403 fn clean(&self, cx: &DocContext) -> Item {
2404 let inner = match self.node {
2405 hir::ImplItemKind::Const(ref ty, expr) => {
2406 AssociatedConstItem(ty.clean(cx),
2407 Some(print_const_expr(cx, expr)))
2409 hir::ImplItemKind::Method(ref sig, body) => {
2410 MethodItem((sig, &self.generics, body).clean(cx))
2412 hir::ImplItemKind::Type(ref ty) => TypedefItem(Typedef {
2413 type_: ty.clean(cx),
2414 generics: Generics::default(),
2418 name: Some(self.ident.name.clean(cx)),
2419 source: self.span.clean(cx),
2420 attrs: self.attrs.clean(cx),
2421 def_id: cx.tcx.hir.local_def_id(self.id),
2422 visibility: self.vis.clean(cx),
2423 stability: get_stability(cx, cx.tcx.hir.local_def_id(self.id)),
2424 deprecation: get_deprecation(cx, cx.tcx.hir.local_def_id(self.id)),
2430 impl<'tcx> Clean<Item> for ty::AssociatedItem {
2431 fn clean(&self, cx: &DocContext) -> Item {
2432 let inner = match self.kind {
2433 ty::AssociatedKind::Const => {
2434 let ty = cx.tcx.type_of(self.def_id);
2435 let default = if self.defaultness.has_value() {
2436 Some(inline::print_inlined_const(cx, self.def_id))
2440 AssociatedConstItem(ty.clean(cx), default)
2442 ty::AssociatedKind::Method => {
2443 let generics = (cx.tcx.generics_of(self.def_id),
2444 &cx.tcx.predicates_of(self.def_id)).clean(cx);
2445 let sig = cx.tcx.fn_sig(self.def_id);
2446 let mut decl = (self.def_id, sig).clean(cx);
2448 if self.method_has_self_argument {
2449 let self_ty = match self.container {
2450 ty::ImplContainer(def_id) => {
2451 cx.tcx.type_of(def_id)
2453 ty::TraitContainer(_) => cx.tcx.mk_self_type()
2455 let self_arg_ty = *sig.input(0).skip_binder();
2456 if self_arg_ty == self_ty {
2457 decl.inputs.values[0].type_ = Generic(String::from("Self"));
2458 } else if let ty::TyRef(_, ty, _) = self_arg_ty.sty {
2460 match decl.inputs.values[0].type_ {
2461 BorrowedRef{ref mut type_, ..} => {
2462 **type_ = Generic(String::from("Self"))
2464 _ => unreachable!(),
2470 let provided = match self.container {
2471 ty::ImplContainer(_) => true,
2472 ty::TraitContainer(_) => self.defaultness.has_value()
2475 let constness = if cx.tcx.is_const_fn(self.def_id) {
2476 hir::Constness::Const
2478 hir::Constness::NotConst
2483 header: hir::FnHeader {
2484 unsafety: sig.unsafety(),
2487 asyncness: hir::IsAsync::NotAsync,
2491 TyMethodItem(TyMethod {
2494 header: hir::FnHeader {
2495 unsafety: sig.unsafety(),
2497 constness: hir::Constness::NotConst,
2498 asyncness: hir::IsAsync::NotAsync,
2503 ty::AssociatedKind::Type => {
2504 let my_name = self.ident.name.clean(cx);
2506 if let ty::TraitContainer(did) = self.container {
2507 // When loading a cross-crate associated type, the bounds for this type
2508 // are actually located on the trait/impl itself, so we need to load
2509 // all of the generics from there and then look for bounds that are
2510 // applied to this associated type in question.
2511 let predicates = cx.tcx.predicates_of(did);
2512 let generics = (cx.tcx.generics_of(did), &predicates).clean(cx);
2513 let mut bounds = generics.where_predicates.iter().filter_map(|pred| {
2514 let (name, self_type, trait_, bounds) = match *pred {
2515 WherePredicate::BoundPredicate {
2516 ty: QPath { ref name, ref self_type, ref trait_ },
2518 } => (name, self_type, trait_, bounds),
2521 if *name != my_name { return None }
2523 ResolvedPath { did, .. } if did == self.container.id() => {}
2527 Generic(ref s) if *s == "Self" => {}
2531 }).flat_map(|i| i.iter().cloned()).collect::<Vec<_>>();
2532 // Our Sized/?Sized bound didn't get handled when creating the generics
2533 // because we didn't actually get our whole set of bounds until just now
2534 // (some of them may have come from the trait). If we do have a sized
2535 // bound, we remove it, and if we don't then we add the `?Sized` bound
2537 match bounds.iter().position(|b| b.is_sized_bound(cx)) {
2538 Some(i) => { bounds.remove(i); }
2539 None => bounds.push(GenericBound::maybe_sized(cx)),
2542 let ty = if self.defaultness.has_value() {
2543 Some(cx.tcx.type_of(self.def_id))
2548 AssociatedTypeItem(bounds, ty.clean(cx))
2550 TypedefItem(Typedef {
2551 type_: cx.tcx.type_of(self.def_id).clean(cx),
2552 generics: Generics {
2554 where_predicates: Vec::new(),
2561 let visibility = match self.container {
2562 ty::ImplContainer(_) => self.vis.clean(cx),
2563 ty::TraitContainer(_) => None,
2567 name: Some(self.ident.name.clean(cx)),
2569 stability: get_stability(cx, self.def_id),
2570 deprecation: get_deprecation(cx, self.def_id),
2571 def_id: self.def_id,
2572 attrs: inline::load_attrs(cx, self.def_id),
2573 source: cx.tcx.def_span(self.def_id).clean(cx),
2579 /// A trait reference, which may have higher ranked lifetimes.
2580 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
2581 pub struct PolyTrait {
2583 pub generic_params: Vec<GenericParamDef>,
2586 /// A representation of a Type suitable for hyperlinking purposes. Ideally one can get the original
2587 /// type out of the AST/TyCtxt given one of these, if more information is needed. Most importantly
2588 /// it does not preserve mutability or boxes.
2589 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
2591 /// structs/enums/traits (most that'd be an hir::TyPath)
2594 typarams: Option<Vec<GenericBound>>,
2596 /// true if is a `T::Name` path for associated types
2599 /// For parameterized types, so the consumer of the JSON don't go
2600 /// looking for types which don't exist anywhere.
2602 /// Primitives are the fixed-size numeric types (plus int/usize/float), char,
2603 /// arrays, slices, and tuples.
2604 Primitive(PrimitiveType),
2606 BareFunction(Box<BareFunctionDecl>),
2609 Array(Box<Type>, String),
2612 RawPointer(Mutability, Box<Type>),
2614 lifetime: Option<Lifetime>,
2615 mutability: Mutability,
2619 // <Type as Trait>::Name
2622 self_type: Box<Type>,
2629 // impl TraitA+TraitB
2630 ImplTrait(Vec<GenericBound>),
2633 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Copy, Debug)]
2634 pub enum PrimitiveType {
2635 Isize, I8, I16, I32, I64, I128,
2636 Usize, U8, U16, U32, U64, U128,
2651 #[derive(Clone, RustcEncodable, RustcDecodable, Copy, Debug)]
2667 pub trait GetDefId {
2668 fn def_id(&self) -> Option<DefId>;
2671 impl<T: GetDefId> GetDefId for Option<T> {
2672 fn def_id(&self) -> Option<DefId> {
2673 self.as_ref().and_then(|d| d.def_id())
2678 pub fn primitive_type(&self) -> Option<PrimitiveType> {
2680 Primitive(p) | BorrowedRef { type_: box Primitive(p), ..} => Some(p),
2681 Slice(..) | BorrowedRef { type_: box Slice(..), .. } => Some(PrimitiveType::Slice),
2682 Array(..) | BorrowedRef { type_: box Array(..), .. } => Some(PrimitiveType::Array),
2683 Tuple(ref tys) => if tys.is_empty() {
2684 Some(PrimitiveType::Unit)
2686 Some(PrimitiveType::Tuple)
2688 RawPointer(..) => Some(PrimitiveType::RawPointer),
2689 BorrowedRef { type_: box Generic(..), .. } => Some(PrimitiveType::Reference),
2690 BareFunction(..) => Some(PrimitiveType::Fn),
2691 Never => Some(PrimitiveType::Never),
2696 pub fn is_generic(&self) -> bool {
2698 ResolvedPath { is_generic, .. } => is_generic,
2703 pub fn is_self_type(&self) -> bool {
2705 Generic(ref name) => name == "Self",
2710 pub fn generics(&self) -> Option<&[Type]> {
2712 ResolvedPath { ref path, .. } => {
2713 path.segments.last().and_then(|seg| {
2714 if let GenericArgs::AngleBracketed { ref types, .. } = seg.args {
2726 impl GetDefId for Type {
2727 fn def_id(&self) -> Option<DefId> {
2729 ResolvedPath { did, .. } => Some(did),
2730 Primitive(p) => ::html::render::cache().primitive_locations.get(&p).cloned(),
2731 BorrowedRef { type_: box Generic(..), .. } =>
2732 Primitive(PrimitiveType::Reference).def_id(),
2733 BorrowedRef { ref type_, .. } => type_.def_id(),
2734 Tuple(ref tys) => if tys.is_empty() {
2735 Primitive(PrimitiveType::Unit).def_id()
2737 Primitive(PrimitiveType::Tuple).def_id()
2739 BareFunction(..) => Primitive(PrimitiveType::Fn).def_id(),
2740 Never => Primitive(PrimitiveType::Never).def_id(),
2741 Slice(..) => Primitive(PrimitiveType::Slice).def_id(),
2742 Array(..) => Primitive(PrimitiveType::Array).def_id(),
2743 RawPointer(..) => Primitive(PrimitiveType::RawPointer).def_id(),
2744 QPath { ref self_type, .. } => self_type.def_id(),
2750 impl PrimitiveType {
2751 fn from_str(s: &str) -> Option<PrimitiveType> {
2753 "isize" => Some(PrimitiveType::Isize),
2754 "i8" => Some(PrimitiveType::I8),
2755 "i16" => Some(PrimitiveType::I16),
2756 "i32" => Some(PrimitiveType::I32),
2757 "i64" => Some(PrimitiveType::I64),
2758 "i128" => Some(PrimitiveType::I128),
2759 "usize" => Some(PrimitiveType::Usize),
2760 "u8" => Some(PrimitiveType::U8),
2761 "u16" => Some(PrimitiveType::U16),
2762 "u32" => Some(PrimitiveType::U32),
2763 "u64" => Some(PrimitiveType::U64),
2764 "u128" => Some(PrimitiveType::U128),
2765 "bool" => Some(PrimitiveType::Bool),
2766 "char" => Some(PrimitiveType::Char),
2767 "str" => Some(PrimitiveType::Str),
2768 "f32" => Some(PrimitiveType::F32),
2769 "f64" => Some(PrimitiveType::F64),
2770 "array" => Some(PrimitiveType::Array),
2771 "slice" => Some(PrimitiveType::Slice),
2772 "tuple" => Some(PrimitiveType::Tuple),
2773 "unit" => Some(PrimitiveType::Unit),
2774 "pointer" => Some(PrimitiveType::RawPointer),
2775 "reference" => Some(PrimitiveType::Reference),
2776 "fn" => Some(PrimitiveType::Fn),
2777 "never" => Some(PrimitiveType::Never),
2782 pub fn as_str(&self) -> &'static str {
2783 use self::PrimitiveType::*;
2806 RawPointer => "pointer",
2807 Reference => "reference",
2813 pub fn to_url_str(&self) -> &'static str {
2818 impl From<ast::IntTy> for PrimitiveType {
2819 fn from(int_ty: ast::IntTy) -> PrimitiveType {
2821 ast::IntTy::Isize => PrimitiveType::Isize,
2822 ast::IntTy::I8 => PrimitiveType::I8,
2823 ast::IntTy::I16 => PrimitiveType::I16,
2824 ast::IntTy::I32 => PrimitiveType::I32,
2825 ast::IntTy::I64 => PrimitiveType::I64,
2826 ast::IntTy::I128 => PrimitiveType::I128,
2831 impl From<ast::UintTy> for PrimitiveType {
2832 fn from(uint_ty: ast::UintTy) -> PrimitiveType {
2834 ast::UintTy::Usize => PrimitiveType::Usize,
2835 ast::UintTy::U8 => PrimitiveType::U8,
2836 ast::UintTy::U16 => PrimitiveType::U16,
2837 ast::UintTy::U32 => PrimitiveType::U32,
2838 ast::UintTy::U64 => PrimitiveType::U64,
2839 ast::UintTy::U128 => PrimitiveType::U128,
2844 impl From<ast::FloatTy> for PrimitiveType {
2845 fn from(float_ty: ast::FloatTy) -> PrimitiveType {
2847 ast::FloatTy::F32 => PrimitiveType::F32,
2848 ast::FloatTy::F64 => PrimitiveType::F64,
2853 impl Clean<Type> for hir::Ty {
2854 fn clean(&self, cx: &DocContext) -> Type {
2858 TyPtr(ref m) => RawPointer(m.mutbl.clean(cx), box m.ty.clean(cx)),
2859 TyRptr(ref l, ref m) => {
2860 let lifetime = if l.is_elided() {
2865 BorrowedRef {lifetime: lifetime, mutability: m.mutbl.clean(cx),
2866 type_: box m.ty.clean(cx)}
2868 TySlice(ref ty) => Slice(box ty.clean(cx)),
2869 TyArray(ref ty, ref length) => {
2870 let def_id = cx.tcx.hir.local_def_id(length.id);
2871 let param_env = cx.tcx.param_env(def_id);
2872 let substs = Substs::identity_for_item(cx.tcx, def_id);
2873 let cid = GlobalId {
2874 instance: ty::Instance::new(def_id, substs),
2877 let length = cx.tcx.const_eval(param_env.and(cid)).unwrap_or_else(|_| {
2878 ty::Const::unevaluated(cx.tcx, def_id, substs, cx.tcx.types.usize)
2880 let length = print_const(cx, length);
2881 Array(box ty.clean(cx), length)
2883 TyTup(ref tys) => Tuple(tys.clean(cx)),
2884 TyPath(hir::QPath::Resolved(None, ref path)) => {
2885 if let Some(new_ty) = cx.ty_substs.borrow().get(&path.def).cloned() {
2889 if let Def::TyParam(did) = path.def {
2890 if let Some(bounds) = cx.impl_trait_bounds.borrow_mut().remove(&did) {
2891 return ImplTrait(bounds);
2895 let mut alias = None;
2896 if let Def::TyAlias(def_id) = path.def {
2897 // Substitute private type aliases
2898 if let Some(node_id) = cx.tcx.hir.as_local_node_id(def_id) {
2899 if !cx.access_levels.borrow().is_exported(def_id) {
2900 alias = Some(&cx.tcx.hir.expect_item(node_id).node);
2905 if let Some(&hir::ItemTy(ref ty, ref generics)) = alias {
2906 let provided_params = &path.segments.last().unwrap();
2907 let mut ty_substs = FxHashMap();
2908 let mut lt_substs = FxHashMap();
2909 provided_params.with_generic_args(|generic_args| {
2910 let mut indices = ty::GenericParamCount {
2914 for param in generics.params.iter() {
2916 hir::GenericParamKind::Lifetime { .. } => {
2918 let lifetime = generic_args.args.iter().find_map(|arg| {
2920 GenericArg::Lifetime(lt) => {
2921 if indices.lifetimes == j {
2930 if let Some(lt) = lifetime.cloned() {
2931 if !lt.is_elided() {
2933 cx.tcx.hir.local_def_id(param.id);
2934 lt_substs.insert(lt_def_id, lt.clean(cx));
2937 indices.lifetimes += 1;
2939 hir::GenericParamKind::Type { ref default, .. } => {
2941 Def::TyParam(cx.tcx.hir.local_def_id(param.id));
2943 let type_ = generic_args.args.iter().find_map(|arg| {
2945 GenericArg::Type(ty) => {
2946 if indices.types == j {
2955 if let Some(ty) = type_.cloned() {
2956 ty_substs.insert(ty_param_def, ty.clean(cx));
2957 } else if let Some(default) = default.clone() {
2958 ty_substs.insert(ty_param_def,
2959 default.into_inner().clean(cx));
2966 return cx.enter_alias(ty_substs, lt_substs, || ty.clean(cx));
2968 resolve_type(cx, path.clean(cx), self.id)
2970 TyPath(hir::QPath::Resolved(Some(ref qself), ref p)) => {
2971 let mut segments: Vec<_> = p.segments.clone().into();
2973 let trait_path = hir::Path {
2975 def: Def::Trait(cx.tcx.associated_item(p.def.def_id()).container.id()),
2976 segments: segments.into(),
2979 name: p.segments.last().unwrap().ident.name.clean(cx),
2980 self_type: box qself.clean(cx),
2981 trait_: box resolve_type(cx, trait_path.clean(cx), self.id)
2984 TyPath(hir::QPath::TypeRelative(ref qself, ref segment)) => {
2985 let mut def = Def::Err;
2986 let ty = hir_ty_to_ty(cx.tcx, self);
2987 if let ty::TyProjection(proj) = ty.sty {
2988 def = Def::Trait(proj.trait_ref(cx.tcx).def_id);
2990 let trait_path = hir::Path {
2993 segments: vec![].into(),
2996 name: segment.ident.name.clean(cx),
2997 self_type: box qself.clean(cx),
2998 trait_: box resolve_type(cx, trait_path.clean(cx), self.id)
3001 TyTraitObject(ref bounds, ref lifetime) => {
3002 match bounds[0].clean(cx).trait_ {
3003 ResolvedPath { path, typarams: None, did, is_generic } => {
3004 let mut bounds: Vec<self::GenericBound> = bounds[1..].iter().map(|bound| {
3005 self::GenericBound::TraitBound(bound.clean(cx),
3006 hir::TraitBoundModifier::None)
3008 if !lifetime.is_elided() {
3009 bounds.push(self::GenericBound::Outlives(lifetime.clean(cx)));
3011 ResolvedPath { path, typarams: Some(bounds), did, is_generic, }
3013 _ => Infer // shouldn't happen
3016 TyBareFn(ref barefn) => BareFunction(box barefn.clean(cx)),
3017 TyInfer | TyErr => Infer,
3018 TyTypeof(..) => panic!("Unimplemented type {:?}", self.node),
3023 impl<'tcx> Clean<Type> for Ty<'tcx> {
3024 fn clean(&self, cx: &DocContext) -> Type {
3026 ty::TyNever => Never,
3027 ty::TyBool => Primitive(PrimitiveType::Bool),
3028 ty::TyChar => Primitive(PrimitiveType::Char),
3029 ty::TyInt(int_ty) => Primitive(int_ty.into()),
3030 ty::TyUint(uint_ty) => Primitive(uint_ty.into()),
3031 ty::TyFloat(float_ty) => Primitive(float_ty.into()),
3032 ty::TyStr => Primitive(PrimitiveType::Str),
3033 ty::TySlice(ty) => Slice(box ty.clean(cx)),
3034 ty::TyArray(ty, n) => {
3035 let mut n = cx.tcx.lift(&n).unwrap();
3036 if let ConstValue::Unevaluated(def_id, substs) = n.val {
3037 let param_env = cx.tcx.param_env(def_id);
3038 let cid = GlobalId {
3039 instance: ty::Instance::new(def_id, substs),
3042 if let Ok(new_n) = cx.tcx.const_eval(param_env.and(cid)) {
3046 let n = print_const(cx, n);
3047 Array(box ty.clean(cx), n)
3049 ty::TyRawPtr(mt) => RawPointer(mt.mutbl.clean(cx), box mt.ty.clean(cx)),
3050 ty::TyRef(r, ty, mutbl) => BorrowedRef {
3051 lifetime: r.clean(cx),
3052 mutability: mutbl.clean(cx),
3053 type_: box ty.clean(cx),
3057 let ty = cx.tcx.lift(self).unwrap();
3058 let sig = ty.fn_sig(cx.tcx);
3059 BareFunction(box BareFunctionDecl {
3060 unsafety: sig.unsafety(),
3061 generic_params: Vec::new(),
3062 decl: (cx.tcx.hir.local_def_id(ast::CRATE_NODE_ID), sig).clean(cx),
3066 ty::TyAdt(def, substs) => {
3068 let kind = match def.adt_kind() {
3069 AdtKind::Struct => TypeKind::Struct,
3070 AdtKind::Union => TypeKind::Union,
3071 AdtKind::Enum => TypeKind::Enum,
3073 inline::record_extern_fqn(cx, did, kind);
3074 let path = external_path(cx, &cx.tcx.item_name(did).as_str(),
3075 None, false, vec![], substs);
3083 ty::TyForeign(did) => {
3084 inline::record_extern_fqn(cx, did, TypeKind::Foreign);
3085 let path = external_path(cx, &cx.tcx.item_name(did).as_str(),
3086 None, false, vec![], Substs::empty());
3094 ty::TyDynamic(ref obj, ref reg) => {
3095 if let Some(principal) = obj.principal() {
3096 let did = principal.def_id();
3097 inline::record_extern_fqn(cx, did, TypeKind::Trait);
3099 let mut typarams = vec![];
3100 reg.clean(cx).map(|b| typarams.push(GenericBound::Outlives(b)));
3101 for did in obj.auto_traits() {
3102 let empty = cx.tcx.intern_substs(&[]);
3103 let path = external_path(cx, &cx.tcx.item_name(did).as_str(),
3104 Some(did), false, vec![], empty);
3105 inline::record_extern_fqn(cx, did, TypeKind::Trait);
3106 let bound = GenericBound::TraitBound(PolyTrait {
3107 trait_: ResolvedPath {
3113 generic_params: Vec::new(),
3114 }, hir::TraitBoundModifier::None);
3115 typarams.push(bound);
3118 let mut bindings = vec![];
3119 for pb in obj.projection_bounds() {
3120 bindings.push(TypeBinding {
3121 name: cx.tcx.associated_item(pb.item_def_id()).ident.name.clean(cx),
3122 ty: pb.skip_binder().ty.clean(cx)
3126 let path = external_path(cx, &cx.tcx.item_name(did).as_str(), Some(did),
3127 false, bindings, principal.skip_binder().substs);
3130 typarams: Some(typarams),
3138 ty::TyTuple(ref t) => Tuple(t.clean(cx)),
3140 ty::TyProjection(ref data) => data.clean(cx),
3142 ty::TyParam(ref p) => Generic(p.name.to_string()),
3144 ty::TyAnon(def_id, substs) => {
3145 // Grab the "TraitA + TraitB" from `impl TraitA + TraitB`,
3146 // by looking up the projections associated with the def_id.
3147 let predicates_of = cx.tcx.predicates_of(def_id);
3148 let substs = cx.tcx.lift(&substs).unwrap();
3149 let bounds = predicates_of.instantiate(cx.tcx, substs);
3150 let mut regions = vec![];
3151 let mut has_sized = false;
3152 let mut bounds = bounds.predicates.iter().filter_map(|predicate| {
3153 let trait_ref = if let Some(tr) = predicate.to_opt_poly_trait_ref() {
3155 } else if let ty::Predicate::TypeOutlives(pred) = *predicate {
3156 // these should turn up at the end
3157 pred.skip_binder().1.clean(cx).map(|r| {
3158 regions.push(GenericBound::Outlives(r))
3165 if let Some(sized) = cx.tcx.lang_items().sized_trait() {
3166 if trait_ref.def_id() == sized {
3172 let bounds = bounds.predicates.iter().filter_map(|pred|
3173 if let ty::Predicate::Projection(proj) = *pred {
3174 let proj = proj.skip_binder();
3175 if proj.projection_ty.trait_ref(cx.tcx) == *trait_ref.skip_binder() {
3177 name: cx.tcx.associated_item(proj.projection_ty.item_def_id)
3178 .ident.name.clean(cx),
3179 ty: proj.ty.clean(cx),
3189 Some((trait_ref.skip_binder(), bounds).clean(cx))
3190 }).collect::<Vec<_>>();
3191 bounds.extend(regions);
3192 if !has_sized && !bounds.is_empty() {
3193 bounds.insert(0, GenericBound::maybe_sized(cx));
3198 ty::TyClosure(..) | ty::TyGenerator(..) => Tuple(vec![]), // FIXME(pcwalton)
3200 ty::TyGeneratorWitness(..) => panic!("TyGeneratorWitness"),
3201 ty::TyInfer(..) => panic!("TyInfer"),
3202 ty::TyError => panic!("TyError"),
3207 impl Clean<Item> for hir::StructField {
3208 fn clean(&self, cx: &DocContext) -> Item {
3210 name: Some(self.ident.name).clean(cx),
3211 attrs: self.attrs.clean(cx),
3212 source: self.span.clean(cx),
3213 visibility: self.vis.clean(cx),
3214 stability: get_stability(cx, cx.tcx.hir.local_def_id(self.id)),
3215 deprecation: get_deprecation(cx, cx.tcx.hir.local_def_id(self.id)),
3216 def_id: cx.tcx.hir.local_def_id(self.id),
3217 inner: StructFieldItem(self.ty.clean(cx)),
3222 impl<'tcx> Clean<Item> for ty::FieldDef {
3223 fn clean(&self, cx: &DocContext) -> Item {
3225 name: Some(self.ident.name).clean(cx),
3226 attrs: cx.tcx.get_attrs(self.did).clean(cx),
3227 source: cx.tcx.def_span(self.did).clean(cx),
3228 visibility: self.vis.clean(cx),
3229 stability: get_stability(cx, self.did),
3230 deprecation: get_deprecation(cx, self.did),
3232 inner: StructFieldItem(cx.tcx.type_of(self.did).clean(cx)),
3237 #[derive(Clone, PartialEq, Eq, RustcDecodable, RustcEncodable, Debug)]
3238 pub enum Visibility {
3242 Restricted(DefId, Path),
3245 impl Clean<Option<Visibility>> for hir::Visibility {
3246 fn clean(&self, cx: &DocContext) -> Option<Visibility> {
3247 Some(match self.node {
3248 hir::VisibilityKind::Public => Visibility::Public,
3249 hir::VisibilityKind::Inherited => Visibility::Inherited,
3250 hir::VisibilityKind::Crate(_) => Visibility::Crate,
3251 hir::VisibilityKind::Restricted { ref path, .. } => {
3252 let path = path.clean(cx);
3253 let did = register_def(cx, path.def);
3254 Visibility::Restricted(did, path)
3260 impl Clean<Option<Visibility>> for ty::Visibility {
3261 fn clean(&self, _: &DocContext) -> Option<Visibility> {
3262 Some(if *self == ty::Visibility::Public { Public } else { Inherited })
3266 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3268 pub struct_type: doctree::StructType,
3269 pub generics: Generics,
3270 pub fields: Vec<Item>,
3271 pub fields_stripped: bool,
3274 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3276 pub struct_type: doctree::StructType,
3277 pub generics: Generics,
3278 pub fields: Vec<Item>,
3279 pub fields_stripped: bool,
3282 impl Clean<Vec<Item>> for doctree::Struct {
3283 fn clean(&self, cx: &DocContext) -> Vec<Item> {
3284 let name = self.name.clean(cx);
3285 let mut ret = get_auto_traits_with_node_id(cx, self.id, name.clone());
3289 attrs: self.attrs.clean(cx),
3290 source: self.whence.clean(cx),
3291 def_id: cx.tcx.hir.local_def_id(self.id),
3292 visibility: self.vis.clean(cx),
3293 stability: self.stab.clean(cx),
3294 deprecation: self.depr.clean(cx),
3295 inner: StructItem(Struct {
3296 struct_type: self.struct_type,
3297 generics: self.generics.clean(cx),
3298 fields: self.fields.clean(cx),
3299 fields_stripped: false,
3307 impl Clean<Vec<Item>> for doctree::Union {
3308 fn clean(&self, cx: &DocContext) -> Vec<Item> {
3309 let name = self.name.clean(cx);
3310 let mut ret = get_auto_traits_with_node_id(cx, self.id, name.clone());
3314 attrs: self.attrs.clean(cx),
3315 source: self.whence.clean(cx),
3316 def_id: cx.tcx.hir.local_def_id(self.id),
3317 visibility: self.vis.clean(cx),
3318 stability: self.stab.clean(cx),
3319 deprecation: self.depr.clean(cx),
3320 inner: UnionItem(Union {
3321 struct_type: self.struct_type,
3322 generics: self.generics.clean(cx),
3323 fields: self.fields.clean(cx),
3324 fields_stripped: false,
3332 /// This is a more limited form of the standard Struct, different in that
3333 /// it lacks the things most items have (name, id, parameterization). Found
3334 /// only as a variant in an enum.
3335 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3336 pub struct VariantStruct {
3337 pub struct_type: doctree::StructType,
3338 pub fields: Vec<Item>,
3339 pub fields_stripped: bool,
3342 impl Clean<VariantStruct> for ::rustc::hir::VariantData {
3343 fn clean(&self, cx: &DocContext) -> VariantStruct {
3345 struct_type: doctree::struct_type_from_def(self),
3346 fields: self.fields().iter().map(|x| x.clean(cx)).collect(),
3347 fields_stripped: false,
3352 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3354 pub variants: Vec<Item>,
3355 pub generics: Generics,
3356 pub variants_stripped: bool,
3359 impl Clean<Vec<Item>> for doctree::Enum {
3360 fn clean(&self, cx: &DocContext) -> Vec<Item> {
3361 let name = self.name.clean(cx);
3362 let mut ret = get_auto_traits_with_node_id(cx, self.id, name.clone());
3366 attrs: self.attrs.clean(cx),
3367 source: self.whence.clean(cx),
3368 def_id: cx.tcx.hir.local_def_id(self.id),
3369 visibility: self.vis.clean(cx),
3370 stability: self.stab.clean(cx),
3371 deprecation: self.depr.clean(cx),
3372 inner: EnumItem(Enum {
3373 variants: self.variants.clean(cx),
3374 generics: self.generics.clean(cx),
3375 variants_stripped: false,
3383 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3384 pub struct Variant {
3385 pub kind: VariantKind,
3388 impl Clean<Item> for doctree::Variant {
3389 fn clean(&self, cx: &DocContext) -> Item {
3391 name: Some(self.name.clean(cx)),
3392 attrs: self.attrs.clean(cx),
3393 source: self.whence.clean(cx),
3395 stability: self.stab.clean(cx),
3396 deprecation: self.depr.clean(cx),
3397 def_id: cx.tcx.hir.local_def_id(self.def.id()),
3398 inner: VariantItem(Variant {
3399 kind: self.def.clean(cx),
3405 impl<'tcx> Clean<Item> for ty::VariantDef {
3406 fn clean(&self, cx: &DocContext) -> Item {
3407 let kind = match self.ctor_kind {
3408 CtorKind::Const => VariantKind::CLike,
3411 self.fields.iter().map(|f| cx.tcx.type_of(f.did).clean(cx)).collect()
3414 CtorKind::Fictive => {
3415 VariantKind::Struct(VariantStruct {
3416 struct_type: doctree::Plain,
3417 fields_stripped: false,
3418 fields: self.fields.iter().map(|field| {
3420 source: cx.tcx.def_span(field.did).clean(cx),
3421 name: Some(field.ident.name.clean(cx)),
3422 attrs: cx.tcx.get_attrs(field.did).clean(cx),
3423 visibility: field.vis.clean(cx),
3425 stability: get_stability(cx, field.did),
3426 deprecation: get_deprecation(cx, field.did),
3427 inner: StructFieldItem(cx.tcx.type_of(field.did).clean(cx))
3434 name: Some(self.name.clean(cx)),
3435 attrs: inline::load_attrs(cx, self.did),
3436 source: cx.tcx.def_span(self.did).clean(cx),
3437 visibility: Some(Inherited),
3439 inner: VariantItem(Variant { kind: kind }),
3440 stability: get_stability(cx, self.did),
3441 deprecation: get_deprecation(cx, self.did),
3446 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3447 pub enum VariantKind {
3450 Struct(VariantStruct),
3453 impl Clean<VariantKind> for hir::VariantData {
3454 fn clean(&self, cx: &DocContext) -> VariantKind {
3455 if self.is_struct() {
3456 VariantKind::Struct(self.clean(cx))
3457 } else if self.is_unit() {
3460 VariantKind::Tuple(self.fields().iter().map(|x| x.ty.clean(cx)).collect())
3465 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3467 pub filename: FileName,
3475 pub fn empty() -> Span {
3477 filename: FileName::Anon,
3478 loline: 0, locol: 0,
3479 hiline: 0, hicol: 0,
3484 impl Clean<Span> for syntax_pos::Span {
3485 fn clean(&self, cx: &DocContext) -> Span {
3486 if self.is_dummy() {
3487 return Span::empty();
3490 let cm = cx.sess().codemap();
3491 let filename = cm.span_to_filename(*self);
3492 let lo = cm.lookup_char_pos(self.lo());
3493 let hi = cm.lookup_char_pos(self.hi());
3497 locol: lo.col.to_usize(),
3499 hicol: hi.col.to_usize(),
3504 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
3508 pub segments: Vec<PathSegment>,
3512 pub fn singleton(name: String) -> Path {
3516 segments: vec![PathSegment {
3518 args: GenericArgs::AngleBracketed {
3519 lifetimes: Vec::new(),
3521 bindings: Vec::new(),
3527 pub fn last_name(&self) -> &str {
3528 self.segments.last().unwrap().name.as_str()
3532 impl Clean<Path> for hir::Path {
3533 fn clean(&self, cx: &DocContext) -> Path {
3535 global: self.is_global(),
3537 segments: if self.is_global() { &self.segments[1..] } else { &self.segments }.clean(cx),
3542 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
3543 pub enum GenericArgs {
3545 lifetimes: Vec<Lifetime>,
3547 bindings: Vec<TypeBinding>,
3551 output: Option<Type>,
3555 impl Clean<GenericArgs> for hir::GenericArgs {
3556 fn clean(&self, cx: &DocContext) -> GenericArgs {
3557 if self.parenthesized {
3558 let output = self.bindings[0].ty.clean(cx);
3559 GenericArgs::Parenthesized {
3560 inputs: self.inputs().clean(cx),
3561 output: if output != Type::Tuple(Vec::new()) { Some(output) } else { None }
3564 let (mut lifetimes, mut types) = (vec![], vec![]);
3565 let mut elided_lifetimes = true;
3566 for arg in &self.args {
3568 GenericArg::Lifetime(lt) => {
3569 if !lt.is_elided() {
3570 elided_lifetimes = false;
3572 lifetimes.push(lt.clean(cx));
3574 GenericArg::Type(ty) => {
3575 types.push(ty.clean(cx));
3579 GenericArgs::AngleBracketed {
3580 lifetimes: if elided_lifetimes { vec![] } else { lifetimes },
3582 bindings: self.bindings.clean(cx),
3588 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
3589 pub struct PathSegment {
3591 pub args: GenericArgs,
3594 impl Clean<PathSegment> for hir::PathSegment {
3595 fn clean(&self, cx: &DocContext) -> PathSegment {
3597 name: self.ident.name.clean(cx),
3598 args: self.with_generic_args(|generic_args| generic_args.clean(cx))
3603 fn strip_type(ty: Type) -> Type {
3605 Type::ResolvedPath { path, typarams, did, is_generic } => {
3606 Type::ResolvedPath { path: strip_path(&path), typarams, did, is_generic }
3608 Type::Tuple(inner_tys) => {
3609 Type::Tuple(inner_tys.iter().map(|t| strip_type(t.clone())).collect())
3611 Type::Slice(inner_ty) => Type::Slice(Box::new(strip_type(*inner_ty))),
3612 Type::Array(inner_ty, s) => Type::Array(Box::new(strip_type(*inner_ty)), s),
3613 Type::Unique(inner_ty) => Type::Unique(Box::new(strip_type(*inner_ty))),
3614 Type::RawPointer(m, inner_ty) => Type::RawPointer(m, Box::new(strip_type(*inner_ty))),
3615 Type::BorrowedRef { lifetime, mutability, type_ } => {
3616 Type::BorrowedRef { lifetime, mutability, type_: Box::new(strip_type(*type_)) }
3618 Type::QPath { name, self_type, trait_ } => {
3621 self_type: Box::new(strip_type(*self_type)), trait_: Box::new(strip_type(*trait_))
3628 fn strip_path(path: &Path) -> Path {
3629 let segments = path.segments.iter().map(|s| {
3631 name: s.name.clone(),
3632 args: GenericArgs::AngleBracketed {
3633 lifetimes: Vec::new(),
3635 bindings: Vec::new(),
3641 global: path.global,
3642 def: path.def.clone(),
3647 fn qpath_to_string(p: &hir::QPath) -> String {
3648 let segments = match *p {
3649 hir::QPath::Resolved(_, ref path) => &path.segments,
3650 hir::QPath::TypeRelative(_, ref segment) => return segment.ident.to_string(),
3653 let mut s = String::new();
3654 for (i, seg) in segments.iter().enumerate() {
3658 if seg.ident.name != keywords::CrateRoot.name() {
3659 s.push_str(&*seg.ident.as_str());
3665 impl Clean<String> for ast::Name {
3666 fn clean(&self, _: &DocContext) -> String {
3671 impl Clean<String> for InternedString {
3672 fn clean(&self, _: &DocContext) -> String {
3677 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3678 pub struct Typedef {
3680 pub generics: Generics,
3683 impl Clean<Item> for doctree::Typedef {
3684 fn clean(&self, cx: &DocContext) -> Item {
3686 name: Some(self.name.clean(cx)),
3687 attrs: self.attrs.clean(cx),
3688 source: self.whence.clean(cx),
3689 def_id: cx.tcx.hir.local_def_id(self.id.clone()),
3690 visibility: self.vis.clean(cx),
3691 stability: self.stab.clean(cx),
3692 deprecation: self.depr.clean(cx),
3693 inner: TypedefItem(Typedef {
3694 type_: self.ty.clean(cx),
3695 generics: self.gen.clean(cx),
3701 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
3702 pub struct BareFunctionDecl {
3703 pub unsafety: hir::Unsafety,
3704 pub generic_params: Vec<GenericParamDef>,
3709 impl Clean<BareFunctionDecl> for hir::BareFnTy {
3710 fn clean(&self, cx: &DocContext) -> BareFunctionDecl {
3711 let (generic_params, decl) = enter_impl_trait(cx, || {
3712 (self.generic_params.clean(cx), (&*self.decl, &self.arg_names[..]).clean(cx))
3715 unsafety: self.unsafety,
3723 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3726 pub mutability: Mutability,
3727 /// It's useful to have the value of a static documented, but I have no
3728 /// desire to represent expressions (that'd basically be all of the AST,
3729 /// which is huge!). So, have a string.
3733 impl Clean<Item> for doctree::Static {
3734 fn clean(&self, cx: &DocContext) -> Item {
3735 debug!("cleaning static {}: {:?}", self.name.clean(cx), self);
3737 name: Some(self.name.clean(cx)),
3738 attrs: self.attrs.clean(cx),
3739 source: self.whence.clean(cx),
3740 def_id: cx.tcx.hir.local_def_id(self.id),
3741 visibility: self.vis.clean(cx),
3742 stability: self.stab.clean(cx),
3743 deprecation: self.depr.clean(cx),
3744 inner: StaticItem(Static {
3745 type_: self.type_.clean(cx),
3746 mutability: self.mutability.clean(cx),
3747 expr: print_const_expr(cx, self.expr),
3753 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3754 pub struct Constant {
3759 impl Clean<Item> for doctree::Constant {
3760 fn clean(&self, cx: &DocContext) -> Item {
3762 name: Some(self.name.clean(cx)),
3763 attrs: self.attrs.clean(cx),
3764 source: self.whence.clean(cx),
3765 def_id: cx.tcx.hir.local_def_id(self.id),
3766 visibility: self.vis.clean(cx),
3767 stability: self.stab.clean(cx),
3768 deprecation: self.depr.clean(cx),
3769 inner: ConstantItem(Constant {
3770 type_: self.type_.clean(cx),
3771 expr: print_const_expr(cx, self.expr),
3777 #[derive(Debug, Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Copy, Hash)]
3778 pub enum Mutability {
3783 impl Clean<Mutability> for hir::Mutability {
3784 fn clean(&self, _: &DocContext) -> Mutability {
3786 &hir::MutMutable => Mutable,
3787 &hir::MutImmutable => Immutable,
3792 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Copy, Debug, Hash)]
3793 pub enum ImplPolarity {
3798 impl Clean<ImplPolarity> for hir::ImplPolarity {
3799 fn clean(&self, _: &DocContext) -> ImplPolarity {
3801 &hir::ImplPolarity::Positive => ImplPolarity::Positive,
3802 &hir::ImplPolarity::Negative => ImplPolarity::Negative,
3807 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3809 pub unsafety: hir::Unsafety,
3810 pub generics: Generics,
3811 pub provided_trait_methods: FxHashSet<String>,
3812 pub trait_: Option<Type>,
3814 pub items: Vec<Item>,
3815 pub polarity: Option<ImplPolarity>,
3816 pub synthetic: bool,
3819 pub fn get_auto_traits_with_node_id(cx: &DocContext, id: ast::NodeId, name: String) -> Vec<Item> {
3820 let finder = AutoTraitFinder::new(cx);
3821 finder.get_with_node_id(id, name)
3824 pub fn get_auto_traits_with_def_id(cx: &DocContext, id: DefId) -> Vec<Item> {
3825 let finder = AutoTraitFinder::new(cx);
3827 finder.get_with_def_id(id)
3830 impl Clean<Vec<Item>> for doctree::Impl {
3831 fn clean(&self, cx: &DocContext) -> Vec<Item> {
3832 let mut ret = Vec::new();
3833 let trait_ = self.trait_.clean(cx);
3834 let items = self.items.clean(cx);
3836 // If this impl block is an implementation of the Deref trait, then we
3837 // need to try inlining the target's inherent impl blocks as well.
3838 if trait_.def_id() == cx.tcx.lang_items().deref_trait() {
3839 build_deref_target_impls(cx, &items, &mut ret);
3842 let provided = trait_.def_id().map(|did| {
3843 cx.tcx.provided_trait_methods(did)
3845 .map(|meth| meth.ident.to_string())
3847 }).unwrap_or(FxHashSet());
3851 attrs: self.attrs.clean(cx),
3852 source: self.whence.clean(cx),
3853 def_id: cx.tcx.hir.local_def_id(self.id),
3854 visibility: self.vis.clean(cx),
3855 stability: self.stab.clean(cx),
3856 deprecation: self.depr.clean(cx),
3857 inner: ImplItem(Impl {
3858 unsafety: self.unsafety,
3859 generics: self.generics.clean(cx),
3860 provided_trait_methods: provided,
3862 for_: self.for_.clean(cx),
3864 polarity: Some(self.polarity.clean(cx)),
3872 fn build_deref_target_impls(cx: &DocContext,
3874 ret: &mut Vec<Item>) {
3875 use self::PrimitiveType::*;
3879 let target = match item.inner {
3880 TypedefItem(ref t, true) => &t.type_,
3883 let primitive = match *target {
3884 ResolvedPath { did, .. } if did.is_local() => continue,
3885 ResolvedPath { did, .. } => {
3886 // We set the last parameter to false to avoid looking for auto-impls for traits
3887 // and therefore avoid an ICE.
3888 // The reason behind this is that auto-traits don't propagate through Deref so
3889 // we're not supposed to synthesise impls for them.
3890 ret.extend(inline::build_impls(cx, did, false));
3893 _ => match target.primitive_type() {
3898 let did = match primitive {
3899 Isize => tcx.lang_items().isize_impl(),
3900 I8 => tcx.lang_items().i8_impl(),
3901 I16 => tcx.lang_items().i16_impl(),
3902 I32 => tcx.lang_items().i32_impl(),
3903 I64 => tcx.lang_items().i64_impl(),
3904 I128 => tcx.lang_items().i128_impl(),
3905 Usize => tcx.lang_items().usize_impl(),
3906 U8 => tcx.lang_items().u8_impl(),
3907 U16 => tcx.lang_items().u16_impl(),
3908 U32 => tcx.lang_items().u32_impl(),
3909 U64 => tcx.lang_items().u64_impl(),
3910 U128 => tcx.lang_items().u128_impl(),
3911 F32 => tcx.lang_items().f32_impl(),
3912 F64 => tcx.lang_items().f64_impl(),
3913 Char => tcx.lang_items().char_impl(),
3915 Str => tcx.lang_items().str_impl(),
3916 Slice => tcx.lang_items().slice_impl(),
3917 Array => tcx.lang_items().slice_impl(),
3920 RawPointer => tcx.lang_items().const_ptr_impl(),
3925 if let Some(did) = did {
3926 if !did.is_local() {
3927 inline::build_impl(cx, did, ret);
3933 impl Clean<Item> for doctree::ExternCrate {
3934 fn clean(&self, cx: &DocContext) -> Item {
3937 attrs: self.attrs.clean(cx),
3938 source: self.whence.clean(cx),
3939 def_id: DefId { krate: self.cnum, index: CRATE_DEF_INDEX },
3940 visibility: self.vis.clean(cx),
3943 inner: ExternCrateItem(self.name.clean(cx), self.path.clone())
3948 impl Clean<Vec<Item>> for doctree::Import {
3949 fn clean(&self, cx: &DocContext) -> Vec<Item> {
3950 // We consider inlining the documentation of `pub use` statements, but we
3951 // forcefully don't inline if this is not public or if the
3952 // #[doc(no_inline)] attribute is present.
3953 // Don't inline doc(hidden) imports so they can be stripped at a later stage.
3954 let denied = !self.vis.node.is_pub() || self.attrs.iter().any(|a| {
3955 a.name() == "doc" && match a.meta_item_list() {
3956 Some(l) => attr::list_contains_name(&l, "no_inline") ||
3957 attr::list_contains_name(&l, "hidden"),
3961 let path = self.path.clean(cx);
3962 let inner = if self.glob {
3964 let mut visited = FxHashSet();
3965 if let Some(items) = inline::try_inline_glob(cx, path.def, &mut visited) {
3970 Import::Glob(resolve_use_source(cx, path))
3972 let name = self.name;
3974 let mut visited = FxHashSet();
3975 if let Some(items) = inline::try_inline(cx, path.def, name, &mut visited) {
3979 Import::Simple(name.clean(cx), resolve_use_source(cx, path))
3983 attrs: self.attrs.clean(cx),
3984 source: self.whence.clean(cx),
3985 def_id: cx.tcx.hir.local_def_id(ast::CRATE_NODE_ID),
3986 visibility: self.vis.clean(cx),
3989 inner: ImportItem(inner)
3994 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3996 // use source as str;
3997 Simple(String, ImportSource),
4002 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
4003 pub struct ImportSource {
4005 pub did: Option<DefId>,
4008 impl Clean<Vec<Item>> for hir::ForeignMod {
4009 fn clean(&self, cx: &DocContext) -> Vec<Item> {
4010 let mut items = self.items.clean(cx);
4011 for item in &mut items {
4012 if let ForeignFunctionItem(ref mut f) = item.inner {
4013 f.header.abi = self.abi;
4020 impl Clean<Item> for hir::ForeignItem {
4021 fn clean(&self, cx: &DocContext) -> Item {
4022 let inner = match self.node {
4023 hir::ForeignItemFn(ref decl, ref names, ref generics) => {
4024 let (generics, decl) = enter_impl_trait(cx, || {
4025 (generics.clean(cx), (&**decl, &names[..]).clean(cx))
4027 ForeignFunctionItem(Function {
4030 header: hir::FnHeader {
4031 unsafety: hir::Unsafety::Unsafe,
4033 constness: hir::Constness::NotConst,
4034 asyncness: hir::IsAsync::NotAsync,
4038 hir::ForeignItemStatic(ref ty, mutbl) => {
4039 ForeignStaticItem(Static {
4040 type_: ty.clean(cx),
4041 mutability: if mutbl {Mutable} else {Immutable},
4042 expr: "".to_string(),
4045 hir::ForeignItemType => {
4050 name: Some(self.name.clean(cx)),
4051 attrs: self.attrs.clean(cx),
4052 source: self.span.clean(cx),
4053 def_id: cx.tcx.hir.local_def_id(self.id),
4054 visibility: self.vis.clean(cx),
4055 stability: get_stability(cx, cx.tcx.hir.local_def_id(self.id)),
4056 deprecation: get_deprecation(cx, cx.tcx.hir.local_def_id(self.id)),
4065 fn to_src(&self, cx: &DocContext) -> String;
4068 impl ToSource for syntax_pos::Span {
4069 fn to_src(&self, cx: &DocContext) -> String {
4070 debug!("converting span {:?} to snippet", self.clean(cx));
4071 let sn = match cx.sess().codemap().span_to_snippet(*self) {
4072 Ok(x) => x.to_string(),
4073 Err(_) => "".to_string()
4075 debug!("got snippet {}", sn);
4080 fn name_from_pat(p: &hir::Pat) -> String {
4082 debug!("Trying to get a name from pattern: {:?}", p);
4085 PatKind::Wild => "_".to_string(),
4086 PatKind::Binding(_, _, ident, _) => ident.to_string(),
4087 PatKind::TupleStruct(ref p, ..) | PatKind::Path(ref p) => qpath_to_string(p),
4088 PatKind::Struct(ref name, ref fields, etc) => {
4089 format!("{} {{ {}{} }}", qpath_to_string(name),
4090 fields.iter().map(|&Spanned { node: ref fp, .. }|
4091 format!("{}: {}", fp.ident, name_from_pat(&*fp.pat)))
4092 .collect::<Vec<String>>().join(", "),
4093 if etc { ", ..." } else { "" }
4096 PatKind::Tuple(ref elts, _) => format!("({})", elts.iter().map(|p| name_from_pat(&**p))
4097 .collect::<Vec<String>>().join(", ")),
4098 PatKind::Box(ref p) => name_from_pat(&**p),
4099 PatKind::Ref(ref p, _) => name_from_pat(&**p),
4100 PatKind::Lit(..) => {
4101 warn!("tried to get argument name from PatKind::Lit, \
4102 which is silly in function arguments");
4105 PatKind::Range(..) => panic!("tried to get argument name from PatKind::Range, \
4106 which is not allowed in function arguments"),
4107 PatKind::Slice(ref begin, ref mid, ref end) => {
4108 let begin = begin.iter().map(|p| name_from_pat(&**p));
4109 let mid = mid.as_ref().map(|p| format!("..{}", name_from_pat(&**p))).into_iter();
4110 let end = end.iter().map(|p| name_from_pat(&**p));
4111 format!("[{}]", begin.chain(mid).chain(end).collect::<Vec<_>>().join(", "))
4116 fn print_const(cx: &DocContext, n: &ty::Const) -> String {
4118 ConstValue::Unevaluated(def_id, _) => {
4119 if let Some(node_id) = cx.tcx.hir.as_local_node_id(def_id) {
4120 print_const_expr(cx, cx.tcx.hir.body_owned_by(node_id))
4122 inline::print_inlined_const(cx, def_id)
4126 let mut s = String::new();
4127 ::rustc::mir::fmt_const_val(&mut s, n).unwrap();
4128 // array lengths are obviously usize
4129 if s.ends_with("usize") {
4130 let n = s.len() - "usize".len();
4138 fn print_const_expr(cx: &DocContext, body: hir::BodyId) -> String {
4139 cx.tcx.hir.node_to_pretty_string(body.node_id)
4142 /// Given a type Path, resolve it to a Type using the TyCtxt
4143 fn resolve_type(cx: &DocContext,
4145 id: ast::NodeId) -> Type {
4146 if id == ast::DUMMY_NODE_ID {
4147 debug!("resolve_type({:?})", path);
4149 debug!("resolve_type({:?},{:?})", path, id);
4152 let is_generic = match path.def {
4153 Def::PrimTy(p) => match p {
4154 hir::TyStr => return Primitive(PrimitiveType::Str),
4155 hir::TyBool => return Primitive(PrimitiveType::Bool),
4156 hir::TyChar => return Primitive(PrimitiveType::Char),
4157 hir::TyInt(int_ty) => return Primitive(int_ty.into()),
4158 hir::TyUint(uint_ty) => return Primitive(uint_ty.into()),
4159 hir::TyFloat(float_ty) => return Primitive(float_ty.into()),
4161 Def::SelfTy(..) if path.segments.len() == 1 => {
4162 return Generic(keywords::SelfType.name().to_string());
4164 Def::TyParam(..) if path.segments.len() == 1 => {
4165 return Generic(format!("{:#}", path));
4167 Def::SelfTy(..) | Def::TyParam(..) | Def::AssociatedTy(..) => true,
4170 let did = register_def(&*cx, path.def);
4171 ResolvedPath { path: path, typarams: None, did: did, is_generic: is_generic }
4174 fn register_def(cx: &DocContext, def: Def) -> DefId {
4175 debug!("register_def({:?})", def);
4177 let (did, kind) = match def {
4178 Def::Fn(i) => (i, TypeKind::Function),
4179 Def::TyAlias(i) => (i, TypeKind::Typedef),
4180 Def::Enum(i) => (i, TypeKind::Enum),
4181 Def::Trait(i) => (i, TypeKind::Trait),
4182 Def::Struct(i) => (i, TypeKind::Struct),
4183 Def::Union(i) => (i, TypeKind::Union),
4184 Def::Mod(i) => (i, TypeKind::Module),
4185 Def::TyForeign(i) => (i, TypeKind::Foreign),
4186 Def::Const(i) => (i, TypeKind::Const),
4187 Def::Static(i, _) => (i, TypeKind::Static),
4188 Def::Variant(i) => (cx.tcx.parent_def_id(i).unwrap(), TypeKind::Enum),
4189 Def::Macro(i, _) => (i, TypeKind::Macro),
4190 Def::SelfTy(Some(def_id), _) => (def_id, TypeKind::Trait),
4191 Def::SelfTy(_, Some(impl_def_id)) => {
4194 _ => return def.def_id()
4196 if did.is_local() { return did }
4197 inline::record_extern_fqn(cx, did, kind);
4198 if let TypeKind::Trait = kind {
4199 inline::record_extern_trait(cx, did);
4204 fn resolve_use_source(cx: &DocContext, path: Path) -> ImportSource {
4206 did: if path.def == Def::Err {
4209 Some(register_def(cx, path.def))
4215 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
4218 pub imported_from: Option<String>,
4221 impl Clean<Item> for doctree::Macro {
4222 fn clean(&self, cx: &DocContext) -> Item {
4223 let name = self.name.clean(cx);
4225 name: Some(name.clone()),
4226 attrs: self.attrs.clean(cx),
4227 source: self.whence.clean(cx),
4228 visibility: Some(Public),
4229 stability: self.stab.clean(cx),
4230 deprecation: self.depr.clean(cx),
4231 def_id: self.def_id,
4232 inner: MacroItem(Macro {
4233 source: format!("macro_rules! {} {{\n{}}}",
4235 self.matchers.iter().map(|span| {
4236 format!(" {} => {{ ... }};\n", span.to_src(cx))
4237 }).collect::<String>()),
4238 imported_from: self.imported_from.clean(cx),
4244 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
4245 pub struct Stability {
4246 pub level: stability::StabilityLevel,
4247 pub feature: String,
4249 pub deprecated_since: String,
4250 pub deprecated_reason: String,
4251 pub unstable_reason: String,
4252 pub issue: Option<u32>
4255 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
4256 pub struct Deprecation {
4261 impl Clean<Stability> for attr::Stability {
4262 fn clean(&self, _: &DocContext) -> Stability {
4264 level: stability::StabilityLevel::from_attr_level(&self.level),
4265 feature: self.feature.to_string(),
4266 since: match self.level {
4267 attr::Stable {ref since} => since.to_string(),
4268 _ => "".to_string(),
4270 deprecated_since: match self.rustc_depr {
4271 Some(attr::RustcDeprecation {ref since, ..}) => since.to_string(),
4274 deprecated_reason: match self.rustc_depr {
4275 Some(ref depr) => depr.reason.to_string(),
4276 _ => "".to_string(),
4278 unstable_reason: match self.level {
4279 attr::Unstable { reason: Some(ref reason), .. } => reason.to_string(),
4280 _ => "".to_string(),
4282 issue: match self.level {
4283 attr::Unstable {issue, ..} => Some(issue),
4290 impl<'a> Clean<Stability> for &'a attr::Stability {
4291 fn clean(&self, dc: &DocContext) -> Stability {
4296 impl Clean<Deprecation> for attr::Deprecation {
4297 fn clean(&self, _: &DocContext) -> Deprecation {
4299 since: self.since.as_ref().map_or("".to_string(), |s| s.to_string()),
4300 note: self.note.as_ref().map_or("".to_string(), |s| s.to_string()),
4305 /// An equality constraint on an associated type, e.g. `A=Bar` in `Foo<A=Bar>`
4306 #[derive(Clone, PartialEq, Eq, RustcDecodable, RustcEncodable, Debug, Hash)]
4307 pub struct TypeBinding {
4312 impl Clean<TypeBinding> for hir::TypeBinding {
4313 fn clean(&self, cx: &DocContext) -> TypeBinding {
4315 name: self.ident.name.clean(cx),
4316 ty: self.ty.clean(cx)
4321 pub fn def_id_to_path(cx: &DocContext, did: DefId, name: Option<String>) -> Vec<String> {
4322 let crate_name = name.unwrap_or_else(|| cx.tcx.crate_name(did.krate).to_string());
4323 let relative = cx.tcx.def_path(did).data.into_iter().filter_map(|elem| {
4324 // extern blocks have an empty name
4325 let s = elem.data.to_string();
4332 once(crate_name).chain(relative).collect()
4335 pub fn enter_impl_trait<F, R>(cx: &DocContext, f: F) -> R
4339 let old_bounds = mem::replace(&mut *cx.impl_trait_bounds.borrow_mut(), Default::default());
4341 assert!(cx.impl_trait_bounds.borrow().is_empty());
4342 *cx.impl_trait_bounds.borrow_mut() = old_bounds;
4346 // Start of code copied from rust-clippy
4348 pub fn get_trait_def_id(tcx: &TyCtxt, path: &[&str], use_local: bool) -> Option<DefId> {
4350 path_to_def_local(tcx, path)
4352 path_to_def(tcx, path)
4356 pub fn path_to_def_local(tcx: &TyCtxt, path: &[&str]) -> Option<DefId> {
4357 let krate = tcx.hir.krate();
4358 let mut items = krate.module.item_ids.clone();
4359 let mut path_it = path.iter().peekable();
4362 let segment = match path_it.next() {
4363 Some(segment) => segment,
4364 None => return None,
4367 for item_id in mem::replace(&mut items, HirVec::new()).iter() {
4368 let item = tcx.hir.expect_item(item_id.id);
4369 if item.name == *segment {
4370 if path_it.peek().is_none() {
4371 return Some(tcx.hir.local_def_id(item_id.id))
4374 items = match &item.node {
4375 &hir::ItemMod(ref m) => m.item_ids.clone(),
4376 _ => panic!("Unexpected item {:?} in path {:?} path")
4384 pub fn path_to_def(tcx: &TyCtxt, path: &[&str]) -> Option<DefId> {
4385 let crates = tcx.crates();
4389 .find(|&&krate| tcx.crate_name(krate) == path[0]);
4391 if let Some(krate) = krate {
4394 index: CRATE_DEF_INDEX,
4396 let mut items = tcx.item_children(krate);
4397 let mut path_it = path.iter().skip(1).peekable();
4400 let segment = match path_it.next() {
4401 Some(segment) => segment,
4402 None => return None,
4405 for item in mem::replace(&mut items, Lrc::new(vec![])).iter() {
4406 if item.ident.name == *segment {
4407 if path_it.peek().is_none() {
4408 return match item.def {
4409 def::Def::Trait(did) => Some(did),
4414 items = tcx.item_children(item.def.def_id());
4424 fn get_path_for_type<F>(tcx: TyCtxt, def_id: DefId, def_ctor: F) -> hir::Path
4425 where F: Fn(DefId) -> Def {
4426 struct AbsolutePathBuffer {
4430 impl ty::item_path::ItemPathBuffer for AbsolutePathBuffer {
4431 fn root_mode(&self) -> &ty::item_path::RootMode {
4432 const ABSOLUTE: &'static ty::item_path::RootMode = &ty::item_path::RootMode::Absolute;
4436 fn push(&mut self, text: &str) {
4437 self.names.push(text.to_owned());
4441 let mut apb = AbsolutePathBuffer { names: vec![] };
4443 tcx.push_item_path(&mut apb, def_id);
4447 def: def_ctor(def_id),
4448 segments: hir::HirVec::from_vec(apb.names.iter().map(|s| hir::PathSegment {
4449 ident: ast::Ident::from_str(&s),
4456 // End of code copied from rust-clippy
4459 #[derive(Eq, PartialEq, Hash, Copy, Clone, Debug)]
4460 enum RegionTarget<'tcx> {
4461 Region(Region<'tcx>),
4462 RegionVid(RegionVid)
4465 #[derive(Default, Debug, Clone)]
4466 struct RegionDeps<'tcx> {
4467 larger: FxHashSet<RegionTarget<'tcx>>,
4468 smaller: FxHashSet<RegionTarget<'tcx>>
4471 #[derive(Eq, PartialEq, Hash, Debug)]
4473 TraitBound(Vec<PathSegment>, Vec<SimpleBound>, Vec<GenericParamDef>, hir::TraitBoundModifier),
4477 enum AutoTraitResult {
4479 PositiveImpl(Generics),
4483 impl AutoTraitResult {
4484 fn is_auto(&self) -> bool {
4486 AutoTraitResult::PositiveImpl(_) | AutoTraitResult::NegativeImpl => true,
4492 impl From<GenericBound> for SimpleBound {
4493 fn from(bound: GenericBound) -> Self {
4494 match bound.clone() {
4495 GenericBound::Outlives(l) => SimpleBound::Outlives(l),
4496 GenericBound::TraitBound(t, mod_) => match t.trait_ {
4497 Type::ResolvedPath { path, typarams, .. } => {
4498 SimpleBound::TraitBound(path.segments,
4500 .map_or_else(|| Vec::new(), |v| v.iter()
4501 .map(|p| SimpleBound::from(p.clone()))
4506 _ => panic!("Unexpected bound {:?}", bound),