1 // ignore-tidy-filelength
3 //! This module contains the "cleaned" pieces of the AST, and the functions
12 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
13 use rustc_target::spec::abi::Abi;
14 use rustc_typeck::hir_ty_to_ty;
15 use rustc::infer::region_constraints::{RegionConstraintData, Constraint};
16 use rustc::middle::resolve_lifetime as rl;
17 use rustc::middle::lang_items;
18 use rustc::middle::stability;
19 use rustc::mir::interpret::{GlobalId, ConstValue};
20 use rustc::hir::{self, HirVec};
21 use rustc::hir::def::{self, Res, DefKind, CtorKind};
22 use rustc::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
23 use rustc::ty::subst::{InternalSubsts, SubstsRef, UnpackedKind};
24 use rustc::ty::{self, DefIdTree, TyCtxt, Region, RegionVid, Ty, AdtKind};
25 use rustc::ty::fold::TypeFolder;
26 use rustc::ty::layout::VariantIdx;
27 use rustc::util::nodemap::{FxHashMap, FxHashSet};
28 use syntax::ast::{self, AttrStyle, Ident};
30 use syntax::ext::base::MacroKind;
31 use syntax::source_map::{dummy_spanned, Spanned};
33 use syntax::symbol::{Symbol, kw, sym};
34 use syntax::symbol::InternedString;
35 use syntax_pos::{self, Pos, FileName};
37 use std::collections::hash_map::Entry;
39 use std::hash::{Hash, Hasher};
40 use std::default::Default;
41 use std::{mem, slice, vec};
42 use std::iter::{FromIterator, once};
44 use std::cell::RefCell;
48 use parking_lot::ReentrantMutex;
50 use crate::core::{self, DocContext};
53 use crate::html::render::{cache, ExternalLocation};
54 use crate::html::item_type::ItemType;
58 use self::auto_trait::AutoTraitFinder;
59 use self::blanket_impl::BlanketImplFinder;
61 pub use self::Type::*;
62 pub use self::Mutability::*;
63 pub use self::ItemEnum::*;
64 pub use self::SelfTy::*;
65 pub use self::FunctionRetTy::*;
66 pub use self::Visibility::{Public, Inherited};
68 thread_local!(pub static MAX_DEF_ID: RefCell<FxHashMap<CrateNum, DefId>> = Default::default());
70 const FN_OUTPUT_NAME: &'static str = "Output";
72 // extract the stability index for a node from tcx, if possible
73 fn get_stability(cx: &DocContext<'_>, def_id: DefId) -> Option<Stability> {
74 cx.tcx.lookup_stability(def_id).clean(cx)
77 fn get_deprecation(cx: &DocContext<'_>, def_id: DefId) -> Option<Deprecation> {
78 cx.tcx.lookup_deprecation(def_id).clean(cx)
82 fn clean(&self, cx: &DocContext<'_>) -> T;
85 impl<T: Clean<U>, U> Clean<Vec<U>> for [T] {
86 fn clean(&self, cx: &DocContext<'_>) -> Vec<U> {
87 self.iter().map(|x| x.clean(cx)).collect()
91 impl<T: Clean<U>, U, V: Idx> Clean<IndexVec<V, U>> for IndexVec<V, T> {
92 fn clean(&self, cx: &DocContext<'_>) -> IndexVec<V, U> {
93 self.iter().map(|x| x.clean(cx)).collect()
97 impl<T: Clean<U>, U> Clean<U> for P<T> {
98 fn clean(&self, cx: &DocContext<'_>) -> U {
103 impl<T: Clean<U>, U> Clean<U> for Rc<T> {
104 fn clean(&self, cx: &DocContext<'_>) -> U {
109 impl<T: Clean<U>, U> Clean<Option<U>> for Option<T> {
110 fn clean(&self, cx: &DocContext<'_>) -> Option<U> {
111 self.as_ref().map(|v| v.clean(cx))
115 impl<T, U> Clean<U> for ty::Binder<T> where T: Clean<U> {
116 fn clean(&self, cx: &DocContext<'_>) -> U {
117 self.skip_binder().clean(cx)
121 impl<T: Clean<U>, U> Clean<Vec<U>> for P<[T]> {
122 fn clean(&self, cx: &DocContext<'_>) -> Vec<U> {
123 self.iter().map(|x| x.clean(cx)).collect()
127 #[derive(Clone, Debug)]
130 pub version: Option<String>,
132 pub module: Option<Item>,
133 pub externs: Vec<(CrateNum, ExternalCrate)>,
134 pub primitives: Vec<(DefId, PrimitiveType, Attributes)>,
135 // These are later on moved into `CACHEKEY`, leaving the map empty.
136 // Only here so that they can be filtered through the rustdoc passes.
137 pub external_traits: Arc<ReentrantMutex<RefCell<FxHashMap<DefId, Trait>>>>,
138 pub masked_crates: FxHashSet<CrateNum>,
141 impl<'a, 'tcx> Clean<Crate> for visit_ast::RustdocVisitor<'a, 'tcx> {
142 fn clean(&self, cx: &DocContext<'_>) -> Crate {
143 use crate::visit_lib::LibEmbargoVisitor;
146 let mut r = cx.renderinfo.borrow_mut();
147 r.deref_trait_did = cx.tcx.lang_items().deref_trait();
148 r.deref_mut_trait_did = cx.tcx.lang_items().deref_mut_trait();
149 r.owned_box_did = cx.tcx.lang_items().owned_box();
152 let mut externs = Vec::new();
153 for &cnum in cx.tcx.crates().iter() {
154 externs.push((cnum, cnum.clean(cx)));
155 // Analyze doc-reachability for extern items
156 LibEmbargoVisitor::new(cx).visit_lib(cnum);
158 externs.sort_by(|&(a, _), &(b, _)| a.cmp(&b));
160 // Clean the crate, translating the entire libsyntax AST to one that is
161 // understood by rustdoc.
162 let mut module = self.module.clean(cx);
163 let mut masked_crates = FxHashSet::default();
166 ModuleItem(ref module) => {
167 for it in &module.items {
168 // `compiler_builtins` should be masked too, but we can't apply
169 // `#[doc(masked)]` to the injected `extern crate` because it's unstable.
170 if it.is_extern_crate()
171 && (it.attrs.has_doc_flag(sym::masked)
172 || self.cx.tcx.is_compiler_builtins(it.def_id.krate))
174 masked_crates.insert(it.def_id.krate);
181 let ExternalCrate { name, src, primitives, keywords, .. } = LOCAL_CRATE.clean(cx);
183 let m = match module.inner {
184 ModuleItem(ref mut m) => m,
187 m.items.extend(primitives.iter().map(|&(def_id, prim, ref attrs)| {
189 source: Span::empty(),
190 name: Some(prim.to_url_str().to_string()),
191 attrs: attrs.clone(),
192 visibility: Some(Public),
193 stability: get_stability(cx, def_id),
194 deprecation: get_deprecation(cx, def_id),
196 inner: PrimitiveItem(prim),
199 m.items.extend(keywords.into_iter().map(|(def_id, kw, attrs)| {
201 source: Span::empty(),
202 name: Some(kw.clone()),
204 visibility: Some(Public),
205 stability: get_stability(cx, def_id),
206 deprecation: get_deprecation(cx, def_id),
208 inner: KeywordItem(kw),
217 module: Some(module),
220 external_traits: cx.external_traits.clone(),
226 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
227 pub struct ExternalCrate {
230 pub attrs: Attributes,
231 pub primitives: Vec<(DefId, PrimitiveType, Attributes)>,
232 pub keywords: Vec<(DefId, String, Attributes)>,
235 impl Clean<ExternalCrate> for CrateNum {
236 fn clean(&self, cx: &DocContext<'_>) -> ExternalCrate {
237 let root = DefId { krate: *self, index: CRATE_DEF_INDEX };
238 let krate_span = cx.tcx.def_span(root);
239 let krate_src = cx.sess().source_map().span_to_filename(krate_span);
241 // Collect all inner modules which are tagged as implementations of
244 // Note that this loop only searches the top-level items of the crate,
245 // and this is intentional. If we were to search the entire crate for an
246 // item tagged with `#[doc(primitive)]` then we would also have to
247 // search the entirety of external modules for items tagged
248 // `#[doc(primitive)]`, which is a pretty inefficient process (decoding
249 // all that metadata unconditionally).
251 // In order to keep the metadata load under control, the
252 // `#[doc(primitive)]` feature is explicitly designed to only allow the
253 // primitive tags to show up as the top level items in a crate.
255 // Also note that this does not attempt to deal with modules tagged
256 // duplicately for the same primitive. This is handled later on when
257 // rendering by delegating everything to a hash map.
258 let as_primitive = |res: Res| {
259 if let Res::Def(DefKind::Mod, def_id) = res {
260 let attrs = cx.tcx.get_attrs(def_id).clean(cx);
262 for attr in attrs.lists(sym::doc) {
263 if let Some(v) = attr.value_str() {
264 if attr.check_name(sym::primitive) {
265 prim = PrimitiveType::from_str(&v.as_str());
269 // FIXME: should warn on unknown primitives?
273 return prim.map(|p| (def_id, p, attrs));
277 let primitives = if root.is_local() {
278 cx.tcx.hir().krate().module.item_ids.iter().filter_map(|&id| {
279 let item = cx.tcx.hir().expect_item_by_hir_id(id.id);
281 hir::ItemKind::Mod(_) => {
282 as_primitive(Res::Def(
284 cx.tcx.hir().local_def_id_from_hir_id(id.id),
287 hir::ItemKind::Use(ref path, hir::UseKind::Single)
288 if item.vis.node.is_pub() => {
289 as_primitive(path.res).map(|(_, prim, attrs)| {
290 // Pretend the primitive is local.
291 (cx.tcx.hir().local_def_id_from_hir_id(id.id), prim, attrs)
298 cx.tcx.item_children(root).iter().map(|item| item.res)
299 .filter_map(as_primitive).collect()
302 let as_keyword = |res: Res| {
303 if let Res::Def(DefKind::Mod, def_id) = res {
304 let attrs = cx.tcx.get_attrs(def_id).clean(cx);
305 let mut keyword = None;
306 for attr in attrs.lists(sym::doc) {
307 if let Some(v) = attr.value_str() {
308 if attr.check_name(sym::keyword) {
309 if v.is_doc_keyword() {
310 keyword = Some(v.to_string());
313 // FIXME: should warn on unknown keywords?
317 return keyword.map(|p| (def_id, p, attrs));
321 let keywords = if root.is_local() {
322 cx.tcx.hir().krate().module.item_ids.iter().filter_map(|&id| {
323 let item = cx.tcx.hir().expect_item_by_hir_id(id.id);
325 hir::ItemKind::Mod(_) => {
328 cx.tcx.hir().local_def_id_from_hir_id(id.id),
331 hir::ItemKind::Use(ref path, hir::UseKind::Single)
332 if item.vis.node.is_pub() => {
333 as_keyword(path.res).map(|(_, prim, attrs)| {
334 (cx.tcx.hir().local_def_id_from_hir_id(id.id), prim, attrs)
341 cx.tcx.item_children(root).iter().map(|item| item.res)
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: &dyn 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_variant(&self) -> bool {
428 self.type_() == ItemType::Variant
430 pub fn is_associated_type(&self) -> bool {
431 self.type_() == ItemType::AssocType
433 pub fn is_associated_const(&self) -> bool {
434 self.type_() == ItemType::AssocConst
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.deprecation.is_some() {
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 pub fn is_non_exhaustive(&self) -> bool {
500 self.attrs.other_attrs.iter()
501 .any(|a| a.check_name(sym::non_exhaustive))
504 /// Returns a documentation-level item type from the item.
505 pub fn type_(&self) -> ItemType {
509 /// Returns the info in the item's `#[deprecated]` or `#[rustc_deprecated]` attributes.
511 /// If the item is not deprecated, returns `None`.
512 pub fn deprecation(&self) -> Option<&Deprecation> {
515 .or_else(|| self.stability.as_ref().and_then(|s| s.deprecation.as_ref()))
517 pub fn is_default(&self) -> bool {
519 ItemEnum::MethodItem(ref meth) => {
520 if let Some(defaultness) = meth.defaultness {
521 defaultness.has_value() && !defaultness.is_final()
531 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
533 ExternCrateItem(String, Option<String>),
538 FunctionItem(Function),
540 TypedefItem(Typedef, bool /* is associated type */),
541 ExistentialItem(Existential, bool /* is associated type */),
543 ConstantItem(Constant),
545 TraitAliasItem(TraitAlias),
547 /// A method signature only. Used for required methods in traits (ie,
548 /// non-default-methods).
549 TyMethodItem(TyMethod),
550 /// A method with a body.
552 StructFieldItem(Type),
553 VariantItem(Variant),
554 /// `fn`s from an extern block
555 ForeignFunctionItem(Function),
556 /// `static`s from an extern block
557 ForeignStaticItem(Static),
558 /// `type`s from an extern block
561 ProcMacroItem(ProcMacro),
562 PrimitiveItem(PrimitiveType),
563 AssocConstItem(Type, Option<String>),
564 AssocTypeItem(Vec<GenericBound>, Option<Type>),
565 /// An item that has been stripped by a rustdoc pass
566 StrippedItem(Box<ItemEnum>),
571 pub fn generics(&self) -> Option<&Generics> {
573 ItemEnum::StructItem(ref s) => &s.generics,
574 ItemEnum::EnumItem(ref e) => &e.generics,
575 ItemEnum::FunctionItem(ref f) => &f.generics,
576 ItemEnum::TypedefItem(ref t, _) => &t.generics,
577 ItemEnum::ExistentialItem(ref t, _) => &t.generics,
578 ItemEnum::TraitItem(ref t) => &t.generics,
579 ItemEnum::ImplItem(ref i) => &i.generics,
580 ItemEnum::TyMethodItem(ref i) => &i.generics,
581 ItemEnum::MethodItem(ref i) => &i.generics,
582 ItemEnum::ForeignFunctionItem(ref f) => &f.generics,
583 ItemEnum::TraitAliasItem(ref ta) => &ta.generics,
588 pub fn is_associated(&self) -> bool {
590 ItemEnum::TypedefItem(_, _) |
591 ItemEnum::AssocTypeItem(_, _) => true,
597 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
599 pub items: Vec<Item>,
603 impl Clean<Item> for doctree::Module {
604 fn clean(&self, cx: &DocContext<'_>) -> Item {
605 let name = if self.name.is_some() {
606 self.name.expect("No name provided").clean(cx)
611 // maintain a stack of mod ids, for doc comment path resolution
612 // but we also need to resolve the module's own docs based on whether its docs were written
613 // inside or outside the module, so check for that
614 let attrs = self.attrs.clean(cx);
616 let mut items: Vec<Item> = vec![];
617 items.extend(self.extern_crates.iter().flat_map(|x| x.clean(cx)));
618 items.extend(self.imports.iter().flat_map(|x| x.clean(cx)));
619 items.extend(self.structs.iter().map(|x| x.clean(cx)));
620 items.extend(self.unions.iter().map(|x| x.clean(cx)));
621 items.extend(self.enums.iter().map(|x| x.clean(cx)));
622 items.extend(self.fns.iter().map(|x| x.clean(cx)));
623 items.extend(self.foreigns.iter().flat_map(|x| x.clean(cx)));
624 items.extend(self.mods.iter().map(|x| x.clean(cx)));
625 items.extend(self.typedefs.iter().map(|x| x.clean(cx)));
626 items.extend(self.existentials.iter().map(|x| x.clean(cx)));
627 items.extend(self.statics.iter().map(|x| x.clean(cx)));
628 items.extend(self.constants.iter().map(|x| x.clean(cx)));
629 items.extend(self.traits.iter().map(|x| x.clean(cx)));
630 items.extend(self.impls.iter().flat_map(|x| x.clean(cx)));
631 items.extend(self.macros.iter().map(|x| x.clean(cx)));
632 items.extend(self.proc_macros.iter().map(|x| x.clean(cx)));
633 items.extend(self.trait_aliases.iter().map(|x| x.clean(cx)));
635 // determine if we should display the inner contents or
636 // the outer `mod` item for the source code.
638 let cm = cx.sess().source_map();
639 let outer = cm.lookup_char_pos(self.where_outer.lo());
640 let inner = cm.lookup_char_pos(self.where_inner.lo());
641 if outer.file.start_pos == inner.file.start_pos {
645 // mod foo; (and a separate SourceFile for the contents)
653 source: whence.clean(cx),
654 visibility: self.vis.clean(cx),
655 stability: self.stab.clean(cx),
656 deprecation: self.depr.clean(cx),
657 def_id: cx.tcx.hir().local_def_id(self.id),
658 inner: ModuleItem(Module {
659 is_crate: self.is_crate,
666 pub struct ListAttributesIter<'a> {
667 attrs: slice::Iter<'a, ast::Attribute>,
668 current_list: vec::IntoIter<ast::NestedMetaItem>,
672 impl<'a> Iterator for ListAttributesIter<'a> {
673 type Item = ast::NestedMetaItem;
675 fn next(&mut self) -> Option<Self::Item> {
676 if let Some(nested) = self.current_list.next() {
680 for attr in &mut self.attrs {
681 if let Some(list) = attr.meta_item_list() {
682 if attr.check_name(self.name) {
683 self.current_list = list.into_iter();
684 if let Some(nested) = self.current_list.next() {
694 fn size_hint(&self) -> (usize, Option<usize>) {
695 let lower = self.current_list.len();
700 pub trait AttributesExt {
701 /// Finds an attribute as List and returns the list of attributes nested inside.
702 fn lists<'a>(&'a self, name: Symbol) -> ListAttributesIter<'a>;
705 impl AttributesExt for [ast::Attribute] {
706 fn lists<'a>(&'a self, name: Symbol) -> ListAttributesIter<'a> {
709 current_list: Vec::new().into_iter(),
715 pub trait NestedAttributesExt {
716 /// Returns `true` if the attribute list contains a specific `Word`
717 fn has_word(self, word: Symbol) -> bool;
720 impl<I: IntoIterator<Item=ast::NestedMetaItem>> NestedAttributesExt for I {
721 fn has_word(self, word: Symbol) -> bool {
722 self.into_iter().any(|attr| attr.is_word() && attr.check_name(word))
726 /// A portion of documentation, extracted from a `#[doc]` attribute.
728 /// Each variant contains the line number within the complete doc-comment where the fragment
729 /// starts, as well as the Span where the corresponding doc comment or attribute is located.
731 /// Included files are kept separate from inline doc comments so that proper line-number
732 /// information can be given when a doctest fails. Sugared doc comments and "raw" doc comments are
733 /// kept separate because of issue #42760.
734 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
735 pub enum DocFragment {
736 /// A doc fragment created from a `///` or `//!` doc comment.
737 SugaredDoc(usize, syntax_pos::Span, String),
738 /// A doc fragment created from a "raw" `#[doc=""]` attribute.
739 RawDoc(usize, syntax_pos::Span, String),
740 /// A doc fragment created from a `#[doc(include="filename")]` attribute. Contains both the
741 /// given filename and the file contents.
742 Include(usize, syntax_pos::Span, String, String),
746 pub fn as_str(&self) -> &str {
748 DocFragment::SugaredDoc(_, _, ref s) => &s[..],
749 DocFragment::RawDoc(_, _, ref s) => &s[..],
750 DocFragment::Include(_, _, _, ref s) => &s[..],
754 pub fn span(&self) -> syntax_pos::Span {
756 DocFragment::SugaredDoc(_, span, _) |
757 DocFragment::RawDoc(_, span, _) |
758 DocFragment::Include(_, span, _, _) => span,
763 impl<'a> FromIterator<&'a DocFragment> for String {
764 fn from_iter<T>(iter: T) -> Self
766 T: IntoIterator<Item = &'a DocFragment>
768 iter.into_iter().fold(String::new(), |mut acc, frag| {
773 DocFragment::SugaredDoc(_, _, ref docs)
774 | DocFragment::RawDoc(_, _, ref docs)
775 | DocFragment::Include(_, _, _, ref docs) =>
784 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, Default)]
785 pub struct Attributes {
786 pub doc_strings: Vec<DocFragment>,
787 pub other_attrs: Vec<ast::Attribute>,
788 pub cfg: Option<Arc<Cfg>>,
789 pub span: Option<syntax_pos::Span>,
790 /// map from Rust paths to resolved defs and potential URL fragments
791 pub links: Vec<(String, Option<DefId>, Option<String>)>,
792 pub inner_docs: bool,
796 /// Extracts the content from an attribute `#[doc(cfg(content))]`.
797 fn extract_cfg(mi: &ast::MetaItem) -> Option<&ast::MetaItem> {
798 use syntax::ast::NestedMetaItem::MetaItem;
800 if let ast::MetaItemKind::List(ref nmis) = mi.node {
802 if let MetaItem(ref cfg_mi) = nmis[0] {
803 if cfg_mi.check_name(sym::cfg) {
804 if let ast::MetaItemKind::List(ref cfg_nmis) = cfg_mi.node {
805 if cfg_nmis.len() == 1 {
806 if let MetaItem(ref content_mi) = cfg_nmis[0] {
807 return Some(content_mi);
819 /// Reads a `MetaItem` from within an attribute, looks for whether it is a
820 /// `#[doc(include="file")]`, and returns the filename and contents of the file as loaded from
822 fn extract_include(mi: &ast::MetaItem)
823 -> Option<(String, String)>
825 mi.meta_item_list().and_then(|list| {
827 if meta.check_name(sym::include) {
828 // the actual compiled `#[doc(include="filename")]` gets expanded to
829 // `#[doc(include(file="filename", contents="file contents")]` so we need to
830 // look for that instead
831 return meta.meta_item_list().and_then(|list| {
832 let mut filename: Option<String> = None;
833 let mut contents: Option<String> = None;
836 if it.check_name(sym::file) {
837 if let Some(name) = it.value_str() {
838 filename = Some(name.to_string());
840 } else if it.check_name(sym::contents) {
841 if let Some(docs) = it.value_str() {
842 contents = Some(docs.to_string());
847 if let (Some(filename), Some(contents)) = (filename, contents) {
848 Some((filename, contents))
860 pub fn has_doc_flag(&self, flag: Symbol) -> bool {
861 for attr in &self.other_attrs {
862 if !attr.check_name(sym::doc) { continue; }
864 if let Some(items) = attr.meta_item_list() {
865 if items.iter().filter_map(|i| i.meta_item()).any(|it| it.check_name(flag)) {
874 pub fn from_ast(diagnostic: &::errors::Handler,
875 attrs: &[ast::Attribute]) -> Attributes {
876 let mut doc_strings = vec![];
878 let mut cfg = Cfg::True;
879 let mut doc_line = 0;
881 let other_attrs = attrs.iter().filter_map(|attr| {
882 attr.with_desugared_doc(|attr| {
883 if attr.check_name(sym::doc) {
884 if let Some(mi) = attr.meta() {
885 if let Some(value) = mi.value_str() {
886 // Extracted #[doc = "..."]
887 let value = value.to_string();
889 doc_line += value.lines().count();
891 if attr.is_sugared_doc {
892 doc_strings.push(DocFragment::SugaredDoc(line, attr.span, value));
894 doc_strings.push(DocFragment::RawDoc(line, attr.span, value));
898 sp = Some(attr.span);
901 } else if let Some(cfg_mi) = Attributes::extract_cfg(&mi) {
902 // Extracted #[doc(cfg(...))]
903 match Cfg::parse(cfg_mi) {
904 Ok(new_cfg) => cfg &= new_cfg,
905 Err(e) => diagnostic.span_err(e.span, e.msg),
908 } else if let Some((filename, contents)) = Attributes::extract_include(&mi)
911 doc_line += contents.lines().count();
912 doc_strings.push(DocFragment::Include(line,
923 // treat #[target_feature(enable = "feat")] attributes as if they were
924 // #[doc(cfg(target_feature = "feat"))] attributes as well
925 for attr in attrs.lists(sym::target_feature) {
926 if attr.check_name(sym::enable) {
927 if let Some(feat) = attr.value_str() {
928 let meta = attr::mk_name_value_item_str(
929 Ident::with_empty_ctxt(sym::target_feature),
930 dummy_spanned(feat));
931 if let Ok(feat_cfg) = Cfg::parse(&meta) {
938 let inner_docs = attrs.iter()
939 .filter(|a| a.check_name(sym::doc))
941 .map_or(true, |a| a.style == AttrStyle::Inner);
946 cfg: if cfg == Cfg::True { None } else { Some(Arc::new(cfg)) },
953 /// Finds the `doc` attribute as a NameValue and returns the corresponding
955 pub fn doc_value<'a>(&'a self) -> Option<&'a str> {
956 self.doc_strings.first().map(|s| s.as_str())
959 /// Finds all `doc` attributes as NameValues and returns their corresponding values, joined
961 pub fn collapsed_doc_value(&self) -> Option<String> {
962 if !self.doc_strings.is_empty() {
963 Some(self.doc_strings.iter().collect())
969 /// Gets links as a vector
971 /// Cache must be populated before call
972 pub fn links(&self, krate: &CrateNum) -> Vec<(String, String)> {
973 use crate::html::format::href;
975 self.links.iter().filter_map(|&(ref s, did, ref fragment)| {
978 if let Some((mut href, ..)) = href(did) {
979 if let Some(ref fragment) = *fragment {
981 href.push_str(fragment);
983 Some((s.clone(), href))
989 if let Some(ref fragment) = *fragment {
991 let url = match cache.extern_locations.get(krate) {
992 Some(&(_, ref src, ExternalLocation::Local)) =>
993 src.to_str().expect("invalid file path"),
994 Some(&(_, _, ExternalLocation::Remote(ref s))) => s,
995 Some(&(_, _, ExternalLocation::Unknown)) | None =>
996 "https://doc.rust-lang.org/nightly",
998 // This is a primitive so the url is done "by hand".
999 let tail = fragment.find('#').unwrap_or_else(|| fragment.len());
1001 format!("{}{}std/primitive.{}.html{}",
1003 if !url.ends_with('/') { "/" } else { "" },
1005 &fragment[tail..])))
1007 panic!("This isn't a primitive?!");
1015 impl PartialEq for Attributes {
1016 fn eq(&self, rhs: &Self) -> bool {
1017 self.doc_strings == rhs.doc_strings &&
1018 self.cfg == rhs.cfg &&
1019 self.span == rhs.span &&
1020 self.links == rhs.links &&
1021 self.other_attrs.iter().map(|attr| attr.id).eq(rhs.other_attrs.iter().map(|attr| attr.id))
1025 impl Eq for Attributes {}
1027 impl Hash for Attributes {
1028 fn hash<H: Hasher>(&self, hasher: &mut H) {
1029 self.doc_strings.hash(hasher);
1030 self.cfg.hash(hasher);
1031 self.span.hash(hasher);
1032 self.links.hash(hasher);
1033 for attr in &self.other_attrs {
1034 attr.id.hash(hasher);
1039 impl AttributesExt for Attributes {
1040 fn lists<'a>(&'a self, name: Symbol) -> ListAttributesIter<'a> {
1041 self.other_attrs.lists(name)
1045 impl Clean<Attributes> for [ast::Attribute] {
1046 fn clean(&self, cx: &DocContext<'_>) -> Attributes {
1047 Attributes::from_ast(cx.sess().diagnostic(), self)
1051 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1052 pub enum GenericBound {
1053 TraitBound(PolyTrait, hir::TraitBoundModifier),
1058 fn maybe_sized(cx: &DocContext<'_>) -> GenericBound {
1059 let did = cx.tcx.require_lang_item(lang_items::SizedTraitLangItem);
1060 let empty = cx.tcx.intern_substs(&[]);
1061 let path = external_path(cx, &cx.tcx.item_name(did).as_str(),
1062 Some(did), false, vec![], empty);
1063 inline::record_extern_fqn(cx, did, TypeKind::Trait);
1064 GenericBound::TraitBound(PolyTrait {
1065 trait_: ResolvedPath {
1071 generic_params: Vec::new(),
1072 }, hir::TraitBoundModifier::Maybe)
1075 fn is_sized_bound(&self, cx: &DocContext<'_>) -> bool {
1076 use rustc::hir::TraitBoundModifier as TBM;
1077 if let GenericBound::TraitBound(PolyTrait { ref trait_, .. }, TBM::None) = *self {
1078 if trait_.def_id() == cx.tcx.lang_items().sized_trait() {
1085 fn get_poly_trait(&self) -> Option<PolyTrait> {
1086 if let GenericBound::TraitBound(ref p, _) = *self {
1087 return Some(p.clone())
1092 fn get_trait_type(&self) -> Option<Type> {
1093 if let GenericBound::TraitBound(PolyTrait { ref trait_, .. }, _) = *self {
1094 Some(trait_.clone())
1101 impl Clean<GenericBound> for hir::GenericBound {
1102 fn clean(&self, cx: &DocContext<'_>) -> GenericBound {
1104 hir::GenericBound::Outlives(lt) => GenericBound::Outlives(lt.clean(cx)),
1105 hir::GenericBound::Trait(ref t, modifier) => {
1106 GenericBound::TraitBound(t.clean(cx), modifier)
1112 fn external_generic_args(
1113 cx: &DocContext<'_>,
1114 trait_did: Option<DefId>,
1116 bindings: Vec<TypeBinding>,
1117 substs: SubstsRef<'_>,
1119 let mut skip_self = has_self;
1120 let mut ty_sty = None;
1121 let args: Vec<_> = substs.iter().filter_map(|kind| match kind.unpack() {
1122 UnpackedKind::Lifetime(lt) => {
1123 lt.clean(cx).and_then(|lt| Some(GenericArg::Lifetime(lt)))
1125 UnpackedKind::Type(_) if skip_self => {
1129 UnpackedKind::Type(ty) => {
1130 ty_sty = Some(&ty.sty);
1131 Some(GenericArg::Type(ty.clean(cx)))
1133 UnpackedKind::Const(ct) => Some(GenericArg::Const(ct.clean(cx))),
1137 // Attempt to sugar an external path like Fn<(A, B,), C> to Fn(A, B) -> C
1138 Some(did) if cx.tcx.lang_items().fn_trait_kind(did).is_some() => {
1139 assert!(ty_sty.is_some());
1140 let inputs = match ty_sty {
1141 Some(ty::Tuple(ref tys)) => tys.iter().map(|t| t.expect_ty().clean(cx)).collect(),
1142 _ => return GenericArgs::AngleBracketed { args, bindings },
1145 // FIXME(#20299) return type comes from a projection now
1146 // match types[1].sty {
1147 // ty::Tuple(ref v) if v.is_empty() => None, // -> ()
1148 // _ => Some(types[1].clean(cx))
1150 GenericArgs::Parenthesized { inputs, output }
1153 GenericArgs::AngleBracketed { args, bindings }
1158 // trait_did should be set to a trait's DefId if called on a TraitRef, in order to sugar
1159 // from Fn<(A, B,), C> to Fn(A, B) -> C
1160 fn external_path(cx: &DocContext<'_>, name: &str, trait_did: Option<DefId>, has_self: bool,
1161 bindings: Vec<TypeBinding>, substs: SubstsRef<'_>) -> Path {
1165 segments: vec![PathSegment {
1166 name: name.to_string(),
1167 args: external_generic_args(cx, trait_did, has_self, bindings, substs)
1172 impl<'a, 'tcx> Clean<GenericBound> for (&'a ty::TraitRef<'tcx>, Vec<TypeBinding>) {
1173 fn clean(&self, cx: &DocContext<'_>) -> GenericBound {
1174 let (trait_ref, ref bounds) = *self;
1175 inline::record_extern_fqn(cx, trait_ref.def_id, TypeKind::Trait);
1176 let path = external_path(cx, &cx.tcx.item_name(trait_ref.def_id).as_str(),
1177 Some(trait_ref.def_id), true, bounds.clone(), trait_ref.substs);
1179 debug!("ty::TraitRef\n subst: {:?}\n", trait_ref.substs);
1181 // collect any late bound regions
1182 let mut late_bounds = vec![];
1183 for ty_s in trait_ref.input_types().skip(1) {
1184 if let ty::Tuple(ts) = ty_s.sty {
1186 if let ty::Ref(ref reg, _, _) = ty_s.expect_ty().sty {
1187 if let &ty::RegionKind::ReLateBound(..) = *reg {
1188 debug!(" hit an ReLateBound {:?}", reg);
1189 if let Some(Lifetime(name)) = reg.clean(cx) {
1190 late_bounds.push(GenericParamDef {
1192 kind: GenericParamDefKind::Lifetime,
1201 GenericBound::TraitBound(
1203 trait_: ResolvedPath {
1206 did: trait_ref.def_id,
1209 generic_params: late_bounds,
1211 hir::TraitBoundModifier::None
1216 impl<'tcx> Clean<GenericBound> for ty::TraitRef<'tcx> {
1217 fn clean(&self, cx: &DocContext<'_>) -> GenericBound {
1218 (self, vec![]).clean(cx)
1222 impl<'tcx> Clean<Option<Vec<GenericBound>>> for InternalSubsts<'tcx> {
1223 fn clean(&self, cx: &DocContext<'_>) -> Option<Vec<GenericBound>> {
1224 let mut v = Vec::new();
1225 v.extend(self.regions().filter_map(|r| r.clean(cx)).map(GenericBound::Outlives));
1226 v.extend(self.types().map(|t| GenericBound::TraitBound(PolyTrait {
1227 trait_: t.clean(cx),
1228 generic_params: Vec::new(),
1229 }, hir::TraitBoundModifier::None)));
1230 if !v.is_empty() {Some(v)} else {None}
1234 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1235 pub struct Lifetime(String);
1238 pub fn get_ref<'a>(&'a self) -> &'a str {
1239 let Lifetime(ref s) = *self;
1244 pub fn statik() -> Lifetime {
1245 Lifetime("'static".to_string())
1249 impl Clean<Lifetime> for hir::Lifetime {
1250 fn clean(&self, cx: &DocContext<'_>) -> Lifetime {
1251 if self.hir_id != hir::DUMMY_HIR_ID {
1252 let def = cx.tcx.named_region(self.hir_id);
1254 Some(rl::Region::EarlyBound(_, node_id, _)) |
1255 Some(rl::Region::LateBound(_, node_id, _)) |
1256 Some(rl::Region::Free(_, node_id)) => {
1257 if let Some(lt) = cx.lt_substs.borrow().get(&node_id).cloned() {
1264 Lifetime(self.name.ident().to_string())
1268 impl Clean<Lifetime> for hir::GenericParam {
1269 fn clean(&self, _: &DocContext<'_>) -> Lifetime {
1271 hir::GenericParamKind::Lifetime { .. } => {
1272 if self.bounds.len() > 0 {
1273 let mut bounds = self.bounds.iter().map(|bound| match bound {
1274 hir::GenericBound::Outlives(lt) => lt,
1277 let name = bounds.next().expect("no more bounds").name.ident();
1278 let mut s = format!("{}: {}", self.name.ident(), name);
1279 for bound in bounds {
1280 s.push_str(&format!(" + {}", bound.name.ident()));
1284 Lifetime(self.name.ident().to_string())
1292 impl Clean<Constant> for hir::ConstArg {
1293 fn clean(&self, cx: &DocContext<'_>) -> Constant {
1295 type_: cx.tcx.type_of(cx.tcx.hir().body_owner_def_id(self.value.body)).clean(cx),
1296 expr: print_const_expr(cx, self.value.body),
1301 impl<'tcx> Clean<Lifetime> for ty::GenericParamDef {
1302 fn clean(&self, _cx: &DocContext<'_>) -> Lifetime {
1303 Lifetime(self.name.to_string())
1307 impl Clean<Option<Lifetime>> for ty::RegionKind {
1308 fn clean(&self, cx: &DocContext<'_>) -> Option<Lifetime> {
1310 ty::ReStatic => Some(Lifetime::statik()),
1311 ty::ReLateBound(_, ty::BrNamed(_, name)) => Some(Lifetime(name.to_string())),
1312 ty::ReEarlyBound(ref data) => Some(Lifetime(data.name.clean(cx))),
1314 ty::ReLateBound(..) |
1318 ty::RePlaceholder(..) |
1320 ty::ReClosureBound(_) |
1322 debug!("Cannot clean region {:?}", self);
1329 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1330 pub enum WherePredicate {
1331 BoundPredicate { ty: Type, bounds: Vec<GenericBound> },
1332 RegionPredicate { lifetime: Lifetime, bounds: Vec<GenericBound> },
1333 EqPredicate { lhs: Type, rhs: Type },
1336 impl WherePredicate {
1337 pub fn get_bounds(&self) -> Option<&[GenericBound]> {
1339 WherePredicate::BoundPredicate { ref bounds, .. } => Some(bounds),
1340 WherePredicate::RegionPredicate { ref bounds, .. } => Some(bounds),
1346 impl Clean<WherePredicate> for hir::WherePredicate {
1347 fn clean(&self, cx: &DocContext<'_>) -> WherePredicate {
1349 hir::WherePredicate::BoundPredicate(ref wbp) => {
1350 WherePredicate::BoundPredicate {
1351 ty: wbp.bounded_ty.clean(cx),
1352 bounds: wbp.bounds.clean(cx)
1356 hir::WherePredicate::RegionPredicate(ref wrp) => {
1357 WherePredicate::RegionPredicate {
1358 lifetime: wrp.lifetime.clean(cx),
1359 bounds: wrp.bounds.clean(cx)
1363 hir::WherePredicate::EqPredicate(ref wrp) => {
1364 WherePredicate::EqPredicate {
1365 lhs: wrp.lhs_ty.clean(cx),
1366 rhs: wrp.rhs_ty.clean(cx)
1373 impl<'a> Clean<Option<WherePredicate>> for ty::Predicate<'a> {
1374 fn clean(&self, cx: &DocContext<'_>) -> Option<WherePredicate> {
1375 use rustc::ty::Predicate;
1378 Predicate::Trait(ref pred) => Some(pred.clean(cx)),
1379 Predicate::Subtype(ref pred) => Some(pred.clean(cx)),
1380 Predicate::RegionOutlives(ref pred) => pred.clean(cx),
1381 Predicate::TypeOutlives(ref pred) => pred.clean(cx),
1382 Predicate::Projection(ref pred) => Some(pred.clean(cx)),
1384 Predicate::WellFormed(..) |
1385 Predicate::ObjectSafe(..) |
1386 Predicate::ClosureKind(..) |
1387 Predicate::ConstEvaluatable(..) => panic!("not user writable"),
1392 impl<'a> Clean<WherePredicate> for ty::TraitPredicate<'a> {
1393 fn clean(&self, cx: &DocContext<'_>) -> WherePredicate {
1394 WherePredicate::BoundPredicate {
1395 ty: self.trait_ref.self_ty().clean(cx),
1396 bounds: vec![self.trait_ref.clean(cx)]
1401 impl<'tcx> Clean<WherePredicate> for ty::SubtypePredicate<'tcx> {
1402 fn clean(&self, _cx: &DocContext<'_>) -> WherePredicate {
1403 panic!("subtype predicates are an internal rustc artifact \
1404 and should not be seen by rustdoc")
1408 impl<'tcx> Clean<Option<WherePredicate>> for
1409 ty::OutlivesPredicate<ty::Region<'tcx>,ty::Region<'tcx>> {
1411 fn clean(&self, cx: &DocContext<'_>) -> Option<WherePredicate> {
1412 let ty::OutlivesPredicate(ref a, ref b) = *self;
1415 (ty::ReEmpty, ty::ReEmpty) => {
1421 Some(WherePredicate::RegionPredicate {
1422 lifetime: a.clean(cx).expect("failed to clean lifetime"),
1423 bounds: vec![GenericBound::Outlives(b.clean(cx).expect("failed to clean bounds"))]
1428 impl<'tcx> Clean<Option<WherePredicate>> for ty::OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>> {
1429 fn clean(&self, cx: &DocContext<'_>) -> Option<WherePredicate> {
1430 let ty::OutlivesPredicate(ref ty, ref lt) = *self;
1433 ty::ReEmpty => return None,
1437 Some(WherePredicate::BoundPredicate {
1439 bounds: vec![GenericBound::Outlives(lt.clean(cx).expect("failed to clean lifetimes"))]
1444 impl<'tcx> Clean<WherePredicate> for ty::ProjectionPredicate<'tcx> {
1445 fn clean(&self, cx: &DocContext<'_>) -> WherePredicate {
1446 WherePredicate::EqPredicate {
1447 lhs: self.projection_ty.clean(cx),
1448 rhs: self.ty.clean(cx)
1453 impl<'tcx> Clean<Type> for ty::ProjectionTy<'tcx> {
1454 fn clean(&self, cx: &DocContext<'_>) -> Type {
1455 let trait_ = match self.trait_ref(cx.tcx).clean(cx) {
1456 GenericBound::TraitBound(t, _) => t.trait_,
1457 GenericBound::Outlives(_) => panic!("cleaning a trait got a lifetime"),
1460 name: cx.tcx.associated_item(self.item_def_id).ident.name.clean(cx),
1461 self_type: box self.self_ty().clean(cx),
1467 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1468 pub enum GenericParamDefKind {
1472 bounds: Vec<GenericBound>,
1473 default: Option<Type>,
1474 synthetic: Option<hir::SyntheticTyParamKind>,
1482 impl GenericParamDefKind {
1483 pub fn is_type(&self) -> bool {
1485 GenericParamDefKind::Type { .. } => true,
1490 pub fn get_type(&self, cx: &DocContext<'_>) -> Option<Type> {
1492 GenericParamDefKind::Type { did, .. } => {
1493 rustc_typeck::checked_type_of(cx.tcx, did, false).map(|t| t.clean(cx))
1495 GenericParamDefKind::Const { ref ty, .. } => Some(ty.clone()),
1496 GenericParamDefKind::Lifetime => None,
1501 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1502 pub struct GenericParamDef {
1505 pub kind: GenericParamDefKind,
1508 impl GenericParamDef {
1509 pub fn is_synthetic_type_param(&self) -> bool {
1511 GenericParamDefKind::Lifetime |
1512 GenericParamDefKind::Const { .. } => false,
1513 GenericParamDefKind::Type { ref synthetic, .. } => synthetic.is_some(),
1517 pub fn is_type(&self) -> bool {
1521 pub fn get_type(&self, cx: &DocContext<'_>) -> Option<Type> {
1522 self.kind.get_type(cx)
1525 pub fn get_bounds(&self) -> Option<&[GenericBound]> {
1527 GenericParamDefKind::Type { ref bounds, .. } => Some(bounds),
1533 impl Clean<GenericParamDef> for ty::GenericParamDef {
1534 fn clean(&self, cx: &DocContext<'_>) -> GenericParamDef {
1535 let (name, kind) = match self.kind {
1536 ty::GenericParamDefKind::Lifetime => {
1537 (self.name.to_string(), GenericParamDefKind::Lifetime)
1539 ty::GenericParamDefKind::Type { has_default, .. } => {
1540 cx.renderinfo.borrow_mut().external_param_names
1541 .insert(self.def_id, self.name.clean(cx));
1542 let default = if has_default {
1543 Some(cx.tcx.type_of(self.def_id).clean(cx))
1547 (self.name.clean(cx), GenericParamDefKind::Type {
1549 bounds: vec![], // These are filled in from the where-clauses.
1554 ty::GenericParamDefKind::Const { .. } => {
1555 (self.name.clean(cx), GenericParamDefKind::Const {
1557 ty: cx.tcx.type_of(self.def_id).clean(cx),
1569 impl Clean<GenericParamDef> for hir::GenericParam {
1570 fn clean(&self, cx: &DocContext<'_>) -> GenericParamDef {
1571 let (name, kind) = match self.kind {
1572 hir::GenericParamKind::Lifetime { .. } => {
1573 let name = if self.bounds.len() > 0 {
1574 let mut bounds = self.bounds.iter().map(|bound| match bound {
1575 hir::GenericBound::Outlives(lt) => lt,
1578 let name = bounds.next().expect("no more bounds").name.ident();
1579 let mut s = format!("{}: {}", self.name.ident(), name);
1580 for bound in bounds {
1581 s.push_str(&format!(" + {}", bound.name.ident()));
1585 self.name.ident().to_string()
1587 (name, GenericParamDefKind::Lifetime)
1589 hir::GenericParamKind::Type { ref default, synthetic } => {
1590 (self.name.ident().name.clean(cx), GenericParamDefKind::Type {
1591 did: cx.tcx.hir().local_def_id_from_hir_id(self.hir_id),
1592 bounds: self.bounds.clean(cx),
1593 default: default.clean(cx),
1594 synthetic: synthetic,
1597 hir::GenericParamKind::Const { ref ty } => {
1598 (self.name.ident().name.clean(cx), GenericParamDefKind::Const {
1599 did: cx.tcx.hir().local_def_id_from_hir_id(self.hir_id),
1612 // maybe use a Generic enum and use Vec<Generic>?
1613 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Default, Hash)]
1614 pub struct Generics {
1615 pub params: Vec<GenericParamDef>,
1616 pub where_predicates: Vec<WherePredicate>,
1619 impl Clean<Generics> for hir::Generics {
1620 fn clean(&self, cx: &DocContext<'_>) -> Generics {
1621 // Synthetic type-parameters are inserted after normal ones.
1622 // In order for normal parameters to be able to refer to synthetic ones,
1623 // scans them first.
1624 fn is_impl_trait(param: &hir::GenericParam) -> bool {
1626 hir::GenericParamKind::Type { synthetic, .. } => {
1627 synthetic == Some(hir::SyntheticTyParamKind::ImplTrait)
1632 let impl_trait_params = self.params
1634 .filter(|param| is_impl_trait(param))
1636 let param: GenericParamDef = param.clean(cx);
1638 GenericParamDefKind::Lifetime => unreachable!(),
1639 GenericParamDefKind::Type { did, ref bounds, .. } => {
1640 cx.impl_trait_bounds.borrow_mut().insert(did, bounds.clone());
1642 GenericParamDefKind::Const { .. } => unreachable!(),
1646 .collect::<Vec<_>>();
1648 let mut params = Vec::with_capacity(self.params.len());
1649 for p in self.params.iter().filter(|p| !is_impl_trait(p)) {
1650 let p = p.clean(cx);
1653 params.extend(impl_trait_params);
1655 let mut generics = Generics {
1657 where_predicates: self.where_clause.predicates.clean(cx),
1660 // Some duplicates are generated for ?Sized bounds between type params and where
1661 // predicates. The point in here is to move the bounds definitions from type params
1662 // to where predicates when such cases occur.
1663 for where_pred in &mut generics.where_predicates {
1665 WherePredicate::BoundPredicate { ty: Generic(ref name), ref mut bounds } => {
1666 if bounds.is_empty() {
1667 for param in &mut generics.params {
1669 GenericParamDefKind::Lifetime => {}
1670 GenericParamDefKind::Type { bounds: ref mut ty_bounds, .. } => {
1671 if ¶m.name == name {
1672 mem::swap(bounds, ty_bounds);
1676 GenericParamDefKind::Const { .. } => {}
1688 impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics,
1689 &'a &'tcx ty::GenericPredicates<'tcx>) {
1690 fn clean(&self, cx: &DocContext<'_>) -> Generics {
1691 use self::WherePredicate as WP;
1693 let (gens, preds) = *self;
1695 // Bounds in the type_params and lifetimes fields are repeated in the
1696 // predicates field (see rustc_typeck::collect::ty_generics), so remove
1698 let stripped_typarams = gens.params.iter().filter_map(|param| match param.kind {
1699 ty::GenericParamDefKind::Lifetime => None,
1700 ty::GenericParamDefKind::Type { .. } => {
1701 if param.name.as_symbol() == kw::SelfUpper {
1702 assert_eq!(param.index, 0);
1705 Some(param.clean(cx))
1707 ty::GenericParamDefKind::Const { .. } => None,
1708 }).collect::<Vec<GenericParamDef>>();
1710 let mut where_predicates = preds.predicates.iter()
1711 .flat_map(|(p, _)| p.clean(cx))
1712 .collect::<Vec<_>>();
1714 // Type parameters and have a Sized bound by default unless removed with
1715 // ?Sized. Scan through the predicates and mark any type parameter with
1716 // a Sized bound, removing the bounds as we find them.
1718 // Note that associated types also have a sized bound by default, but we
1719 // don't actually know the set of associated types right here so that's
1720 // handled in cleaning associated types
1721 let mut sized_params = FxHashSet::default();
1722 where_predicates.retain(|pred| {
1724 WP::BoundPredicate { ty: Generic(ref g), ref bounds } => {
1725 if bounds.iter().any(|b| b.is_sized_bound(cx)) {
1726 sized_params.insert(g.clone());
1736 // Run through the type parameters again and insert a ?Sized
1737 // unbound for any we didn't find to be Sized.
1738 for tp in &stripped_typarams {
1739 if !sized_params.contains(&tp.name) {
1740 where_predicates.push(WP::BoundPredicate {
1741 ty: Type::Generic(tp.name.clone()),
1742 bounds: vec![GenericBound::maybe_sized(cx)],
1747 // It would be nice to collect all of the bounds on a type and recombine
1748 // them if possible, to avoid e.g., `where T: Foo, T: Bar, T: Sized, T: 'a`
1749 // and instead see `where T: Foo + Bar + Sized + 'a`
1754 .flat_map(|param| match param.kind {
1755 ty::GenericParamDefKind::Lifetime => Some(param.clean(cx)),
1756 ty::GenericParamDefKind::Type { .. } => None,
1757 ty::GenericParamDefKind::Const { .. } => Some(param.clean(cx)),
1758 }).chain(simplify::ty_params(stripped_typarams).into_iter())
1760 where_predicates: simplify::where_clauses(cx, where_predicates),
1765 /// The point of this function is to replace bounds with types.
1767 /// i.e. `[T, U]` when you have the following bounds: `T: Display, U: Option<T>` will return
1768 /// `[Display, Option]` (we just returns the list of the types, we don't care about the
1769 /// wrapped types in here).
1771 generics: &Generics,
1773 cx: &DocContext<'_>,
1775 ) -> FxHashSet<Type> {
1776 let arg_s = arg.to_string();
1777 let mut res = FxHashSet::default();
1778 if recurse >= 10 { // FIXME: remove this whole recurse thing when the recursion bug is fixed
1781 if arg.is_full_generic() {
1782 if let Some(where_pred) = generics.where_predicates.iter().find(|g| {
1784 &WherePredicate::BoundPredicate { ref ty, .. } => ty.def_id() == arg.def_id(),
1788 let bounds = where_pred.get_bounds().unwrap_or_else(|| &[]);
1789 for bound in bounds.iter() {
1791 GenericBound::TraitBound(ref poly_trait, _) => {
1792 for x in poly_trait.generic_params.iter() {
1796 if let Some(ty) = x.get_type(cx) {
1797 let adds = get_real_types(generics, &ty, cx, recurse + 1);
1798 if !adds.is_empty() {
1800 } else if !ty.is_full_generic() {
1810 if let Some(bound) = generics.params.iter().find(|g| {
1811 g.is_type() && g.name == arg_s
1813 for bound in bound.get_bounds().unwrap_or_else(|| &[]) {
1814 if let Some(ty) = bound.get_trait_type() {
1815 let adds = get_real_types(generics, &ty, cx, recurse + 1);
1816 if !adds.is_empty() {
1818 } else if !ty.is_full_generic() {
1819 res.insert(ty.clone());
1825 res.insert(arg.clone());
1826 if let Some(gens) = arg.generics() {
1827 for gen in gens.iter() {
1828 if gen.is_full_generic() {
1829 let adds = get_real_types(generics, gen, cx, recurse + 1);
1830 if !adds.is_empty() {
1834 res.insert(gen.clone());
1842 /// Return the full list of types when bounds have been resolved.
1844 /// i.e. `fn foo<A: Display, B: Option<A>>(x: u32, y: B)` will return
1845 /// `[u32, Display, Option]`.
1846 pub fn get_all_types(
1847 generics: &Generics,
1849 cx: &DocContext<'_>,
1850 ) -> (Vec<Type>, Vec<Type>) {
1851 let mut all_types = FxHashSet::default();
1852 for arg in decl.inputs.values.iter() {
1853 if arg.type_.is_self_type() {
1856 let args = get_real_types(generics, &arg.type_, cx, 0);
1857 if !args.is_empty() {
1858 all_types.extend(args);
1860 all_types.insert(arg.type_.clone());
1864 let ret_types = match decl.output {
1865 FunctionRetTy::Return(ref return_type) => {
1866 let mut ret = get_real_types(generics, &return_type, cx, 0);
1868 ret.insert(return_type.clone());
1870 ret.into_iter().collect()
1874 (all_types.into_iter().collect(), ret_types)
1877 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1879 pub generics: Generics,
1881 pub header: hir::FnHeader,
1882 pub defaultness: Option<hir::Defaultness>,
1883 pub all_types: Vec<Type>,
1884 pub ret_types: Vec<Type>,
1887 impl<'a> Clean<Method> for (&'a hir::MethodSig, &'a hir::Generics, hir::BodyId,
1888 Option<hir::Defaultness>) {
1889 fn clean(&self, cx: &DocContext<'_>) -> Method {
1890 let (generics, decl) = enter_impl_trait(cx, || {
1891 (self.1.clean(cx), (&*self.0.decl, self.2).clean(cx))
1893 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
1897 header: self.0.header,
1898 defaultness: self.3,
1905 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1906 pub struct TyMethod {
1907 pub header: hir::FnHeader,
1909 pub generics: Generics,
1910 pub all_types: Vec<Type>,
1911 pub ret_types: Vec<Type>,
1914 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1915 pub struct Function {
1917 pub generics: Generics,
1918 pub header: hir::FnHeader,
1919 pub all_types: Vec<Type>,
1920 pub ret_types: Vec<Type>,
1923 impl Clean<Item> for doctree::Function {
1924 fn clean(&self, cx: &DocContext<'_>) -> Item {
1925 let (generics, decl) = enter_impl_trait(cx, || {
1926 (self.generics.clean(cx), (&self.decl, self.body).clean(cx))
1929 let did = cx.tcx.hir().local_def_id_from_hir_id(self.id);
1930 let constness = if cx.tcx.is_min_const_fn(did) {
1931 hir::Constness::Const
1933 hir::Constness::NotConst
1935 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
1937 name: Some(self.name.clean(cx)),
1938 attrs: self.attrs.clean(cx),
1939 source: self.whence.clean(cx),
1940 visibility: self.vis.clean(cx),
1941 stability: self.stab.clean(cx),
1942 deprecation: self.depr.clean(cx),
1944 inner: FunctionItem(Function {
1947 header: hir::FnHeader { constness, ..self.header },
1955 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1957 pub inputs: Arguments,
1958 pub output: FunctionRetTy,
1959 pub attrs: Attributes,
1963 pub fn self_type(&self) -> Option<SelfTy> {
1964 self.inputs.values.get(0).and_then(|v| v.to_self())
1967 /// Returns the sugared return type for an async function.
1969 /// For example, if the return type is `impl std::future::Future<Output = i32>`, this function
1970 /// will return `i32`.
1974 /// This function will panic if the return type does not match the expected sugaring for async
1976 pub fn sugared_async_return_type(&self) -> FunctionRetTy {
1977 match &self.output {
1978 FunctionRetTy::Return(Type::ImplTrait(bounds)) => {
1980 GenericBound::TraitBound(PolyTrait { trait_, .. }, ..) => {
1981 let bindings = trait_.bindings().unwrap();
1982 FunctionRetTy::Return(bindings[0].ty.clone())
1984 _ => panic!("unexpected desugaring of async function"),
1987 _ => panic!("unexpected desugaring of async function"),
1992 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1993 pub struct Arguments {
1994 pub values: Vec<Argument>,
1997 impl<'a> Clean<Arguments> for (&'a [hir::Ty], &'a [ast::Ident]) {
1998 fn clean(&self, cx: &DocContext<'_>) -> Arguments {
2000 values: self.0.iter().enumerate().map(|(i, ty)| {
2001 let mut name = self.1.get(i).map(|ident| ident.to_string())
2002 .unwrap_or(String::new());
2003 if name.is_empty() {
2004 name = "_".to_string();
2008 type_: ty.clean(cx),
2015 impl<'a> Clean<Arguments> for (&'a [hir::Ty], hir::BodyId) {
2016 fn clean(&self, cx: &DocContext<'_>) -> Arguments {
2017 let body = cx.tcx.hir().body(self.1);
2020 values: self.0.iter().enumerate().map(|(i, ty)| {
2021 let original_pat = cx.tcx.hir().original_pat_of_argument(&body.arguments[i]);
2023 name: name_from_pat(original_pat),
2024 type_: ty.clean(cx),
2031 impl<'a, A: Copy> Clean<FnDecl> for (&'a hir::FnDecl, A)
2032 where (&'a [hir::Ty], A): Clean<Arguments>
2034 fn clean(&self, cx: &DocContext<'_>) -> FnDecl {
2036 inputs: (&self.0.inputs[..], self.1).clean(cx),
2037 output: self.0.output.clean(cx),
2038 attrs: Attributes::default(),
2043 impl<'a, 'tcx> Clean<FnDecl> for (DefId, ty::PolyFnSig<'tcx>) {
2044 fn clean(&self, cx: &DocContext<'_>) -> FnDecl {
2045 let (did, sig) = *self;
2046 let mut names = if cx.tcx.hir().as_local_hir_id(did).is_some() {
2049 cx.tcx.fn_arg_names(did).into_iter()
2053 output: Return(sig.skip_binder().output().clean(cx)),
2054 attrs: Attributes::default(),
2056 values: sig.skip_binder().inputs().iter().map(|t| {
2059 name: names.next().map_or(String::new(), |name| name.to_string()),
2067 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
2068 pub struct Argument {
2073 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug)]
2076 SelfBorrowed(Option<Lifetime>, Mutability),
2081 pub fn to_self(&self) -> Option<SelfTy> {
2082 if self.name != "self" {
2085 if self.type_.is_self_type() {
2086 return Some(SelfValue);
2089 BorrowedRef{ref lifetime, mutability, ref type_} if type_.is_self_type() => {
2090 Some(SelfBorrowed(lifetime.clone(), mutability))
2092 _ => Some(SelfExplicit(self.type_.clone()))
2097 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
2098 pub enum FunctionRetTy {
2103 impl Clean<FunctionRetTy> for hir::FunctionRetTy {
2104 fn clean(&self, cx: &DocContext<'_>) -> FunctionRetTy {
2106 hir::Return(ref typ) => Return(typ.clean(cx)),
2107 hir::DefaultReturn(..) => DefaultReturn,
2112 impl GetDefId for FunctionRetTy {
2113 fn def_id(&self) -> Option<DefId> {
2115 Return(ref ty) => ty.def_id(),
2116 DefaultReturn => None,
2121 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2124 pub unsafety: hir::Unsafety,
2125 pub items: Vec<Item>,
2126 pub generics: Generics,
2127 pub bounds: Vec<GenericBound>,
2128 pub is_spotlight: bool,
2132 impl Clean<Item> for doctree::Trait {
2133 fn clean(&self, cx: &DocContext<'_>) -> Item {
2134 let attrs = self.attrs.clean(cx);
2135 let is_spotlight = attrs.has_doc_flag(sym::spotlight);
2137 name: Some(self.name.clean(cx)),
2139 source: self.whence.clean(cx),
2140 def_id: cx.tcx.hir().local_def_id_from_hir_id(self.id),
2141 visibility: self.vis.clean(cx),
2142 stability: self.stab.clean(cx),
2143 deprecation: self.depr.clean(cx),
2144 inner: TraitItem(Trait {
2145 auto: self.is_auto.clean(cx),
2146 unsafety: self.unsafety,
2147 items: self.items.clean(cx),
2148 generics: self.generics.clean(cx),
2149 bounds: self.bounds.clean(cx),
2151 is_auto: self.is_auto.clean(cx),
2157 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2158 pub struct TraitAlias {
2159 pub generics: Generics,
2160 pub bounds: Vec<GenericBound>,
2163 impl Clean<Item> for doctree::TraitAlias {
2164 fn clean(&self, cx: &DocContext<'_>) -> Item {
2165 let attrs = self.attrs.clean(cx);
2167 name: Some(self.name.clean(cx)),
2169 source: self.whence.clean(cx),
2170 def_id: cx.tcx.hir().local_def_id_from_hir_id(self.id),
2171 visibility: self.vis.clean(cx),
2172 stability: self.stab.clean(cx),
2173 deprecation: self.depr.clean(cx),
2174 inner: TraitAliasItem(TraitAlias {
2175 generics: self.generics.clean(cx),
2176 bounds: self.bounds.clean(cx),
2182 impl Clean<bool> for hir::IsAuto {
2183 fn clean(&self, _: &DocContext<'_>) -> bool {
2185 hir::IsAuto::Yes => true,
2186 hir::IsAuto::No => false,
2191 impl Clean<Type> for hir::TraitRef {
2192 fn clean(&self, cx: &DocContext<'_>) -> Type {
2193 resolve_type(cx, self.path.clean(cx), self.hir_ref_id)
2197 impl Clean<PolyTrait> for hir::PolyTraitRef {
2198 fn clean(&self, cx: &DocContext<'_>) -> PolyTrait {
2200 trait_: self.trait_ref.clean(cx),
2201 generic_params: self.bound_generic_params.clean(cx)
2206 impl Clean<Item> for hir::TraitItem {
2207 fn clean(&self, cx: &DocContext<'_>) -> Item {
2208 let inner = match self.node {
2209 hir::TraitItemKind::Const(ref ty, default) => {
2210 AssocConstItem(ty.clean(cx),
2211 default.map(|e| print_const_expr(cx, e)))
2213 hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Provided(body)) => {
2214 MethodItem((sig, &self.generics, body, None).clean(cx))
2216 hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Required(ref names)) => {
2217 let (generics, decl) = enter_impl_trait(cx, || {
2218 (self.generics.clean(cx), (&*sig.decl, &names[..]).clean(cx))
2220 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
2221 TyMethodItem(TyMethod {
2229 hir::TraitItemKind::Type(ref bounds, ref default) => {
2230 AssocTypeItem(bounds.clean(cx), default.clean(cx))
2233 let local_did = cx.tcx.hir().local_def_id_from_hir_id(self.hir_id);
2235 name: Some(self.ident.name.clean(cx)),
2236 attrs: self.attrs.clean(cx),
2237 source: self.span.clean(cx),
2240 stability: get_stability(cx, local_did),
2241 deprecation: get_deprecation(cx, local_did),
2247 impl Clean<Item> for hir::ImplItem {
2248 fn clean(&self, cx: &DocContext<'_>) -> Item {
2249 let inner = match self.node {
2250 hir::ImplItemKind::Const(ref ty, expr) => {
2251 AssocConstItem(ty.clean(cx),
2252 Some(print_const_expr(cx, expr)))
2254 hir::ImplItemKind::Method(ref sig, body) => {
2255 MethodItem((sig, &self.generics, body, Some(self.defaultness)).clean(cx))
2257 hir::ImplItemKind::Type(ref ty) => TypedefItem(Typedef {
2258 type_: ty.clean(cx),
2259 generics: Generics::default(),
2261 hir::ImplItemKind::Existential(ref bounds) => ExistentialItem(Existential {
2262 bounds: bounds.clean(cx),
2263 generics: Generics::default(),
2266 let local_did = cx.tcx.hir().local_def_id_from_hir_id(self.hir_id);
2268 name: Some(self.ident.name.clean(cx)),
2269 source: self.span.clean(cx),
2270 attrs: self.attrs.clean(cx),
2272 visibility: self.vis.clean(cx),
2273 stability: get_stability(cx, local_did),
2274 deprecation: get_deprecation(cx, local_did),
2280 impl<'tcx> Clean<Item> for ty::AssocItem {
2281 fn clean(&self, cx: &DocContext<'_>) -> Item {
2282 let inner = match self.kind {
2283 ty::AssocKind::Const => {
2284 let ty = cx.tcx.type_of(self.def_id);
2285 let default = if self.defaultness.has_value() {
2286 Some(inline::print_inlined_const(cx, self.def_id))
2290 AssocConstItem(ty.clean(cx), default)
2292 ty::AssocKind::Method => {
2293 let generics = (cx.tcx.generics_of(self.def_id),
2294 &cx.tcx.explicit_predicates_of(self.def_id)).clean(cx);
2295 let sig = cx.tcx.fn_sig(self.def_id);
2296 let mut decl = (self.def_id, sig).clean(cx);
2298 if self.method_has_self_argument {
2299 let self_ty = match self.container {
2300 ty::ImplContainer(def_id) => {
2301 cx.tcx.type_of(def_id)
2303 ty::TraitContainer(_) => cx.tcx.mk_self_type()
2305 let self_arg_ty = *sig.input(0).skip_binder();
2306 if self_arg_ty == self_ty {
2307 decl.inputs.values[0].type_ = Generic(String::from("Self"));
2308 } else if let ty::Ref(_, ty, _) = self_arg_ty.sty {
2310 match decl.inputs.values[0].type_ {
2311 BorrowedRef{ref mut type_, ..} => {
2312 **type_ = Generic(String::from("Self"))
2314 _ => unreachable!(),
2320 let provided = match self.container {
2321 ty::ImplContainer(_) => true,
2322 ty::TraitContainer(_) => self.defaultness.has_value()
2324 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
2326 let constness = if cx.tcx.is_min_const_fn(self.def_id) {
2327 hir::Constness::Const
2329 hir::Constness::NotConst
2331 let defaultness = match self.container {
2332 ty::ImplContainer(_) => Some(self.defaultness),
2333 ty::TraitContainer(_) => None,
2338 header: hir::FnHeader {
2339 unsafety: sig.unsafety(),
2342 asyncness: hir::IsAsync::NotAsync,
2349 TyMethodItem(TyMethod {
2352 header: hir::FnHeader {
2353 unsafety: sig.unsafety(),
2355 constness: hir::Constness::NotConst,
2356 asyncness: hir::IsAsync::NotAsync,
2363 ty::AssocKind::Type => {
2364 let my_name = self.ident.name.clean(cx);
2366 if let ty::TraitContainer(did) = self.container {
2367 // When loading a cross-crate associated type, the bounds for this type
2368 // are actually located on the trait/impl itself, so we need to load
2369 // all of the generics from there and then look for bounds that are
2370 // applied to this associated type in question.
2371 let predicates = cx.tcx.explicit_predicates_of(did);
2372 let generics = (cx.tcx.generics_of(did), &predicates).clean(cx);
2373 let mut bounds = generics.where_predicates.iter().filter_map(|pred| {
2374 let (name, self_type, trait_, bounds) = match *pred {
2375 WherePredicate::BoundPredicate {
2376 ty: QPath { ref name, ref self_type, ref trait_ },
2378 } => (name, self_type, trait_, bounds),
2381 if *name != my_name { return None }
2383 ResolvedPath { did, .. } if did == self.container.id() => {}
2387 Generic(ref s) if *s == "Self" => {}
2391 }).flat_map(|i| i.iter().cloned()).collect::<Vec<_>>();
2392 // Our Sized/?Sized bound didn't get handled when creating the generics
2393 // because we didn't actually get our whole set of bounds until just now
2394 // (some of them may have come from the trait). If we do have a sized
2395 // bound, we remove it, and if we don't then we add the `?Sized` bound
2397 match bounds.iter().position(|b| b.is_sized_bound(cx)) {
2398 Some(i) => { bounds.remove(i); }
2399 None => bounds.push(GenericBound::maybe_sized(cx)),
2402 let ty = if self.defaultness.has_value() {
2403 Some(cx.tcx.type_of(self.def_id))
2408 AssocTypeItem(bounds, ty.clean(cx))
2410 TypedefItem(Typedef {
2411 type_: cx.tcx.type_of(self.def_id).clean(cx),
2412 generics: Generics {
2414 where_predicates: Vec::new(),
2419 ty::AssocKind::Existential => unimplemented!(),
2422 let visibility = match self.container {
2423 ty::ImplContainer(_) => self.vis.clean(cx),
2424 ty::TraitContainer(_) => None,
2428 name: Some(self.ident.name.clean(cx)),
2430 stability: get_stability(cx, self.def_id),
2431 deprecation: get_deprecation(cx, self.def_id),
2432 def_id: self.def_id,
2433 attrs: inline::load_attrs(cx, self.def_id),
2434 source: cx.tcx.def_span(self.def_id).clean(cx),
2440 /// A trait reference, which may have higher ranked lifetimes.
2441 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
2442 pub struct PolyTrait {
2444 pub generic_params: Vec<GenericParamDef>,
2447 /// A representation of a Type suitable for hyperlinking purposes. Ideally one can get the original
2448 /// type out of the AST/TyCtxt given one of these, if more information is needed. Most importantly
2449 /// it does not preserve mutability or boxes.
2450 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
2452 /// Structs/enums/traits (most that'd be an `hir::TyKind::Path`).
2455 param_names: Option<Vec<GenericBound>>,
2457 /// `true` if is a `T::Name` path for associated types.
2460 /// For parameterized types, so the consumer of the JSON don't go
2461 /// looking for types which don't exist anywhere.
2463 /// Primitives are the fixed-size numeric types (plus int/usize/float), char,
2464 /// arrays, slices, and tuples.
2465 Primitive(PrimitiveType),
2467 BareFunction(Box<BareFunctionDecl>),
2470 Array(Box<Type>, String),
2474 RawPointer(Mutability, Box<Type>),
2476 lifetime: Option<Lifetime>,
2477 mutability: Mutability,
2481 // <Type as Trait>::Name
2484 self_type: Box<Type>,
2491 // impl TraitA+TraitB
2492 ImplTrait(Vec<GenericBound>),
2495 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Copy, Debug)]
2496 pub enum PrimitiveType {
2497 Isize, I8, I16, I32, I64, I128,
2498 Usize, U8, U16, U32, U64, U128,
2514 #[derive(Clone, RustcEncodable, RustcDecodable, Copy, Debug)]
2533 pub trait GetDefId {
2534 fn def_id(&self) -> Option<DefId>;
2537 impl<T: GetDefId> GetDefId for Option<T> {
2538 fn def_id(&self) -> Option<DefId> {
2539 self.as_ref().and_then(|d| d.def_id())
2544 pub fn primitive_type(&self) -> Option<PrimitiveType> {
2546 Primitive(p) | BorrowedRef { type_: box Primitive(p), ..} => Some(p),
2547 Slice(..) | BorrowedRef { type_: box Slice(..), .. } => Some(PrimitiveType::Slice),
2548 Array(..) | BorrowedRef { type_: box Array(..), .. } => Some(PrimitiveType::Array),
2549 Tuple(ref tys) => if tys.is_empty() {
2550 Some(PrimitiveType::Unit)
2552 Some(PrimitiveType::Tuple)
2554 RawPointer(..) => Some(PrimitiveType::RawPointer),
2555 BorrowedRef { type_: box Generic(..), .. } => Some(PrimitiveType::Reference),
2556 BareFunction(..) => Some(PrimitiveType::Fn),
2557 Never => Some(PrimitiveType::Never),
2562 pub fn is_generic(&self) -> bool {
2564 ResolvedPath { is_generic, .. } => is_generic,
2569 pub fn is_self_type(&self) -> bool {
2571 Generic(ref name) => name == "Self",
2576 pub fn generics(&self) -> Option<Vec<Type>> {
2578 ResolvedPath { ref path, .. } => {
2579 path.segments.last().and_then(|seg| {
2580 if let GenericArgs::AngleBracketed { ref args, .. } = seg.args {
2581 Some(args.iter().filter_map(|arg| match arg {
2582 GenericArg::Type(ty) => Some(ty.clone()),
2594 pub fn bindings(&self) -> Option<&[TypeBinding]> {
2596 ResolvedPath { ref path, .. } => {
2597 path.segments.last().and_then(|seg| {
2598 if let GenericArgs::AngleBracketed { ref bindings, .. } = seg.args {
2609 pub fn is_full_generic(&self) -> bool {
2611 Type::Generic(_) => true,
2617 impl GetDefId for Type {
2618 fn def_id(&self) -> Option<DefId> {
2620 ResolvedPath { did, .. } => Some(did),
2621 Primitive(p) => crate::html::render::cache().primitive_locations.get(&p).cloned(),
2622 BorrowedRef { type_: box Generic(..), .. } =>
2623 Primitive(PrimitiveType::Reference).def_id(),
2624 BorrowedRef { ref type_, .. } => type_.def_id(),
2625 Tuple(ref tys) => if tys.is_empty() {
2626 Primitive(PrimitiveType::Unit).def_id()
2628 Primitive(PrimitiveType::Tuple).def_id()
2630 BareFunction(..) => Primitive(PrimitiveType::Fn).def_id(),
2631 Never => Primitive(PrimitiveType::Never).def_id(),
2632 Slice(..) => Primitive(PrimitiveType::Slice).def_id(),
2633 Array(..) => Primitive(PrimitiveType::Array).def_id(),
2634 RawPointer(..) => Primitive(PrimitiveType::RawPointer).def_id(),
2635 QPath { ref self_type, .. } => self_type.def_id(),
2641 impl PrimitiveType {
2642 fn from_str(s: &str) -> Option<PrimitiveType> {
2644 "isize" => Some(PrimitiveType::Isize),
2645 "i8" => Some(PrimitiveType::I8),
2646 "i16" => Some(PrimitiveType::I16),
2647 "i32" => Some(PrimitiveType::I32),
2648 "i64" => Some(PrimitiveType::I64),
2649 "i128" => Some(PrimitiveType::I128),
2650 "usize" => Some(PrimitiveType::Usize),
2651 "u8" => Some(PrimitiveType::U8),
2652 "u16" => Some(PrimitiveType::U16),
2653 "u32" => Some(PrimitiveType::U32),
2654 "u64" => Some(PrimitiveType::U64),
2655 "u128" => Some(PrimitiveType::U128),
2656 "bool" => Some(PrimitiveType::Bool),
2657 "char" => Some(PrimitiveType::Char),
2658 "str" => Some(PrimitiveType::Str),
2659 "f32" => Some(PrimitiveType::F32),
2660 "f64" => Some(PrimitiveType::F64),
2661 "array" => Some(PrimitiveType::Array),
2662 "slice" => Some(PrimitiveType::Slice),
2663 "tuple" => Some(PrimitiveType::Tuple),
2664 "unit" => Some(PrimitiveType::Unit),
2665 "pointer" => Some(PrimitiveType::RawPointer),
2666 "reference" => Some(PrimitiveType::Reference),
2667 "fn" => Some(PrimitiveType::Fn),
2668 "never" => Some(PrimitiveType::Never),
2673 pub fn as_str(&self) -> &'static str {
2674 use self::PrimitiveType::*;
2697 RawPointer => "pointer",
2698 Reference => "reference",
2705 pub fn to_url_str(&self) -> &'static str {
2710 impl From<ast::IntTy> for PrimitiveType {
2711 fn from(int_ty: ast::IntTy) -> PrimitiveType {
2713 ast::IntTy::Isize => PrimitiveType::Isize,
2714 ast::IntTy::I8 => PrimitiveType::I8,
2715 ast::IntTy::I16 => PrimitiveType::I16,
2716 ast::IntTy::I32 => PrimitiveType::I32,
2717 ast::IntTy::I64 => PrimitiveType::I64,
2718 ast::IntTy::I128 => PrimitiveType::I128,
2723 impl From<ast::UintTy> for PrimitiveType {
2724 fn from(uint_ty: ast::UintTy) -> PrimitiveType {
2726 ast::UintTy::Usize => PrimitiveType::Usize,
2727 ast::UintTy::U8 => PrimitiveType::U8,
2728 ast::UintTy::U16 => PrimitiveType::U16,
2729 ast::UintTy::U32 => PrimitiveType::U32,
2730 ast::UintTy::U64 => PrimitiveType::U64,
2731 ast::UintTy::U128 => PrimitiveType::U128,
2736 impl From<ast::FloatTy> for PrimitiveType {
2737 fn from(float_ty: ast::FloatTy) -> PrimitiveType {
2739 ast::FloatTy::F32 => PrimitiveType::F32,
2740 ast::FloatTy::F64 => PrimitiveType::F64,
2745 impl Clean<Type> for hir::Ty {
2746 fn clean(&self, cx: &DocContext<'_>) -> Type {
2750 TyKind::Never => Never,
2751 TyKind::CVarArgs(_) => CVarArgs,
2752 TyKind::Ptr(ref m) => RawPointer(m.mutbl.clean(cx), box m.ty.clean(cx)),
2753 TyKind::Rptr(ref l, ref m) => {
2754 let lifetime = if l.is_elided() {
2759 BorrowedRef {lifetime: lifetime, mutability: m.mutbl.clean(cx),
2760 type_: box m.ty.clean(cx)}
2762 TyKind::Slice(ref ty) => Slice(box ty.clean(cx)),
2763 TyKind::Array(ref ty, ref length) => {
2764 let def_id = cx.tcx.hir().local_def_id_from_hir_id(length.hir_id);
2765 let param_env = cx.tcx.param_env(def_id);
2766 let substs = InternalSubsts::identity_for_item(cx.tcx, def_id);
2767 let cid = GlobalId {
2768 instance: ty::Instance::new(def_id, substs),
2771 let length = match cx.tcx.const_eval(param_env.and(cid)) {
2772 Ok(length) => print_const(cx, length),
2773 Err(_) => "_".to_string(),
2775 Array(box ty.clean(cx), length)
2777 TyKind::Tup(ref tys) => Tuple(tys.clean(cx)),
2778 TyKind::Def(item_id, _) => {
2779 let item = cx.tcx.hir().expect_item_by_hir_id(item_id.id);
2780 if let hir::ItemKind::Existential(ref ty) = item.node {
2781 ImplTrait(ty.bounds.clean(cx))
2786 TyKind::Path(hir::QPath::Resolved(None, ref path)) => {
2787 if let Res::Def(DefKind::TyParam, did) = path.res {
2788 if let Some(new_ty) = cx.ty_substs.borrow().get(&did).cloned() {
2791 if let Some(bounds) = cx.impl_trait_bounds.borrow_mut().remove(&did) {
2792 return ImplTrait(bounds);
2796 let mut alias = None;
2797 if let Res::Def(DefKind::TyAlias, def_id) = path.res {
2798 // Substitute private type aliases
2799 if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(def_id) {
2800 if !cx.renderinfo.borrow().access_levels.is_exported(def_id) {
2801 alias = Some(&cx.tcx.hir().expect_item_by_hir_id(hir_id).node);
2806 if let Some(&hir::ItemKind::Ty(ref ty, ref generics)) = alias {
2807 let provided_params = &path.segments.last().expect("segments were empty");
2808 let mut ty_substs = FxHashMap::default();
2809 let mut lt_substs = FxHashMap::default();
2810 let mut ct_substs = FxHashMap::default();
2811 provided_params.with_generic_args(|generic_args| {
2812 let mut indices: GenericParamCount = Default::default();
2813 for param in generics.params.iter() {
2815 hir::GenericParamKind::Lifetime { .. } => {
2817 let lifetime = generic_args.args.iter().find_map(|arg| {
2819 hir::GenericArg::Lifetime(lt) => {
2820 if indices.lifetimes == j {
2829 if let Some(lt) = lifetime.cloned() {
2830 if !lt.is_elided() {
2832 cx.tcx.hir().local_def_id_from_hir_id(param.hir_id);
2833 lt_substs.insert(lt_def_id, lt.clean(cx));
2836 indices.lifetimes += 1;
2838 hir::GenericParamKind::Type { ref default, .. } => {
2839 let ty_param_def_id =
2840 cx.tcx.hir().local_def_id_from_hir_id(param.hir_id);
2842 let type_ = generic_args.args.iter().find_map(|arg| {
2844 hir::GenericArg::Type(ty) => {
2845 if indices.types == j {
2854 if let Some(ty) = type_.cloned() {
2855 ty_substs.insert(ty_param_def_id, ty.clean(cx));
2856 } else if let Some(default) = default.clone() {
2857 ty_substs.insert(ty_param_def_id,
2858 default.into_inner().clean(cx));
2862 hir::GenericParamKind::Const { .. } => {
2863 let const_param_def_id =
2864 cx.tcx.hir().local_def_id_from_hir_id(param.hir_id);
2866 let const_ = generic_args.args.iter().find_map(|arg| {
2868 hir::GenericArg::Const(ct) => {
2869 if indices.consts == j {
2878 if let Some(ct) = const_.cloned() {
2879 ct_substs.insert(const_param_def_id, ct.clean(cx));
2881 // FIXME(const_generics:defaults)
2882 indices.consts += 1;
2887 return cx.enter_alias(ty_substs, lt_substs, ct_substs, || ty.clean(cx));
2889 resolve_type(cx, path.clean(cx), self.hir_id)
2891 TyKind::Path(hir::QPath::Resolved(Some(ref qself), ref p)) => {
2892 let mut segments: Vec<_> = p.segments.clone().into();
2894 let trait_path = hir::Path {
2898 cx.tcx.associated_item(p.res.def_id()).container.id(),
2900 segments: segments.into(),
2903 name: p.segments.last().expect("segments were empty").ident.name.clean(cx),
2904 self_type: box qself.clean(cx),
2905 trait_: box resolve_type(cx, trait_path.clean(cx), self.hir_id)
2908 TyKind::Path(hir::QPath::TypeRelative(ref qself, ref segment)) => {
2909 let mut res = Res::Err;
2910 let ty = hir_ty_to_ty(cx.tcx, self);
2911 if let ty::Projection(proj) = ty.sty {
2912 res = Res::Def(DefKind::Trait, proj.trait_ref(cx.tcx).def_id);
2914 let trait_path = hir::Path {
2917 segments: vec![].into(),
2920 name: segment.ident.name.clean(cx),
2921 self_type: box qself.clean(cx),
2922 trait_: box resolve_type(cx, trait_path.clean(cx), self.hir_id)
2925 TyKind::TraitObject(ref bounds, ref lifetime) => {
2926 match bounds[0].clean(cx).trait_ {
2927 ResolvedPath { path, param_names: None, did, is_generic } => {
2928 let mut bounds: Vec<self::GenericBound> = bounds[1..].iter().map(|bound| {
2929 self::GenericBound::TraitBound(bound.clean(cx),
2930 hir::TraitBoundModifier::None)
2932 if !lifetime.is_elided() {
2933 bounds.push(self::GenericBound::Outlives(lifetime.clean(cx)));
2935 ResolvedPath { path, param_names: Some(bounds), did, is_generic, }
2937 _ => Infer // shouldn't happen
2940 TyKind::BareFn(ref barefn) => BareFunction(box barefn.clean(cx)),
2941 TyKind::Infer | TyKind::Err => Infer,
2942 TyKind::Typeof(..) => panic!("Unimplemented type {:?}", self.node),
2947 impl<'tcx> Clean<Type> for Ty<'tcx> {
2948 fn clean(&self, cx: &DocContext<'_>) -> Type {
2949 debug!("cleaning type: {:?}", self);
2952 ty::Bool => Primitive(PrimitiveType::Bool),
2953 ty::Char => Primitive(PrimitiveType::Char),
2954 ty::Int(int_ty) => Primitive(int_ty.into()),
2955 ty::Uint(uint_ty) => Primitive(uint_ty.into()),
2956 ty::Float(float_ty) => Primitive(float_ty.into()),
2957 ty::Str => Primitive(PrimitiveType::Str),
2958 ty::Slice(ty) => Slice(box ty.clean(cx)),
2959 ty::Array(ty, n) => {
2960 let mut n = cx.tcx.lift(&n).expect("array lift failed");
2961 if let ConstValue::Unevaluated(def_id, substs) = n.val {
2962 let param_env = cx.tcx.param_env(def_id);
2963 let cid = GlobalId {
2964 instance: ty::Instance::new(def_id, substs),
2967 if let Ok(new_n) = cx.tcx.const_eval(param_env.and(cid)) {
2971 let n = print_const(cx, n);
2972 Array(box ty.clean(cx), n)
2974 ty::RawPtr(mt) => RawPointer(mt.mutbl.clean(cx), box mt.ty.clean(cx)),
2975 ty::Ref(r, ty, mutbl) => BorrowedRef {
2976 lifetime: r.clean(cx),
2977 mutability: mutbl.clean(cx),
2978 type_: box ty.clean(cx),
2982 let ty = cx.tcx.lift(self).expect("FnPtr lift failed");
2983 let sig = ty.fn_sig(cx.tcx);
2984 BareFunction(box BareFunctionDecl {
2985 unsafety: sig.unsafety(),
2986 generic_params: Vec::new(),
2987 decl: (cx.tcx.hir().local_def_id(ast::CRATE_NODE_ID), sig).clean(cx),
2991 ty::Adt(def, substs) => {
2993 let kind = match def.adt_kind() {
2994 AdtKind::Struct => TypeKind::Struct,
2995 AdtKind::Union => TypeKind::Union,
2996 AdtKind::Enum => TypeKind::Enum,
2998 inline::record_extern_fqn(cx, did, kind);
2999 let path = external_path(cx, &cx.tcx.item_name(did).as_str(),
3000 None, false, vec![], substs);
3008 ty::Foreign(did) => {
3009 inline::record_extern_fqn(cx, did, TypeKind::Foreign);
3010 let path = external_path(cx, &cx.tcx.item_name(did).as_str(),
3011 None, false, vec![], InternalSubsts::empty());
3019 ty::Dynamic(ref obj, ref reg) => {
3020 // HACK: pick the first `did` as the `did` of the trait object. Someone
3021 // might want to implement "native" support for marker-trait-only
3023 let mut dids = obj.principal_def_id().into_iter().chain(obj.auto_traits());
3024 let did = dids.next().unwrap_or_else(|| {
3025 panic!("found trait object `{:?}` with no traits?", self)
3027 let substs = match obj.principal() {
3028 Some(principal) => principal.skip_binder().substs,
3029 // marker traits have no substs.
3030 _ => cx.tcx.intern_substs(&[])
3033 inline::record_extern_fqn(cx, did, TypeKind::Trait);
3035 let mut param_names = vec![];
3036 reg.clean(cx).map(|b| param_names.push(GenericBound::Outlives(b)));
3038 let empty = cx.tcx.intern_substs(&[]);
3039 let path = external_path(cx, &cx.tcx.item_name(did).as_str(),
3040 Some(did), false, vec![], empty);
3041 inline::record_extern_fqn(cx, did, TypeKind::Trait);
3042 let bound = GenericBound::TraitBound(PolyTrait {
3043 trait_: ResolvedPath {
3049 generic_params: Vec::new(),
3050 }, hir::TraitBoundModifier::None);
3051 param_names.push(bound);
3054 let mut bindings = vec![];
3055 for pb in obj.projection_bounds() {
3056 bindings.push(TypeBinding {
3057 name: cx.tcx.associated_item(pb.item_def_id()).ident.name.clean(cx),
3058 ty: pb.skip_binder().ty.clean(cx)
3062 let path = external_path(cx, &cx.tcx.item_name(did).as_str(), Some(did),
3063 false, bindings, substs);
3066 param_names: Some(param_names),
3071 ty::Tuple(ref t) => {
3072 Tuple(t.iter().map(|t| t.expect_ty()).collect::<Vec<_>>().clean(cx))
3075 ty::Projection(ref data) => data.clean(cx),
3077 ty::Param(ref p) => Generic(p.name.to_string()),
3079 ty::Opaque(def_id, substs) => {
3080 // Grab the "TraitA + TraitB" from `impl TraitA + TraitB`,
3081 // by looking up the projections associated with the def_id.
3082 let predicates_of = cx.tcx.explicit_predicates_of(def_id);
3083 let substs = cx.tcx.lift(&substs).expect("Opaque lift failed");
3084 let bounds = predicates_of.instantiate(cx.tcx, substs);
3085 let mut regions = vec![];
3086 let mut has_sized = false;
3087 let mut bounds = bounds.predicates.iter().filter_map(|predicate| {
3088 let trait_ref = if let Some(tr) = predicate.to_opt_poly_trait_ref() {
3090 } else if let ty::Predicate::TypeOutlives(pred) = *predicate {
3091 // these should turn up at the end
3092 pred.skip_binder().1.clean(cx).map(|r| {
3093 regions.push(GenericBound::Outlives(r))
3100 if let Some(sized) = cx.tcx.lang_items().sized_trait() {
3101 if trait_ref.def_id() == sized {
3107 let bounds = bounds.predicates.iter().filter_map(|pred|
3108 if let ty::Predicate::Projection(proj) = *pred {
3109 let proj = proj.skip_binder();
3110 if proj.projection_ty.trait_ref(cx.tcx) == *trait_ref.skip_binder() {
3112 name: cx.tcx.associated_item(proj.projection_ty.item_def_id)
3113 .ident.name.clean(cx),
3114 ty: proj.ty.clean(cx),
3124 Some((trait_ref.skip_binder(), bounds).clean(cx))
3125 }).collect::<Vec<_>>();
3126 bounds.extend(regions);
3127 if !has_sized && !bounds.is_empty() {
3128 bounds.insert(0, GenericBound::maybe_sized(cx));
3133 ty::Closure(..) | ty::Generator(..) => Tuple(vec![]), // FIXME(pcwalton)
3135 ty::Bound(..) => panic!("Bound"),
3136 ty::Placeholder(..) => panic!("Placeholder"),
3137 ty::UnnormalizedProjection(..) => panic!("UnnormalizedProjection"),
3138 ty::GeneratorWitness(..) => panic!("GeneratorWitness"),
3139 ty::Infer(..) => panic!("Infer"),
3140 ty::Error => panic!("Error"),
3145 impl<'tcx> Clean<Constant> for ty::Const<'tcx> {
3146 fn clean(&self, cx: &DocContext<'_>) -> Constant {
3148 type_: self.ty.clean(cx),
3149 expr: format!("{}", self),
3154 impl Clean<Item> for hir::StructField {
3155 fn clean(&self, cx: &DocContext<'_>) -> Item {
3156 let local_did = cx.tcx.hir().local_def_id_from_hir_id(self.hir_id);
3159 name: Some(self.ident.name).clean(cx),
3160 attrs: self.attrs.clean(cx),
3161 source: self.span.clean(cx),
3162 visibility: self.vis.clean(cx),
3163 stability: get_stability(cx, local_did),
3164 deprecation: get_deprecation(cx, local_did),
3166 inner: StructFieldItem(self.ty.clean(cx)),
3171 impl<'tcx> Clean<Item> for ty::FieldDef {
3172 fn clean(&self, cx: &DocContext<'_>) -> Item {
3174 name: Some(self.ident.name).clean(cx),
3175 attrs: cx.tcx.get_attrs(self.did).clean(cx),
3176 source: cx.tcx.def_span(self.did).clean(cx),
3177 visibility: self.vis.clean(cx),
3178 stability: get_stability(cx, self.did),
3179 deprecation: get_deprecation(cx, self.did),
3181 inner: StructFieldItem(cx.tcx.type_of(self.did).clean(cx)),
3186 #[derive(Clone, PartialEq, Eq, RustcDecodable, RustcEncodable, Debug)]
3187 pub enum Visibility {
3191 Restricted(DefId, Path),
3194 impl Clean<Option<Visibility>> for hir::Visibility {
3195 fn clean(&self, cx: &DocContext<'_>) -> Option<Visibility> {
3196 Some(match self.node {
3197 hir::VisibilityKind::Public => Visibility::Public,
3198 hir::VisibilityKind::Inherited => Visibility::Inherited,
3199 hir::VisibilityKind::Crate(_) => Visibility::Crate,
3200 hir::VisibilityKind::Restricted { ref path, .. } => {
3201 let path = path.clean(cx);
3202 let did = register_res(cx, path.res);
3203 Visibility::Restricted(did, path)
3209 impl Clean<Option<Visibility>> for ty::Visibility {
3210 fn clean(&self, _: &DocContext<'_>) -> Option<Visibility> {
3211 Some(if *self == ty::Visibility::Public { Public } else { Inherited })
3215 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3217 pub struct_type: doctree::StructType,
3218 pub generics: Generics,
3219 pub fields: Vec<Item>,
3220 pub fields_stripped: bool,
3223 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3225 pub struct_type: doctree::StructType,
3226 pub generics: Generics,
3227 pub fields: Vec<Item>,
3228 pub fields_stripped: bool,
3231 impl Clean<Item> for doctree::Struct {
3232 fn clean(&self, cx: &DocContext<'_>) -> Item {
3234 name: Some(self.name.clean(cx)),
3235 attrs: self.attrs.clean(cx),
3236 source: self.whence.clean(cx),
3237 def_id: cx.tcx.hir().local_def_id_from_hir_id(self.id),
3238 visibility: self.vis.clean(cx),
3239 stability: self.stab.clean(cx),
3240 deprecation: self.depr.clean(cx),
3241 inner: StructItem(Struct {
3242 struct_type: self.struct_type,
3243 generics: self.generics.clean(cx),
3244 fields: self.fields.clean(cx),
3245 fields_stripped: false,
3251 impl Clean<Item> for doctree::Union {
3252 fn clean(&self, cx: &DocContext<'_>) -> Item {
3254 name: Some(self.name.clean(cx)),
3255 attrs: self.attrs.clean(cx),
3256 source: self.whence.clean(cx),
3257 def_id: cx.tcx.hir().local_def_id_from_hir_id(self.id),
3258 visibility: self.vis.clean(cx),
3259 stability: self.stab.clean(cx),
3260 deprecation: self.depr.clean(cx),
3261 inner: UnionItem(Union {
3262 struct_type: self.struct_type,
3263 generics: self.generics.clean(cx),
3264 fields: self.fields.clean(cx),
3265 fields_stripped: false,
3271 /// This is a more limited form of the standard Struct, different in that
3272 /// it lacks the things most items have (name, id, parameterization). Found
3273 /// only as a variant in an enum.
3274 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3275 pub struct VariantStruct {
3276 pub struct_type: doctree::StructType,
3277 pub fields: Vec<Item>,
3278 pub fields_stripped: bool,
3281 impl Clean<VariantStruct> for ::rustc::hir::VariantData {
3282 fn clean(&self, cx: &DocContext<'_>) -> VariantStruct {
3284 struct_type: doctree::struct_type_from_def(self),
3285 fields: self.fields().iter().map(|x| x.clean(cx)).collect(),
3286 fields_stripped: false,
3291 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3293 pub variants: IndexVec<VariantIdx, Item>,
3294 pub generics: Generics,
3295 pub variants_stripped: bool,
3298 impl Clean<Item> for doctree::Enum {
3299 fn clean(&self, cx: &DocContext<'_>) -> Item {
3301 name: Some(self.name.clean(cx)),
3302 attrs: self.attrs.clean(cx),
3303 source: self.whence.clean(cx),
3304 def_id: cx.tcx.hir().local_def_id_from_hir_id(self.id),
3305 visibility: self.vis.clean(cx),
3306 stability: self.stab.clean(cx),
3307 deprecation: self.depr.clean(cx),
3308 inner: EnumItem(Enum {
3309 variants: self.variants.iter().map(|v| v.clean(cx)).collect(),
3310 generics: self.generics.clean(cx),
3311 variants_stripped: false,
3317 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3318 pub struct Variant {
3319 pub kind: VariantKind,
3322 impl Clean<Item> for doctree::Variant {
3323 fn clean(&self, cx: &DocContext<'_>) -> Item {
3325 name: Some(self.name.clean(cx)),
3326 attrs: self.attrs.clean(cx),
3327 source: self.whence.clean(cx),
3329 stability: self.stab.clean(cx),
3330 deprecation: self.depr.clean(cx),
3331 def_id: cx.tcx.hir().local_def_id_from_hir_id(self.id),
3332 inner: VariantItem(Variant {
3333 kind: self.def.clean(cx),
3339 impl<'tcx> Clean<Item> for ty::VariantDef {
3340 fn clean(&self, cx: &DocContext<'_>) -> Item {
3341 let kind = match self.ctor_kind {
3342 CtorKind::Const => VariantKind::CLike,
3345 self.fields.iter().map(|f| cx.tcx.type_of(f.did).clean(cx)).collect()
3348 CtorKind::Fictive => {
3349 VariantKind::Struct(VariantStruct {
3350 struct_type: doctree::Plain,
3351 fields_stripped: false,
3352 fields: self.fields.iter().map(|field| {
3354 source: cx.tcx.def_span(field.did).clean(cx),
3355 name: Some(field.ident.name.clean(cx)),
3356 attrs: cx.tcx.get_attrs(field.did).clean(cx),
3357 visibility: field.vis.clean(cx),
3359 stability: get_stability(cx, field.did),
3360 deprecation: get_deprecation(cx, field.did),
3361 inner: StructFieldItem(cx.tcx.type_of(field.did).clean(cx))
3368 name: Some(self.ident.clean(cx)),
3369 attrs: inline::load_attrs(cx, self.def_id),
3370 source: cx.tcx.def_span(self.def_id).clean(cx),
3371 visibility: Some(Inherited),
3372 def_id: self.def_id,
3373 inner: VariantItem(Variant { kind }),
3374 stability: get_stability(cx, self.def_id),
3375 deprecation: get_deprecation(cx, self.def_id),
3380 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3381 pub enum VariantKind {
3384 Struct(VariantStruct),
3387 impl Clean<VariantKind> for hir::VariantData {
3388 fn clean(&self, cx: &DocContext<'_>) -> VariantKind {
3390 hir::VariantData::Struct(..) => VariantKind::Struct(self.clean(cx)),
3391 hir::VariantData::Tuple(..) =>
3392 VariantKind::Tuple(self.fields().iter().map(|x| x.ty.clean(cx)).collect()),
3393 hir::VariantData::Unit(..) => VariantKind::CLike,
3398 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3400 pub filename: FileName,
3405 pub original: syntax_pos::Span,
3409 pub fn empty() -> Span {
3411 filename: FileName::Anon(0),
3412 loline: 0, locol: 0,
3413 hiline: 0, hicol: 0,
3414 original: syntax_pos::DUMMY_SP,
3418 pub fn span(&self) -> syntax_pos::Span {
3423 impl Clean<Span> for syntax_pos::Span {
3424 fn clean(&self, cx: &DocContext<'_>) -> Span {
3425 if self.is_dummy() {
3426 return Span::empty();
3429 let cm = cx.sess().source_map();
3430 let filename = cm.span_to_filename(*self);
3431 let lo = cm.lookup_char_pos(self.lo());
3432 let hi = cm.lookup_char_pos(self.hi());
3436 locol: lo.col.to_usize(),
3438 hicol: hi.col.to_usize(),
3444 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
3448 pub segments: Vec<PathSegment>,
3452 pub fn last_name(&self) -> &str {
3453 self.segments.last().expect("segments were empty").name.as_str()
3457 impl Clean<Path> for hir::Path {
3458 fn clean(&self, cx: &DocContext<'_>) -> Path {
3460 global: self.is_global(),
3462 segments: if self.is_global() { &self.segments[1..] } else { &self.segments }.clean(cx),
3467 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
3468 pub enum GenericArg {
3474 impl fmt::Display for GenericArg {
3475 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3477 GenericArg::Lifetime(lt) => lt.fmt(f),
3478 GenericArg::Type(ty) => ty.fmt(f),
3479 GenericArg::Const(ct) => ct.fmt(f),
3484 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
3485 pub enum GenericArgs {
3487 args: Vec<GenericArg>,
3488 bindings: Vec<TypeBinding>,
3492 output: Option<Type>,
3496 impl Clean<GenericArgs> for hir::GenericArgs {
3497 fn clean(&self, cx: &DocContext<'_>) -> GenericArgs {
3498 if self.parenthesized {
3499 let output = self.bindings[0].ty.clean(cx);
3500 GenericArgs::Parenthesized {
3501 inputs: self.inputs().clean(cx),
3502 output: if output != Type::Tuple(Vec::new()) { Some(output) } else { None }
3505 let elide_lifetimes = self.args.iter().all(|arg| match arg {
3506 hir::GenericArg::Lifetime(lt) => lt.is_elided(),
3509 GenericArgs::AngleBracketed {
3510 args: self.args.iter().filter_map(|arg| match arg {
3511 hir::GenericArg::Lifetime(lt) if !elide_lifetimes => {
3512 Some(GenericArg::Lifetime(lt.clean(cx)))
3514 hir::GenericArg::Lifetime(_) => None,
3515 hir::GenericArg::Type(ty) => Some(GenericArg::Type(ty.clean(cx))),
3516 hir::GenericArg::Const(ct) => Some(GenericArg::Const(ct.clean(cx))),
3518 bindings: self.bindings.clean(cx),
3524 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
3525 pub struct PathSegment {
3527 pub args: GenericArgs,
3530 impl Clean<PathSegment> for hir::PathSegment {
3531 fn clean(&self, cx: &DocContext<'_>) -> PathSegment {
3533 name: self.ident.name.clean(cx),
3534 args: self.with_generic_args(|generic_args| generic_args.clean(cx))
3539 fn strip_type(ty: Type) -> Type {
3541 Type::ResolvedPath { path, param_names, did, is_generic } => {
3542 Type::ResolvedPath { path: strip_path(&path), param_names, did, is_generic }
3544 Type::Tuple(inner_tys) => {
3545 Type::Tuple(inner_tys.iter().map(|t| strip_type(t.clone())).collect())
3547 Type::Slice(inner_ty) => Type::Slice(Box::new(strip_type(*inner_ty))),
3548 Type::Array(inner_ty, s) => Type::Array(Box::new(strip_type(*inner_ty)), s),
3549 Type::Unique(inner_ty) => Type::Unique(Box::new(strip_type(*inner_ty))),
3550 Type::RawPointer(m, inner_ty) => Type::RawPointer(m, Box::new(strip_type(*inner_ty))),
3551 Type::BorrowedRef { lifetime, mutability, type_ } => {
3552 Type::BorrowedRef { lifetime, mutability, type_: Box::new(strip_type(*type_)) }
3554 Type::QPath { name, self_type, trait_ } => {
3557 self_type: Box::new(strip_type(*self_type)), trait_: Box::new(strip_type(*trait_))
3564 fn strip_path(path: &Path) -> Path {
3565 let segments = path.segments.iter().map(|s| {
3567 name: s.name.clone(),
3568 args: GenericArgs::AngleBracketed {
3576 global: path.global,
3577 res: path.res.clone(),
3582 fn qpath_to_string(p: &hir::QPath) -> String {
3583 let segments = match *p {
3584 hir::QPath::Resolved(_, ref path) => &path.segments,
3585 hir::QPath::TypeRelative(_, ref segment) => return segment.ident.to_string(),
3588 let mut s = String::new();
3589 for (i, seg) in segments.iter().enumerate() {
3593 if seg.ident.name != kw::PathRoot {
3594 s.push_str(&*seg.ident.as_str());
3600 impl Clean<String> for Ident {
3602 fn clean(&self, cx: &DocContext<'_>) -> String {
3607 impl Clean<String> for ast::Name {
3609 fn clean(&self, _: &DocContext<'_>) -> String {
3614 impl Clean<String> for InternedString {
3616 fn clean(&self, _: &DocContext<'_>) -> String {
3621 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3622 pub struct Typedef {
3624 pub generics: Generics,
3627 impl Clean<Item> for doctree::Typedef {
3628 fn clean(&self, cx: &DocContext<'_>) -> Item {
3630 name: Some(self.name.clean(cx)),
3631 attrs: self.attrs.clean(cx),
3632 source: self.whence.clean(cx),
3633 def_id: cx.tcx.hir().local_def_id_from_hir_id(self.id),
3634 visibility: self.vis.clean(cx),
3635 stability: self.stab.clean(cx),
3636 deprecation: self.depr.clean(cx),
3637 inner: TypedefItem(Typedef {
3638 type_: self.ty.clean(cx),
3639 generics: self.gen.clean(cx),
3645 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3646 pub struct Existential {
3647 pub bounds: Vec<GenericBound>,
3648 pub generics: Generics,
3651 impl Clean<Item> for doctree::Existential {
3652 fn clean(&self, cx: &DocContext<'_>) -> Item {
3654 name: Some(self.name.clean(cx)),
3655 attrs: self.attrs.clean(cx),
3656 source: self.whence.clean(cx),
3657 def_id: cx.tcx.hir().local_def_id_from_hir_id(self.id),
3658 visibility: self.vis.clean(cx),
3659 stability: self.stab.clean(cx),
3660 deprecation: self.depr.clean(cx),
3661 inner: ExistentialItem(Existential {
3662 bounds: self.exist_ty.bounds.clean(cx),
3663 generics: self.exist_ty.generics.clean(cx),
3669 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
3670 pub struct BareFunctionDecl {
3671 pub unsafety: hir::Unsafety,
3672 pub generic_params: Vec<GenericParamDef>,
3677 impl Clean<BareFunctionDecl> for hir::BareFnTy {
3678 fn clean(&self, cx: &DocContext<'_>) -> BareFunctionDecl {
3679 let (generic_params, decl) = enter_impl_trait(cx, || {
3680 (self.generic_params.clean(cx), (&*self.decl, &self.arg_names[..]).clean(cx))
3683 unsafety: self.unsafety,
3691 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3694 pub mutability: Mutability,
3695 /// It's useful to have the value of a static documented, but I have no
3696 /// desire to represent expressions (that'd basically be all of the AST,
3697 /// which is huge!). So, have a string.
3701 impl Clean<Item> for doctree::Static {
3702 fn clean(&self, cx: &DocContext<'_>) -> Item {
3703 debug!("cleaning static {}: {:?}", self.name.clean(cx), self);
3705 name: Some(self.name.clean(cx)),
3706 attrs: self.attrs.clean(cx),
3707 source: self.whence.clean(cx),
3708 def_id: cx.tcx.hir().local_def_id_from_hir_id(self.id),
3709 visibility: self.vis.clean(cx),
3710 stability: self.stab.clean(cx),
3711 deprecation: self.depr.clean(cx),
3712 inner: StaticItem(Static {
3713 type_: self.type_.clean(cx),
3714 mutability: self.mutability.clean(cx),
3715 expr: print_const_expr(cx, self.expr),
3721 #[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug)]
3722 pub struct Constant {
3727 impl Clean<Item> for doctree::Constant {
3728 fn clean(&self, cx: &DocContext<'_>) -> Item {
3730 name: Some(self.name.clean(cx)),
3731 attrs: self.attrs.clean(cx),
3732 source: self.whence.clean(cx),
3733 def_id: cx.tcx.hir().local_def_id_from_hir_id(self.id),
3734 visibility: self.vis.clean(cx),
3735 stability: self.stab.clean(cx),
3736 deprecation: self.depr.clean(cx),
3737 inner: ConstantItem(Constant {
3738 type_: self.type_.clean(cx),
3739 expr: print_const_expr(cx, self.expr),
3745 #[derive(Debug, Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Copy, Hash)]
3746 pub enum Mutability {
3751 impl Clean<Mutability> for hir::Mutability {
3752 fn clean(&self, _: &DocContext<'_>) -> Mutability {
3754 &hir::MutMutable => Mutable,
3755 &hir::MutImmutable => Immutable,
3760 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Copy, Debug, Hash)]
3761 pub enum ImplPolarity {
3766 impl Clean<ImplPolarity> for hir::ImplPolarity {
3767 fn clean(&self, _: &DocContext<'_>) -> ImplPolarity {
3769 &hir::ImplPolarity::Positive => ImplPolarity::Positive,
3770 &hir::ImplPolarity::Negative => ImplPolarity::Negative,
3775 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3777 pub unsafety: hir::Unsafety,
3778 pub generics: Generics,
3779 pub provided_trait_methods: FxHashSet<String>,
3780 pub trait_: Option<Type>,
3782 pub items: Vec<Item>,
3783 pub polarity: Option<ImplPolarity>,
3784 pub synthetic: bool,
3785 pub blanket_impl: Option<Type>,
3788 pub fn get_auto_trait_and_blanket_impls(
3789 cx: &DocContext<'tcx>,
3791 param_env_def_id: DefId,
3792 ) -> impl Iterator<Item = Item> {
3793 AutoTraitFinder::new(cx).get_auto_trait_impls(ty, param_env_def_id).into_iter()
3794 .chain(BlanketImplFinder::new(cx).get_blanket_impls(ty, param_env_def_id))
3797 impl Clean<Vec<Item>> for doctree::Impl {
3798 fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
3799 let mut ret = Vec::new();
3800 let trait_ = self.trait_.clean(cx);
3801 let items = self.items.clean(cx);
3803 // If this impl block is an implementation of the Deref trait, then we
3804 // need to try inlining the target's inherent impl blocks as well.
3805 if trait_.def_id() == cx.tcx.lang_items().deref_trait() {
3806 build_deref_target_impls(cx, &items, &mut ret);
3809 let provided = trait_.def_id().map(|did| {
3810 cx.tcx.provided_trait_methods(did)
3812 .map(|meth| meth.ident.to_string())
3814 }).unwrap_or_default();
3818 attrs: self.attrs.clean(cx),
3819 source: self.whence.clean(cx),
3820 def_id: cx.tcx.hir().local_def_id_from_hir_id(self.id),
3821 visibility: self.vis.clean(cx),
3822 stability: self.stab.clean(cx),
3823 deprecation: self.depr.clean(cx),
3824 inner: ImplItem(Impl {
3825 unsafety: self.unsafety,
3826 generics: self.generics.clean(cx),
3827 provided_trait_methods: provided,
3829 for_: self.for_.clean(cx),
3831 polarity: Some(self.polarity.clean(cx)),
3840 fn build_deref_target_impls(cx: &DocContext<'_>,
3842 ret: &mut Vec<Item>) {
3843 use self::PrimitiveType::*;
3847 let target = match item.inner {
3848 TypedefItem(ref t, true) => &t.type_,
3851 let primitive = match *target {
3852 ResolvedPath { did, .. } if did.is_local() => continue,
3853 ResolvedPath { did, .. } => {
3854 ret.extend(inline::build_impls(cx, did));
3857 _ => match target.primitive_type() {
3862 let did = match primitive {
3863 Isize => tcx.lang_items().isize_impl(),
3864 I8 => tcx.lang_items().i8_impl(),
3865 I16 => tcx.lang_items().i16_impl(),
3866 I32 => tcx.lang_items().i32_impl(),
3867 I64 => tcx.lang_items().i64_impl(),
3868 I128 => tcx.lang_items().i128_impl(),
3869 Usize => tcx.lang_items().usize_impl(),
3870 U8 => tcx.lang_items().u8_impl(),
3871 U16 => tcx.lang_items().u16_impl(),
3872 U32 => tcx.lang_items().u32_impl(),
3873 U64 => tcx.lang_items().u64_impl(),
3874 U128 => tcx.lang_items().u128_impl(),
3875 F32 => tcx.lang_items().f32_impl(),
3876 F64 => tcx.lang_items().f64_impl(),
3877 Char => tcx.lang_items().char_impl(),
3879 Str => tcx.lang_items().str_impl(),
3880 Slice => tcx.lang_items().slice_impl(),
3881 Array => tcx.lang_items().slice_impl(),
3884 RawPointer => tcx.lang_items().const_ptr_impl(),
3888 CVarArgs => tcx.lang_items().va_list(),
3890 if let Some(did) = did {
3891 if !did.is_local() {
3892 inline::build_impl(cx, did, ret);
3898 impl Clean<Vec<Item>> for doctree::ExternCrate {
3899 fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
3901 let please_inline = self.vis.node.is_pub() && self.attrs.iter().any(|a| {
3902 a.check_name(sym::doc) && match a.meta_item_list() {
3903 Some(l) => attr::list_contains_name(&l, sym::inline),
3909 let mut visited = FxHashSet::default();
3915 index: CRATE_DEF_INDEX,
3919 if let Some(items) = inline::try_inline(cx, res, self.name, &mut visited) {
3926 attrs: self.attrs.clean(cx),
3927 source: self.whence.clean(cx),
3928 def_id: DefId { krate: self.cnum, index: CRATE_DEF_INDEX },
3929 visibility: self.vis.clean(cx),
3932 inner: ExternCrateItem(self.name.clean(cx), self.path.clone())
3937 impl Clean<Vec<Item>> for doctree::Import {
3938 fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
3939 // We consider inlining the documentation of `pub use` statements, but we
3940 // forcefully don't inline if this is not public or if the
3941 // #[doc(no_inline)] attribute is present.
3942 // Don't inline doc(hidden) imports so they can be stripped at a later stage.
3943 let mut denied = !self.vis.node.is_pub() || self.attrs.iter().any(|a| {
3944 a.check_name(sym::doc) && match a.meta_item_list() {
3945 Some(l) => attr::list_contains_name(&l, sym::no_inline) ||
3946 attr::list_contains_name(&l, sym::hidden),
3950 // Also check whether imports were asked to be inlined, in case we're trying to re-export a
3951 // crate in Rust 2018+
3952 let please_inline = self.attrs.lists(sym::doc).has_word(sym::inline);
3953 let path = self.path.clean(cx);
3954 let inner = if self.glob {
3956 let mut visited = FxHashSet::default();
3957 if let Some(items) = inline::try_inline_glob(cx, path.res, &mut visited) {
3962 Import::Glob(resolve_use_source(cx, path))
3964 let name = self.name;
3967 Res::Def(DefKind::Mod, did) => {
3968 if !did.is_local() && did.index == CRATE_DEF_INDEX {
3969 // if we're `pub use`ing an extern crate root, don't inline it unless we
3970 // were specifically asked for it
3978 let mut visited = FxHashSet::default();
3979 if let Some(items) = inline::try_inline(cx, path.res, name, &mut visited) {
3983 Import::Simple(name.clean(cx), resolve_use_source(cx, path))
3988 attrs: self.attrs.clean(cx),
3989 source: self.whence.clean(cx),
3990 def_id: cx.tcx.hir().local_def_id(ast::CRATE_NODE_ID),
3991 visibility: self.vis.clean(cx),
3994 inner: ImportItem(inner)
3999 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
4001 // use source as str;
4002 Simple(String, ImportSource),
4007 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
4008 pub struct ImportSource {
4010 pub did: Option<DefId>,
4013 impl Clean<Vec<Item>> for hir::ForeignMod {
4014 fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
4015 let mut items = self.items.clean(cx);
4016 for item in &mut items {
4017 if let ForeignFunctionItem(ref mut f) = item.inner {
4018 f.header.abi = self.abi;
4025 impl Clean<Item> for hir::ForeignItem {
4026 fn clean(&self, cx: &DocContext<'_>) -> Item {
4027 let inner = match self.node {
4028 hir::ForeignItemKind::Fn(ref decl, ref names, ref generics) => {
4029 let (generics, decl) = enter_impl_trait(cx, || {
4030 (generics.clean(cx), (&**decl, &names[..]).clean(cx))
4032 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
4033 ForeignFunctionItem(Function {
4036 header: hir::FnHeader {
4037 unsafety: hir::Unsafety::Unsafe,
4039 constness: hir::Constness::NotConst,
4040 asyncness: hir::IsAsync::NotAsync,
4046 hir::ForeignItemKind::Static(ref ty, mutbl) => {
4047 ForeignStaticItem(Static {
4048 type_: ty.clean(cx),
4049 mutability: mutbl.clean(cx),
4050 expr: String::new(),
4053 hir::ForeignItemKind::Type => {
4058 let local_did = cx.tcx.hir().local_def_id_from_hir_id(self.hir_id);
4061 name: Some(self.ident.clean(cx)),
4062 attrs: self.attrs.clean(cx),
4063 source: self.span.clean(cx),
4065 visibility: self.vis.clean(cx),
4066 stability: get_stability(cx, local_did),
4067 deprecation: get_deprecation(cx, local_did),
4075 pub trait ToSource {
4076 fn to_src(&self, cx: &DocContext<'_>) -> String;
4079 impl ToSource for syntax_pos::Span {
4080 fn to_src(&self, cx: &DocContext<'_>) -> String {
4081 debug!("converting span {:?} to snippet", self.clean(cx));
4082 let sn = match cx.sess().source_map().span_to_snippet(*self) {
4084 Err(_) => String::new()
4086 debug!("got snippet {}", sn);
4091 fn name_from_pat(p: &hir::Pat) -> String {
4093 debug!("Trying to get a name from pattern: {:?}", p);
4096 PatKind::Wild => "_".to_string(),
4097 PatKind::Binding(_, _, ident, _) => ident.to_string(),
4098 PatKind::TupleStruct(ref p, ..) | PatKind::Path(ref p) => qpath_to_string(p),
4099 PatKind::Struct(ref name, ref fields, etc) => {
4100 format!("{} {{ {}{} }}", qpath_to_string(name),
4101 fields.iter().map(|&Spanned { node: ref fp, .. }|
4102 format!("{}: {}", fp.ident, name_from_pat(&*fp.pat)))
4103 .collect::<Vec<String>>().join(", "),
4104 if etc { ", .." } else { "" }
4107 PatKind::Tuple(ref elts, _) => format!("({})", elts.iter().map(|p| name_from_pat(&**p))
4108 .collect::<Vec<String>>().join(", ")),
4109 PatKind::Box(ref p) => name_from_pat(&**p),
4110 PatKind::Ref(ref p, _) => name_from_pat(&**p),
4111 PatKind::Lit(..) => {
4112 warn!("tried to get argument name from PatKind::Lit, \
4113 which is silly in function arguments");
4116 PatKind::Range(..) => panic!("tried to get argument name from PatKind::Range, \
4117 which is not allowed in function arguments"),
4118 PatKind::Slice(ref begin, ref mid, ref end) => {
4119 let begin = begin.iter().map(|p| name_from_pat(&**p));
4120 let mid = mid.as_ref().map(|p| format!("..{}", name_from_pat(&**p))).into_iter();
4121 let end = end.iter().map(|p| name_from_pat(&**p));
4122 format!("[{}]", begin.chain(mid).chain(end).collect::<Vec<_>>().join(", "))
4127 fn print_const(cx: &DocContext<'_>, n: &ty::Const<'_>) -> String {
4129 ConstValue::Unevaluated(def_id, _) => {
4130 if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(def_id) {
4131 print_const_expr(cx, cx.tcx.hir().body_owned_by(hir_id))
4133 inline::print_inlined_const(cx, def_id)
4137 let mut s = n.to_string();
4138 // array lengths are obviously usize
4139 if s.ends_with("usize") {
4140 let n = s.len() - "usize".len();
4142 if s.ends_with(": ") {
4143 let n = s.len() - ": ".len();
4152 fn print_const_expr(cx: &DocContext<'_>, body: hir::BodyId) -> String {
4153 cx.tcx.hir().hir_to_pretty_string(body.hir_id)
4156 /// Given a type Path, resolve it to a Type using the TyCtxt
4157 fn resolve_type(cx: &DocContext<'_>,
4159 id: hir::HirId) -> Type {
4160 if id == hir::DUMMY_HIR_ID {
4161 debug!("resolve_type({:?})", path);
4163 debug!("resolve_type({:?},{:?})", path, id);
4166 let is_generic = match path.res {
4167 Res::PrimTy(p) => match p {
4168 hir::Str => return Primitive(PrimitiveType::Str),
4169 hir::Bool => return Primitive(PrimitiveType::Bool),
4170 hir::Char => return Primitive(PrimitiveType::Char),
4171 hir::Int(int_ty) => return Primitive(int_ty.into()),
4172 hir::Uint(uint_ty) => return Primitive(uint_ty.into()),
4173 hir::Float(float_ty) => return Primitive(float_ty.into()),
4175 Res::SelfTy(..) if path.segments.len() == 1 => {
4176 return Generic(kw::SelfUpper.to_string());
4178 Res::Def(DefKind::TyParam, _) if path.segments.len() == 1 => {
4179 return Generic(format!("{:#}", path));
4182 | Res::Def(DefKind::TyParam, _)
4183 | Res::Def(DefKind::AssocTy, _) => true,
4186 let did = register_res(&*cx, path.res);
4187 ResolvedPath { path: path, param_names: None, did: did, is_generic: is_generic }
4190 pub fn register_res(cx: &DocContext<'_>, res: Res) -> DefId {
4191 debug!("register_res({:?})", res);
4193 let (did, kind) = match res {
4194 Res::Def(DefKind::Fn, i) => (i, TypeKind::Function),
4195 Res::Def(DefKind::TyAlias, i) => (i, TypeKind::Typedef),
4196 Res::Def(DefKind::Enum, i) => (i, TypeKind::Enum),
4197 Res::Def(DefKind::Trait, i) => (i, TypeKind::Trait),
4198 Res::Def(DefKind::Struct, i) => (i, TypeKind::Struct),
4199 Res::Def(DefKind::Union, i) => (i, TypeKind::Union),
4200 Res::Def(DefKind::Mod, i) => (i, TypeKind::Module),
4201 Res::Def(DefKind::ForeignTy, i) => (i, TypeKind::Foreign),
4202 Res::Def(DefKind::Const, i) => (i, TypeKind::Const),
4203 Res::Def(DefKind::Static, i) => (i, TypeKind::Static),
4204 Res::Def(DefKind::Variant, i) => (cx.tcx.parent(i).expect("cannot get parent def id"),
4206 Res::Def(DefKind::Macro(mac_kind), i) => match mac_kind {
4207 MacroKind::Bang => (i, TypeKind::Macro),
4208 MacroKind::Attr => (i, TypeKind::Attr),
4209 MacroKind::Derive => (i, TypeKind::Derive),
4210 MacroKind::ProcMacroStub => unreachable!(),
4212 Res::Def(DefKind::TraitAlias, i) => (i, TypeKind::TraitAlias),
4213 Res::SelfTy(Some(def_id), _) => (def_id, TypeKind::Trait),
4214 Res::SelfTy(_, Some(impl_def_id)) => return impl_def_id,
4215 _ => return res.def_id()
4217 if did.is_local() { return did }
4218 inline::record_extern_fqn(cx, did, kind);
4219 if let TypeKind::Trait = kind {
4220 inline::record_extern_trait(cx, did);
4225 fn resolve_use_source(cx: &DocContext<'_>, path: Path) -> ImportSource {
4227 did: if path.res.opt_def_id().is_none() {
4230 Some(register_res(cx, path.res))
4236 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
4239 pub imported_from: Option<String>,
4242 impl Clean<Item> for doctree::Macro {
4243 fn clean(&self, cx: &DocContext<'_>) -> Item {
4244 let name = self.name.clean(cx);
4246 name: Some(name.clone()),
4247 attrs: self.attrs.clean(cx),
4248 source: self.whence.clean(cx),
4249 visibility: Some(Public),
4250 stability: self.stab.clean(cx),
4251 deprecation: self.depr.clean(cx),
4252 def_id: self.def_id,
4253 inner: MacroItem(Macro {
4254 source: format!("macro_rules! {} {{\n{}}}",
4256 self.matchers.iter().map(|span| {
4257 format!(" {} => {{ ... }};\n", span.to_src(cx))
4258 }).collect::<String>()),
4259 imported_from: self.imported_from.clean(cx),
4265 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
4266 pub struct ProcMacro {
4267 pub kind: MacroKind,
4268 pub helpers: Vec<String>,
4271 impl Clean<Item> for doctree::ProcMacro {
4272 fn clean(&self, cx: &DocContext<'_>) -> Item {
4274 name: Some(self.name.clean(cx)),
4275 attrs: self.attrs.clean(cx),
4276 source: self.whence.clean(cx),
4277 visibility: Some(Public),
4278 stability: self.stab.clean(cx),
4279 deprecation: self.depr.clean(cx),
4280 def_id: cx.tcx.hir().local_def_id_from_hir_id(self.id),
4281 inner: ProcMacroItem(ProcMacro {
4283 helpers: self.helpers.clean(cx),
4289 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
4290 pub struct Stability {
4291 pub level: stability::StabilityLevel,
4292 pub feature: Option<String>,
4294 pub deprecation: Option<Deprecation>,
4295 pub unstable_reason: Option<String>,
4296 pub issue: Option<u32>,
4299 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
4300 pub struct Deprecation {
4301 pub since: Option<String>,
4302 pub note: Option<String>,
4305 impl Clean<Stability> for attr::Stability {
4306 fn clean(&self, _: &DocContext<'_>) -> Stability {
4308 level: stability::StabilityLevel::from_attr_level(&self.level),
4309 feature: Some(self.feature.to_string()).filter(|f| !f.is_empty()),
4310 since: match self.level {
4311 attr::Stable {ref since} => since.to_string(),
4314 deprecation: self.rustc_depr.as_ref().map(|d| {
4316 note: Some(d.reason.to_string()).filter(|r| !r.is_empty()),
4317 since: Some(d.since.to_string()).filter(|d| !d.is_empty()),
4320 unstable_reason: match self.level {
4321 attr::Unstable { reason: Some(ref reason), .. } => Some(reason.to_string()),
4324 issue: match self.level {
4325 attr::Unstable {issue, ..} => Some(issue),
4332 impl<'a> Clean<Stability> for &'a attr::Stability {
4333 fn clean(&self, dc: &DocContext<'_>) -> Stability {
4338 impl Clean<Deprecation> for attr::Deprecation {
4339 fn clean(&self, _: &DocContext<'_>) -> Deprecation {
4341 since: self.since.map(|s| s.to_string()).filter(|s| !s.is_empty()),
4342 note: self.note.map(|n| n.to_string()).filter(|n| !n.is_empty()),
4347 /// An equality constraint on an associated type, e.g., `A = Bar` in `Foo<A = Bar>`
4348 #[derive(Clone, PartialEq, Eq, RustcDecodable, RustcEncodable, Debug, Hash)]
4349 pub struct TypeBinding {
4354 impl Clean<TypeBinding> for hir::TypeBinding {
4355 fn clean(&self, cx: &DocContext<'_>) -> TypeBinding {
4357 name: self.ident.name.clean(cx),
4358 ty: self.ty.clean(cx)
4363 pub fn def_id_to_path(
4364 cx: &DocContext<'_>,
4366 name: Option<String>
4368 let crate_name = name.unwrap_or_else(|| cx.tcx.crate_name(did.krate).to_string());
4369 let relative = cx.tcx.def_path(did).data.into_iter().filter_map(|elem| {
4370 // extern blocks have an empty name
4371 let s = elem.data.to_string();
4378 once(crate_name).chain(relative).collect()
4381 pub fn enter_impl_trait<F, R>(cx: &DocContext<'_>, f: F) -> R
4385 let old_bounds = mem::replace(&mut *cx.impl_trait_bounds.borrow_mut(), Default::default());
4387 assert!(cx.impl_trait_bounds.borrow().is_empty());
4388 *cx.impl_trait_bounds.borrow_mut() = old_bounds;
4392 // Start of code copied from rust-clippy
4394 pub fn path_to_def_local(tcx: TyCtxt<'_, '_, '_>, path: &[Symbol]) -> Option<DefId> {
4395 let krate = tcx.hir().krate();
4396 let mut items = krate.module.item_ids.clone();
4397 let mut path_it = path.iter().peekable();
4400 let segment = path_it.next()?;
4402 for item_id in mem::replace(&mut items, HirVec::new()).iter() {
4403 let item = tcx.hir().expect_item_by_hir_id(item_id.id);
4404 if item.ident.name == *segment {
4405 if path_it.peek().is_none() {
4406 return Some(tcx.hir().local_def_id_from_hir_id(item_id.id))
4409 items = match &item.node {
4410 &hir::ItemKind::Mod(ref m) => m.item_ids.clone(),
4411 _ => panic!("Unexpected item {:?} in path {:?} path")
4419 pub fn path_to_def(tcx: TyCtxt<'_, '_, '_>, path: &[Symbol]) -> Option<DefId> {
4420 let crates = tcx.crates();
4424 .find(|&&krate| tcx.crate_name(krate) == path[0]);
4426 if let Some(krate) = krate {
4429 index: CRATE_DEF_INDEX,
4431 let mut items = tcx.item_children(krate);
4432 let mut path_it = path.iter().skip(1).peekable();
4435 let segment = path_it.next()?;
4437 for item in mem::replace(&mut items, &[]).iter() {
4438 if item.ident.name == *segment {
4439 if path_it.peek().is_none() {
4440 return match item.res {
4441 def::Res::Def(DefKind::Trait, did) => Some(did),
4446 items = tcx.item_children(item.res.def_id());
4456 // End of code copied from rust-clippy
4458 #[derive(Eq, PartialEq, Hash, Copy, Clone, Debug)]
4459 enum RegionTarget<'tcx> {
4460 Region(Region<'tcx>),
4461 RegionVid(RegionVid)
4464 #[derive(Default, Debug, Clone)]
4465 struct RegionDeps<'tcx> {
4466 larger: FxHashSet<RegionTarget<'tcx>>,
4467 smaller: FxHashSet<RegionTarget<'tcx>>
4470 #[derive(Eq, PartialEq, Hash, Debug)]
4472 TraitBound(Vec<PathSegment>, Vec<SimpleBound>, Vec<GenericParamDef>, hir::TraitBoundModifier),
4476 impl From<GenericBound> for SimpleBound {
4477 fn from(bound: GenericBound) -> Self {
4478 match bound.clone() {
4479 GenericBound::Outlives(l) => SimpleBound::Outlives(l),
4480 GenericBound::TraitBound(t, mod_) => match t.trait_ {
4481 Type::ResolvedPath { path, param_names, .. } => {
4482 SimpleBound::TraitBound(path.segments,
4484 .map_or_else(|| Vec::new(), |v| v.iter()
4485 .map(|p| SimpleBound::from(p.clone()))
4490 _ => panic!("Unexpected bound {:?}", bound),