1 //! This module contains the "cleaned" pieces of the AST, and the functions
11 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
12 use rustc_data_structures::sync::Lrc;
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, Def, CtorKind};
22 use rustc::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
23 use rustc::hir::map::DisambiguatedDefPathData;
24 use rustc::ty::subst::{Kind, InternalSubsts, SubstsRef, UnpackedKind};
25 use rustc::ty::{self, DefIdTree, TyCtxt, Region, RegionVid, Ty, AdtKind};
26 use rustc::ty::fold::TypeFolder;
27 use rustc::ty::layout::VariantIdx;
28 use rustc::util::nodemap::{FxHashMap, FxHashSet};
29 use syntax::ast::{self, AttrStyle, Ident};
31 use syntax::ext::base::MacroKind;
32 use syntax::source_map::{dummy_spanned, Spanned};
34 use syntax::symbol::keywords::{self, Keyword};
35 use syntax::symbol::InternedString;
36 use syntax_pos::{self, DUMMY_SP, Pos, FileName};
38 use std::collections::hash_map::Entry;
40 use std::hash::{Hash, Hasher};
41 use std::default::Default;
42 use std::{mem, slice, vec};
43 use std::iter::{FromIterator, once};
45 use std::str::FromStr;
46 use std::cell::RefCell;
50 use parking_lot::ReentrantMutex;
52 use crate::core::{self, DocContext};
55 use crate::html::render::{cache, ExternalLocation};
56 use crate::html::item_type::ItemType;
60 use self::auto_trait::AutoTraitFinder;
61 use self::blanket_impl::BlanketImplFinder;
63 pub use self::Type::*;
64 pub use self::Mutability::*;
65 pub use self::ItemEnum::*;
66 pub use self::SelfTy::*;
67 pub use self::FunctionRetTy::*;
68 pub use self::Visibility::{Public, Inherited};
70 thread_local!(pub static MAX_DEF_ID: RefCell<FxHashMap<CrateNum, DefId>> = Default::default());
72 const FN_OUTPUT_NAME: &'static str = "Output";
74 // extract the stability index for a node from tcx, if possible
75 fn get_stability(cx: &DocContext<'_>, def_id: DefId) -> Option<Stability> {
76 cx.tcx.lookup_stability(def_id).clean(cx)
79 fn get_deprecation(cx: &DocContext<'_>, def_id: DefId) -> Option<Deprecation> {
80 cx.tcx.lookup_deprecation(def_id).clean(cx)
84 fn clean(&self, cx: &DocContext<'_>) -> T;
87 impl<T: Clean<U>, U> Clean<Vec<U>> for [T] {
88 fn clean(&self, cx: &DocContext<'_>) -> Vec<U> {
89 self.iter().map(|x| x.clean(cx)).collect()
93 impl<T: Clean<U>, U, V: Idx> Clean<IndexVec<V, U>> for IndexVec<V, T> {
94 fn clean(&self, cx: &DocContext<'_>) -> IndexVec<V, U> {
95 self.iter().map(|x| x.clean(cx)).collect()
99 impl<T: Clean<U>, U> Clean<U> for P<T> {
100 fn clean(&self, cx: &DocContext<'_>) -> U {
105 impl<T: Clean<U>, U> Clean<U> for Rc<T> {
106 fn clean(&self, cx: &DocContext<'_>) -> U {
111 impl<T: Clean<U>, U> Clean<Option<U>> for Option<T> {
112 fn clean(&self, cx: &DocContext<'_>) -> Option<U> {
113 self.as_ref().map(|v| v.clean(cx))
117 impl<T, U> Clean<U> for ty::Binder<T> where T: Clean<U> {
118 fn clean(&self, cx: &DocContext<'_>) -> U {
119 self.skip_binder().clean(cx)
123 impl<T: Clean<U>, U> Clean<Vec<U>> for P<[T]> {
124 fn clean(&self, cx: &DocContext<'_>) -> Vec<U> {
125 self.iter().map(|x| x.clean(cx)).collect()
129 #[derive(Clone, Debug)]
132 pub version: Option<String>,
134 pub module: Option<Item>,
135 pub externs: Vec<(CrateNum, ExternalCrate)>,
136 pub primitives: Vec<(DefId, PrimitiveType, Attributes)>,
137 // These are later on moved into `CACHEKEY`, leaving the map empty.
138 // Only here so that they can be filtered through the rustdoc passes.
139 pub external_traits: Arc<ReentrantMutex<RefCell<FxHashMap<DefId, Trait>>>>,
140 pub masked_crates: FxHashSet<CrateNum>,
143 impl<'a, 'tcx> Clean<Crate> for visit_ast::RustdocVisitor<'a, 'tcx> {
144 fn clean(&self, cx: &DocContext<'_>) -> Crate {
145 use crate::visit_lib::LibEmbargoVisitor;
148 let mut r = cx.renderinfo.borrow_mut();
149 r.deref_trait_did = cx.tcx.lang_items().deref_trait();
150 r.deref_mut_trait_did = cx.tcx.lang_items().deref_mut_trait();
151 r.owned_box_did = cx.tcx.lang_items().owned_box();
154 let mut externs = Vec::new();
155 for &cnum in cx.tcx.crates().iter() {
156 externs.push((cnum, cnum.clean(cx)));
157 // Analyze doc-reachability for extern items
158 LibEmbargoVisitor::new(cx).visit_lib(cnum);
160 externs.sort_by(|&(a, _), &(b, _)| a.cmp(&b));
162 // Clean the crate, translating the entire libsyntax AST to one that is
163 // understood by rustdoc.
164 let mut module = self.module.clean(cx);
165 let mut masked_crates = FxHashSet::default();
168 ModuleItem(ref module) => {
169 for it in &module.items {
170 // `compiler_builtins` should be masked too, but we can't apply
171 // `#[doc(masked)]` to the injected `extern crate` because it's unstable.
172 if it.is_extern_crate()
173 && (it.attrs.has_doc_flag("masked")
174 || self.cx.tcx.is_compiler_builtins(it.def_id.krate))
176 masked_crates.insert(it.def_id.krate);
183 let ExternalCrate { name, src, primitives, keywords, .. } = LOCAL_CRATE.clean(cx);
185 let m = match module.inner {
186 ModuleItem(ref mut m) => m,
189 m.items.extend(primitives.iter().map(|&(def_id, prim, ref attrs)| {
191 source: Span::empty(),
192 name: Some(prim.to_url_str().to_string()),
193 attrs: attrs.clone(),
194 visibility: Some(Public),
195 stability: get_stability(cx, def_id),
196 deprecation: get_deprecation(cx, def_id),
198 inner: PrimitiveItem(prim),
201 m.items.extend(keywords.into_iter().map(|(def_id, kw, attrs)| {
203 source: Span::empty(),
204 name: Some(kw.clone()),
206 visibility: Some(Public),
207 stability: get_stability(cx, def_id),
208 deprecation: get_deprecation(cx, def_id),
210 inner: KeywordItem(kw),
219 module: Some(module),
222 external_traits: cx.external_traits.clone(),
228 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
229 pub struct ExternalCrate {
232 pub attrs: Attributes,
233 pub primitives: Vec<(DefId, PrimitiveType, Attributes)>,
234 pub keywords: Vec<(DefId, String, Attributes)>,
237 impl Clean<ExternalCrate> for CrateNum {
238 fn clean(&self, cx: &DocContext<'_>) -> ExternalCrate {
239 let root = DefId { krate: *self, index: CRATE_DEF_INDEX };
240 let krate_span = cx.tcx.def_span(root);
241 let krate_src = cx.sess().source_map().span_to_filename(krate_span);
243 // Collect all inner modules which are tagged as implementations of
246 // Note that this loop only searches the top-level items of the crate,
247 // and this is intentional. If we were to search the entire crate for an
248 // item tagged with `#[doc(primitive)]` then we would also have to
249 // search the entirety of external modules for items tagged
250 // `#[doc(primitive)]`, which is a pretty inefficient process (decoding
251 // all that metadata unconditionally).
253 // In order to keep the metadata load under control, the
254 // `#[doc(primitive)]` feature is explicitly designed to only allow the
255 // primitive tags to show up as the top level items in a crate.
257 // Also note that this does not attempt to deal with modules tagged
258 // duplicately for the same primitive. This is handled later on when
259 // rendering by delegating everything to a hash map.
260 let as_primitive = |def: Def| {
261 if let Def::Mod(def_id) = def {
262 let attrs = cx.tcx.get_attrs(def_id).clean(cx);
264 for attr in attrs.lists("doc") {
265 if let Some(v) = attr.value_str() {
266 if attr.check_name("primitive") {
267 prim = PrimitiveType::from_str(&v.as_str());
271 // FIXME: should warn on unknown primitives?
275 return prim.map(|p| (def_id, p, attrs));
279 let primitives = if root.is_local() {
280 cx.tcx.hir().krate().module.item_ids.iter().filter_map(|&id| {
281 let item = cx.tcx.hir().expect_item_by_hir_id(id.id);
283 hir::ItemKind::Mod(_) => {
284 as_primitive(Def::Mod(cx.tcx.hir().local_def_id_from_hir_id(id.id)))
286 hir::ItemKind::Use(ref path, hir::UseKind::Single)
287 if item.vis.node.is_pub() => {
288 as_primitive(path.def).map(|(_, prim, attrs)| {
289 // Pretend the primitive is local.
290 (cx.tcx.hir().local_def_id_from_hir_id(id.id), prim, attrs)
297 cx.tcx.item_children(root).iter().map(|item| item.def)
298 .filter_map(as_primitive).collect()
301 let as_keyword = |def: Def| {
302 if let Def::Mod(def_id) = def {
303 let attrs = cx.tcx.get_attrs(def_id).clean(cx);
304 let mut keyword = None;
305 for attr in attrs.lists("doc") {
306 if let Some(v) = attr.value_str() {
307 if attr.check_name("keyword") {
308 keyword = Keyword::from_str(&v.as_str()).ok()
309 .map(|x| x.name().to_string());
310 if keyword.is_some() {
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(_) => {
326 as_keyword(Def::Mod(cx.tcx.hir().local_def_id_from_hir_id(id.id)))
328 hir::ItemKind::Use(ref path, hir::UseKind::Single)
329 if item.vis.node.is_pub() => {
330 as_keyword(path.def).map(|(_, prim, attrs)| {
331 (cx.tcx.hir().local_def_id_from_hir_id(id.id), prim, attrs)
338 cx.tcx.item_children(root).iter().map(|item| item.def)
339 .filter_map(as_keyword).collect()
343 name: cx.tcx.crate_name(*self).to_string(),
345 attrs: cx.tcx.get_attrs(root).clean(cx),
352 /// Anything with a source location and set of attributes and, optionally, a
353 /// name. That is, anything that can be documented. This doesn't correspond
354 /// directly to the AST's concept of an item; it's a strict superset.
355 #[derive(Clone, RustcEncodable, RustcDecodable)]
359 /// Not everything has a name. E.g., impls
360 pub name: Option<String>,
361 pub attrs: Attributes,
363 pub visibility: Option<Visibility>,
365 pub stability: Option<Stability>,
366 pub deprecation: Option<Deprecation>,
369 impl fmt::Debug for Item {
370 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
372 let fake = MAX_DEF_ID.with(|m| m.borrow().get(&self.def_id.krate)
373 .map(|id| self.def_id >= *id).unwrap_or(false));
374 let def_id: &dyn fmt::Debug = if fake { &"**FAKE**" } else { &self.def_id };
376 fmt.debug_struct("Item")
377 .field("source", &self.source)
378 .field("name", &self.name)
379 .field("attrs", &self.attrs)
380 .field("inner", &self.inner)
381 .field("visibility", &self.visibility)
382 .field("def_id", def_id)
383 .field("stability", &self.stability)
384 .field("deprecation", &self.deprecation)
390 /// Finds the `doc` attribute as a NameValue and returns the corresponding
392 pub fn doc_value<'a>(&'a self) -> Option<&'a str> {
393 self.attrs.doc_value()
395 /// Finds all `doc` attributes as NameValues and returns their corresponding values, joined
397 pub fn collapsed_doc_value(&self) -> Option<String> {
398 self.attrs.collapsed_doc_value()
401 pub fn links(&self) -> Vec<(String, String)> {
402 self.attrs.links(&self.def_id.krate)
405 pub fn is_crate(&self) -> bool {
407 StrippedItem(box ModuleItem(Module { is_crate: true, ..})) |
408 ModuleItem(Module { is_crate: true, ..}) => true,
412 pub fn is_mod(&self) -> bool {
413 self.type_() == ItemType::Module
415 pub fn is_trait(&self) -> bool {
416 self.type_() == ItemType::Trait
418 pub fn is_struct(&self) -> bool {
419 self.type_() == ItemType::Struct
421 pub fn is_enum(&self) -> bool {
422 self.type_() == ItemType::Enum
424 pub fn is_variant(&self) -> bool {
425 self.type_() == ItemType::Variant
427 pub fn is_associated_type(&self) -> bool {
428 self.type_() == ItemType::AssociatedType
430 pub fn is_associated_const(&self) -> bool {
431 self.type_() == ItemType::AssociatedConst
433 pub fn is_method(&self) -> bool {
434 self.type_() == ItemType::Method
436 pub fn is_ty_method(&self) -> bool {
437 self.type_() == ItemType::TyMethod
439 pub fn is_typedef(&self) -> bool {
440 self.type_() == ItemType::Typedef
442 pub fn is_primitive(&self) -> bool {
443 self.type_() == ItemType::Primitive
445 pub fn is_union(&self) -> bool {
446 self.type_() == ItemType::Union
448 pub fn is_import(&self) -> bool {
449 self.type_() == ItemType::Import
451 pub fn is_extern_crate(&self) -> bool {
452 self.type_() == ItemType::ExternCrate
454 pub fn is_keyword(&self) -> bool {
455 self.type_() == ItemType::Keyword
458 pub fn is_stripped(&self) -> bool {
459 match self.inner { StrippedItem(..) => true, _ => false }
461 pub fn has_stripped_fields(&self) -> Option<bool> {
463 StructItem(ref _struct) => Some(_struct.fields_stripped),
464 UnionItem(ref union) => Some(union.fields_stripped),
465 VariantItem(Variant { kind: VariantKind::Struct(ref vstruct)} ) => {
466 Some(vstruct.fields_stripped)
472 pub fn stability_class(&self) -> Option<String> {
473 self.stability.as_ref().and_then(|ref s| {
474 let mut classes = Vec::with_capacity(2);
476 if s.level == stability::Unstable {
477 classes.push("unstable");
480 if s.deprecation.is_some() {
481 classes.push("deprecated");
484 if classes.len() != 0 {
485 Some(classes.join(" "))
492 pub fn stable_since(&self) -> Option<&str> {
493 self.stability.as_ref().map(|s| &s.since[..])
496 pub fn is_non_exhaustive(&self) -> bool {
497 self.attrs.other_attrs.iter()
498 .any(|a| a.check_name("non_exhaustive"))
501 /// Returns a documentation-level item type from the item.
502 pub fn type_(&self) -> ItemType {
506 /// Returns the info in the item's `#[deprecated]` or `#[rustc_deprecated]` attributes.
508 /// If the item is not deprecated, returns `None`.
509 pub fn deprecation(&self) -> Option<&Deprecation> {
512 .or_else(|| self.stability.as_ref().and_then(|s| s.deprecation.as_ref()))
514 pub fn is_default(&self) -> bool {
516 ItemEnum::MethodItem(ref meth) => {
517 if let Some(defaultness) = meth.defaultness {
518 defaultness.has_value() && !defaultness.is_final()
528 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
530 ExternCrateItem(String, Option<String>),
535 FunctionItem(Function),
537 TypedefItem(Typedef, bool /* is associated type */),
538 ExistentialItem(Existential, bool /* is associated type */),
540 ConstantItem(Constant),
542 TraitAliasItem(TraitAlias),
544 /// A method signature only. Used for required methods in traits (ie,
545 /// non-default-methods).
546 TyMethodItem(TyMethod),
547 /// A method with a body.
549 StructFieldItem(Type),
550 VariantItem(Variant),
551 /// `fn`s from an extern block
552 ForeignFunctionItem(Function),
553 /// `static`s from an extern block
554 ForeignStaticItem(Static),
555 /// `type`s from an extern block
558 ProcMacroItem(ProcMacro),
559 PrimitiveItem(PrimitiveType),
560 AssociatedConstItem(Type, Option<String>),
561 AssociatedTypeItem(Vec<GenericBound>, Option<Type>),
562 /// An item that has been stripped by a rustdoc pass
563 StrippedItem(Box<ItemEnum>),
568 pub fn generics(&self) -> Option<&Generics> {
570 ItemEnum::StructItem(ref s) => &s.generics,
571 ItemEnum::EnumItem(ref e) => &e.generics,
572 ItemEnum::FunctionItem(ref f) => &f.generics,
573 ItemEnum::TypedefItem(ref t, _) => &t.generics,
574 ItemEnum::ExistentialItem(ref t, _) => &t.generics,
575 ItemEnum::TraitItem(ref t) => &t.generics,
576 ItemEnum::ImplItem(ref i) => &i.generics,
577 ItemEnum::TyMethodItem(ref i) => &i.generics,
578 ItemEnum::MethodItem(ref i) => &i.generics,
579 ItemEnum::ForeignFunctionItem(ref f) => &f.generics,
580 ItemEnum::TraitAliasItem(ref ta) => &ta.generics,
585 pub fn is_associated(&self) -> bool {
587 ItemEnum::TypedefItem(_, _) |
588 ItemEnum::AssociatedTypeItem(_, _) => true,
594 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
596 pub items: Vec<Item>,
600 impl Clean<Item> for doctree::Module {
601 fn clean(&self, cx: &DocContext<'_>) -> Item {
602 let name = if self.name.is_some() {
603 self.name.expect("No name provided").clean(cx)
608 // maintain a stack of mod ids, for doc comment path resolution
609 // but we also need to resolve the module's own docs based on whether its docs were written
610 // inside or outside the module, so check for that
611 let attrs = self.attrs.clean(cx);
613 let mut items: Vec<Item> = vec![];
614 items.extend(self.extern_crates.iter().flat_map(|x| x.clean(cx)));
615 items.extend(self.imports.iter().flat_map(|x| x.clean(cx)));
616 items.extend(self.structs.iter().map(|x| x.clean(cx)));
617 items.extend(self.unions.iter().map(|x| x.clean(cx)));
618 items.extend(self.enums.iter().map(|x| x.clean(cx)));
619 items.extend(self.fns.iter().map(|x| x.clean(cx)));
620 items.extend(self.foreigns.iter().flat_map(|x| x.clean(cx)));
621 items.extend(self.mods.iter().map(|x| x.clean(cx)));
622 items.extend(self.typedefs.iter().map(|x| x.clean(cx)));
623 items.extend(self.existentials.iter().map(|x| x.clean(cx)));
624 items.extend(self.statics.iter().map(|x| x.clean(cx)));
625 items.extend(self.constants.iter().map(|x| x.clean(cx)));
626 items.extend(self.traits.iter().map(|x| x.clean(cx)));
627 items.extend(self.impls.iter().flat_map(|x| x.clean(cx)));
628 items.extend(self.macros.iter().map(|x| x.clean(cx)));
629 items.extend(self.proc_macros.iter().map(|x| x.clean(cx)));
630 items.extend(self.trait_aliases.iter().map(|x| x.clean(cx)));
632 // determine if we should display the inner contents or
633 // the outer `mod` item for the source code.
635 let cm = cx.sess().source_map();
636 let outer = cm.lookup_char_pos(self.where_outer.lo());
637 let inner = cm.lookup_char_pos(self.where_inner.lo());
638 if outer.file.start_pos == inner.file.start_pos {
642 // mod foo; (and a separate SourceFile for the contents)
650 source: whence.clean(cx),
651 visibility: self.vis.clean(cx),
652 stability: self.stab.clean(cx),
653 deprecation: self.depr.clean(cx),
654 def_id: cx.tcx.hir().local_def_id(self.id),
655 inner: ModuleItem(Module {
656 is_crate: self.is_crate,
663 pub struct ListAttributesIter<'a> {
664 attrs: slice::Iter<'a, ast::Attribute>,
665 current_list: vec::IntoIter<ast::NestedMetaItem>,
669 impl<'a> Iterator for ListAttributesIter<'a> {
670 type Item = ast::NestedMetaItem;
672 fn next(&mut self) -> Option<Self::Item> {
673 if let Some(nested) = self.current_list.next() {
677 for attr in &mut self.attrs {
678 if let Some(list) = attr.meta_item_list() {
679 if attr.check_name(self.name) {
680 self.current_list = list.into_iter();
681 if let Some(nested) = self.current_list.next() {
691 fn size_hint(&self) -> (usize, Option<usize>) {
692 let lower = self.current_list.len();
697 pub trait AttributesExt {
698 /// Finds an attribute as List and returns the list of attributes nested inside.
699 fn lists<'a>(&'a self, name: &'a str) -> ListAttributesIter<'a>;
702 impl AttributesExt for [ast::Attribute] {
703 fn lists<'a>(&'a self, name: &'a str) -> ListAttributesIter<'a> {
706 current_list: Vec::new().into_iter(),
712 pub trait NestedAttributesExt {
713 /// Returns `true` if the attribute list contains a specific `Word`
714 fn has_word(self, word: &str) -> bool;
717 impl<I: IntoIterator<Item=ast::NestedMetaItem>> NestedAttributesExt for I {
718 fn has_word(self, word: &str) -> bool {
719 self.into_iter().any(|attr| attr.is_word() && attr.check_name(word))
723 /// A portion of documentation, extracted from a `#[doc]` attribute.
725 /// Each variant contains the line number within the complete doc-comment where the fragment
726 /// starts, as well as the Span where the corresponding doc comment or attribute is located.
728 /// Included files are kept separate from inline doc comments so that proper line-number
729 /// information can be given when a doctest fails. Sugared doc comments and "raw" doc comments are
730 /// kept separate because of issue #42760.
731 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
732 pub enum DocFragment {
733 /// A doc fragment created from a `///` or `//!` doc comment.
734 SugaredDoc(usize, syntax_pos::Span, String),
735 /// A doc fragment created from a "raw" `#[doc=""]` attribute.
736 RawDoc(usize, syntax_pos::Span, String),
737 /// A doc fragment created from a `#[doc(include="filename")]` attribute. Contains both the
738 /// given filename and the file contents.
739 Include(usize, syntax_pos::Span, String, String),
743 pub fn as_str(&self) -> &str {
745 DocFragment::SugaredDoc(_, _, ref s) => &s[..],
746 DocFragment::RawDoc(_, _, ref s) => &s[..],
747 DocFragment::Include(_, _, _, ref s) => &s[..],
751 pub fn span(&self) -> syntax_pos::Span {
753 DocFragment::SugaredDoc(_, span, _) |
754 DocFragment::RawDoc(_, span, _) |
755 DocFragment::Include(_, span, _, _) => span,
760 impl<'a> FromIterator<&'a DocFragment> for String {
761 fn from_iter<T>(iter: T) -> Self
763 T: IntoIterator<Item = &'a DocFragment>
765 iter.into_iter().fold(String::new(), |mut acc, frag| {
770 DocFragment::SugaredDoc(_, _, ref docs)
771 | DocFragment::RawDoc(_, _, ref docs)
772 | DocFragment::Include(_, _, _, ref docs) =>
781 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, Default)]
782 pub struct Attributes {
783 pub doc_strings: Vec<DocFragment>,
784 pub other_attrs: Vec<ast::Attribute>,
785 pub cfg: Option<Arc<Cfg>>,
786 pub span: Option<syntax_pos::Span>,
787 /// map from Rust paths to resolved defs and potential URL fragments
788 pub links: Vec<(String, Option<DefId>, Option<String>)>,
789 pub inner_docs: bool,
793 /// Extracts the content from an attribute `#[doc(cfg(content))]`.
794 fn extract_cfg(mi: &ast::MetaItem) -> Option<&ast::MetaItem> {
795 use syntax::ast::NestedMetaItem::MetaItem;
797 if let ast::MetaItemKind::List(ref nmis) = mi.node {
799 if let MetaItem(ref cfg_mi) = nmis[0] {
800 if cfg_mi.check_name("cfg") {
801 if let ast::MetaItemKind::List(ref cfg_nmis) = cfg_mi.node {
802 if cfg_nmis.len() == 1 {
803 if let MetaItem(ref content_mi) = cfg_nmis[0] {
804 return Some(content_mi);
816 /// Reads a `MetaItem` from within an attribute, looks for whether it is a
817 /// `#[doc(include="file")]`, and returns the filename and contents of the file as loaded from
819 fn extract_include(mi: &ast::MetaItem)
820 -> Option<(String, String)>
822 mi.meta_item_list().and_then(|list| {
824 if meta.check_name("include") {
825 // the actual compiled `#[doc(include="filename")]` gets expanded to
826 // `#[doc(include(file="filename", contents="file contents")]` so we need to
827 // look for that instead
828 return meta.meta_item_list().and_then(|list| {
829 let mut filename: Option<String> = None;
830 let mut contents: Option<String> = None;
833 if it.check_name("file") {
834 if let Some(name) = it.value_str() {
835 filename = Some(name.to_string());
837 } else if it.check_name("contents") {
838 if let Some(docs) = it.value_str() {
839 contents = Some(docs.to_string());
844 if let (Some(filename), Some(contents)) = (filename, contents) {
845 Some((filename, contents))
857 pub fn has_doc_flag(&self, flag: &str) -> bool {
858 for attr in &self.other_attrs {
859 if !attr.check_name("doc") { continue; }
861 if let Some(items) = attr.meta_item_list() {
862 if items.iter().filter_map(|i| i.meta_item()).any(|it| it.check_name(flag)) {
871 pub fn from_ast(diagnostic: &::errors::Handler,
872 attrs: &[ast::Attribute]) -> Attributes {
873 let mut doc_strings = vec![];
875 let mut cfg = Cfg::True;
876 let mut doc_line = 0;
878 let other_attrs = attrs.iter().filter_map(|attr| {
879 attr.with_desugared_doc(|attr| {
880 if attr.check_name("doc") {
881 if let Some(mi) = attr.meta() {
882 if let Some(value) = mi.value_str() {
883 // Extracted #[doc = "..."]
884 let value = value.to_string();
886 doc_line += value.lines().count();
888 if attr.is_sugared_doc {
889 doc_strings.push(DocFragment::SugaredDoc(line, attr.span, value));
891 doc_strings.push(DocFragment::RawDoc(line, attr.span, value));
895 sp = Some(attr.span);
898 } else if let Some(cfg_mi) = Attributes::extract_cfg(&mi) {
899 // Extracted #[doc(cfg(...))]
900 match Cfg::parse(cfg_mi) {
901 Ok(new_cfg) => cfg &= new_cfg,
902 Err(e) => diagnostic.span_err(e.span, e.msg),
905 } else if let Some((filename, contents)) = Attributes::extract_include(&mi)
908 doc_line += contents.lines().count();
909 doc_strings.push(DocFragment::Include(line,
920 // treat #[target_feature(enable = "feat")] attributes as if they were
921 // #[doc(cfg(target_feature = "feat"))] attributes as well
922 for attr in attrs.lists("target_feature") {
923 if attr.check_name("enable") {
924 if let Some(feat) = attr.value_str() {
925 let meta = attr::mk_name_value_item_str(Ident::from_str("target_feature"),
926 dummy_spanned(feat));
927 if let Ok(feat_cfg) = Cfg::parse(&meta) {
934 let inner_docs = attrs.iter()
935 .filter(|a| a.check_name("doc"))
937 .map_or(true, |a| a.style == AttrStyle::Inner);
942 cfg: if cfg == Cfg::True { None } else { Some(Arc::new(cfg)) },
949 /// Finds the `doc` attribute as a NameValue and returns the corresponding
951 pub fn doc_value<'a>(&'a self) -> Option<&'a str> {
952 self.doc_strings.first().map(|s| s.as_str())
955 /// Finds all `doc` attributes as NameValues and returns their corresponding values, joined
957 pub fn collapsed_doc_value(&self) -> Option<String> {
958 if !self.doc_strings.is_empty() {
959 Some(self.doc_strings.iter().collect())
965 /// Gets links as a vector
967 /// Cache must be populated before call
968 pub fn links(&self, krate: &CrateNum) -> Vec<(String, String)> {
969 use crate::html::format::href;
971 self.links.iter().filter_map(|&(ref s, did, ref fragment)| {
974 if let Some((mut href, ..)) = href(did) {
975 if let Some(ref fragment) = *fragment {
977 href.push_str(fragment);
979 Some((s.clone(), href))
985 if let Some(ref fragment) = *fragment {
987 let url = match cache.extern_locations.get(krate) {
988 Some(&(_, ref src, ExternalLocation::Local)) =>
989 src.to_str().expect("invalid file path"),
990 Some(&(_, _, ExternalLocation::Remote(ref s))) => s,
991 Some(&(_, _, ExternalLocation::Unknown)) | None =>
992 "https://doc.rust-lang.org/nightly",
994 // This is a primitive so the url is done "by hand".
995 let tail = fragment.find('#').unwrap_or_else(|| fragment.len());
997 format!("{}{}std/primitive.{}.html{}",
999 if !url.ends_with('/') { "/" } else { "" },
1001 &fragment[tail..])))
1003 panic!("This isn't a primitive?!");
1011 impl PartialEq for Attributes {
1012 fn eq(&self, rhs: &Self) -> bool {
1013 self.doc_strings == rhs.doc_strings &&
1014 self.cfg == rhs.cfg &&
1015 self.span == rhs.span &&
1016 self.links == rhs.links &&
1017 self.other_attrs.iter().map(|attr| attr.id).eq(rhs.other_attrs.iter().map(|attr| attr.id))
1021 impl Eq for Attributes {}
1023 impl Hash for Attributes {
1024 fn hash<H: Hasher>(&self, hasher: &mut H) {
1025 self.doc_strings.hash(hasher);
1026 self.cfg.hash(hasher);
1027 self.span.hash(hasher);
1028 self.links.hash(hasher);
1029 for attr in &self.other_attrs {
1030 attr.id.hash(hasher);
1035 impl AttributesExt for Attributes {
1036 fn lists<'a>(&'a self, name: &'a str) -> ListAttributesIter<'a> {
1037 self.other_attrs.lists(name)
1041 impl Clean<Attributes> for [ast::Attribute] {
1042 fn clean(&self, cx: &DocContext<'_>) -> Attributes {
1043 Attributes::from_ast(cx.sess().diagnostic(), self)
1047 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1048 pub enum GenericBound {
1049 TraitBound(PolyTrait, hir::TraitBoundModifier),
1054 fn maybe_sized(cx: &DocContext<'_>) -> GenericBound {
1055 let did = cx.tcx.require_lang_item(lang_items::SizedTraitLangItem);
1056 let empty = cx.tcx.intern_substs(&[]);
1057 let path = external_path(cx, &cx.tcx.item_name(did).as_str(),
1058 Some(did), false, vec![], empty);
1059 inline::record_extern_fqn(cx, did, TypeKind::Trait);
1060 GenericBound::TraitBound(PolyTrait {
1061 trait_: ResolvedPath {
1067 generic_params: Vec::new(),
1068 }, hir::TraitBoundModifier::Maybe)
1071 fn is_sized_bound(&self, cx: &DocContext<'_>) -> bool {
1072 use rustc::hir::TraitBoundModifier as TBM;
1073 if let GenericBound::TraitBound(PolyTrait { ref trait_, .. }, TBM::None) = *self {
1074 if trait_.def_id() == cx.tcx.lang_items().sized_trait() {
1081 fn get_poly_trait(&self) -> Option<PolyTrait> {
1082 if let GenericBound::TraitBound(ref p, _) = *self {
1083 return Some(p.clone())
1088 fn get_trait_type(&self) -> Option<Type> {
1089 if let GenericBound::TraitBound(PolyTrait { ref trait_, .. }, _) = *self {
1090 Some(trait_.clone())
1097 impl Clean<GenericBound> for hir::GenericBound {
1098 fn clean(&self, cx: &DocContext<'_>) -> GenericBound {
1100 hir::GenericBound::Outlives(lt) => GenericBound::Outlives(lt.clean(cx)),
1101 hir::GenericBound::Trait(ref t, modifier) => {
1102 GenericBound::TraitBound(t.clean(cx), modifier)
1108 fn external_generic_args(
1109 cx: &DocContext<'_>,
1110 trait_did: Option<DefId>,
1112 bindings: Vec<TypeBinding>,
1113 substs: SubstsRef<'_>,
1115 let mut skip_self = has_self;
1116 let mut ty_sty = None;
1117 let args: Vec<_> = substs.iter().filter_map(|kind| match kind.unpack() {
1118 UnpackedKind::Lifetime(lt) => {
1119 lt.clean(cx).and_then(|lt| Some(GenericArg::Lifetime(lt)))
1121 UnpackedKind::Type(_) if skip_self => {
1125 UnpackedKind::Type(ty) => {
1126 ty_sty = Some(&ty.sty);
1127 Some(GenericArg::Type(ty.clean(cx)))
1129 UnpackedKind::Const(ct) => Some(GenericArg::Const(ct.clean(cx))),
1133 // Attempt to sugar an external path like Fn<(A, B,), C> to Fn(A, B) -> C
1134 Some(did) if cx.tcx.lang_items().fn_trait_kind(did).is_some() => {
1135 assert!(ty_sty.is_some());
1136 let inputs = match ty_sty {
1137 Some(ty::Tuple(ref tys)) => tys.iter().map(|t| t.clean(cx)).collect(),
1138 _ => return GenericArgs::AngleBracketed { args, bindings },
1141 // FIXME(#20299) return type comes from a projection now
1142 // match types[1].sty {
1143 // ty::Tuple(ref v) if v.is_empty() => None, // -> ()
1144 // _ => Some(types[1].clean(cx))
1146 GenericArgs::Parenthesized { inputs, output }
1149 GenericArgs::AngleBracketed { args, bindings }
1154 // trait_did should be set to a trait's DefId if called on a TraitRef, in order to sugar
1155 // from Fn<(A, B,), C> to Fn(A, B) -> C
1156 fn external_path(cx: &DocContext<'_>, name: &str, trait_did: Option<DefId>, has_self: bool,
1157 bindings: Vec<TypeBinding>, substs: SubstsRef<'_>) -> Path {
1161 segments: vec![PathSegment {
1162 name: name.to_string(),
1163 args: external_generic_args(cx, trait_did, has_self, bindings, substs)
1168 impl<'a, 'tcx> Clean<GenericBound> for (&'a ty::TraitRef<'tcx>, Vec<TypeBinding>) {
1169 fn clean(&self, cx: &DocContext<'_>) -> GenericBound {
1170 let (trait_ref, ref bounds) = *self;
1171 inline::record_extern_fqn(cx, trait_ref.def_id, TypeKind::Trait);
1172 let path = external_path(cx, &cx.tcx.item_name(trait_ref.def_id).as_str(),
1173 Some(trait_ref.def_id), true, bounds.clone(), trait_ref.substs);
1175 debug!("ty::TraitRef\n subst: {:?}\n", trait_ref.substs);
1177 // collect any late bound regions
1178 let mut late_bounds = vec![];
1179 for ty_s in trait_ref.input_types().skip(1) {
1180 if let ty::Tuple(ts) = ty_s.sty {
1182 if let ty::Ref(ref reg, _, _) = ty_s.sty {
1183 if let &ty::RegionKind::ReLateBound(..) = *reg {
1184 debug!(" hit an ReLateBound {:?}", reg);
1185 if let Some(Lifetime(name)) = reg.clean(cx) {
1186 late_bounds.push(GenericParamDef {
1188 kind: GenericParamDefKind::Lifetime,
1197 GenericBound::TraitBound(
1199 trait_: ResolvedPath {
1202 did: trait_ref.def_id,
1205 generic_params: late_bounds,
1207 hir::TraitBoundModifier::None
1212 impl<'tcx> Clean<GenericBound> for ty::TraitRef<'tcx> {
1213 fn clean(&self, cx: &DocContext<'_>) -> GenericBound {
1214 (self, vec![]).clean(cx)
1218 impl<'tcx> Clean<Option<Vec<GenericBound>>> for InternalSubsts<'tcx> {
1219 fn clean(&self, cx: &DocContext<'_>) -> Option<Vec<GenericBound>> {
1220 let mut v = Vec::new();
1221 v.extend(self.regions().filter_map(|r| r.clean(cx)).map(GenericBound::Outlives));
1222 v.extend(self.types().map(|t| GenericBound::TraitBound(PolyTrait {
1223 trait_: t.clean(cx),
1224 generic_params: Vec::new(),
1225 }, hir::TraitBoundModifier::None)));
1226 if !v.is_empty() {Some(v)} else {None}
1230 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1231 pub struct Lifetime(String);
1234 pub fn get_ref<'a>(&'a self) -> &'a str {
1235 let Lifetime(ref s) = *self;
1240 pub fn statik() -> Lifetime {
1241 Lifetime("'static".to_string())
1245 impl Clean<Lifetime> for hir::Lifetime {
1246 fn clean(&self, cx: &DocContext<'_>) -> Lifetime {
1247 if self.hir_id != hir::DUMMY_HIR_ID {
1248 let def = cx.tcx.named_region(self.hir_id);
1250 Some(rl::Region::EarlyBound(_, node_id, _)) |
1251 Some(rl::Region::LateBound(_, node_id, _)) |
1252 Some(rl::Region::Free(_, node_id)) => {
1253 if let Some(lt) = cx.lt_substs.borrow().get(&node_id).cloned() {
1260 Lifetime(self.name.ident().to_string())
1264 impl Clean<Lifetime> for hir::GenericParam {
1265 fn clean(&self, _: &DocContext<'_>) -> Lifetime {
1267 hir::GenericParamKind::Lifetime { .. } => {
1268 if self.bounds.len() > 0 {
1269 let mut bounds = self.bounds.iter().map(|bound| match bound {
1270 hir::GenericBound::Outlives(lt) => lt,
1273 let name = bounds.next().expect("no more bounds").name.ident();
1274 let mut s = format!("{}: {}", self.name.ident(), name);
1275 for bound in bounds {
1276 s.push_str(&format!(" + {}", bound.name.ident()));
1280 Lifetime(self.name.ident().to_string())
1288 impl Clean<Constant> for hir::ConstArg {
1289 fn clean(&self, cx: &DocContext<'_>) -> Constant {
1291 type_: cx.tcx.type_of(cx.tcx.hir().body_owner_def_id(self.value.body)).clean(cx),
1292 expr: print_const_expr(cx, self.value.body),
1297 impl<'tcx> Clean<Lifetime> for ty::GenericParamDef {
1298 fn clean(&self, _cx: &DocContext<'_>) -> Lifetime {
1299 Lifetime(self.name.to_string())
1303 impl Clean<Option<Lifetime>> for ty::RegionKind {
1304 fn clean(&self, cx: &DocContext<'_>) -> Option<Lifetime> {
1306 ty::ReStatic => Some(Lifetime::statik()),
1307 ty::ReLateBound(_, ty::BrNamed(_, name)) => Some(Lifetime(name.to_string())),
1308 ty::ReEarlyBound(ref data) => Some(Lifetime(data.name.clean(cx))),
1310 ty::ReLateBound(..) |
1314 ty::RePlaceholder(..) |
1316 ty::ReClosureBound(_) |
1318 debug!("Cannot clean region {:?}", self);
1325 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1326 pub enum WherePredicate {
1327 BoundPredicate { ty: Type, bounds: Vec<GenericBound> },
1328 RegionPredicate { lifetime: Lifetime, bounds: Vec<GenericBound> },
1329 EqPredicate { lhs: Type, rhs: Type },
1332 impl WherePredicate {
1333 pub fn get_bounds(&self) -> Option<&[GenericBound]> {
1335 WherePredicate::BoundPredicate { ref bounds, .. } => Some(bounds),
1336 WherePredicate::RegionPredicate { ref bounds, .. } => Some(bounds),
1342 impl Clean<WherePredicate> for hir::WherePredicate {
1343 fn clean(&self, cx: &DocContext<'_>) -> WherePredicate {
1345 hir::WherePredicate::BoundPredicate(ref wbp) => {
1346 WherePredicate::BoundPredicate {
1347 ty: wbp.bounded_ty.clean(cx),
1348 bounds: wbp.bounds.clean(cx)
1352 hir::WherePredicate::RegionPredicate(ref wrp) => {
1353 WherePredicate::RegionPredicate {
1354 lifetime: wrp.lifetime.clean(cx),
1355 bounds: wrp.bounds.clean(cx)
1359 hir::WherePredicate::EqPredicate(ref wrp) => {
1360 WherePredicate::EqPredicate {
1361 lhs: wrp.lhs_ty.clean(cx),
1362 rhs: wrp.rhs_ty.clean(cx)
1369 impl<'a> Clean<Option<WherePredicate>> for ty::Predicate<'a> {
1370 fn clean(&self, cx: &DocContext<'_>) -> Option<WherePredicate> {
1371 use rustc::ty::Predicate;
1374 Predicate::Trait(ref pred) => Some(pred.clean(cx)),
1375 Predicate::Subtype(ref pred) => Some(pred.clean(cx)),
1376 Predicate::RegionOutlives(ref pred) => pred.clean(cx),
1377 Predicate::TypeOutlives(ref pred) => pred.clean(cx),
1378 Predicate::Projection(ref pred) => Some(pred.clean(cx)),
1380 Predicate::WellFormed(..) |
1381 Predicate::ObjectSafe(..) |
1382 Predicate::ClosureKind(..) |
1383 Predicate::ConstEvaluatable(..) => panic!("not user writable"),
1388 impl<'a> Clean<WherePredicate> for ty::TraitPredicate<'a> {
1389 fn clean(&self, cx: &DocContext<'_>) -> WherePredicate {
1390 WherePredicate::BoundPredicate {
1391 ty: self.trait_ref.self_ty().clean(cx),
1392 bounds: vec![self.trait_ref.clean(cx)]
1397 impl<'tcx> Clean<WherePredicate> for ty::SubtypePredicate<'tcx> {
1398 fn clean(&self, _cx: &DocContext<'_>) -> WherePredicate {
1399 panic!("subtype predicates are an internal rustc artifact \
1400 and should not be seen by rustdoc")
1404 impl<'tcx> Clean<Option<WherePredicate>> for
1405 ty::OutlivesPredicate<ty::Region<'tcx>,ty::Region<'tcx>> {
1407 fn clean(&self, cx: &DocContext<'_>) -> Option<WherePredicate> {
1408 let ty::OutlivesPredicate(ref a, ref b) = *self;
1411 (ty::ReEmpty, ty::ReEmpty) => {
1417 Some(WherePredicate::RegionPredicate {
1418 lifetime: a.clean(cx).expect("failed to clean lifetime"),
1419 bounds: vec![GenericBound::Outlives(b.clean(cx).expect("failed to clean bounds"))]
1424 impl<'tcx> Clean<Option<WherePredicate>> for ty::OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>> {
1425 fn clean(&self, cx: &DocContext<'_>) -> Option<WherePredicate> {
1426 let ty::OutlivesPredicate(ref ty, ref lt) = *self;
1429 ty::ReEmpty => return None,
1433 Some(WherePredicate::BoundPredicate {
1435 bounds: vec![GenericBound::Outlives(lt.clean(cx).expect("failed to clean lifetimes"))]
1440 impl<'tcx> Clean<WherePredicate> for ty::ProjectionPredicate<'tcx> {
1441 fn clean(&self, cx: &DocContext<'_>) -> WherePredicate {
1442 WherePredicate::EqPredicate {
1443 lhs: self.projection_ty.clean(cx),
1444 rhs: self.ty.clean(cx)
1449 impl<'tcx> Clean<Type> for ty::ProjectionTy<'tcx> {
1450 fn clean(&self, cx: &DocContext<'_>) -> Type {
1451 let trait_ = match self.trait_ref(cx.tcx).clean(cx) {
1452 GenericBound::TraitBound(t, _) => t.trait_,
1453 GenericBound::Outlives(_) => panic!("cleaning a trait got a lifetime"),
1456 name: cx.tcx.associated_item(self.item_def_id).ident.name.clean(cx),
1457 self_type: box self.self_ty().clean(cx),
1463 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1464 pub enum GenericParamDefKind {
1468 bounds: Vec<GenericBound>,
1469 default: Option<Type>,
1470 synthetic: Option<hir::SyntheticTyParamKind>,
1478 impl GenericParamDefKind {
1479 pub fn is_type(&self) -> bool {
1481 GenericParamDefKind::Type { .. } => true,
1486 pub fn get_type(&self, cx: &DocContext<'_>) -> Option<Type> {
1488 GenericParamDefKind::Type { did, .. } => {
1489 rustc_typeck::checked_type_of(cx.tcx, did, false).map(|t| t.clean(cx))
1491 GenericParamDefKind::Const { ref ty, .. } => Some(ty.clone()),
1492 GenericParamDefKind::Lifetime => None,
1497 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1498 pub struct GenericParamDef {
1501 pub kind: GenericParamDefKind,
1504 impl GenericParamDef {
1505 pub fn is_synthetic_type_param(&self) -> bool {
1507 GenericParamDefKind::Lifetime |
1508 GenericParamDefKind::Const { .. } => false,
1509 GenericParamDefKind::Type { ref synthetic, .. } => synthetic.is_some(),
1513 pub fn is_type(&self) -> bool {
1517 pub fn get_type(&self, cx: &DocContext<'_>) -> Option<Type> {
1518 self.kind.get_type(cx)
1521 pub fn get_bounds(&self) -> Option<&[GenericBound]> {
1523 GenericParamDefKind::Type { ref bounds, .. } => Some(bounds),
1529 impl Clean<GenericParamDef> for ty::GenericParamDef {
1530 fn clean(&self, cx: &DocContext<'_>) -> GenericParamDef {
1531 let (name, kind) = match self.kind {
1532 ty::GenericParamDefKind::Lifetime => {
1533 (self.name.to_string(), GenericParamDefKind::Lifetime)
1535 ty::GenericParamDefKind::Type { has_default, .. } => {
1536 cx.renderinfo.borrow_mut().external_param_names
1537 .insert(self.def_id, self.name.clean(cx));
1538 let default = if has_default {
1539 Some(cx.tcx.type_of(self.def_id).clean(cx))
1543 (self.name.clean(cx), GenericParamDefKind::Type {
1545 bounds: vec![], // These are filled in from the where-clauses.
1550 ty::GenericParamDefKind::Const { .. } => {
1551 (self.name.clean(cx), GenericParamDefKind::Const {
1553 ty: cx.tcx.type_of(self.def_id).clean(cx),
1565 impl Clean<GenericParamDef> for hir::GenericParam {
1566 fn clean(&self, cx: &DocContext<'_>) -> GenericParamDef {
1567 let (name, kind) = match self.kind {
1568 hir::GenericParamKind::Lifetime { .. } => {
1569 let name = if self.bounds.len() > 0 {
1570 let mut bounds = self.bounds.iter().map(|bound| match bound {
1571 hir::GenericBound::Outlives(lt) => lt,
1574 let name = bounds.next().expect("no more bounds").name.ident();
1575 let mut s = format!("{}: {}", self.name.ident(), name);
1576 for bound in bounds {
1577 s.push_str(&format!(" + {}", bound.name.ident()));
1581 self.name.ident().to_string()
1583 (name, GenericParamDefKind::Lifetime)
1585 hir::GenericParamKind::Type { ref default, synthetic } => {
1586 (self.name.ident().name.clean(cx), GenericParamDefKind::Type {
1587 did: cx.tcx.hir().local_def_id_from_hir_id(self.hir_id),
1588 bounds: self.bounds.clean(cx),
1589 default: default.clean(cx),
1590 synthetic: synthetic,
1593 hir::GenericParamKind::Const { ref ty } => {
1594 (self.name.ident().name.clean(cx), GenericParamDefKind::Const {
1595 did: cx.tcx.hir().local_def_id_from_hir_id(self.hir_id),
1608 // maybe use a Generic enum and use Vec<Generic>?
1609 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Default, Hash)]
1610 pub struct Generics {
1611 pub params: Vec<GenericParamDef>,
1612 pub where_predicates: Vec<WherePredicate>,
1615 impl Clean<Generics> for hir::Generics {
1616 fn clean(&self, cx: &DocContext<'_>) -> Generics {
1617 // Synthetic type-parameters are inserted after normal ones.
1618 // In order for normal parameters to be able to refer to synthetic ones,
1619 // scans them first.
1620 fn is_impl_trait(param: &hir::GenericParam) -> bool {
1622 hir::GenericParamKind::Type { synthetic, .. } => {
1623 synthetic == Some(hir::SyntheticTyParamKind::ImplTrait)
1628 let impl_trait_params = self.params
1630 .filter(|param| is_impl_trait(param))
1632 let param: GenericParamDef = param.clean(cx);
1634 GenericParamDefKind::Lifetime => unreachable!(),
1635 GenericParamDefKind::Type { did, ref bounds, .. } => {
1636 cx.impl_trait_bounds.borrow_mut().insert(did, bounds.clone());
1638 GenericParamDefKind::Const { .. } => unreachable!(),
1642 .collect::<Vec<_>>();
1644 let mut params = Vec::with_capacity(self.params.len());
1645 for p in self.params.iter().filter(|p| !is_impl_trait(p)) {
1646 let p = p.clean(cx);
1649 params.extend(impl_trait_params);
1651 let mut generics = Generics {
1653 where_predicates: self.where_clause.predicates.clean(cx),
1656 // Some duplicates are generated for ?Sized bounds between type params and where
1657 // predicates. The point in here is to move the bounds definitions from type params
1658 // to where predicates when such cases occur.
1659 for where_pred in &mut generics.where_predicates {
1661 WherePredicate::BoundPredicate { ty: Generic(ref name), ref mut bounds } => {
1662 if bounds.is_empty() {
1663 for param in &mut generics.params {
1665 GenericParamDefKind::Lifetime => {}
1666 GenericParamDefKind::Type { bounds: ref mut ty_bounds, .. } => {
1667 if ¶m.name == name {
1668 mem::swap(bounds, ty_bounds);
1672 GenericParamDefKind::Const { .. } => {}
1684 impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics,
1685 &'a Lrc<ty::GenericPredicates<'tcx>>) {
1686 fn clean(&self, cx: &DocContext<'_>) -> Generics {
1687 use self::WherePredicate as WP;
1689 let (gens, preds) = *self;
1691 // Bounds in the type_params and lifetimes fields are repeated in the
1692 // predicates field (see rustc_typeck::collect::ty_generics), so remove
1694 let stripped_typarams = gens.params.iter().filter_map(|param| match param.kind {
1695 ty::GenericParamDefKind::Lifetime => None,
1696 ty::GenericParamDefKind::Type { .. } => {
1697 if param.name == keywords::SelfUpper.name().as_str() {
1698 assert_eq!(param.index, 0);
1701 Some(param.clean(cx))
1703 ty::GenericParamDefKind::Const { .. } => {
1704 unimplemented!() // FIXME(const_generics)
1706 }).collect::<Vec<GenericParamDef>>();
1708 let mut where_predicates = preds.predicates.iter()
1709 .flat_map(|(p, _)| p.clean(cx))
1710 .collect::<Vec<_>>();
1712 // Type parameters and have a Sized bound by default unless removed with
1713 // ?Sized. Scan through the predicates and mark any type parameter with
1714 // a Sized bound, removing the bounds as we find them.
1716 // Note that associated types also have a sized bound by default, but we
1717 // don't actually know the set of associated types right here so that's
1718 // handled in cleaning associated types
1719 let mut sized_params = FxHashSet::default();
1720 where_predicates.retain(|pred| {
1722 WP::BoundPredicate { ty: Generic(ref g), ref bounds } => {
1723 if bounds.iter().any(|b| b.is_sized_bound(cx)) {
1724 sized_params.insert(g.clone());
1734 // Run through the type parameters again and insert a ?Sized
1735 // unbound for any we didn't find to be Sized.
1736 for tp in &stripped_typarams {
1737 if !sized_params.contains(&tp.name) {
1738 where_predicates.push(WP::BoundPredicate {
1739 ty: Type::Generic(tp.name.clone()),
1740 bounds: vec![GenericBound::maybe_sized(cx)],
1745 // It would be nice to collect all of the bounds on a type and recombine
1746 // them if possible, to avoid e.g., `where T: Foo, T: Bar, T: Sized, T: 'a`
1747 // and instead see `where T: Foo + Bar + Sized + 'a`
1752 .flat_map(|param| match param.kind {
1753 ty::GenericParamDefKind::Lifetime => Some(param.clean(cx)),
1754 ty::GenericParamDefKind::Type { .. } => None,
1755 ty::GenericParamDefKind::Const { .. } => Some(param.clean(cx)),
1756 }).chain(simplify::ty_params(stripped_typarams).into_iter())
1758 where_predicates: simplify::where_clauses(cx, where_predicates),
1763 /// The point of this function is to replace bounds with types.
1765 /// i.e. `[T, U]` when you have the following bounds: `T: Display, U: Option<T>` will return
1766 /// `[Display, Option]` (we just returns the list of the types, we don't care about the
1767 /// wrapped types in here).
1769 generics: &Generics,
1771 cx: &DocContext<'_>,
1773 ) -> FxHashSet<Type> {
1774 let arg_s = arg.to_string();
1775 let mut res = FxHashSet::default();
1776 if recurse >= 10 { // FIXME: remove this whole recurse thing when the recursion bug is fixed
1779 if arg.is_full_generic() {
1780 if let Some(where_pred) = generics.where_predicates.iter().find(|g| {
1782 &WherePredicate::BoundPredicate { ref ty, .. } => ty.def_id() == arg.def_id(),
1786 let bounds = where_pred.get_bounds().unwrap_or_else(|| &[]);
1787 for bound in bounds.iter() {
1789 GenericBound::TraitBound(ref poly_trait, _) => {
1790 for x in poly_trait.generic_params.iter() {
1794 if let Some(ty) = x.get_type(cx) {
1795 let adds = get_real_types(generics, &ty, cx, recurse + 1);
1796 if !adds.is_empty() {
1798 } else if !ty.is_full_generic() {
1808 if let Some(bound) = generics.params.iter().find(|g| {
1809 g.is_type() && g.name == arg_s
1811 for bound in bound.get_bounds().unwrap_or_else(|| &[]) {
1812 if let Some(ty) = bound.get_trait_type() {
1813 let adds = get_real_types(generics, &ty, cx, recurse + 1);
1814 if !adds.is_empty() {
1816 } else if !ty.is_full_generic() {
1817 res.insert(ty.clone());
1823 res.insert(arg.clone());
1824 if let Some(gens) = arg.generics() {
1825 for gen in gens.iter() {
1826 if gen.is_full_generic() {
1827 let adds = get_real_types(generics, gen, cx, recurse + 1);
1828 if !adds.is_empty() {
1832 res.insert(gen.clone());
1840 /// Return the full list of types when bounds have been resolved.
1842 /// i.e. `fn foo<A: Display, B: Option<A>>(x: u32, y: B)` will return
1843 /// `[u32, Display, Option]`.
1844 pub fn get_all_types(
1845 generics: &Generics,
1847 cx: &DocContext<'_>,
1848 ) -> (Vec<Type>, Vec<Type>) {
1849 let mut all_types = FxHashSet::default();
1850 for arg in decl.inputs.values.iter() {
1851 if arg.type_.is_self_type() {
1854 let args = get_real_types(generics, &arg.type_, cx, 0);
1855 if !args.is_empty() {
1856 all_types.extend(args);
1858 all_types.insert(arg.type_.clone());
1862 let ret_types = match decl.output {
1863 FunctionRetTy::Return(ref return_type) => {
1864 let mut ret = get_real_types(generics, &return_type, cx, 0);
1866 ret.insert(return_type.clone());
1868 ret.into_iter().collect()
1872 (all_types.into_iter().collect(), ret_types)
1875 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1877 pub generics: Generics,
1879 pub header: hir::FnHeader,
1880 pub defaultness: Option<hir::Defaultness>,
1881 pub all_types: Vec<Type>,
1882 pub ret_types: Vec<Type>,
1885 impl<'a> Clean<Method> for (&'a hir::MethodSig, &'a hir::Generics, hir::BodyId,
1886 Option<hir::Defaultness>) {
1887 fn clean(&self, cx: &DocContext<'_>) -> Method {
1888 let (generics, decl) = enter_impl_trait(cx, || {
1889 (self.1.clean(cx), (&*self.0.decl, self.2).clean(cx))
1891 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
1895 header: self.0.header,
1896 defaultness: self.3,
1903 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1904 pub struct TyMethod {
1905 pub header: hir::FnHeader,
1907 pub generics: Generics,
1908 pub all_types: Vec<Type>,
1909 pub ret_types: Vec<Type>,
1912 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1913 pub struct Function {
1915 pub generics: Generics,
1916 pub header: hir::FnHeader,
1917 pub all_types: Vec<Type>,
1918 pub ret_types: Vec<Type>,
1921 impl Clean<Item> for doctree::Function {
1922 fn clean(&self, cx: &DocContext<'_>) -> Item {
1923 let (generics, decl) = enter_impl_trait(cx, || {
1924 (self.generics.clean(cx), (&self.decl, self.body).clean(cx))
1927 let did = cx.tcx.hir().local_def_id_from_hir_id(self.id);
1928 let constness = if cx.tcx.is_min_const_fn(did) {
1929 hir::Constness::Const
1931 hir::Constness::NotConst
1933 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
1935 name: Some(self.name.clean(cx)),
1936 attrs: self.attrs.clean(cx),
1937 source: self.whence.clean(cx),
1938 visibility: self.vis.clean(cx),
1939 stability: self.stab.clean(cx),
1940 deprecation: self.depr.clean(cx),
1942 inner: FunctionItem(Function {
1945 header: hir::FnHeader { constness, ..self.header },
1953 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1955 pub inputs: Arguments,
1956 pub output: FunctionRetTy,
1957 pub attrs: Attributes,
1961 pub fn self_type(&self) -> Option<SelfTy> {
1962 self.inputs.values.get(0).and_then(|v| v.to_self())
1965 /// Returns the sugared return type for an async function.
1967 /// For example, if the return type is `impl std::future::Future<Output = i32>`, this function
1968 /// will return `i32`.
1972 /// This function will panic if the return type does not match the expected sugaring for async
1974 pub fn sugared_async_return_type(&self) -> FunctionRetTy {
1975 match &self.output {
1976 FunctionRetTy::Return(Type::ImplTrait(bounds)) => {
1978 GenericBound::TraitBound(PolyTrait { trait_, .. }, ..) => {
1979 let bindings = trait_.bindings().unwrap();
1980 FunctionRetTy::Return(bindings[0].ty.clone())
1982 _ => panic!("unexpected desugaring of async function"),
1985 _ => panic!("unexpected desugaring of async function"),
1990 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1991 pub struct Arguments {
1992 pub values: Vec<Argument>,
1995 impl<'a> Clean<Arguments> for (&'a [hir::Ty], &'a [ast::Ident]) {
1996 fn clean(&self, cx: &DocContext<'_>) -> Arguments {
1998 values: self.0.iter().enumerate().map(|(i, ty)| {
1999 let mut name = self.1.get(i).map(|ident| ident.to_string())
2000 .unwrap_or(String::new());
2001 if name.is_empty() {
2002 name = "_".to_string();
2006 type_: ty.clean(cx),
2013 impl<'a> Clean<Arguments> for (&'a [hir::Ty], hir::BodyId) {
2014 fn clean(&self, cx: &DocContext<'_>) -> Arguments {
2015 let body = cx.tcx.hir().body(self.1);
2018 values: self.0.iter().enumerate().map(|(i, ty)| {
2020 name: name_from_pat(&body.arguments[i].pat),
2021 type_: ty.clean(cx),
2028 impl<'a, A: Copy> Clean<FnDecl> for (&'a hir::FnDecl, A)
2029 where (&'a [hir::Ty], A): Clean<Arguments>
2031 fn clean(&self, cx: &DocContext<'_>) -> FnDecl {
2033 inputs: (&self.0.inputs[..], self.1).clean(cx),
2034 output: self.0.output.clean(cx),
2035 attrs: Attributes::default(),
2040 impl<'a, 'tcx> Clean<FnDecl> for (DefId, ty::PolyFnSig<'tcx>) {
2041 fn clean(&self, cx: &DocContext<'_>) -> FnDecl {
2042 let (did, sig) = *self;
2043 let mut names = if cx.tcx.hir().as_local_hir_id(did).is_some() {
2046 cx.tcx.fn_arg_names(did).into_iter()
2050 output: Return(sig.skip_binder().output().clean(cx)),
2051 attrs: Attributes::default(),
2053 values: sig.skip_binder().inputs().iter().map(|t| {
2056 name: names.next().map_or(String::new(), |name| name.to_string()),
2064 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
2065 pub struct Argument {
2070 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug)]
2073 SelfBorrowed(Option<Lifetime>, Mutability),
2078 pub fn to_self(&self) -> Option<SelfTy> {
2079 if self.name != "self" {
2082 if self.type_.is_self_type() {
2083 return Some(SelfValue);
2086 BorrowedRef{ref lifetime, mutability, ref type_} if type_.is_self_type() => {
2087 Some(SelfBorrowed(lifetime.clone(), mutability))
2089 _ => Some(SelfExplicit(self.type_.clone()))
2094 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
2095 pub enum FunctionRetTy {
2100 impl Clean<FunctionRetTy> for hir::FunctionRetTy {
2101 fn clean(&self, cx: &DocContext<'_>) -> FunctionRetTy {
2103 hir::Return(ref typ) => Return(typ.clean(cx)),
2104 hir::DefaultReturn(..) => DefaultReturn,
2109 impl GetDefId for FunctionRetTy {
2110 fn def_id(&self) -> Option<DefId> {
2112 Return(ref ty) => ty.def_id(),
2113 DefaultReturn => None,
2118 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2121 pub unsafety: hir::Unsafety,
2122 pub items: Vec<Item>,
2123 pub generics: Generics,
2124 pub bounds: Vec<GenericBound>,
2125 pub is_spotlight: bool,
2129 impl Clean<Item> for doctree::Trait {
2130 fn clean(&self, cx: &DocContext<'_>) -> Item {
2131 let attrs = self.attrs.clean(cx);
2132 let is_spotlight = attrs.has_doc_flag("spotlight");
2134 name: Some(self.name.clean(cx)),
2136 source: self.whence.clean(cx),
2137 def_id: cx.tcx.hir().local_def_id_from_hir_id(self.id),
2138 visibility: self.vis.clean(cx),
2139 stability: self.stab.clean(cx),
2140 deprecation: self.depr.clean(cx),
2141 inner: TraitItem(Trait {
2142 auto: self.is_auto.clean(cx),
2143 unsafety: self.unsafety,
2144 items: self.items.clean(cx),
2145 generics: self.generics.clean(cx),
2146 bounds: self.bounds.clean(cx),
2148 is_auto: self.is_auto.clean(cx),
2154 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2155 pub struct TraitAlias {
2156 pub generics: Generics,
2157 pub bounds: Vec<GenericBound>,
2160 impl Clean<Item> for doctree::TraitAlias {
2161 fn clean(&self, cx: &DocContext<'_>) -> Item {
2162 let attrs = self.attrs.clean(cx);
2164 name: Some(self.name.clean(cx)),
2166 source: self.whence.clean(cx),
2167 def_id: cx.tcx.hir().local_def_id_from_hir_id(self.id),
2168 visibility: self.vis.clean(cx),
2169 stability: self.stab.clean(cx),
2170 deprecation: self.depr.clean(cx),
2171 inner: TraitAliasItem(TraitAlias {
2172 generics: self.generics.clean(cx),
2173 bounds: self.bounds.clean(cx),
2179 impl Clean<bool> for hir::IsAuto {
2180 fn clean(&self, _: &DocContext<'_>) -> bool {
2182 hir::IsAuto::Yes => true,
2183 hir::IsAuto::No => false,
2188 impl Clean<Type> for hir::TraitRef {
2189 fn clean(&self, cx: &DocContext<'_>) -> Type {
2190 resolve_type(cx, self.path.clean(cx), self.hir_ref_id)
2194 impl Clean<PolyTrait> for hir::PolyTraitRef {
2195 fn clean(&self, cx: &DocContext<'_>) -> PolyTrait {
2197 trait_: self.trait_ref.clean(cx),
2198 generic_params: self.bound_generic_params.clean(cx)
2203 impl Clean<Item> for hir::TraitItem {
2204 fn clean(&self, cx: &DocContext<'_>) -> Item {
2205 let inner = match self.node {
2206 hir::TraitItemKind::Const(ref ty, default) => {
2207 AssociatedConstItem(ty.clean(cx),
2208 default.map(|e| print_const_expr(cx, e)))
2210 hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Provided(body)) => {
2211 MethodItem((sig, &self.generics, body, None).clean(cx))
2213 hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Required(ref names)) => {
2214 let (generics, decl) = enter_impl_trait(cx, || {
2215 (self.generics.clean(cx), (&*sig.decl, &names[..]).clean(cx))
2217 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
2218 TyMethodItem(TyMethod {
2226 hir::TraitItemKind::Type(ref bounds, ref default) => {
2227 AssociatedTypeItem(bounds.clean(cx), default.clean(cx))
2230 let local_did = cx.tcx.hir().local_def_id_from_hir_id(self.hir_id);
2232 name: Some(self.ident.name.clean(cx)),
2233 attrs: self.attrs.clean(cx),
2234 source: self.span.clean(cx),
2237 stability: get_stability(cx, local_did),
2238 deprecation: get_deprecation(cx, local_did),
2244 impl Clean<Item> for hir::ImplItem {
2245 fn clean(&self, cx: &DocContext<'_>) -> Item {
2246 let inner = match self.node {
2247 hir::ImplItemKind::Const(ref ty, expr) => {
2248 AssociatedConstItem(ty.clean(cx),
2249 Some(print_const_expr(cx, expr)))
2251 hir::ImplItemKind::Method(ref sig, body) => {
2252 MethodItem((sig, &self.generics, body, Some(self.defaultness)).clean(cx))
2254 hir::ImplItemKind::Type(ref ty) => TypedefItem(Typedef {
2255 type_: ty.clean(cx),
2256 generics: Generics::default(),
2258 hir::ImplItemKind::Existential(ref bounds) => ExistentialItem(Existential {
2259 bounds: bounds.clean(cx),
2260 generics: Generics::default(),
2263 let local_did = cx.tcx.hir().local_def_id_from_hir_id(self.hir_id);
2265 name: Some(self.ident.name.clean(cx)),
2266 source: self.span.clean(cx),
2267 attrs: self.attrs.clean(cx),
2269 visibility: self.vis.clean(cx),
2270 stability: get_stability(cx, local_did),
2271 deprecation: get_deprecation(cx, local_did),
2277 impl<'tcx> Clean<Item> for ty::AssociatedItem {
2278 fn clean(&self, cx: &DocContext<'_>) -> Item {
2279 let inner = match self.kind {
2280 ty::AssociatedKind::Const => {
2281 let ty = cx.tcx.type_of(self.def_id);
2282 let default = if self.defaultness.has_value() {
2283 Some(inline::print_inlined_const(cx, self.def_id))
2287 AssociatedConstItem(ty.clean(cx), default)
2289 ty::AssociatedKind::Method => {
2290 let generics = (cx.tcx.generics_of(self.def_id),
2291 &cx.tcx.explicit_predicates_of(self.def_id)).clean(cx);
2292 let sig = cx.tcx.fn_sig(self.def_id);
2293 let mut decl = (self.def_id, sig).clean(cx);
2295 if self.method_has_self_argument {
2296 let self_ty = match self.container {
2297 ty::ImplContainer(def_id) => {
2298 cx.tcx.type_of(def_id)
2300 ty::TraitContainer(_) => cx.tcx.mk_self_type()
2302 let self_arg_ty = *sig.input(0).skip_binder();
2303 if self_arg_ty == self_ty {
2304 decl.inputs.values[0].type_ = Generic(String::from("Self"));
2305 } else if let ty::Ref(_, ty, _) = self_arg_ty.sty {
2307 match decl.inputs.values[0].type_ {
2308 BorrowedRef{ref mut type_, ..} => {
2309 **type_ = Generic(String::from("Self"))
2311 _ => unreachable!(),
2317 let provided = match self.container {
2318 ty::ImplContainer(_) => true,
2319 ty::TraitContainer(_) => self.defaultness.has_value()
2321 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
2323 let constness = if cx.tcx.is_min_const_fn(self.def_id) {
2324 hir::Constness::Const
2326 hir::Constness::NotConst
2331 header: hir::FnHeader {
2332 unsafety: sig.unsafety(),
2335 asyncness: hir::IsAsync::NotAsync,
2337 defaultness: Some(self.defaultness),
2342 TyMethodItem(TyMethod {
2345 header: hir::FnHeader {
2346 unsafety: sig.unsafety(),
2348 constness: hir::Constness::NotConst,
2349 asyncness: hir::IsAsync::NotAsync,
2356 ty::AssociatedKind::Type => {
2357 let my_name = self.ident.name.clean(cx);
2359 if let ty::TraitContainer(did) = self.container {
2360 // When loading a cross-crate associated type, the bounds for this type
2361 // are actually located on the trait/impl itself, so we need to load
2362 // all of the generics from there and then look for bounds that are
2363 // applied to this associated type in question.
2364 let predicates = cx.tcx.explicit_predicates_of(did);
2365 let generics = (cx.tcx.generics_of(did), &predicates).clean(cx);
2366 let mut bounds = generics.where_predicates.iter().filter_map(|pred| {
2367 let (name, self_type, trait_, bounds) = match *pred {
2368 WherePredicate::BoundPredicate {
2369 ty: QPath { ref name, ref self_type, ref trait_ },
2371 } => (name, self_type, trait_, bounds),
2374 if *name != my_name { return None }
2376 ResolvedPath { did, .. } if did == self.container.id() => {}
2380 Generic(ref s) if *s == "Self" => {}
2384 }).flat_map(|i| i.iter().cloned()).collect::<Vec<_>>();
2385 // Our Sized/?Sized bound didn't get handled when creating the generics
2386 // because we didn't actually get our whole set of bounds until just now
2387 // (some of them may have come from the trait). If we do have a sized
2388 // bound, we remove it, and if we don't then we add the `?Sized` bound
2390 match bounds.iter().position(|b| b.is_sized_bound(cx)) {
2391 Some(i) => { bounds.remove(i); }
2392 None => bounds.push(GenericBound::maybe_sized(cx)),
2395 let ty = if self.defaultness.has_value() {
2396 Some(cx.tcx.type_of(self.def_id))
2401 AssociatedTypeItem(bounds, ty.clean(cx))
2403 TypedefItem(Typedef {
2404 type_: cx.tcx.type_of(self.def_id).clean(cx),
2405 generics: Generics {
2407 where_predicates: Vec::new(),
2412 ty::AssociatedKind::Existential => unimplemented!(),
2415 let visibility = match self.container {
2416 ty::ImplContainer(_) => self.vis.clean(cx),
2417 ty::TraitContainer(_) => None,
2421 name: Some(self.ident.name.clean(cx)),
2423 stability: get_stability(cx, self.def_id),
2424 deprecation: get_deprecation(cx, self.def_id),
2425 def_id: self.def_id,
2426 attrs: inline::load_attrs(cx, self.def_id),
2427 source: cx.tcx.def_span(self.def_id).clean(cx),
2433 /// A trait reference, which may have higher ranked lifetimes.
2434 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
2435 pub struct PolyTrait {
2437 pub generic_params: Vec<GenericParamDef>,
2440 /// A representation of a Type suitable for hyperlinking purposes. Ideally one can get the original
2441 /// type out of the AST/TyCtxt given one of these, if more information is needed. Most importantly
2442 /// it does not preserve mutability or boxes.
2443 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
2445 /// Structs/enums/traits (most that'd be an `hir::TyKind::Path`).
2448 param_names: Option<Vec<GenericBound>>,
2450 /// `true` if is a `T::Name` path for associated types.
2453 /// For parameterized types, so the consumer of the JSON don't go
2454 /// looking for types which don't exist anywhere.
2456 /// Primitives are the fixed-size numeric types (plus int/usize/float), char,
2457 /// arrays, slices, and tuples.
2458 Primitive(PrimitiveType),
2460 BareFunction(Box<BareFunctionDecl>),
2463 Array(Box<Type>, String),
2467 RawPointer(Mutability, Box<Type>),
2469 lifetime: Option<Lifetime>,
2470 mutability: Mutability,
2474 // <Type as Trait>::Name
2477 self_type: Box<Type>,
2484 // impl TraitA+TraitB
2485 ImplTrait(Vec<GenericBound>),
2488 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Copy, Debug)]
2489 pub enum PrimitiveType {
2490 Isize, I8, I16, I32, I64, I128,
2491 Usize, U8, U16, U32, U64, U128,
2507 #[derive(Clone, RustcEncodable, RustcDecodable, Copy, Debug)]
2526 pub trait GetDefId {
2527 fn def_id(&self) -> Option<DefId>;
2530 impl<T: GetDefId> GetDefId for Option<T> {
2531 fn def_id(&self) -> Option<DefId> {
2532 self.as_ref().and_then(|d| d.def_id())
2537 pub fn primitive_type(&self) -> Option<PrimitiveType> {
2539 Primitive(p) | BorrowedRef { type_: box Primitive(p), ..} => Some(p),
2540 Slice(..) | BorrowedRef { type_: box Slice(..), .. } => Some(PrimitiveType::Slice),
2541 Array(..) | BorrowedRef { type_: box Array(..), .. } => Some(PrimitiveType::Array),
2542 Tuple(ref tys) => if tys.is_empty() {
2543 Some(PrimitiveType::Unit)
2545 Some(PrimitiveType::Tuple)
2547 RawPointer(..) => Some(PrimitiveType::RawPointer),
2548 BorrowedRef { type_: box Generic(..), .. } => Some(PrimitiveType::Reference),
2549 BareFunction(..) => Some(PrimitiveType::Fn),
2550 Never => Some(PrimitiveType::Never),
2555 pub fn is_generic(&self) -> bool {
2557 ResolvedPath { is_generic, .. } => is_generic,
2562 pub fn is_self_type(&self) -> bool {
2564 Generic(ref name) => name == "Self",
2569 pub fn generics(&self) -> Option<Vec<Type>> {
2571 ResolvedPath { ref path, .. } => {
2572 path.segments.last().and_then(|seg| {
2573 if let GenericArgs::AngleBracketed { ref args, .. } = seg.args {
2574 Some(args.iter().filter_map(|arg| match arg {
2575 GenericArg::Type(ty) => Some(ty.clone()),
2587 pub fn bindings(&self) -> Option<&[TypeBinding]> {
2589 ResolvedPath { ref path, .. } => {
2590 path.segments.last().and_then(|seg| {
2591 if let GenericArgs::AngleBracketed { ref bindings, .. } = seg.args {
2602 pub fn is_full_generic(&self) -> bool {
2604 Type::Generic(_) => true,
2610 impl GetDefId for Type {
2611 fn def_id(&self) -> Option<DefId> {
2613 ResolvedPath { did, .. } => Some(did),
2614 Primitive(p) => crate::html::render::cache().primitive_locations.get(&p).cloned(),
2615 BorrowedRef { type_: box Generic(..), .. } =>
2616 Primitive(PrimitiveType::Reference).def_id(),
2617 BorrowedRef { ref type_, .. } => type_.def_id(),
2618 Tuple(ref tys) => if tys.is_empty() {
2619 Primitive(PrimitiveType::Unit).def_id()
2621 Primitive(PrimitiveType::Tuple).def_id()
2623 BareFunction(..) => Primitive(PrimitiveType::Fn).def_id(),
2624 Never => Primitive(PrimitiveType::Never).def_id(),
2625 Slice(..) => Primitive(PrimitiveType::Slice).def_id(),
2626 Array(..) => Primitive(PrimitiveType::Array).def_id(),
2627 RawPointer(..) => Primitive(PrimitiveType::RawPointer).def_id(),
2628 QPath { ref self_type, .. } => self_type.def_id(),
2634 impl PrimitiveType {
2635 fn from_str(s: &str) -> Option<PrimitiveType> {
2637 "isize" => Some(PrimitiveType::Isize),
2638 "i8" => Some(PrimitiveType::I8),
2639 "i16" => Some(PrimitiveType::I16),
2640 "i32" => Some(PrimitiveType::I32),
2641 "i64" => Some(PrimitiveType::I64),
2642 "i128" => Some(PrimitiveType::I128),
2643 "usize" => Some(PrimitiveType::Usize),
2644 "u8" => Some(PrimitiveType::U8),
2645 "u16" => Some(PrimitiveType::U16),
2646 "u32" => Some(PrimitiveType::U32),
2647 "u64" => Some(PrimitiveType::U64),
2648 "u128" => Some(PrimitiveType::U128),
2649 "bool" => Some(PrimitiveType::Bool),
2650 "char" => Some(PrimitiveType::Char),
2651 "str" => Some(PrimitiveType::Str),
2652 "f32" => Some(PrimitiveType::F32),
2653 "f64" => Some(PrimitiveType::F64),
2654 "array" => Some(PrimitiveType::Array),
2655 "slice" => Some(PrimitiveType::Slice),
2656 "tuple" => Some(PrimitiveType::Tuple),
2657 "unit" => Some(PrimitiveType::Unit),
2658 "pointer" => Some(PrimitiveType::RawPointer),
2659 "reference" => Some(PrimitiveType::Reference),
2660 "fn" => Some(PrimitiveType::Fn),
2661 "never" => Some(PrimitiveType::Never),
2666 pub fn as_str(&self) -> &'static str {
2667 use self::PrimitiveType::*;
2690 RawPointer => "pointer",
2691 Reference => "reference",
2698 pub fn to_url_str(&self) -> &'static str {
2703 impl From<ast::IntTy> for PrimitiveType {
2704 fn from(int_ty: ast::IntTy) -> PrimitiveType {
2706 ast::IntTy::Isize => PrimitiveType::Isize,
2707 ast::IntTy::I8 => PrimitiveType::I8,
2708 ast::IntTy::I16 => PrimitiveType::I16,
2709 ast::IntTy::I32 => PrimitiveType::I32,
2710 ast::IntTy::I64 => PrimitiveType::I64,
2711 ast::IntTy::I128 => PrimitiveType::I128,
2716 impl From<ast::UintTy> for PrimitiveType {
2717 fn from(uint_ty: ast::UintTy) -> PrimitiveType {
2719 ast::UintTy::Usize => PrimitiveType::Usize,
2720 ast::UintTy::U8 => PrimitiveType::U8,
2721 ast::UintTy::U16 => PrimitiveType::U16,
2722 ast::UintTy::U32 => PrimitiveType::U32,
2723 ast::UintTy::U64 => PrimitiveType::U64,
2724 ast::UintTy::U128 => PrimitiveType::U128,
2729 impl From<ast::FloatTy> for PrimitiveType {
2730 fn from(float_ty: ast::FloatTy) -> PrimitiveType {
2732 ast::FloatTy::F32 => PrimitiveType::F32,
2733 ast::FloatTy::F64 => PrimitiveType::F64,
2738 impl Clean<Type> for hir::Ty {
2739 fn clean(&self, cx: &DocContext<'_>) -> Type {
2743 TyKind::Never => Never,
2744 TyKind::CVarArgs(_) => CVarArgs,
2745 TyKind::Ptr(ref m) => RawPointer(m.mutbl.clean(cx), box m.ty.clean(cx)),
2746 TyKind::Rptr(ref l, ref m) => {
2747 let lifetime = if l.is_elided() {
2752 BorrowedRef {lifetime: lifetime, mutability: m.mutbl.clean(cx),
2753 type_: box m.ty.clean(cx)}
2755 TyKind::Slice(ref ty) => Slice(box ty.clean(cx)),
2756 TyKind::Array(ref ty, ref length) => {
2757 let def_id = cx.tcx.hir().local_def_id_from_hir_id(length.hir_id);
2758 let param_env = cx.tcx.param_env(def_id);
2759 let substs = InternalSubsts::identity_for_item(cx.tcx, def_id);
2760 let cid = GlobalId {
2761 instance: ty::Instance::new(def_id, substs),
2764 let length = match cx.tcx.const_eval(param_env.and(cid)) {
2765 Ok(length) => print_const(cx, length),
2766 Err(_) => "_".to_string(),
2768 Array(box ty.clean(cx), length)
2770 TyKind::Tup(ref tys) => Tuple(tys.clean(cx)),
2771 TyKind::Def(item_id, _) => {
2772 let item = cx.tcx.hir().expect_item_by_hir_id(item_id.id);
2773 if let hir::ItemKind::Existential(ref ty) = item.node {
2774 ImplTrait(ty.bounds.clean(cx))
2779 TyKind::Path(hir::QPath::Resolved(None, ref path)) => {
2780 if let Some(new_ty) = cx.ty_substs.borrow().get(&path.def).cloned() {
2784 if let Def::TyParam(did) = path.def {
2785 if let Some(bounds) = cx.impl_trait_bounds.borrow_mut().remove(&did) {
2786 return ImplTrait(bounds);
2790 let mut alias = None;
2791 if let Def::TyAlias(def_id) = path.def {
2792 // Substitute private type aliases
2793 if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(def_id) {
2794 if !cx.renderinfo.borrow().access_levels.is_exported(def_id) {
2795 alias = Some(&cx.tcx.hir().expect_item_by_hir_id(hir_id).node);
2800 if let Some(&hir::ItemKind::Ty(ref ty, ref generics)) = alias {
2801 let provided_params = &path.segments.last().expect("segments were empty");
2802 let mut ty_substs = FxHashMap::default();
2803 let mut lt_substs = FxHashMap::default();
2804 let mut const_substs = FxHashMap::default();
2805 provided_params.with_generic_args(|generic_args| {
2806 let mut indices: GenericParamCount = Default::default();
2807 for param in generics.params.iter() {
2809 hir::GenericParamKind::Lifetime { .. } => {
2811 let lifetime = generic_args.args.iter().find_map(|arg| {
2813 hir::GenericArg::Lifetime(lt) => {
2814 if indices.lifetimes == j {
2823 if let Some(lt) = lifetime.cloned() {
2824 if !lt.is_elided() {
2826 cx.tcx.hir().local_def_id_from_hir_id(param.hir_id);
2827 lt_substs.insert(lt_def_id, lt.clean(cx));
2830 indices.lifetimes += 1;
2832 hir::GenericParamKind::Type { ref default, .. } => {
2835 cx.tcx.hir().local_def_id_from_hir_id(param.hir_id));
2837 let type_ = generic_args.args.iter().find_map(|arg| {
2839 hir::GenericArg::Type(ty) => {
2840 if indices.types == j {
2849 if let Some(ty) = type_.cloned() {
2850 ty_substs.insert(ty_param_def, ty.clean(cx));
2851 } else if let Some(default) = default.clone() {
2852 ty_substs.insert(ty_param_def,
2853 default.into_inner().clean(cx));
2857 hir::GenericParamKind::Const { .. } => {
2858 let const_param_def =
2860 cx.tcx.hir().local_def_id_from_hir_id(param.hir_id));
2862 let const_ = generic_args.args.iter().find_map(|arg| {
2864 hir::GenericArg::Const(ct) => {
2865 if indices.consts == j {
2874 if let Some(ct) = const_.cloned() {
2875 const_substs.insert(const_param_def, ct.clean(cx));
2877 // FIXME(const_generics:defaults)
2878 indices.consts += 1;
2883 return cx.enter_alias(ty_substs, lt_substs, const_substs, || ty.clean(cx));
2885 resolve_type(cx, path.clean(cx), self.hir_id)
2887 TyKind::Path(hir::QPath::Resolved(Some(ref qself), ref p)) => {
2888 let mut segments: Vec<_> = p.segments.clone().into();
2890 let trait_path = hir::Path {
2892 def: Def::Trait(cx.tcx.associated_item(p.def.def_id()).container.id()),
2893 segments: segments.into(),
2896 name: p.segments.last().expect("segments were empty").ident.name.clean(cx),
2897 self_type: box qself.clean(cx),
2898 trait_: box resolve_type(cx, trait_path.clean(cx), self.hir_id)
2901 TyKind::Path(hir::QPath::TypeRelative(ref qself, ref segment)) => {
2902 let mut def = Def::Err;
2903 let ty = hir_ty_to_ty(cx.tcx, self);
2904 if let ty::Projection(proj) = ty.sty {
2905 def = Def::Trait(proj.trait_ref(cx.tcx).def_id);
2907 let trait_path = hir::Path {
2910 segments: vec![].into(),
2913 name: segment.ident.name.clean(cx),
2914 self_type: box qself.clean(cx),
2915 trait_: box resolve_type(cx, trait_path.clean(cx), self.hir_id)
2918 TyKind::TraitObject(ref bounds, ref lifetime) => {
2919 match bounds[0].clean(cx).trait_ {
2920 ResolvedPath { path, param_names: None, did, is_generic } => {
2921 let mut bounds: Vec<self::GenericBound> = bounds[1..].iter().map(|bound| {
2922 self::GenericBound::TraitBound(bound.clean(cx),
2923 hir::TraitBoundModifier::None)
2925 if !lifetime.is_elided() {
2926 bounds.push(self::GenericBound::Outlives(lifetime.clean(cx)));
2928 ResolvedPath { path, param_names: Some(bounds), did, is_generic, }
2930 _ => Infer // shouldn't happen
2933 TyKind::BareFn(ref barefn) => BareFunction(box barefn.clean(cx)),
2934 TyKind::Infer | TyKind::Err => Infer,
2935 TyKind::Typeof(..) => panic!("Unimplemented type {:?}", self.node),
2940 impl<'tcx> Clean<Type> for Ty<'tcx> {
2941 fn clean(&self, cx: &DocContext<'_>) -> Type {
2944 ty::Bool => Primitive(PrimitiveType::Bool),
2945 ty::Char => Primitive(PrimitiveType::Char),
2946 ty::Int(int_ty) => Primitive(int_ty.into()),
2947 ty::Uint(uint_ty) => Primitive(uint_ty.into()),
2948 ty::Float(float_ty) => Primitive(float_ty.into()),
2949 ty::Str => Primitive(PrimitiveType::Str),
2950 ty::Slice(ty) => Slice(box ty.clean(cx)),
2951 ty::Array(ty, n) => {
2952 let mut n = *cx.tcx.lift(&n).expect("array lift failed");
2953 if let ConstValue::Unevaluated(def_id, substs) = n.val {
2954 let param_env = cx.tcx.param_env(def_id);
2955 let cid = GlobalId {
2956 instance: ty::Instance::new(def_id, substs),
2959 if let Ok(new_n) = cx.tcx.const_eval(param_env.and(cid)) {
2963 let n = print_const(cx, n);
2964 Array(box ty.clean(cx), n)
2966 ty::RawPtr(mt) => RawPointer(mt.mutbl.clean(cx), box mt.ty.clean(cx)),
2967 ty::Ref(r, ty, mutbl) => BorrowedRef {
2968 lifetime: r.clean(cx),
2969 mutability: mutbl.clean(cx),
2970 type_: box ty.clean(cx),
2974 let ty = cx.tcx.lift(self).expect("FnPtr lift failed");
2975 let sig = ty.fn_sig(cx.tcx);
2976 BareFunction(box BareFunctionDecl {
2977 unsafety: sig.unsafety(),
2978 generic_params: Vec::new(),
2979 decl: (cx.tcx.hir().local_def_id(ast::CRATE_NODE_ID), sig).clean(cx),
2983 ty::Adt(def, substs) => {
2985 let kind = match def.adt_kind() {
2986 AdtKind::Struct => TypeKind::Struct,
2987 AdtKind::Union => TypeKind::Union,
2988 AdtKind::Enum => TypeKind::Enum,
2990 inline::record_extern_fqn(cx, did, kind);
2991 let path = external_path(cx, &cx.tcx.item_name(did).as_str(),
2992 None, false, vec![], substs);
3000 ty::Foreign(did) => {
3001 inline::record_extern_fqn(cx, did, TypeKind::Foreign);
3002 let path = external_path(cx, &cx.tcx.item_name(did).as_str(),
3003 None, false, vec![], InternalSubsts::empty());
3011 ty::Dynamic(ref obj, ref reg) => {
3012 // HACK: pick the first `did` as the `did` of the trait object. Someone
3013 // might want to implement "native" support for marker-trait-only
3015 let mut dids = obj.principal_def_id().into_iter().chain(obj.auto_traits());
3016 let did = dids.next().unwrap_or_else(|| {
3017 panic!("found trait object `{:?}` with no traits?", self)
3019 let substs = match obj.principal() {
3020 Some(principal) => principal.skip_binder().substs,
3021 // marker traits have no substs.
3022 _ => cx.tcx.intern_substs(&[])
3025 inline::record_extern_fqn(cx, did, TypeKind::Trait);
3027 let mut param_names = vec![];
3028 reg.clean(cx).map(|b| param_names.push(GenericBound::Outlives(b)));
3030 let empty = cx.tcx.intern_substs(&[]);
3031 let path = external_path(cx, &cx.tcx.item_name(did).as_str(),
3032 Some(did), false, vec![], empty);
3033 inline::record_extern_fqn(cx, did, TypeKind::Trait);
3034 let bound = GenericBound::TraitBound(PolyTrait {
3035 trait_: ResolvedPath {
3041 generic_params: Vec::new(),
3042 }, hir::TraitBoundModifier::None);
3043 param_names.push(bound);
3046 let mut bindings = vec![];
3047 for pb in obj.projection_bounds() {
3048 bindings.push(TypeBinding {
3049 name: cx.tcx.associated_item(pb.item_def_id()).ident.name.clean(cx),
3050 ty: pb.skip_binder().ty.clean(cx)
3054 let path = external_path(cx, &cx.tcx.item_name(did).as_str(), Some(did),
3055 false, bindings, substs);
3058 param_names: Some(param_names),
3063 ty::Tuple(ref t) => Tuple(t.clean(cx)),
3065 ty::Projection(ref data) => data.clean(cx),
3067 ty::Param(ref p) => Generic(p.name.to_string()),
3069 ty::Opaque(def_id, substs) => {
3070 // Grab the "TraitA + TraitB" from `impl TraitA + TraitB`,
3071 // by looking up the projections associated with the def_id.
3072 let predicates_of = cx.tcx.explicit_predicates_of(def_id);
3073 let substs = cx.tcx.lift(&substs).expect("Opaque lift failed");
3074 let bounds = predicates_of.instantiate(cx.tcx, substs);
3075 let mut regions = vec![];
3076 let mut has_sized = false;
3077 let mut bounds = bounds.predicates.iter().filter_map(|predicate| {
3078 let trait_ref = if let Some(tr) = predicate.to_opt_poly_trait_ref() {
3080 } else if let ty::Predicate::TypeOutlives(pred) = *predicate {
3081 // these should turn up at the end
3082 pred.skip_binder().1.clean(cx).map(|r| {
3083 regions.push(GenericBound::Outlives(r))
3090 if let Some(sized) = cx.tcx.lang_items().sized_trait() {
3091 if trait_ref.def_id() == sized {
3097 let bounds = bounds.predicates.iter().filter_map(|pred|
3098 if let ty::Predicate::Projection(proj) = *pred {
3099 let proj = proj.skip_binder();
3100 if proj.projection_ty.trait_ref(cx.tcx) == *trait_ref.skip_binder() {
3102 name: cx.tcx.associated_item(proj.projection_ty.item_def_id)
3103 .ident.name.clean(cx),
3104 ty: proj.ty.clean(cx),
3114 Some((trait_ref.skip_binder(), bounds).clean(cx))
3115 }).collect::<Vec<_>>();
3116 bounds.extend(regions);
3117 if !has_sized && !bounds.is_empty() {
3118 bounds.insert(0, GenericBound::maybe_sized(cx));
3123 ty::Closure(..) | ty::Generator(..) => Tuple(vec![]), // FIXME(pcwalton)
3125 ty::Bound(..) => panic!("Bound"),
3126 ty::Placeholder(..) => panic!("Placeholder"),
3127 ty::UnnormalizedProjection(..) => panic!("UnnormalizedProjection"),
3128 ty::GeneratorWitness(..) => panic!("GeneratorWitness"),
3129 ty::Infer(..) => panic!("Infer"),
3130 ty::Error => panic!("Error"),
3135 impl<'tcx> Clean<Constant> for ty::Const<'tcx> {
3136 fn clean(&self, cx: &DocContext<'_>) -> Constant {
3138 type_: self.ty.clean(cx),
3139 expr: format!("{:?}", self.val), // FIXME(const_generics)
3144 impl Clean<Item> for hir::StructField {
3145 fn clean(&self, cx: &DocContext<'_>) -> Item {
3146 let local_did = cx.tcx.hir().local_def_id_from_hir_id(self.hir_id);
3149 name: Some(self.ident.name).clean(cx),
3150 attrs: self.attrs.clean(cx),
3151 source: self.span.clean(cx),
3152 visibility: self.vis.clean(cx),
3153 stability: get_stability(cx, local_did),
3154 deprecation: get_deprecation(cx, local_did),
3156 inner: StructFieldItem(self.ty.clean(cx)),
3161 impl<'tcx> Clean<Item> for ty::FieldDef {
3162 fn clean(&self, cx: &DocContext<'_>) -> Item {
3164 name: Some(self.ident.name).clean(cx),
3165 attrs: cx.tcx.get_attrs(self.did).clean(cx),
3166 source: cx.tcx.def_span(self.did).clean(cx),
3167 visibility: self.vis.clean(cx),
3168 stability: get_stability(cx, self.did),
3169 deprecation: get_deprecation(cx, self.did),
3171 inner: StructFieldItem(cx.tcx.type_of(self.did).clean(cx)),
3176 #[derive(Clone, PartialEq, Eq, RustcDecodable, RustcEncodable, Debug)]
3177 pub enum Visibility {
3181 Restricted(DefId, Path),
3184 impl Clean<Option<Visibility>> for hir::Visibility {
3185 fn clean(&self, cx: &DocContext<'_>) -> Option<Visibility> {
3186 Some(match self.node {
3187 hir::VisibilityKind::Public => Visibility::Public,
3188 hir::VisibilityKind::Inherited => Visibility::Inherited,
3189 hir::VisibilityKind::Crate(_) => Visibility::Crate,
3190 hir::VisibilityKind::Restricted { ref path, .. } => {
3191 let path = path.clean(cx);
3192 let did = register_def(cx, path.def);
3193 Visibility::Restricted(did, path)
3199 impl Clean<Option<Visibility>> for ty::Visibility {
3200 fn clean(&self, _: &DocContext<'_>) -> Option<Visibility> {
3201 Some(if *self == ty::Visibility::Public { Public } else { Inherited })
3205 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3207 pub struct_type: doctree::StructType,
3208 pub generics: Generics,
3209 pub fields: Vec<Item>,
3210 pub fields_stripped: bool,
3213 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3215 pub struct_type: doctree::StructType,
3216 pub generics: Generics,
3217 pub fields: Vec<Item>,
3218 pub fields_stripped: bool,
3221 impl Clean<Item> for doctree::Struct {
3222 fn clean(&self, cx: &DocContext<'_>) -> Item {
3224 name: Some(self.name.clean(cx)),
3225 attrs: self.attrs.clean(cx),
3226 source: self.whence.clean(cx),
3227 def_id: cx.tcx.hir().local_def_id_from_hir_id(self.id),
3228 visibility: self.vis.clean(cx),
3229 stability: self.stab.clean(cx),
3230 deprecation: self.depr.clean(cx),
3231 inner: StructItem(Struct {
3232 struct_type: self.struct_type,
3233 generics: self.generics.clean(cx),
3234 fields: self.fields.clean(cx),
3235 fields_stripped: false,
3241 impl Clean<Item> for doctree::Union {
3242 fn clean(&self, cx: &DocContext<'_>) -> Item {
3244 name: Some(self.name.clean(cx)),
3245 attrs: self.attrs.clean(cx),
3246 source: self.whence.clean(cx),
3247 def_id: cx.tcx.hir().local_def_id_from_hir_id(self.id),
3248 visibility: self.vis.clean(cx),
3249 stability: self.stab.clean(cx),
3250 deprecation: self.depr.clean(cx),
3251 inner: UnionItem(Union {
3252 struct_type: self.struct_type,
3253 generics: self.generics.clean(cx),
3254 fields: self.fields.clean(cx),
3255 fields_stripped: false,
3261 /// This is a more limited form of the standard Struct, different in that
3262 /// it lacks the things most items have (name, id, parameterization). Found
3263 /// only as a variant in an enum.
3264 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3265 pub struct VariantStruct {
3266 pub struct_type: doctree::StructType,
3267 pub fields: Vec<Item>,
3268 pub fields_stripped: bool,
3271 impl Clean<VariantStruct> for ::rustc::hir::VariantData {
3272 fn clean(&self, cx: &DocContext<'_>) -> VariantStruct {
3274 struct_type: doctree::struct_type_from_def(self),
3275 fields: self.fields().iter().map(|x| x.clean(cx)).collect(),
3276 fields_stripped: false,
3281 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3283 pub variants: IndexVec<VariantIdx, Item>,
3284 pub generics: Generics,
3285 pub variants_stripped: bool,
3288 impl Clean<Item> for doctree::Enum {
3289 fn clean(&self, cx: &DocContext<'_>) -> Item {
3291 name: Some(self.name.clean(cx)),
3292 attrs: self.attrs.clean(cx),
3293 source: self.whence.clean(cx),
3294 def_id: cx.tcx.hir().local_def_id_from_hir_id(self.id),
3295 visibility: self.vis.clean(cx),
3296 stability: self.stab.clean(cx),
3297 deprecation: self.depr.clean(cx),
3298 inner: EnumItem(Enum {
3299 variants: self.variants.iter().map(|v| v.clean(cx)).collect(),
3300 generics: self.generics.clean(cx),
3301 variants_stripped: false,
3307 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3308 pub struct Variant {
3309 pub kind: VariantKind,
3312 impl Clean<Item> for doctree::Variant {
3313 fn clean(&self, cx: &DocContext<'_>) -> Item {
3315 name: Some(self.name.clean(cx)),
3316 attrs: self.attrs.clean(cx),
3317 source: self.whence.clean(cx),
3319 stability: self.stab.clean(cx),
3320 deprecation: self.depr.clean(cx),
3321 def_id: cx.tcx.hir().local_def_id_from_hir_id(self.id),
3322 inner: VariantItem(Variant {
3323 kind: self.def.clean(cx),
3329 impl<'tcx> Clean<Item> for ty::VariantDef {
3330 fn clean(&self, cx: &DocContext<'_>) -> Item {
3331 let kind = match self.ctor_kind {
3332 CtorKind::Const => VariantKind::CLike,
3335 self.fields.iter().map(|f| cx.tcx.type_of(f.did).clean(cx)).collect()
3338 CtorKind::Fictive => {
3339 VariantKind::Struct(VariantStruct {
3340 struct_type: doctree::Plain,
3341 fields_stripped: false,
3342 fields: self.fields.iter().map(|field| {
3344 source: cx.tcx.def_span(field.did).clean(cx),
3345 name: Some(field.ident.name.clean(cx)),
3346 attrs: cx.tcx.get_attrs(field.did).clean(cx),
3347 visibility: field.vis.clean(cx),
3349 stability: get_stability(cx, field.did),
3350 deprecation: get_deprecation(cx, field.did),
3351 inner: StructFieldItem(cx.tcx.type_of(field.did).clean(cx))
3358 name: Some(self.ident.clean(cx)),
3359 attrs: inline::load_attrs(cx, self.def_id),
3360 source: cx.tcx.def_span(self.def_id).clean(cx),
3361 visibility: Some(Inherited),
3362 def_id: self.def_id,
3363 inner: VariantItem(Variant { kind }),
3364 stability: get_stability(cx, self.def_id),
3365 deprecation: get_deprecation(cx, self.def_id),
3370 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3371 pub enum VariantKind {
3374 Struct(VariantStruct),
3377 impl Clean<VariantKind> for hir::VariantData {
3378 fn clean(&self, cx: &DocContext<'_>) -> VariantKind {
3380 hir::VariantData::Struct(..) => VariantKind::Struct(self.clean(cx)),
3381 hir::VariantData::Tuple(..) =>
3382 VariantKind::Tuple(self.fields().iter().map(|x| x.ty.clean(cx)).collect()),
3383 hir::VariantData::Unit(..) => VariantKind::CLike,
3388 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3390 pub filename: FileName,
3398 pub fn empty() -> Span {
3400 filename: FileName::Anon(0),
3401 loline: 0, locol: 0,
3402 hiline: 0, hicol: 0,
3407 impl Clean<Span> for syntax_pos::Span {
3408 fn clean(&self, cx: &DocContext<'_>) -> Span {
3409 if self.is_dummy() {
3410 return Span::empty();
3413 let cm = cx.sess().source_map();
3414 let filename = cm.span_to_filename(*self);
3415 let lo = cm.lookup_char_pos(self.lo());
3416 let hi = cm.lookup_char_pos(self.hi());
3420 locol: lo.col.to_usize(),
3422 hicol: hi.col.to_usize(),
3427 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
3431 pub segments: Vec<PathSegment>,
3435 pub fn last_name(&self) -> &str {
3436 self.segments.last().expect("segments were empty").name.as_str()
3440 impl Clean<Path> for hir::Path {
3441 fn clean(&self, cx: &DocContext<'_>) -> Path {
3443 global: self.is_global(),
3445 segments: if self.is_global() { &self.segments[1..] } else { &self.segments }.clean(cx),
3450 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
3451 pub enum GenericArg {
3457 impl fmt::Display for GenericArg {
3458 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3460 GenericArg::Lifetime(lt) => lt.fmt(f),
3461 GenericArg::Type(ty) => ty.fmt(f),
3462 GenericArg::Const(ct) => ct.fmt(f),
3467 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
3468 pub enum GenericArgs {
3470 args: Vec<GenericArg>,
3471 bindings: Vec<TypeBinding>,
3475 output: Option<Type>,
3479 impl Clean<GenericArgs> for hir::GenericArgs {
3480 fn clean(&self, cx: &DocContext<'_>) -> GenericArgs {
3481 if self.parenthesized {
3482 let output = self.bindings[0].ty.clean(cx);
3483 GenericArgs::Parenthesized {
3484 inputs: self.inputs().clean(cx),
3485 output: if output != Type::Tuple(Vec::new()) { Some(output) } else { None }
3488 let elide_lifetimes = self.args.iter().all(|arg| match arg {
3489 hir::GenericArg::Lifetime(lt) => lt.is_elided(),
3492 GenericArgs::AngleBracketed {
3493 args: self.args.iter().filter_map(|arg| match arg {
3494 hir::GenericArg::Lifetime(lt) if !elide_lifetimes => {
3495 Some(GenericArg::Lifetime(lt.clean(cx)))
3497 hir::GenericArg::Lifetime(_) => None,
3498 hir::GenericArg::Type(ty) => Some(GenericArg::Type(ty.clean(cx))),
3499 hir::GenericArg::Const(ct) => Some(GenericArg::Const(ct.clean(cx))),
3501 bindings: self.bindings.clean(cx),
3507 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
3508 pub struct PathSegment {
3510 pub args: GenericArgs,
3513 impl Clean<PathSegment> for hir::PathSegment {
3514 fn clean(&self, cx: &DocContext<'_>) -> PathSegment {
3516 name: self.ident.name.clean(cx),
3517 args: self.with_generic_args(|generic_args| generic_args.clean(cx))
3522 fn strip_type(ty: Type) -> Type {
3524 Type::ResolvedPath { path, param_names, did, is_generic } => {
3525 Type::ResolvedPath { path: strip_path(&path), param_names, did, is_generic }
3527 Type::Tuple(inner_tys) => {
3528 Type::Tuple(inner_tys.iter().map(|t| strip_type(t.clone())).collect())
3530 Type::Slice(inner_ty) => Type::Slice(Box::new(strip_type(*inner_ty))),
3531 Type::Array(inner_ty, s) => Type::Array(Box::new(strip_type(*inner_ty)), s),
3532 Type::Unique(inner_ty) => Type::Unique(Box::new(strip_type(*inner_ty))),
3533 Type::RawPointer(m, inner_ty) => Type::RawPointer(m, Box::new(strip_type(*inner_ty))),
3534 Type::BorrowedRef { lifetime, mutability, type_ } => {
3535 Type::BorrowedRef { lifetime, mutability, type_: Box::new(strip_type(*type_)) }
3537 Type::QPath { name, self_type, trait_ } => {
3540 self_type: Box::new(strip_type(*self_type)), trait_: Box::new(strip_type(*trait_))
3547 fn strip_path(path: &Path) -> Path {
3548 let segments = path.segments.iter().map(|s| {
3550 name: s.name.clone(),
3551 args: GenericArgs::AngleBracketed {
3559 global: path.global,
3560 def: path.def.clone(),
3565 fn qpath_to_string(p: &hir::QPath) -> String {
3566 let segments = match *p {
3567 hir::QPath::Resolved(_, ref path) => &path.segments,
3568 hir::QPath::TypeRelative(_, ref segment) => return segment.ident.to_string(),
3571 let mut s = String::new();
3572 for (i, seg) in segments.iter().enumerate() {
3576 if seg.ident.name != keywords::PathRoot.name() {
3577 s.push_str(&*seg.ident.as_str());
3583 impl Clean<String> for Ident {
3585 fn clean(&self, cx: &DocContext<'_>) -> String {
3590 impl Clean<String> for ast::Name {
3592 fn clean(&self, _: &DocContext<'_>) -> String {
3597 impl Clean<String> for InternedString {
3599 fn clean(&self, _: &DocContext<'_>) -> String {
3604 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3605 pub struct Typedef {
3607 pub generics: Generics,
3610 impl Clean<Item> for doctree::Typedef {
3611 fn clean(&self, cx: &DocContext<'_>) -> Item {
3613 name: Some(self.name.clean(cx)),
3614 attrs: self.attrs.clean(cx),
3615 source: self.whence.clean(cx),
3616 def_id: cx.tcx.hir().local_def_id_from_hir_id(self.id),
3617 visibility: self.vis.clean(cx),
3618 stability: self.stab.clean(cx),
3619 deprecation: self.depr.clean(cx),
3620 inner: TypedefItem(Typedef {
3621 type_: self.ty.clean(cx),
3622 generics: self.gen.clean(cx),
3628 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3629 pub struct Existential {
3630 pub bounds: Vec<GenericBound>,
3631 pub generics: Generics,
3634 impl Clean<Item> for doctree::Existential {
3635 fn clean(&self, cx: &DocContext<'_>) -> Item {
3637 name: Some(self.name.clean(cx)),
3638 attrs: self.attrs.clean(cx),
3639 source: self.whence.clean(cx),
3640 def_id: cx.tcx.hir().local_def_id_from_hir_id(self.id),
3641 visibility: self.vis.clean(cx),
3642 stability: self.stab.clean(cx),
3643 deprecation: self.depr.clean(cx),
3644 inner: ExistentialItem(Existential {
3645 bounds: self.exist_ty.bounds.clean(cx),
3646 generics: self.exist_ty.generics.clean(cx),
3652 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
3653 pub struct BareFunctionDecl {
3654 pub unsafety: hir::Unsafety,
3655 pub generic_params: Vec<GenericParamDef>,
3660 impl Clean<BareFunctionDecl> for hir::BareFnTy {
3661 fn clean(&self, cx: &DocContext<'_>) -> BareFunctionDecl {
3662 let (generic_params, decl) = enter_impl_trait(cx, || {
3663 (self.generic_params.clean(cx), (&*self.decl, &self.arg_names[..]).clean(cx))
3666 unsafety: self.unsafety,
3674 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3677 pub mutability: Mutability,
3678 /// It's useful to have the value of a static documented, but I have no
3679 /// desire to represent expressions (that'd basically be all of the AST,
3680 /// which is huge!). So, have a string.
3684 impl Clean<Item> for doctree::Static {
3685 fn clean(&self, cx: &DocContext<'_>) -> Item {
3686 debug!("cleaning static {}: {:?}", self.name.clean(cx), self);
3688 name: Some(self.name.clean(cx)),
3689 attrs: self.attrs.clean(cx),
3690 source: self.whence.clean(cx),
3691 def_id: cx.tcx.hir().local_def_id_from_hir_id(self.id),
3692 visibility: self.vis.clean(cx),
3693 stability: self.stab.clean(cx),
3694 deprecation: self.depr.clean(cx),
3695 inner: StaticItem(Static {
3696 type_: self.type_.clean(cx),
3697 mutability: self.mutability.clean(cx),
3698 expr: print_const_expr(cx, self.expr),
3704 #[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug)]
3705 pub struct Constant {
3710 impl Clean<Item> for doctree::Constant {
3711 fn clean(&self, cx: &DocContext<'_>) -> Item {
3713 name: Some(self.name.clean(cx)),
3714 attrs: self.attrs.clean(cx),
3715 source: self.whence.clean(cx),
3716 def_id: cx.tcx.hir().local_def_id_from_hir_id(self.id),
3717 visibility: self.vis.clean(cx),
3718 stability: self.stab.clean(cx),
3719 deprecation: self.depr.clean(cx),
3720 inner: ConstantItem(Constant {
3721 type_: self.type_.clean(cx),
3722 expr: print_const_expr(cx, self.expr),
3728 #[derive(Debug, Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Copy, Hash)]
3729 pub enum Mutability {
3734 impl Clean<Mutability> for hir::Mutability {
3735 fn clean(&self, _: &DocContext<'_>) -> Mutability {
3737 &hir::MutMutable => Mutable,
3738 &hir::MutImmutable => Immutable,
3743 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Copy, Debug, Hash)]
3744 pub enum ImplPolarity {
3749 impl Clean<ImplPolarity> for hir::ImplPolarity {
3750 fn clean(&self, _: &DocContext<'_>) -> ImplPolarity {
3752 &hir::ImplPolarity::Positive => ImplPolarity::Positive,
3753 &hir::ImplPolarity::Negative => ImplPolarity::Negative,
3758 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3760 pub unsafety: hir::Unsafety,
3761 pub generics: Generics,
3762 pub provided_trait_methods: FxHashSet<String>,
3763 pub trait_: Option<Type>,
3765 pub items: Vec<Item>,
3766 pub polarity: Option<ImplPolarity>,
3767 pub synthetic: bool,
3768 pub blanket_impl: Option<Type>,
3771 pub fn get_auto_traits_with_hir_id(
3772 cx: &DocContext<'_>,
3776 let finder = AutoTraitFinder::new(cx);
3777 finder.get_with_hir_id(id, name)
3780 pub fn get_auto_traits_with_def_id(
3781 cx: &DocContext<'_>,
3784 let finder = AutoTraitFinder::new(cx);
3786 finder.get_with_def_id(id)
3789 pub fn get_blanket_impls_with_hir_id(
3790 cx: &DocContext<'_>,
3794 let finder = BlanketImplFinder::new(cx);
3795 finder.get_with_hir_id(id, name)
3798 pub fn get_blanket_impls_with_def_id(
3799 cx: &DocContext<'_>,
3802 let finder = BlanketImplFinder::new(cx);
3804 finder.get_with_def_id(id)
3807 impl Clean<Vec<Item>> for doctree::Impl {
3808 fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
3809 let mut ret = Vec::new();
3810 let trait_ = self.trait_.clean(cx);
3811 let items = self.items.clean(cx);
3813 // If this impl block is an implementation of the Deref trait, then we
3814 // need to try inlining the target's inherent impl blocks as well.
3815 if trait_.def_id() == cx.tcx.lang_items().deref_trait() {
3816 build_deref_target_impls(cx, &items, &mut ret);
3819 let provided = trait_.def_id().map(|did| {
3820 cx.tcx.provided_trait_methods(did)
3822 .map(|meth| meth.ident.to_string())
3824 }).unwrap_or_default();
3828 attrs: self.attrs.clean(cx),
3829 source: self.whence.clean(cx),
3830 def_id: cx.tcx.hir().local_def_id_from_hir_id(self.id),
3831 visibility: self.vis.clean(cx),
3832 stability: self.stab.clean(cx),
3833 deprecation: self.depr.clean(cx),
3834 inner: ImplItem(Impl {
3835 unsafety: self.unsafety,
3836 generics: self.generics.clean(cx),
3837 provided_trait_methods: provided,
3839 for_: self.for_.clean(cx),
3841 polarity: Some(self.polarity.clean(cx)),
3850 fn build_deref_target_impls(cx: &DocContext<'_>,
3852 ret: &mut Vec<Item>) {
3853 use self::PrimitiveType::*;
3857 let target = match item.inner {
3858 TypedefItem(ref t, true) => &t.type_,
3861 let primitive = match *target {
3862 ResolvedPath { did, .. } if did.is_local() => continue,
3863 ResolvedPath { did, .. } => {
3864 ret.extend(inline::build_impls(cx, did));
3867 _ => match target.primitive_type() {
3872 let did = match primitive {
3873 Isize => tcx.lang_items().isize_impl(),
3874 I8 => tcx.lang_items().i8_impl(),
3875 I16 => tcx.lang_items().i16_impl(),
3876 I32 => tcx.lang_items().i32_impl(),
3877 I64 => tcx.lang_items().i64_impl(),
3878 I128 => tcx.lang_items().i128_impl(),
3879 Usize => tcx.lang_items().usize_impl(),
3880 U8 => tcx.lang_items().u8_impl(),
3881 U16 => tcx.lang_items().u16_impl(),
3882 U32 => tcx.lang_items().u32_impl(),
3883 U64 => tcx.lang_items().u64_impl(),
3884 U128 => tcx.lang_items().u128_impl(),
3885 F32 => tcx.lang_items().f32_impl(),
3886 F64 => tcx.lang_items().f64_impl(),
3887 Char => tcx.lang_items().char_impl(),
3889 Str => tcx.lang_items().str_impl(),
3890 Slice => tcx.lang_items().slice_impl(),
3891 Array => tcx.lang_items().slice_impl(),
3894 RawPointer => tcx.lang_items().const_ptr_impl(),
3898 CVarArgs => tcx.lang_items().va_list(),
3900 if let Some(did) = did {
3901 if !did.is_local() {
3902 inline::build_impl(cx, did, ret);
3908 impl Clean<Vec<Item>> for doctree::ExternCrate {
3909 fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
3911 let please_inline = self.vis.node.is_pub() && self.attrs.iter().any(|a| {
3912 a.check_name("doc") && match a.meta_item_list() {
3913 Some(l) => attr::list_contains_name(&l, "inline"),
3919 let mut visited = FxHashSet::default();
3921 let def = Def::Mod(DefId {
3923 index: CRATE_DEF_INDEX,
3926 if let Some(items) = inline::try_inline(cx, def, self.name, &mut visited) {
3933 attrs: self.attrs.clean(cx),
3934 source: self.whence.clean(cx),
3935 def_id: DefId { krate: self.cnum, index: CRATE_DEF_INDEX },
3936 visibility: self.vis.clean(cx),
3939 inner: ExternCrateItem(self.name.clean(cx), self.path.clone())
3944 impl Clean<Vec<Item>> for doctree::Import {
3945 fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
3946 // We consider inlining the documentation of `pub use` statements, but we
3947 // forcefully don't inline if this is not public or if the
3948 // #[doc(no_inline)] attribute is present.
3949 // Don't inline doc(hidden) imports so they can be stripped at a later stage.
3950 let mut denied = !self.vis.node.is_pub() || self.attrs.iter().any(|a| {
3951 a.check_name("doc") && match a.meta_item_list() {
3952 Some(l) => attr::list_contains_name(&l, "no_inline") ||
3953 attr::list_contains_name(&l, "hidden"),
3957 // Also check whether imports were asked to be inlined, in case we're trying to re-export a
3958 // crate in Rust 2018+
3959 let please_inline = self.attrs.lists("doc").has_word("inline");
3960 let path = self.path.clean(cx);
3961 let inner = if self.glob {
3963 let mut visited = FxHashSet::default();
3964 if let Some(items) = inline::try_inline_glob(cx, path.def, &mut visited) {
3969 Import::Glob(resolve_use_source(cx, path))
3971 let name = self.name;
3974 Def::Mod(did) => if !did.is_local() && did.index == CRATE_DEF_INDEX {
3975 // if we're `pub use`ing an extern crate root, don't inline it unless we
3976 // were specifically asked for it
3983 let mut visited = FxHashSet::default();
3984 if let Some(items) = inline::try_inline(cx, path.def, name, &mut visited) {
3988 Import::Simple(name.clean(cx), resolve_use_source(cx, path))
3993 attrs: self.attrs.clean(cx),
3994 source: self.whence.clean(cx),
3995 def_id: cx.tcx.hir().local_def_id(ast::CRATE_NODE_ID),
3996 visibility: self.vis.clean(cx),
3999 inner: ImportItem(inner)
4004 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
4006 // use source as str;
4007 Simple(String, ImportSource),
4012 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
4013 pub struct ImportSource {
4015 pub did: Option<DefId>,
4018 impl Clean<Vec<Item>> for hir::ForeignMod {
4019 fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
4020 let mut items = self.items.clean(cx);
4021 for item in &mut items {
4022 if let ForeignFunctionItem(ref mut f) = item.inner {
4023 f.header.abi = self.abi;
4030 impl Clean<Item> for hir::ForeignItem {
4031 fn clean(&self, cx: &DocContext<'_>) -> Item {
4032 let inner = match self.node {
4033 hir::ForeignItemKind::Fn(ref decl, ref names, ref generics) => {
4034 let (generics, decl) = enter_impl_trait(cx, || {
4035 (generics.clean(cx), (&**decl, &names[..]).clean(cx))
4037 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
4038 ForeignFunctionItem(Function {
4041 header: hir::FnHeader {
4042 unsafety: hir::Unsafety::Unsafe,
4044 constness: hir::Constness::NotConst,
4045 asyncness: hir::IsAsync::NotAsync,
4051 hir::ForeignItemKind::Static(ref ty, mutbl) => {
4052 ForeignStaticItem(Static {
4053 type_: ty.clean(cx),
4054 mutability: if mutbl {Mutable} else {Immutable},
4055 expr: String::new(),
4058 hir::ForeignItemKind::Type => {
4063 let local_did = cx.tcx.hir().local_def_id_from_hir_id(self.hir_id);
4066 name: Some(self.ident.clean(cx)),
4067 attrs: self.attrs.clean(cx),
4068 source: self.span.clean(cx),
4070 visibility: self.vis.clean(cx),
4071 stability: get_stability(cx, local_did),
4072 deprecation: get_deprecation(cx, local_did),
4080 pub trait ToSource {
4081 fn to_src(&self, cx: &DocContext<'_>) -> String;
4084 impl ToSource for syntax_pos::Span {
4085 fn to_src(&self, cx: &DocContext<'_>) -> String {
4086 debug!("converting span {:?} to snippet", self.clean(cx));
4087 let sn = match cx.sess().source_map().span_to_snippet(*self) {
4089 Err(_) => String::new()
4091 debug!("got snippet {}", sn);
4096 fn name_from_pat(p: &hir::Pat) -> String {
4098 debug!("Trying to get a name from pattern: {:?}", p);
4101 PatKind::Wild => "_".to_string(),
4102 PatKind::Binding(_, _, ident, _) => ident.to_string(),
4103 PatKind::TupleStruct(ref p, ..) | PatKind::Path(ref p) => qpath_to_string(p),
4104 PatKind::Struct(ref name, ref fields, etc) => {
4105 format!("{} {{ {}{} }}", qpath_to_string(name),
4106 fields.iter().map(|&Spanned { node: ref fp, .. }|
4107 format!("{}: {}", fp.ident, name_from_pat(&*fp.pat)))
4108 .collect::<Vec<String>>().join(", "),
4109 if etc { ", .." } else { "" }
4112 PatKind::Tuple(ref elts, _) => format!("({})", elts.iter().map(|p| name_from_pat(&**p))
4113 .collect::<Vec<String>>().join(", ")),
4114 PatKind::Box(ref p) => name_from_pat(&**p),
4115 PatKind::Ref(ref p, _) => name_from_pat(&**p),
4116 PatKind::Lit(..) => {
4117 warn!("tried to get argument name from PatKind::Lit, \
4118 which is silly in function arguments");
4121 PatKind::Range(..) => panic!("tried to get argument name from PatKind::Range, \
4122 which is not allowed in function arguments"),
4123 PatKind::Slice(ref begin, ref mid, ref end) => {
4124 let begin = begin.iter().map(|p| name_from_pat(&**p));
4125 let mid = mid.as_ref().map(|p| format!("..{}", name_from_pat(&**p))).into_iter();
4126 let end = end.iter().map(|p| name_from_pat(&**p));
4127 format!("[{}]", begin.chain(mid).chain(end).collect::<Vec<_>>().join(", "))
4132 fn print_const(cx: &DocContext<'_>, n: ty::Const<'_>) -> String {
4134 ConstValue::Unevaluated(def_id, _) => {
4135 if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(def_id) {
4136 print_const_expr(cx, cx.tcx.hir().body_owned_by(hir_id))
4138 inline::print_inlined_const(cx, def_id)
4142 let mut s = String::new();
4143 ::rustc::mir::fmt_const_val(&mut s, n).expect("fmt_const_val failed");
4144 // array lengths are obviously usize
4145 if s.ends_with("usize") {
4146 let n = s.len() - "usize".len();
4154 fn print_const_expr(cx: &DocContext<'_>, body: hir::BodyId) -> String {
4155 cx.tcx.hir().hir_to_pretty_string(body.hir_id)
4158 /// Given a type Path, resolve it to a Type using the TyCtxt
4159 fn resolve_type(cx: &DocContext<'_>,
4161 id: hir::HirId) -> Type {
4162 if id == hir::DUMMY_HIR_ID {
4163 debug!("resolve_type({:?})", path);
4165 debug!("resolve_type({:?},{:?})", path, id);
4168 let is_generic = match path.def {
4169 Def::PrimTy(p) => match p {
4170 hir::Str => return Primitive(PrimitiveType::Str),
4171 hir::Bool => return Primitive(PrimitiveType::Bool),
4172 hir::Char => return Primitive(PrimitiveType::Char),
4173 hir::Int(int_ty) => return Primitive(int_ty.into()),
4174 hir::Uint(uint_ty) => return Primitive(uint_ty.into()),
4175 hir::Float(float_ty) => return Primitive(float_ty.into()),
4177 Def::SelfTy(..) if path.segments.len() == 1 => {
4178 return Generic(keywords::SelfUpper.name().to_string());
4180 Def::TyParam(..) if path.segments.len() == 1 => {
4181 return Generic(format!("{:#}", path));
4183 Def::SelfTy(..) | Def::TyParam(..) | Def::AssociatedTy(..) => true,
4186 let did = register_def(&*cx, path.def);
4187 ResolvedPath { path: path, param_names: None, did: did, is_generic: is_generic }
4190 pub fn register_def(cx: &DocContext<'_>, def: Def) -> DefId {
4191 debug!("register_def({:?})", def);
4193 let (did, kind) = match def {
4194 Def::Fn(i) => (i, TypeKind::Function),
4195 Def::TyAlias(i) => (i, TypeKind::Typedef),
4196 Def::Enum(i) => (i, TypeKind::Enum),
4197 Def::Trait(i) => (i, TypeKind::Trait),
4198 Def::Struct(i) => (i, TypeKind::Struct),
4199 Def::Union(i) => (i, TypeKind::Union),
4200 Def::Mod(i) => (i, TypeKind::Module),
4201 Def::ForeignTy(i) => (i, TypeKind::Foreign),
4202 Def::Const(i) => (i, TypeKind::Const),
4203 Def::Static(i, _) => (i, TypeKind::Static),
4204 Def::Variant(i) => (cx.tcx.parent(i).expect("cannot get parent def id"),
4206 Def::Macro(i, mac_kind) => 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 Def::TraitAlias(i) => (i, TypeKind::TraitAlias),
4213 Def::SelfTy(Some(def_id), _) => (def_id, TypeKind::Trait),
4214 Def::SelfTy(_, Some(impl_def_id)) => return impl_def_id,
4215 _ => return def.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.def.opt_def_id().is_none() {
4230 Some(register_def(cx, path.def))
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: &[&str]) -> 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: &[&str]) -> 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, Lrc::new(vec![])).iter() {
4438 if item.ident.name == *segment {
4439 if path_it.peek().is_none() {
4440 return match item.def {
4441 def::Def::Trait(did) => Some(did),
4446 items = tcx.item_children(item.def.def_id());
4456 pub fn get_path_for_type(
4457 tcx: TyCtxt<'_, '_, '_>,
4459 def_ctor: impl Fn(DefId) -> Def,
4461 use rustc::ty::print::Printer;
4463 struct AbsolutePathPrinter<'a, 'tcx> {
4464 tcx: TyCtxt<'a, 'tcx, 'tcx>,
4467 impl Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> {
4470 type Path = Vec<String>;
4473 type DynExistential = ();
4475 fn tcx(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {
4481 _region: ty::Region<'_>,
4482 ) -> Result<Self::Region, Self::Error> {
4489 ) -> Result<Self::Type, Self::Error> {
4493 fn print_dyn_existential(
4495 _predicates: &'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
4496 ) -> Result<Self::DynExistential, Self::Error> {
4503 ) -> Result<Self::Path, Self::Error> {
4504 Ok(vec![self.tcx.original_crate_name(cnum).to_string()])
4509 trait_ref: Option<ty::TraitRef<'tcx>>,
4510 ) -> Result<Self::Path, Self::Error> {
4511 // This shouldn't ever be needed, but just in case:
4512 Ok(vec![match trait_ref {
4513 Some(trait_ref) => format!("{:?}", trait_ref),
4514 None => format!("<{}>", self_ty),
4518 fn path_append_impl(
4520 print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
4521 _disambiguated_data: &DisambiguatedDefPathData,
4523 trait_ref: Option<ty::TraitRef<'tcx>>,
4524 ) -> Result<Self::Path, Self::Error> {
4525 let mut path = print_prefix(self)?;
4527 // This shouldn't ever be needed, but just in case:
4528 path.push(match trait_ref {
4529 Some(trait_ref) => {
4530 format!("<impl {} for {}>", trait_ref, self_ty)
4532 None => format!("<impl {}>", self_ty),
4539 print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
4540 disambiguated_data: &DisambiguatedDefPathData,
4541 ) -> Result<Self::Path, Self::Error> {
4542 let mut path = print_prefix(self)?;
4543 path.push(disambiguated_data.data.as_interned_str().to_string());
4546 fn path_generic_args(
4548 print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
4549 _args: &[Kind<'tcx>],
4550 ) -> Result<Self::Path, Self::Error> {
4555 let names = AbsolutePathPrinter { tcx: tcx.global_tcx() }
4556 .print_def_path(def_id, &[])
4561 def: def_ctor(def_id),
4562 segments: hir::HirVec::from_vec(names.iter().map(|s| hir::PathSegment {
4563 ident: ast::Ident::from_str(&s),
4572 // End of code copied from rust-clippy
4575 #[derive(Eq, PartialEq, Hash, Copy, Clone, Debug)]
4576 enum RegionTarget<'tcx> {
4577 Region(Region<'tcx>),
4578 RegionVid(RegionVid)
4581 #[derive(Default, Debug, Clone)]
4582 struct RegionDeps<'tcx> {
4583 larger: FxHashSet<RegionTarget<'tcx>>,
4584 smaller: FxHashSet<RegionTarget<'tcx>>
4587 #[derive(Eq, PartialEq, Hash, Debug)]
4589 TraitBound(Vec<PathSegment>, Vec<SimpleBound>, Vec<GenericParamDef>, hir::TraitBoundModifier),
4593 enum AutoTraitResult {
4595 PositiveImpl(Generics),
4599 impl AutoTraitResult {
4600 fn is_auto(&self) -> bool {
4602 AutoTraitResult::PositiveImpl(_) | AutoTraitResult::NegativeImpl => true,
4608 impl From<GenericBound> for SimpleBound {
4609 fn from(bound: GenericBound) -> Self {
4610 match bound.clone() {
4611 GenericBound::Outlives(l) => SimpleBound::Outlives(l),
4612 GenericBound::TraitBound(t, mod_) => match t.trait_ {
4613 Type::ResolvedPath { path, param_names, .. } => {
4614 SimpleBound::TraitBound(path.segments,
4616 .map_or_else(|| Vec::new(), |v| v.iter()
4617 .map(|p| SimpleBound::from(p.clone()))
4622 _ => panic!("Unexpected bound {:?}", bound),