1 // ignore-tidy-filelength
3 //! This module contains the "cleaned" pieces of the AST, and the functions
12 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
13 use rustc_target::spec::abi::Abi;
14 use rustc_typeck::hir_ty_to_ty;
15 use rustc::infer::region_constraints::{RegionConstraintData, Constraint};
16 use rustc::middle::resolve_lifetime as rl;
17 use rustc::middle::lang_items;
18 use rustc::middle::stability;
19 use rustc::mir::interpret::{GlobalId, ConstValue};
21 use rustc::hir::def::{CtorKind, DefKind, Res};
22 use rustc::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
23 use rustc::hir::ptr::P;
24 use rustc::ty::subst::{InternalSubsts, SubstsRef, GenericArgKind};
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_SP;
33 use syntax::symbol::{Symbol, kw, sym};
34 use syntax::symbol::InternedString;
35 use syntax_pos::{self, Pos, FileName};
37 use std::collections::hash_map::Entry;
39 use std::hash::{Hash, Hasher};
40 use std::default::Default;
41 use std::{mem, slice, vec};
42 use std::iter::FromIterator;
44 use std::cell::RefCell;
48 use crate::core::{self, DocContext, ImplTraitParam};
50 use crate::html::render::{cache, ExternalLocation};
51 use crate::html::item_type::ItemType;
55 use self::auto_trait::AutoTraitFinder;
56 use self::blanket_impl::BlanketImplFinder;
58 pub use self::Type::*;
59 pub use self::Mutability::*;
60 pub use self::ItemEnum::*;
61 pub use self::SelfTy::*;
62 pub use self::FunctionRetTy::*;
63 pub use self::Visibility::{Public, Inherited};
65 thread_local!(pub static MAX_DEF_ID: RefCell<FxHashMap<CrateNum, DefId>> = Default::default());
67 const FN_OUTPUT_NAME: &'static str = "Output";
69 // extract the stability index for a node from tcx, if possible
70 fn get_stability(cx: &DocContext<'_>, def_id: DefId) -> Option<Stability> {
71 cx.tcx.lookup_stability(def_id).clean(cx)
74 fn get_deprecation(cx: &DocContext<'_>, def_id: DefId) -> Option<Deprecation> {
75 cx.tcx.lookup_deprecation(def_id).clean(cx)
79 fn clean(&self, cx: &DocContext<'_>) -> T;
82 impl<T: Clean<U>, U> Clean<Vec<U>> for [T] {
83 fn clean(&self, cx: &DocContext<'_>) -> Vec<U> {
84 self.iter().map(|x| x.clean(cx)).collect()
88 impl<T: Clean<U>, U, V: Idx> Clean<IndexVec<V, U>> for IndexVec<V, T> {
89 fn clean(&self, cx: &DocContext<'_>) -> IndexVec<V, U> {
90 self.iter().map(|x| x.clean(cx)).collect()
94 impl<T: Clean<U>, U> Clean<U> for P<T> {
95 fn clean(&self, cx: &DocContext<'_>) -> U {
100 impl<T: Clean<U>, U> Clean<U> for Rc<T> {
101 fn clean(&self, cx: &DocContext<'_>) -> U {
106 impl<T: Clean<U>, U> Clean<Option<U>> for Option<T> {
107 fn clean(&self, cx: &DocContext<'_>) -> Option<U> {
108 self.as_ref().map(|v| v.clean(cx))
112 impl<T, U> Clean<U> for ty::Binder<T> where T: Clean<U> {
113 fn clean(&self, cx: &DocContext<'_>) -> U {
114 self.skip_binder().clean(cx)
118 impl<T: Clean<U>, U> Clean<Vec<U>> for P<[T]> {
119 fn clean(&self, cx: &DocContext<'_>) -> Vec<U> {
120 self.iter().map(|x| x.clean(cx)).collect()
124 #[derive(Clone, Debug)]
127 pub version: Option<String>,
129 pub module: Option<Item>,
130 pub externs: Vec<(CrateNum, ExternalCrate)>,
131 pub primitives: Vec<(DefId, PrimitiveType, Attributes)>,
132 // These are later on moved into `CACHEKEY`, leaving the map empty.
133 // Only here so that they can be filtered through the rustdoc passes.
134 pub external_traits: Rc<RefCell<FxHashMap<DefId, Trait>>>,
135 pub masked_crates: FxHashSet<CrateNum>,
139 pub fn krate(mut cx: &mut DocContext<'_>) -> Crate {
140 use crate::visit_lib::LibEmbargoVisitor;
142 let krate = cx.tcx.hir().krate();
143 let module = crate::visit_ast::RustdocVisitor::new(&mut cx).visit(krate);
145 let mut r = cx.renderinfo.get_mut();
146 r.deref_trait_did = cx.tcx.lang_items().deref_trait();
147 r.deref_mut_trait_did = cx.tcx.lang_items().deref_mut_trait();
148 r.owned_box_did = cx.tcx.lang_items().owned_box();
150 let mut externs = Vec::new();
151 for &cnum in cx.tcx.crates().iter() {
152 externs.push((cnum, cnum.clean(cx)));
153 // Analyze doc-reachability for extern items
154 LibEmbargoVisitor::new(&mut cx).visit_lib(cnum);
156 externs.sort_by(|&(a, _), &(b, _)| a.cmp(&b));
158 // Clean the crate, translating the entire libsyntax AST to one that is
159 // understood by rustdoc.
160 let mut module = module.clean(cx);
161 let mut masked_crates = FxHashSet::default();
164 ModuleItem(ref module) => {
165 for it in &module.items {
166 // `compiler_builtins` should be masked too, but we can't apply
167 // `#[doc(masked)]` to the injected `extern crate` because it's unstable.
168 if it.is_extern_crate()
169 && (it.attrs.has_doc_flag(sym::masked)
170 || cx.tcx.is_compiler_builtins(it.def_id.krate))
172 masked_crates.insert(it.def_id.krate);
179 let ExternalCrate { name, src, primitives, keywords, .. } = LOCAL_CRATE.clean(cx);
181 let m = match module.inner {
182 ModuleItem(ref mut m) => m,
185 m.items.extend(primitives.iter().map(|&(def_id, prim, ref attrs)| {
187 source: Span::empty(),
188 name: Some(prim.to_url_str().to_string()),
189 attrs: attrs.clone(),
191 stability: get_stability(cx, def_id),
192 deprecation: get_deprecation(cx, def_id),
194 inner: PrimitiveItem(prim),
197 m.items.extend(keywords.into_iter().map(|(def_id, kw, attrs)| {
199 source: Span::empty(),
200 name: Some(kw.clone()),
203 stability: get_stability(cx, def_id),
204 deprecation: get_deprecation(cx, def_id),
206 inner: KeywordItem(kw),
215 module: Some(module),
218 external_traits: cx.external_traits.clone(),
224 #[derive(Clone, Debug)]
225 pub struct ExternalCrate {
228 pub attrs: Attributes,
229 pub primitives: Vec<(DefId, PrimitiveType, Attributes)>,
230 pub keywords: Vec<(DefId, String, Attributes)>,
233 impl Clean<ExternalCrate> for CrateNum {
234 fn clean(&self, cx: &DocContext<'_>) -> ExternalCrate {
235 let root = DefId { krate: *self, index: CRATE_DEF_INDEX };
236 let krate_span = cx.tcx.def_span(root);
237 let krate_src = cx.sess().source_map().span_to_filename(krate_span);
239 // Collect all inner modules which are tagged as implementations of
242 // Note that this loop only searches the top-level items of the crate,
243 // and this is intentional. If we were to search the entire crate for an
244 // item tagged with `#[doc(primitive)]` then we would also have to
245 // search the entirety of external modules for items tagged
246 // `#[doc(primitive)]`, which is a pretty inefficient process (decoding
247 // all that metadata unconditionally).
249 // In order to keep the metadata load under control, the
250 // `#[doc(primitive)]` feature is explicitly designed to only allow the
251 // primitive tags to show up as the top level items in a crate.
253 // Also note that this does not attempt to deal with modules tagged
254 // duplicately for the same primitive. This is handled later on when
255 // rendering by delegating everything to a hash map.
256 let as_primitive = |res: Res| {
257 if let Res::Def(DefKind::Mod, def_id) = res {
258 let attrs = cx.tcx.get_attrs(def_id).clean(cx);
260 for attr in attrs.lists(sym::doc) {
261 if let Some(v) = attr.value_str() {
262 if attr.check_name(sym::primitive) {
263 prim = PrimitiveType::from_str(&v.as_str());
267 // FIXME: should warn on unknown primitives?
271 return prim.map(|p| (def_id, p, attrs));
275 let primitives = if root.is_local() {
276 cx.tcx.hir().krate().module.item_ids.iter().filter_map(|&id| {
277 let item = cx.tcx.hir().expect_item(id.id);
279 hir::ItemKind::Mod(_) => {
280 as_primitive(Res::Def(
282 cx.tcx.hir().local_def_id(id.id),
285 hir::ItemKind::Use(ref path, hir::UseKind::Single)
286 if item.vis.node.is_pub() => {
287 as_primitive(path.res).map(|(_, prim, attrs)| {
288 // Pretend the primitive is local.
289 (cx.tcx.hir().local_def_id(id.id), prim, attrs)
296 cx.tcx.item_children(root).iter().map(|item| item.res)
297 .filter_map(as_primitive).collect()
300 let as_keyword = |res: Res| {
301 if let Res::Def(DefKind::Mod, def_id) = res {
302 let attrs = cx.tcx.get_attrs(def_id).clean(cx);
303 let mut keyword = None;
304 for attr in attrs.lists(sym::doc) {
305 if let Some(v) = attr.value_str() {
306 if attr.check_name(sym::keyword) {
307 if v.is_doc_keyword() {
308 keyword = Some(v.to_string());
311 // FIXME: should warn on unknown keywords?
315 return keyword.map(|p| (def_id, p, attrs));
319 let keywords = if root.is_local() {
320 cx.tcx.hir().krate().module.item_ids.iter().filter_map(|&id| {
321 let item = cx.tcx.hir().expect_item(id.id);
323 hir::ItemKind::Mod(_) => {
326 cx.tcx.hir().local_def_id(id.id),
329 hir::ItemKind::Use(ref path, hir::UseKind::Single)
330 if item.vis.node.is_pub() => {
331 as_keyword(path.res).map(|(_, prim, attrs)| {
332 (cx.tcx.hir().local_def_id(id.id), prim, attrs)
339 cx.tcx.item_children(root).iter().map(|item| item.res)
340 .filter_map(as_keyword).collect()
344 name: cx.tcx.crate_name(*self).to_string(),
346 attrs: cx.tcx.get_attrs(root).clean(cx),
353 /// Anything with a source location and set of attributes and, optionally, a
354 /// name. That is, anything that can be documented. This doesn't correspond
355 /// directly to the AST's concept of an item; it's a strict superset.
360 /// Not everything has a name. E.g., impls
361 pub name: Option<String>,
362 pub attrs: Attributes,
364 pub visibility: Visibility,
366 pub stability: Option<Stability>,
367 pub deprecation: Option<Deprecation>,
370 impl fmt::Debug for Item {
371 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
373 let fake = MAX_DEF_ID.with(|m| m.borrow().get(&self.def_id.krate)
374 .map(|id| self.def_id >= *id).unwrap_or(false));
375 let def_id: &dyn fmt::Debug = if fake { &"**FAKE**" } else { &self.def_id };
377 fmt.debug_struct("Item")
378 .field("source", &self.source)
379 .field("name", &self.name)
380 .field("attrs", &self.attrs)
381 .field("inner", &self.inner)
382 .field("visibility", &self.visibility)
383 .field("def_id", def_id)
384 .field("stability", &self.stability)
385 .field("deprecation", &self.deprecation)
391 /// Finds the `doc` attribute as a NameValue and returns the corresponding
393 pub fn doc_value(&self) -> Option<&str> {
394 self.attrs.doc_value()
396 /// Finds all `doc` attributes as NameValues and returns their corresponding values, joined
398 pub fn collapsed_doc_value(&self) -> Option<String> {
399 self.attrs.collapsed_doc_value()
402 pub fn links(&self) -> Vec<(String, String)> {
403 self.attrs.links(&self.def_id.krate)
406 pub fn is_crate(&self) -> bool {
408 StrippedItem(box ModuleItem(Module { is_crate: true, ..})) |
409 ModuleItem(Module { is_crate: true, ..}) => true,
413 pub fn is_mod(&self) -> bool {
414 self.type_() == ItemType::Module
416 pub fn is_trait(&self) -> bool {
417 self.type_() == ItemType::Trait
419 pub fn is_struct(&self) -> bool {
420 self.type_() == ItemType::Struct
422 pub fn is_enum(&self) -> bool {
423 self.type_() == ItemType::Enum
425 pub fn is_variant(&self) -> bool {
426 self.type_() == ItemType::Variant
428 pub fn is_associated_type(&self) -> bool {
429 self.type_() == ItemType::AssocType
431 pub fn is_associated_const(&self) -> bool {
432 self.type_() == ItemType::AssocConst
434 pub fn is_method(&self) -> bool {
435 self.type_() == ItemType::Method
437 pub fn is_ty_method(&self) -> bool {
438 self.type_() == ItemType::TyMethod
440 pub fn is_typedef(&self) -> bool {
441 self.type_() == ItemType::Typedef
443 pub fn is_primitive(&self) -> bool {
444 self.type_() == ItemType::Primitive
446 pub fn is_union(&self) -> bool {
447 self.type_() == ItemType::Union
449 pub fn is_import(&self) -> bool {
450 self.type_() == ItemType::Import
452 pub fn is_extern_crate(&self) -> bool {
453 self.type_() == ItemType::ExternCrate
455 pub fn is_keyword(&self) -> bool {
456 self.type_() == ItemType::Keyword
459 pub fn is_stripped(&self) -> bool {
460 match self.inner { StrippedItem(..) => true, _ => false }
462 pub fn has_stripped_fields(&self) -> Option<bool> {
464 StructItem(ref _struct) => Some(_struct.fields_stripped),
465 UnionItem(ref union) => Some(union.fields_stripped),
466 VariantItem(Variant { kind: VariantKind::Struct(ref vstruct)} ) => {
467 Some(vstruct.fields_stripped)
473 pub fn stability_class(&self) -> Option<String> {
474 self.stability.as_ref().and_then(|ref s| {
475 let mut classes = Vec::with_capacity(2);
477 if s.level == stability::Unstable {
478 classes.push("unstable");
481 if s.deprecation.is_some() {
482 classes.push("deprecated");
485 if classes.len() != 0 {
486 Some(classes.join(" "))
493 pub fn stable_since(&self) -> Option<&str> {
494 self.stability.as_ref().map(|s| &s.since[..])
497 pub fn is_non_exhaustive(&self) -> bool {
498 self.attrs.other_attrs.iter()
499 .any(|a| a.check_name(sym::non_exhaustive))
502 /// Returns a documentation-level item type from the item.
503 pub fn type_(&self) -> ItemType {
507 /// Returns the info in the item's `#[deprecated]` or `#[rustc_deprecated]` attributes.
509 /// If the item is not deprecated, returns `None`.
510 pub fn deprecation(&self) -> Option<&Deprecation> {
513 .or_else(|| self.stability.as_ref().and_then(|s| s.deprecation.as_ref()))
515 pub fn is_default(&self) -> bool {
517 ItemEnum::MethodItem(ref meth) => {
518 if let Some(defaultness) = meth.defaultness {
519 defaultness.has_value() && !defaultness.is_final()
529 #[derive(Clone, Debug)]
531 ExternCrateItem(String, Option<String>),
536 FunctionItem(Function),
538 TypedefItem(Typedef, bool /* is associated type */),
539 OpaqueTyItem(OpaqueTy, bool /* is associated type */),
541 ConstantItem(Constant),
543 TraitAliasItem(TraitAlias),
545 /// A method signature only. Used for required methods in traits (ie,
546 /// non-default-methods).
547 TyMethodItem(TyMethod),
548 /// A method with a body.
550 StructFieldItem(Type),
551 VariantItem(Variant),
552 /// `fn`s from an extern block
553 ForeignFunctionItem(Function),
554 /// `static`s from an extern block
555 ForeignStaticItem(Static),
556 /// `type`s from an extern block
559 ProcMacroItem(ProcMacro),
560 PrimitiveItem(PrimitiveType),
561 AssocConstItem(Type, Option<String>),
562 AssocTypeItem(Vec<GenericBound>, Option<Type>),
563 /// An item that has been stripped by a rustdoc pass
564 StrippedItem(Box<ItemEnum>),
569 pub fn is_associated(&self) -> bool {
571 ItemEnum::TypedefItem(_, _) |
572 ItemEnum::AssocTypeItem(_, _) => true,
578 #[derive(Clone, Debug)]
580 pub items: Vec<Item>,
584 impl Clean<Item> for doctree::Module<'_> {
585 fn clean(&self, cx: &DocContext<'_>) -> Item {
586 let name = if self.name.is_some() {
587 self.name.expect("No name provided").clean(cx)
592 // maintain a stack of mod ids, for doc comment path resolution
593 // but we also need to resolve the module's own docs based on whether its docs were written
594 // inside or outside the module, so check for that
595 let attrs = self.attrs.clean(cx);
597 let mut items: Vec<Item> = vec![];
598 items.extend(self.extern_crates.iter().flat_map(|x| x.clean(cx)));
599 items.extend(self.imports.iter().flat_map(|x| x.clean(cx)));
600 items.extend(self.structs.iter().map(|x| x.clean(cx)));
601 items.extend(self.unions.iter().map(|x| x.clean(cx)));
602 items.extend(self.enums.iter().map(|x| x.clean(cx)));
603 items.extend(self.fns.iter().map(|x| x.clean(cx)));
604 items.extend(self.foreigns.iter().map(|x| x.clean(cx)));
605 items.extend(self.mods.iter().map(|x| x.clean(cx)));
606 items.extend(self.typedefs.iter().map(|x| x.clean(cx)));
607 items.extend(self.opaque_tys.iter().map(|x| x.clean(cx)));
608 items.extend(self.statics.iter().map(|x| x.clean(cx)));
609 items.extend(self.constants.iter().map(|x| x.clean(cx)));
610 items.extend(self.traits.iter().map(|x| x.clean(cx)));
611 items.extend(self.impls.iter().flat_map(|x| x.clean(cx)));
612 items.extend(self.macros.iter().map(|x| x.clean(cx)));
613 items.extend(self.proc_macros.iter().map(|x| x.clean(cx)));
614 items.extend(self.trait_aliases.iter().map(|x| x.clean(cx)));
616 // determine if we should display the inner contents or
617 // the outer `mod` item for the source code.
619 let cm = cx.sess().source_map();
620 let outer = cm.lookup_char_pos(self.where_outer.lo());
621 let inner = cm.lookup_char_pos(self.where_inner.lo());
622 if outer.file.start_pos == inner.file.start_pos {
626 // mod foo; (and a separate SourceFile for the contents)
634 source: whence.clean(cx),
635 visibility: self.vis.clean(cx),
636 stability: cx.stability(self.id).clean(cx),
637 deprecation: cx.deprecation(self.id).clean(cx),
638 def_id: cx.tcx.hir().local_def_id(self.id),
639 inner: ModuleItem(Module {
640 is_crate: self.is_crate,
647 pub struct ListAttributesIter<'a> {
648 attrs: slice::Iter<'a, ast::Attribute>,
649 current_list: vec::IntoIter<ast::NestedMetaItem>,
653 impl<'a> Iterator for ListAttributesIter<'a> {
654 type Item = ast::NestedMetaItem;
656 fn next(&mut self) -> Option<Self::Item> {
657 if let Some(nested) = self.current_list.next() {
661 for attr in &mut self.attrs {
662 if let Some(list) = attr.meta_item_list() {
663 if attr.check_name(self.name) {
664 self.current_list = list.into_iter();
665 if let Some(nested) = self.current_list.next() {
675 fn size_hint(&self) -> (usize, Option<usize>) {
676 let lower = self.current_list.len();
681 pub trait AttributesExt {
682 /// Finds an attribute as List and returns the list of attributes nested inside.
683 fn lists(&self, name: Symbol) -> ListAttributesIter<'_>;
686 impl AttributesExt for [ast::Attribute] {
687 fn lists(&self, name: Symbol) -> ListAttributesIter<'_> {
690 current_list: Vec::new().into_iter(),
696 pub trait NestedAttributesExt {
697 /// Returns `true` if the attribute list contains a specific `Word`
698 fn has_word(self, word: Symbol) -> bool;
701 impl<I: IntoIterator<Item=ast::NestedMetaItem>> NestedAttributesExt for I {
702 fn has_word(self, word: Symbol) -> bool {
703 self.into_iter().any(|attr| attr.is_word() && attr.check_name(word))
707 /// A portion of documentation, extracted from a `#[doc]` attribute.
709 /// Each variant contains the line number within the complete doc-comment where the fragment
710 /// starts, as well as the Span where the corresponding doc comment or attribute is located.
712 /// Included files are kept separate from inline doc comments so that proper line-number
713 /// information can be given when a doctest fails. Sugared doc comments and "raw" doc comments are
714 /// kept separate because of issue #42760.
715 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
716 pub enum DocFragment {
717 /// A doc fragment created from a `///` or `//!` doc comment.
718 SugaredDoc(usize, syntax_pos::Span, String),
719 /// A doc fragment created from a "raw" `#[doc=""]` attribute.
720 RawDoc(usize, syntax_pos::Span, String),
721 /// A doc fragment created from a `#[doc(include="filename")]` attribute. Contains both the
722 /// given filename and the file contents.
723 Include(usize, syntax_pos::Span, String, String),
727 pub fn as_str(&self) -> &str {
729 DocFragment::SugaredDoc(_, _, ref s) => &s[..],
730 DocFragment::RawDoc(_, _, ref s) => &s[..],
731 DocFragment::Include(_, _, _, ref s) => &s[..],
735 pub fn span(&self) -> syntax_pos::Span {
737 DocFragment::SugaredDoc(_, span, _) |
738 DocFragment::RawDoc(_, span, _) |
739 DocFragment::Include(_, span, _, _) => span,
744 impl<'a> FromIterator<&'a DocFragment> for String {
745 fn from_iter<T>(iter: T) -> Self
747 T: IntoIterator<Item = &'a DocFragment>
749 iter.into_iter().fold(String::new(), |mut acc, frag| {
754 DocFragment::SugaredDoc(_, _, ref docs)
755 | DocFragment::RawDoc(_, _, ref docs)
756 | DocFragment::Include(_, _, _, ref docs) =>
765 #[derive(Clone, Debug, Default)]
766 pub struct Attributes {
767 pub doc_strings: Vec<DocFragment>,
768 pub other_attrs: Vec<ast::Attribute>,
769 pub cfg: Option<Arc<Cfg>>,
770 pub span: Option<syntax_pos::Span>,
771 /// map from Rust paths to resolved defs and potential URL fragments
772 pub links: Vec<(String, Option<DefId>, Option<String>)>,
773 pub inner_docs: bool,
777 /// Extracts the content from an attribute `#[doc(cfg(content))]`.
778 fn extract_cfg(mi: &ast::MetaItem) -> Option<&ast::MetaItem> {
779 use syntax::ast::NestedMetaItem::MetaItem;
781 if let ast::MetaItemKind::List(ref nmis) = mi.node {
783 if let MetaItem(ref cfg_mi) = nmis[0] {
784 if cfg_mi.check_name(sym::cfg) {
785 if let ast::MetaItemKind::List(ref cfg_nmis) = cfg_mi.node {
786 if cfg_nmis.len() == 1 {
787 if let MetaItem(ref content_mi) = cfg_nmis[0] {
788 return Some(content_mi);
800 /// Reads a `MetaItem` from within an attribute, looks for whether it is a
801 /// `#[doc(include="file")]`, and returns the filename and contents of the file as loaded from
803 fn extract_include(mi: &ast::MetaItem)
804 -> Option<(String, String)>
806 mi.meta_item_list().and_then(|list| {
808 if meta.check_name(sym::include) {
809 // the actual compiled `#[doc(include="filename")]` gets expanded to
810 // `#[doc(include(file="filename", contents="file contents")]` so we need to
811 // look for that instead
812 return meta.meta_item_list().and_then(|list| {
813 let mut filename: Option<String> = None;
814 let mut contents: Option<String> = None;
817 if it.check_name(sym::file) {
818 if let Some(name) = it.value_str() {
819 filename = Some(name.to_string());
821 } else if it.check_name(sym::contents) {
822 if let Some(docs) = it.value_str() {
823 contents = Some(docs.to_string());
828 if let (Some(filename), Some(contents)) = (filename, contents) {
829 Some((filename, contents))
841 pub fn has_doc_flag(&self, flag: Symbol) -> bool {
842 for attr in &self.other_attrs {
843 if !attr.check_name(sym::doc) { continue; }
845 if let Some(items) = attr.meta_item_list() {
846 if items.iter().filter_map(|i| i.meta_item()).any(|it| it.check_name(flag)) {
855 pub fn from_ast(diagnostic: &::errors::Handler,
856 attrs: &[ast::Attribute]) -> Attributes {
857 let mut doc_strings = vec![];
859 let mut cfg = Cfg::True;
860 let mut doc_line = 0;
862 let other_attrs = attrs.iter().filter_map(|attr| {
863 attr.with_desugared_doc(|attr| {
864 if attr.check_name(sym::doc) {
865 if let Some(mi) = attr.meta() {
866 if let Some(value) = mi.value_str() {
867 // Extracted #[doc = "..."]
868 let value = value.to_string();
870 doc_line += value.lines().count();
872 if attr.is_sugared_doc {
873 doc_strings.push(DocFragment::SugaredDoc(line, attr.span, value));
875 doc_strings.push(DocFragment::RawDoc(line, attr.span, value));
879 sp = Some(attr.span);
882 } else if let Some(cfg_mi) = Attributes::extract_cfg(&mi) {
883 // Extracted #[doc(cfg(...))]
884 match Cfg::parse(cfg_mi) {
885 Ok(new_cfg) => cfg &= new_cfg,
886 Err(e) => diagnostic.span_err(e.span, e.msg),
889 } else if let Some((filename, contents)) = Attributes::extract_include(&mi)
892 doc_line += contents.lines().count();
893 doc_strings.push(DocFragment::Include(line,
904 // treat #[target_feature(enable = "feat")] attributes as if they were
905 // #[doc(cfg(target_feature = "feat"))] attributes as well
906 for attr in attrs.lists(sym::target_feature) {
907 if attr.check_name(sym::enable) {
908 if let Some(feat) = attr.value_str() {
909 let meta = attr::mk_name_value_item_str(
910 Ident::with_dummy_span(sym::target_feature), feat, DUMMY_SP
912 if let Ok(feat_cfg) = Cfg::parse(&meta) {
919 let inner_docs = attrs.iter()
920 .filter(|a| a.check_name(sym::doc))
922 .map_or(true, |a| a.style == AttrStyle::Inner);
927 cfg: if cfg == Cfg::True { None } else { Some(Arc::new(cfg)) },
934 /// Finds the `doc` attribute as a NameValue and returns the corresponding
936 pub fn doc_value(&self) -> Option<&str> {
937 self.doc_strings.first().map(|s| s.as_str())
940 /// Finds all `doc` attributes as NameValues and returns their corresponding values, joined
942 pub fn collapsed_doc_value(&self) -> Option<String> {
943 if !self.doc_strings.is_empty() {
944 Some(self.doc_strings.iter().collect())
950 /// Gets links as a vector
952 /// Cache must be populated before call
953 pub fn links(&self, krate: &CrateNum) -> Vec<(String, String)> {
954 use crate::html::format::href;
956 self.links.iter().filter_map(|&(ref s, did, ref fragment)| {
959 if let Some((mut href, ..)) = href(did) {
960 if let Some(ref fragment) = *fragment {
962 href.push_str(fragment);
964 Some((s.clone(), href))
970 if let Some(ref fragment) = *fragment {
972 let url = match cache.extern_locations.get(krate) {
973 Some(&(_, ref src, ExternalLocation::Local)) =>
974 src.to_str().expect("invalid file path"),
975 Some(&(_, _, ExternalLocation::Remote(ref s))) => s,
976 Some(&(_, _, ExternalLocation::Unknown)) | None =>
977 "https://doc.rust-lang.org/nightly",
979 // This is a primitive so the url is done "by hand".
980 let tail = fragment.find('#').unwrap_or_else(|| fragment.len());
982 format!("{}{}std/primitive.{}.html{}",
984 if !url.ends_with('/') { "/" } else { "" },
988 panic!("This isn't a primitive?!");
996 impl PartialEq for Attributes {
997 fn eq(&self, rhs: &Self) -> bool {
998 self.doc_strings == rhs.doc_strings &&
999 self.cfg == rhs.cfg &&
1000 self.span == rhs.span &&
1001 self.links == rhs.links &&
1002 self.other_attrs.iter().map(|attr| attr.id).eq(rhs.other_attrs.iter().map(|attr| attr.id))
1006 impl Eq for Attributes {}
1008 impl Hash for Attributes {
1009 fn hash<H: Hasher>(&self, hasher: &mut H) {
1010 self.doc_strings.hash(hasher);
1011 self.cfg.hash(hasher);
1012 self.span.hash(hasher);
1013 self.links.hash(hasher);
1014 for attr in &self.other_attrs {
1015 attr.id.hash(hasher);
1020 impl AttributesExt for Attributes {
1021 fn lists(&self, name: Symbol) -> ListAttributesIter<'_> {
1022 self.other_attrs.lists(name)
1026 impl Clean<Attributes> for [ast::Attribute] {
1027 fn clean(&self, cx: &DocContext<'_>) -> Attributes {
1028 Attributes::from_ast(cx.sess().diagnostic(), self)
1032 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
1033 pub enum GenericBound {
1034 TraitBound(PolyTrait, hir::TraitBoundModifier),
1039 fn maybe_sized(cx: &DocContext<'_>) -> GenericBound {
1040 let did = cx.tcx.require_lang_item(lang_items::SizedTraitLangItem, None);
1041 let empty = cx.tcx.intern_substs(&[]);
1042 let path = external_path(cx, cx.tcx.item_name(did),
1043 Some(did), false, vec![], empty);
1044 inline::record_extern_fqn(cx, did, TypeKind::Trait);
1045 GenericBound::TraitBound(PolyTrait {
1046 trait_: ResolvedPath {
1052 generic_params: Vec::new(),
1053 }, hir::TraitBoundModifier::Maybe)
1056 fn is_sized_bound(&self, cx: &DocContext<'_>) -> bool {
1057 use rustc::hir::TraitBoundModifier as TBM;
1058 if let GenericBound::TraitBound(PolyTrait { ref trait_, .. }, TBM::None) = *self {
1059 if trait_.def_id() == cx.tcx.lang_items().sized_trait() {
1066 fn get_poly_trait(&self) -> Option<PolyTrait> {
1067 if let GenericBound::TraitBound(ref p, _) = *self {
1068 return Some(p.clone())
1073 fn get_trait_type(&self) -> Option<Type> {
1074 if let GenericBound::TraitBound(PolyTrait { ref trait_, .. }, _) = *self {
1075 Some(trait_.clone())
1082 impl Clean<GenericBound> for hir::GenericBound {
1083 fn clean(&self, cx: &DocContext<'_>) -> GenericBound {
1085 hir::GenericBound::Outlives(lt) => GenericBound::Outlives(lt.clean(cx)),
1086 hir::GenericBound::Trait(ref t, modifier) => {
1087 GenericBound::TraitBound(t.clean(cx), modifier)
1093 fn external_generic_args(
1094 cx: &DocContext<'_>,
1095 trait_did: Option<DefId>,
1097 bindings: Vec<TypeBinding>,
1098 substs: SubstsRef<'_>,
1100 let mut skip_self = has_self;
1101 let mut ty_kind = None;
1102 let args: Vec<_> = substs.iter().filter_map(|kind| match kind.unpack() {
1103 GenericArgKind::Lifetime(lt) => {
1104 lt.clean(cx).and_then(|lt| Some(GenericArg::Lifetime(lt)))
1106 GenericArgKind::Type(_) if skip_self => {
1110 GenericArgKind::Type(ty) => {
1111 ty_kind = Some(&ty.kind);
1112 Some(GenericArg::Type(ty.clean(cx)))
1114 GenericArgKind::Const(ct) => Some(GenericArg::Const(ct.clean(cx))),
1118 // Attempt to sugar an external path like Fn<(A, B,), C> to Fn(A, B) -> C
1119 Some(did) if cx.tcx.lang_items().fn_trait_kind(did).is_some() => {
1120 assert!(ty_kind.is_some());
1121 let inputs = match ty_kind {
1122 Some(ty::Tuple(ref tys)) => tys.iter().map(|t| t.expect_ty().clean(cx)).collect(),
1123 _ => return GenericArgs::AngleBracketed { args, bindings },
1126 // FIXME(#20299) return type comes from a projection now
1127 // match types[1].kind {
1128 // ty::Tuple(ref v) if v.is_empty() => None, // -> ()
1129 // _ => Some(types[1].clean(cx))
1131 GenericArgs::Parenthesized { inputs, output }
1134 GenericArgs::AngleBracketed { args, bindings }
1139 // trait_did should be set to a trait's DefId if called on a TraitRef, in order to sugar
1140 // from Fn<(A, B,), C> to Fn(A, B) -> C
1141 fn external_path(cx: &DocContext<'_>, name: Symbol, trait_did: Option<DefId>, has_self: bool,
1142 bindings: Vec<TypeBinding>, substs: SubstsRef<'_>) -> Path {
1146 segments: vec![PathSegment {
1147 name: name.as_str().to_string(),
1148 args: external_generic_args(cx, trait_did, has_self, bindings, substs)
1153 impl<'a, 'tcx> Clean<GenericBound> for (&'a ty::TraitRef<'tcx>, Vec<TypeBinding>) {
1154 fn clean(&self, cx: &DocContext<'_>) -> GenericBound {
1155 let (trait_ref, ref bounds) = *self;
1156 inline::record_extern_fqn(cx, trait_ref.def_id, TypeKind::Trait);
1157 let path = external_path(cx, cx.tcx.item_name(trait_ref.def_id),
1158 Some(trait_ref.def_id), true, bounds.clone(), trait_ref.substs);
1160 debug!("ty::TraitRef\n subst: {:?}\n", trait_ref.substs);
1162 // collect any late bound regions
1163 let mut late_bounds = vec![];
1164 for ty_s in trait_ref.input_types().skip(1) {
1165 if let ty::Tuple(ts) = ty_s.kind {
1167 if let ty::Ref(ref reg, _, _) = ty_s.expect_ty().kind {
1168 if let &ty::RegionKind::ReLateBound(..) = *reg {
1169 debug!(" hit an ReLateBound {:?}", reg);
1170 if let Some(Lifetime(name)) = reg.clean(cx) {
1171 late_bounds.push(GenericParamDef {
1173 kind: GenericParamDefKind::Lifetime,
1182 GenericBound::TraitBound(
1184 trait_: ResolvedPath {
1187 did: trait_ref.def_id,
1190 generic_params: late_bounds,
1192 hir::TraitBoundModifier::None
1197 impl<'tcx> Clean<GenericBound> for ty::TraitRef<'tcx> {
1198 fn clean(&self, cx: &DocContext<'_>) -> GenericBound {
1199 (self, vec![]).clean(cx)
1203 impl<'tcx> Clean<Option<Vec<GenericBound>>> for InternalSubsts<'tcx> {
1204 fn clean(&self, cx: &DocContext<'_>) -> Option<Vec<GenericBound>> {
1205 let mut v = Vec::new();
1206 v.extend(self.regions().filter_map(|r| r.clean(cx)).map(GenericBound::Outlives));
1207 v.extend(self.types().map(|t| GenericBound::TraitBound(PolyTrait {
1208 trait_: t.clean(cx),
1209 generic_params: Vec::new(),
1210 }, hir::TraitBoundModifier::None)));
1211 if !v.is_empty() {Some(v)} else {None}
1215 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
1216 pub struct Lifetime(String);
1219 pub fn get_ref<'a>(&'a self) -> &'a str {
1220 let Lifetime(ref s) = *self;
1225 pub fn statik() -> Lifetime {
1226 Lifetime("'static".to_string())
1230 impl Clean<Lifetime> for hir::Lifetime {
1231 fn clean(&self, cx: &DocContext<'_>) -> Lifetime {
1232 if self.hir_id != hir::DUMMY_HIR_ID {
1233 let def = cx.tcx.named_region(self.hir_id);
1235 Some(rl::Region::EarlyBound(_, node_id, _)) |
1236 Some(rl::Region::LateBound(_, node_id, _)) |
1237 Some(rl::Region::Free(_, node_id)) => {
1238 if let Some(lt) = cx.lt_substs.borrow().get(&node_id).cloned() {
1245 Lifetime(self.name.ident().to_string())
1249 impl Clean<Lifetime> for hir::GenericParam {
1250 fn clean(&self, _: &DocContext<'_>) -> Lifetime {
1252 hir::GenericParamKind::Lifetime { .. } => {
1253 if self.bounds.len() > 0 {
1254 let mut bounds = self.bounds.iter().map(|bound| match bound {
1255 hir::GenericBound::Outlives(lt) => lt,
1258 let name = bounds.next().expect("no more bounds").name.ident();
1259 let mut s = format!("{}: {}", self.name.ident(), name);
1260 for bound in bounds {
1261 s.push_str(&format!(" + {}", bound.name.ident()));
1265 Lifetime(self.name.ident().to_string())
1273 impl Clean<Constant> for hir::ConstArg {
1274 fn clean(&self, cx: &DocContext<'_>) -> Constant {
1276 type_: cx.tcx.type_of(cx.tcx.hir().body_owner_def_id(self.value.body)).clean(cx),
1277 expr: print_const_expr(cx, self.value.body),
1282 impl Clean<Lifetime> for ty::GenericParamDef {
1283 fn clean(&self, _cx: &DocContext<'_>) -> Lifetime {
1284 Lifetime(self.name.to_string())
1288 impl Clean<Option<Lifetime>> for ty::RegionKind {
1289 fn clean(&self, cx: &DocContext<'_>) -> Option<Lifetime> {
1291 ty::ReStatic => Some(Lifetime::statik()),
1292 ty::ReLateBound(_, ty::BrNamed(_, name)) => Some(Lifetime(name.to_string())),
1293 ty::ReEarlyBound(ref data) => Some(Lifetime(data.name.clean(cx))),
1295 ty::ReLateBound(..) |
1299 ty::RePlaceholder(..) |
1301 ty::ReClosureBound(_) |
1303 debug!("cannot clean region {:?}", self);
1310 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
1311 pub enum WherePredicate {
1312 BoundPredicate { ty: Type, bounds: Vec<GenericBound> },
1313 RegionPredicate { lifetime: Lifetime, bounds: Vec<GenericBound> },
1314 EqPredicate { lhs: Type, rhs: Type },
1317 impl WherePredicate {
1318 pub fn get_bounds(&self) -> Option<&[GenericBound]> {
1320 WherePredicate::BoundPredicate { ref bounds, .. } => Some(bounds),
1321 WherePredicate::RegionPredicate { ref bounds, .. } => Some(bounds),
1327 impl Clean<WherePredicate> for hir::WherePredicate {
1328 fn clean(&self, cx: &DocContext<'_>) -> WherePredicate {
1330 hir::WherePredicate::BoundPredicate(ref wbp) => {
1331 WherePredicate::BoundPredicate {
1332 ty: wbp.bounded_ty.clean(cx),
1333 bounds: wbp.bounds.clean(cx)
1337 hir::WherePredicate::RegionPredicate(ref wrp) => {
1338 WherePredicate::RegionPredicate {
1339 lifetime: wrp.lifetime.clean(cx),
1340 bounds: wrp.bounds.clean(cx)
1344 hir::WherePredicate::EqPredicate(ref wrp) => {
1345 WherePredicate::EqPredicate {
1346 lhs: wrp.lhs_ty.clean(cx),
1347 rhs: wrp.rhs_ty.clean(cx)
1354 impl<'a> Clean<Option<WherePredicate>> for ty::Predicate<'a> {
1355 fn clean(&self, cx: &DocContext<'_>) -> Option<WherePredicate> {
1356 use rustc::ty::Predicate;
1359 Predicate::Trait(ref pred) => Some(pred.clean(cx)),
1360 Predicate::Subtype(ref pred) => Some(pred.clean(cx)),
1361 Predicate::RegionOutlives(ref pred) => pred.clean(cx),
1362 Predicate::TypeOutlives(ref pred) => pred.clean(cx),
1363 Predicate::Projection(ref pred) => Some(pred.clean(cx)),
1365 Predicate::WellFormed(..) |
1366 Predicate::ObjectSafe(..) |
1367 Predicate::ClosureKind(..) |
1368 Predicate::ConstEvaluatable(..) => panic!("not user writable"),
1373 impl<'a> Clean<WherePredicate> for ty::TraitPredicate<'a> {
1374 fn clean(&self, cx: &DocContext<'_>) -> WherePredicate {
1375 WherePredicate::BoundPredicate {
1376 ty: self.trait_ref.self_ty().clean(cx),
1377 bounds: vec![self.trait_ref.clean(cx)]
1382 impl<'tcx> Clean<WherePredicate> for ty::SubtypePredicate<'tcx> {
1383 fn clean(&self, _cx: &DocContext<'_>) -> WherePredicate {
1384 panic!("subtype predicates are an internal rustc artifact \
1385 and should not be seen by rustdoc")
1389 impl<'tcx> Clean<Option<WherePredicate>> for
1390 ty::OutlivesPredicate<ty::Region<'tcx>,ty::Region<'tcx>> {
1392 fn clean(&self, cx: &DocContext<'_>) -> Option<WherePredicate> {
1393 let ty::OutlivesPredicate(ref a, ref b) = *self;
1396 (ty::ReEmpty, ty::ReEmpty) => {
1402 Some(WherePredicate::RegionPredicate {
1403 lifetime: a.clean(cx).expect("failed to clean lifetime"),
1404 bounds: vec![GenericBound::Outlives(b.clean(cx).expect("failed to clean bounds"))]
1409 impl<'tcx> Clean<Option<WherePredicate>> for ty::OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>> {
1410 fn clean(&self, cx: &DocContext<'_>) -> Option<WherePredicate> {
1411 let ty::OutlivesPredicate(ref ty, ref lt) = *self;
1414 ty::ReEmpty => return None,
1418 Some(WherePredicate::BoundPredicate {
1420 bounds: vec![GenericBound::Outlives(lt.clean(cx).expect("failed to clean lifetimes"))]
1425 impl<'tcx> Clean<WherePredicate> for ty::ProjectionPredicate<'tcx> {
1426 fn clean(&self, cx: &DocContext<'_>) -> WherePredicate {
1427 WherePredicate::EqPredicate {
1428 lhs: self.projection_ty.clean(cx),
1429 rhs: self.ty.clean(cx)
1434 impl<'tcx> Clean<Type> for ty::ProjectionTy<'tcx> {
1435 fn clean(&self, cx: &DocContext<'_>) -> Type {
1436 let trait_ = match self.trait_ref(cx.tcx).clean(cx) {
1437 GenericBound::TraitBound(t, _) => t.trait_,
1438 GenericBound::Outlives(_) => panic!("cleaning a trait got a lifetime"),
1441 name: cx.tcx.associated_item(self.item_def_id).ident.name.clean(cx),
1442 self_type: box self.self_ty().clean(cx),
1448 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
1449 pub enum GenericParamDefKind {
1453 bounds: Vec<GenericBound>,
1454 default: Option<Type>,
1455 synthetic: Option<hir::SyntheticTyParamKind>,
1463 impl GenericParamDefKind {
1464 pub fn is_type(&self) -> bool {
1466 GenericParamDefKind::Type { .. } => true,
1471 pub fn get_type(&self, cx: &DocContext<'_>) -> Option<Type> {
1473 GenericParamDefKind::Type { did, .. } => {
1474 rustc_typeck::checked_type_of(cx.tcx, did, false).map(|t| t.clean(cx))
1476 GenericParamDefKind::Const { ref ty, .. } => Some(ty.clone()),
1477 GenericParamDefKind::Lifetime => None,
1482 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
1483 pub struct GenericParamDef {
1486 pub kind: GenericParamDefKind,
1489 impl GenericParamDef {
1490 pub fn is_synthetic_type_param(&self) -> bool {
1492 GenericParamDefKind::Lifetime |
1493 GenericParamDefKind::Const { .. } => false,
1494 GenericParamDefKind::Type { ref synthetic, .. } => synthetic.is_some(),
1498 pub fn is_type(&self) -> bool {
1502 pub fn get_type(&self, cx: &DocContext<'_>) -> Option<Type> {
1503 self.kind.get_type(cx)
1506 pub fn get_bounds(&self) -> Option<&[GenericBound]> {
1508 GenericParamDefKind::Type { ref bounds, .. } => Some(bounds),
1514 impl Clean<GenericParamDef> for ty::GenericParamDef {
1515 fn clean(&self, cx: &DocContext<'_>) -> GenericParamDef {
1516 let (name, kind) = match self.kind {
1517 ty::GenericParamDefKind::Lifetime => {
1518 (self.name.to_string(), GenericParamDefKind::Lifetime)
1520 ty::GenericParamDefKind::Type { has_default, synthetic, .. } => {
1521 let default = if has_default {
1522 Some(cx.tcx.type_of(self.def_id).clean(cx))
1526 (self.name.clean(cx), GenericParamDefKind::Type {
1528 bounds: vec![], // These are filled in from the where-clauses.
1533 ty::GenericParamDefKind::Const { .. } => {
1534 (self.name.clean(cx), GenericParamDefKind::Const {
1536 ty: cx.tcx.type_of(self.def_id).clean(cx),
1548 impl Clean<GenericParamDef> for hir::GenericParam {
1549 fn clean(&self, cx: &DocContext<'_>) -> GenericParamDef {
1550 let (name, kind) = match self.kind {
1551 hir::GenericParamKind::Lifetime { .. } => {
1552 let name = if self.bounds.len() > 0 {
1553 let mut bounds = self.bounds.iter().map(|bound| match bound {
1554 hir::GenericBound::Outlives(lt) => lt,
1557 let name = bounds.next().expect("no more bounds").name.ident();
1558 let mut s = format!("{}: {}", self.name.ident(), name);
1559 for bound in bounds {
1560 s.push_str(&format!(" + {}", bound.name.ident()));
1564 self.name.ident().to_string()
1566 (name, GenericParamDefKind::Lifetime)
1568 hir::GenericParamKind::Type { ref default, synthetic } => {
1569 (self.name.ident().name.clean(cx), GenericParamDefKind::Type {
1570 did: cx.tcx.hir().local_def_id(self.hir_id),
1571 bounds: self.bounds.clean(cx),
1572 default: default.clean(cx),
1573 synthetic: synthetic,
1576 hir::GenericParamKind::Const { ref ty } => {
1577 (self.name.ident().name.clean(cx), GenericParamDefKind::Const {
1578 did: cx.tcx.hir().local_def_id(self.hir_id),
1591 // maybe use a Generic enum and use Vec<Generic>?
1592 #[derive(Clone, PartialEq, Eq, Debug, Default, Hash)]
1593 pub struct Generics {
1594 pub params: Vec<GenericParamDef>,
1595 pub where_predicates: Vec<WherePredicate>,
1598 impl Clean<Generics> for hir::Generics {
1599 fn clean(&self, cx: &DocContext<'_>) -> Generics {
1600 // Synthetic type-parameters are inserted after normal ones.
1601 // In order for normal parameters to be able to refer to synthetic ones,
1602 // scans them first.
1603 fn is_impl_trait(param: &hir::GenericParam) -> bool {
1605 hir::GenericParamKind::Type { synthetic, .. } => {
1606 synthetic == Some(hir::SyntheticTyParamKind::ImplTrait)
1611 let impl_trait_params = self.params
1613 .filter(|param| is_impl_trait(param))
1615 let param: GenericParamDef = param.clean(cx);
1617 GenericParamDefKind::Lifetime => unreachable!(),
1618 GenericParamDefKind::Type { did, ref bounds, .. } => {
1619 cx.impl_trait_bounds.borrow_mut().insert(did.into(), bounds.clone());
1621 GenericParamDefKind::Const { .. } => unreachable!(),
1625 .collect::<Vec<_>>();
1627 let mut params = Vec::with_capacity(self.params.len());
1628 for p in self.params.iter().filter(|p| !is_impl_trait(p)) {
1629 let p = p.clean(cx);
1632 params.extend(impl_trait_params);
1634 let mut generics = Generics {
1636 where_predicates: self.where_clause.predicates.clean(cx),
1639 // Some duplicates are generated for ?Sized bounds between type params and where
1640 // predicates. The point in here is to move the bounds definitions from type params
1641 // to where predicates when such cases occur.
1642 for where_pred in &mut generics.where_predicates {
1644 WherePredicate::BoundPredicate { ty: Generic(ref name), ref mut bounds } => {
1645 if bounds.is_empty() {
1646 for param in &mut generics.params {
1648 GenericParamDefKind::Lifetime => {}
1649 GenericParamDefKind::Type { bounds: ref mut ty_bounds, .. } => {
1650 if ¶m.name == name {
1651 mem::swap(bounds, ty_bounds);
1655 GenericParamDefKind::Const { .. } => {}
1667 impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics,
1668 &'a &'tcx ty::GenericPredicates<'tcx>) {
1669 fn clean(&self, cx: &DocContext<'_>) -> Generics {
1670 use self::WherePredicate as WP;
1671 use std::collections::BTreeMap;
1673 let (gens, preds) = *self;
1675 // Don't populate `cx.impl_trait_bounds` before `clean`ning `where` clauses,
1676 // since `Clean for ty::Predicate` would consume them.
1677 let mut impl_trait = BTreeMap::<ImplTraitParam, Vec<GenericBound>>::default();
1679 // Bounds in the type_params and lifetimes fields are repeated in the
1680 // predicates field (see rustc_typeck::collect::ty_generics), so remove
1682 let stripped_typarams = gens.params.iter()
1683 .filter_map(|param| match param.kind {
1684 ty::GenericParamDefKind::Lifetime => None,
1685 ty::GenericParamDefKind::Type { synthetic, .. } => {
1686 if param.name.as_symbol() == kw::SelfUpper {
1687 assert_eq!(param.index, 0);
1690 if synthetic == Some(hir::SyntheticTyParamKind::ImplTrait) {
1691 impl_trait.insert(param.index.into(), vec![]);
1694 Some(param.clean(cx))
1696 ty::GenericParamDefKind::Const { .. } => None,
1697 }).collect::<Vec<GenericParamDef>>();
1699 // param index -> [(DefId of trait, associated type name, type)]
1700 let mut impl_trait_proj =
1701 FxHashMap::<u32, Vec<(DefId, String, Ty<'tcx>)>>::default();
1703 let where_predicates = preds.predicates.iter()
1704 .flat_map(|(p, _)| {
1705 let mut projection = None;
1706 let param_idx = (|| {
1707 if let Some(trait_ref) = p.to_opt_poly_trait_ref() {
1708 if let ty::Param(param) = trait_ref.self_ty().kind {
1709 return Some(param.index);
1711 } else if let Some(outlives) = p.to_opt_type_outlives() {
1712 if let ty::Param(param) = outlives.skip_binder().0.kind {
1713 return Some(param.index);
1715 } else if let ty::Predicate::Projection(p) = p {
1716 if let ty::Param(param) = p.skip_binder().projection_ty.self_ty().kind {
1717 projection = Some(p);
1718 return Some(param.index);
1725 if let Some(param_idx) = param_idx {
1726 if let Some(b) = impl_trait.get_mut(¶m_idx.into()) {
1727 let p = p.clean(cx)?;
1734 .filter(|b| !b.is_sized_bound(cx))
1737 let proj = projection
1738 .map(|p| (p.skip_binder().projection_ty.clean(cx), p.skip_binder().ty));
1739 if let Some(((_, trait_did, name), rhs)) =
1740 proj.as_ref().and_then(|(lhs, rhs)| Some((lhs.projection()?, rhs)))
1745 .push((trait_did, name.to_string(), rhs));
1754 .collect::<Vec<_>>();
1756 for (param, mut bounds) in impl_trait {
1757 // Move trait bounds to the front.
1758 bounds.sort_by_key(|b| if let GenericBound::TraitBound(..) = b {
1764 if let crate::core::ImplTraitParam::ParamIndex(idx) = param {
1765 if let Some(proj) = impl_trait_proj.remove(&idx) {
1766 for (trait_did, name, rhs) in proj {
1767 simplify::merge_bounds(
1780 cx.impl_trait_bounds.borrow_mut().insert(param, bounds);
1783 // Now that `cx.impl_trait_bounds` is populated, we can process
1784 // remaining predicates which could contain `impl Trait`.
1785 let mut where_predicates = where_predicates
1787 .flat_map(|p| p.clean(cx))
1788 .collect::<Vec<_>>();
1790 // Type parameters and have a Sized bound by default unless removed with
1791 // ?Sized. Scan through the predicates and mark any type parameter with
1792 // a Sized bound, removing the bounds as we find them.
1794 // Note that associated types also have a sized bound by default, but we
1795 // don't actually know the set of associated types right here so that's
1796 // handled in cleaning associated types
1797 let mut sized_params = FxHashSet::default();
1798 where_predicates.retain(|pred| {
1800 WP::BoundPredicate { ty: Generic(ref g), ref bounds } => {
1801 if bounds.iter().any(|b| b.is_sized_bound(cx)) {
1802 sized_params.insert(g.clone());
1812 // Run through the type parameters again and insert a ?Sized
1813 // unbound for any we didn't find to be Sized.
1814 for tp in &stripped_typarams {
1815 if !sized_params.contains(&tp.name) {
1816 where_predicates.push(WP::BoundPredicate {
1817 ty: Type::Generic(tp.name.clone()),
1818 bounds: vec![GenericBound::maybe_sized(cx)],
1823 // It would be nice to collect all of the bounds on a type and recombine
1824 // them if possible, to avoid e.g., `where T: Foo, T: Bar, T: Sized, T: 'a`
1825 // and instead see `where T: Foo + Bar + Sized + 'a`
1830 .flat_map(|param| match param.kind {
1831 ty::GenericParamDefKind::Lifetime => Some(param.clean(cx)),
1832 ty::GenericParamDefKind::Type { .. } => None,
1833 ty::GenericParamDefKind::Const { .. } => Some(param.clean(cx)),
1834 }).chain(simplify::ty_params(stripped_typarams).into_iter())
1836 where_predicates: simplify::where_clauses(cx, where_predicates),
1841 /// The point of this function is to replace bounds with types.
1843 /// i.e. `[T, U]` when you have the following bounds: `T: Display, U: Option<T>` will return
1844 /// `[Display, Option]` (we just returns the list of the types, we don't care about the
1845 /// wrapped types in here).
1847 generics: &Generics,
1849 cx: &DocContext<'_>,
1851 ) -> FxHashSet<Type> {
1852 let arg_s = arg.print().to_string();
1853 let mut res = FxHashSet::default();
1854 if recurse >= 10 { // FIXME: remove this whole recurse thing when the recursion bug is fixed
1857 if arg.is_full_generic() {
1858 if let Some(where_pred) = generics.where_predicates.iter().find(|g| {
1860 &WherePredicate::BoundPredicate { ref ty, .. } => ty.def_id() == arg.def_id(),
1864 let bounds = where_pred.get_bounds().unwrap_or_else(|| &[]);
1865 for bound in bounds.iter() {
1867 GenericBound::TraitBound(ref poly_trait, _) => {
1868 for x in poly_trait.generic_params.iter() {
1872 if let Some(ty) = x.get_type(cx) {
1873 let adds = get_real_types(generics, &ty, cx, recurse + 1);
1874 if !adds.is_empty() {
1876 } else if !ty.is_full_generic() {
1886 if let Some(bound) = generics.params.iter().find(|g| {
1887 g.is_type() && g.name == arg_s
1889 for bound in bound.get_bounds().unwrap_or_else(|| &[]) {
1890 if let Some(ty) = bound.get_trait_type() {
1891 let adds = get_real_types(generics, &ty, cx, recurse + 1);
1892 if !adds.is_empty() {
1894 } else if !ty.is_full_generic() {
1895 res.insert(ty.clone());
1901 res.insert(arg.clone());
1902 if let Some(gens) = arg.generics() {
1903 for gen in gens.iter() {
1904 if gen.is_full_generic() {
1905 let adds = get_real_types(generics, gen, cx, recurse + 1);
1906 if !adds.is_empty() {
1910 res.insert(gen.clone());
1918 /// Return the full list of types when bounds have been resolved.
1920 /// i.e. `fn foo<A: Display, B: Option<A>>(x: u32, y: B)` will return
1921 /// `[u32, Display, Option]`.
1922 pub fn get_all_types(
1923 generics: &Generics,
1925 cx: &DocContext<'_>,
1926 ) -> (Vec<Type>, Vec<Type>) {
1927 let mut all_types = FxHashSet::default();
1928 for arg in decl.inputs.values.iter() {
1929 if arg.type_.is_self_type() {
1932 let args = get_real_types(generics, &arg.type_, cx, 0);
1933 if !args.is_empty() {
1934 all_types.extend(args);
1936 all_types.insert(arg.type_.clone());
1940 let ret_types = match decl.output {
1941 FunctionRetTy::Return(ref return_type) => {
1942 let mut ret = get_real_types(generics, &return_type, cx, 0);
1944 ret.insert(return_type.clone());
1946 ret.into_iter().collect()
1950 (all_types.into_iter().collect(), ret_types)
1953 #[derive(Clone, Debug)]
1955 pub generics: Generics,
1957 pub header: hir::FnHeader,
1958 pub defaultness: Option<hir::Defaultness>,
1959 pub all_types: Vec<Type>,
1960 pub ret_types: Vec<Type>,
1963 impl<'a> Clean<Method> for (&'a hir::MethodSig, &'a hir::Generics, hir::BodyId,
1964 Option<hir::Defaultness>) {
1965 fn clean(&self, cx: &DocContext<'_>) -> Method {
1966 let (generics, decl) = enter_impl_trait(cx, || {
1967 (self.1.clean(cx), (&*self.0.decl, self.2).clean(cx))
1969 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
1973 header: self.0.header,
1974 defaultness: self.3,
1981 #[derive(Clone, Debug)]
1982 pub struct TyMethod {
1983 pub header: hir::FnHeader,
1985 pub generics: Generics,
1986 pub all_types: Vec<Type>,
1987 pub ret_types: Vec<Type>,
1990 #[derive(Clone, Debug)]
1991 pub struct Function {
1993 pub generics: Generics,
1994 pub header: hir::FnHeader,
1995 pub all_types: Vec<Type>,
1996 pub ret_types: Vec<Type>,
1999 impl Clean<Item> for doctree::Function<'_> {
2000 fn clean(&self, cx: &DocContext<'_>) -> Item {
2001 let (generics, decl) = enter_impl_trait(cx, || {
2002 (self.generics.clean(cx), (self.decl, self.body).clean(cx))
2005 let did = cx.tcx.hir().local_def_id(self.id);
2006 let constness = if cx.tcx.is_min_const_fn(did) {
2007 hir::Constness::Const
2009 hir::Constness::NotConst
2011 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
2013 name: Some(self.name.clean(cx)),
2014 attrs: self.attrs.clean(cx),
2015 source: self.whence.clean(cx),
2016 visibility: self.vis.clean(cx),
2017 stability: cx.stability(self.id).clean(cx),
2018 deprecation: cx.deprecation(self.id).clean(cx),
2020 inner: FunctionItem(Function {
2023 header: hir::FnHeader { constness, ..self.header },
2031 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
2033 pub inputs: Arguments,
2034 pub output: FunctionRetTy,
2035 pub attrs: Attributes,
2039 pub fn self_type(&self) -> Option<SelfTy> {
2040 self.inputs.values.get(0).and_then(|v| v.to_self())
2043 /// Returns the sugared return type for an async function.
2045 /// For example, if the return type is `impl std::future::Future<Output = i32>`, this function
2046 /// will return `i32`.
2050 /// This function will panic if the return type does not match the expected sugaring for async
2052 pub fn sugared_async_return_type(&self) -> FunctionRetTy {
2053 match &self.output {
2054 FunctionRetTy::Return(Type::ImplTrait(bounds)) => {
2056 GenericBound::TraitBound(PolyTrait { trait_, .. }, ..) => {
2057 let bindings = trait_.bindings().unwrap();
2058 FunctionRetTy::Return(bindings[0].ty().clone())
2060 _ => panic!("unexpected desugaring of async function"),
2063 _ => panic!("unexpected desugaring of async function"),
2068 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
2069 pub struct Arguments {
2070 pub values: Vec<Argument>,
2073 impl<'a> Clean<Arguments> for (&'a [hir::Ty], &'a [ast::Ident]) {
2074 fn clean(&self, cx: &DocContext<'_>) -> Arguments {
2076 values: self.0.iter().enumerate().map(|(i, ty)| {
2077 let mut name = self.1.get(i).map(|ident| ident.to_string())
2078 .unwrap_or(String::new());
2079 if name.is_empty() {
2080 name = "_".to_string();
2084 type_: ty.clean(cx),
2091 impl<'a> Clean<Arguments> for (&'a [hir::Ty], hir::BodyId) {
2092 fn clean(&self, cx: &DocContext<'_>) -> Arguments {
2093 let body = cx.tcx.hir().body(self.1);
2096 values: self.0.iter().enumerate().map(|(i, ty)| {
2098 name: name_from_pat(&body.params[i].pat),
2099 type_: ty.clean(cx),
2106 impl<'a, A: Copy> Clean<FnDecl> for (&'a hir::FnDecl, A)
2107 where (&'a [hir::Ty], A): Clean<Arguments>
2109 fn clean(&self, cx: &DocContext<'_>) -> FnDecl {
2111 inputs: (&self.0.inputs[..], self.1).clean(cx),
2112 output: self.0.output.clean(cx),
2113 attrs: Attributes::default(),
2118 impl<'tcx> Clean<FnDecl> for (DefId, ty::PolyFnSig<'tcx>) {
2119 fn clean(&self, cx: &DocContext<'_>) -> FnDecl {
2120 let (did, sig) = *self;
2121 let mut names = if cx.tcx.hir().as_local_hir_id(did).is_some() {
2124 cx.tcx.fn_arg_names(did).into_iter()
2128 output: Return(sig.skip_binder().output().clean(cx)),
2129 attrs: Attributes::default(),
2131 values: sig.skip_binder().inputs().iter().map(|t| {
2134 name: names.next().map_or(String::new(), |name| name.to_string()),
2142 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
2143 pub struct Argument {
2148 #[derive(Clone, PartialEq, Debug)]
2151 SelfBorrowed(Option<Lifetime>, Mutability),
2156 pub fn to_self(&self) -> Option<SelfTy> {
2157 if self.name != "self" {
2160 if self.type_.is_self_type() {
2161 return Some(SelfValue);
2164 BorrowedRef{ref lifetime, mutability, ref type_} if type_.is_self_type() => {
2165 Some(SelfBorrowed(lifetime.clone(), mutability))
2167 _ => Some(SelfExplicit(self.type_.clone()))
2172 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
2173 pub enum FunctionRetTy {
2178 impl Clean<FunctionRetTy> for hir::FunctionRetTy {
2179 fn clean(&self, cx: &DocContext<'_>) -> FunctionRetTy {
2181 hir::Return(ref typ) => Return(typ.clean(cx)),
2182 hir::DefaultReturn(..) => DefaultReturn,
2187 impl GetDefId for FunctionRetTy {
2188 fn def_id(&self) -> Option<DefId> {
2190 Return(ref ty) => ty.def_id(),
2191 DefaultReturn => None,
2196 #[derive(Clone, Debug)]
2199 pub unsafety: hir::Unsafety,
2200 pub items: Vec<Item>,
2201 pub generics: Generics,
2202 pub bounds: Vec<GenericBound>,
2203 pub is_spotlight: bool,
2207 impl Clean<Item> for doctree::Trait<'_> {
2208 fn clean(&self, cx: &DocContext<'_>) -> Item {
2209 let attrs = self.attrs.clean(cx);
2210 let is_spotlight = attrs.has_doc_flag(sym::spotlight);
2212 name: Some(self.name.clean(cx)),
2214 source: self.whence.clean(cx),
2215 def_id: cx.tcx.hir().local_def_id(self.id),
2216 visibility: self.vis.clean(cx),
2217 stability: cx.stability(self.id).clean(cx),
2218 deprecation: cx.deprecation(self.id).clean(cx),
2219 inner: TraitItem(Trait {
2220 auto: self.is_auto.clean(cx),
2221 unsafety: self.unsafety,
2222 items: self.items.iter().map(|ti| ti.clean(cx)).collect(),
2223 generics: self.generics.clean(cx),
2224 bounds: self.bounds.clean(cx),
2226 is_auto: self.is_auto.clean(cx),
2232 #[derive(Clone, Debug)]
2233 pub struct TraitAlias {
2234 pub generics: Generics,
2235 pub bounds: Vec<GenericBound>,
2238 impl Clean<Item> for doctree::TraitAlias<'_> {
2239 fn clean(&self, cx: &DocContext<'_>) -> Item {
2240 let attrs = self.attrs.clean(cx);
2242 name: Some(self.name.clean(cx)),
2244 source: self.whence.clean(cx),
2245 def_id: cx.tcx.hir().local_def_id(self.id),
2246 visibility: self.vis.clean(cx),
2247 stability: cx.stability(self.id).clean(cx),
2248 deprecation: cx.deprecation(self.id).clean(cx),
2249 inner: TraitAliasItem(TraitAlias {
2250 generics: self.generics.clean(cx),
2251 bounds: self.bounds.clean(cx),
2257 impl Clean<bool> for hir::IsAuto {
2258 fn clean(&self, _: &DocContext<'_>) -> bool {
2260 hir::IsAuto::Yes => true,
2261 hir::IsAuto::No => false,
2266 impl Clean<Type> for hir::TraitRef {
2267 fn clean(&self, cx: &DocContext<'_>) -> Type {
2268 resolve_type(cx, self.path.clean(cx), self.hir_ref_id)
2272 impl Clean<PolyTrait> for hir::PolyTraitRef {
2273 fn clean(&self, cx: &DocContext<'_>) -> PolyTrait {
2275 trait_: self.trait_ref.clean(cx),
2276 generic_params: self.bound_generic_params.clean(cx)
2281 impl Clean<Item> for hir::TraitItem {
2282 fn clean(&self, cx: &DocContext<'_>) -> Item {
2283 let inner = match self.node {
2284 hir::TraitItemKind::Const(ref ty, default) => {
2285 AssocConstItem(ty.clean(cx),
2286 default.map(|e| print_const_expr(cx, e)))
2288 hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Provided(body)) => {
2289 MethodItem((sig, &self.generics, body, None).clean(cx))
2291 hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Required(ref names)) => {
2292 let (generics, decl) = enter_impl_trait(cx, || {
2293 (self.generics.clean(cx), (&*sig.decl, &names[..]).clean(cx))
2295 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
2296 TyMethodItem(TyMethod {
2304 hir::TraitItemKind::Type(ref bounds, ref default) => {
2305 AssocTypeItem(bounds.clean(cx), default.clean(cx))
2308 let local_did = cx.tcx.hir().local_def_id(self.hir_id);
2310 name: Some(self.ident.name.clean(cx)),
2311 attrs: self.attrs.clean(cx),
2312 source: self.span.clean(cx),
2314 visibility: Visibility::Inherited,
2315 stability: get_stability(cx, local_did),
2316 deprecation: get_deprecation(cx, local_did),
2322 impl Clean<Item> for hir::ImplItem {
2323 fn clean(&self, cx: &DocContext<'_>) -> Item {
2324 let inner = match self.kind {
2325 hir::ImplItemKind::Const(ref ty, expr) => {
2326 AssocConstItem(ty.clean(cx),
2327 Some(print_const_expr(cx, expr)))
2329 hir::ImplItemKind::Method(ref sig, body) => {
2330 MethodItem((sig, &self.generics, body, Some(self.defaultness)).clean(cx))
2332 hir::ImplItemKind::TyAlias(ref ty) => TypedefItem(Typedef {
2333 type_: ty.clean(cx),
2334 generics: Generics::default(),
2336 hir::ImplItemKind::OpaqueTy(ref bounds) => OpaqueTyItem(OpaqueTy {
2337 bounds: bounds.clean(cx),
2338 generics: Generics::default(),
2341 let local_did = cx.tcx.hir().local_def_id(self.hir_id);
2343 name: Some(self.ident.name.clean(cx)),
2344 source: self.span.clean(cx),
2345 attrs: self.attrs.clean(cx),
2347 visibility: self.vis.clean(cx),
2348 stability: get_stability(cx, local_did),
2349 deprecation: get_deprecation(cx, local_did),
2355 impl Clean<Item> for ty::AssocItem {
2356 fn clean(&self, cx: &DocContext<'_>) -> Item {
2357 let inner = match self.kind {
2358 ty::AssocKind::Const => {
2359 let ty = cx.tcx.type_of(self.def_id);
2360 let default = if self.defaultness.has_value() {
2361 Some(inline::print_inlined_const(cx, self.def_id))
2365 AssocConstItem(ty.clean(cx), default)
2367 ty::AssocKind::Method => {
2368 let generics = (cx.tcx.generics_of(self.def_id),
2369 &cx.tcx.explicit_predicates_of(self.def_id)).clean(cx);
2370 let sig = cx.tcx.fn_sig(self.def_id);
2371 let mut decl = (self.def_id, sig).clean(cx);
2373 if self.method_has_self_argument {
2374 let self_ty = match self.container {
2375 ty::ImplContainer(def_id) => {
2376 cx.tcx.type_of(def_id)
2378 ty::TraitContainer(_) => cx.tcx.types.self_param,
2380 let self_arg_ty = *sig.input(0).skip_binder();
2381 if self_arg_ty == self_ty {
2382 decl.inputs.values[0].type_ = Generic(String::from("Self"));
2383 } else if let ty::Ref(_, ty, _) = self_arg_ty.kind {
2385 match decl.inputs.values[0].type_ {
2386 BorrowedRef{ref mut type_, ..} => {
2387 **type_ = Generic(String::from("Self"))
2389 _ => unreachable!(),
2395 let provided = match self.container {
2396 ty::ImplContainer(_) => true,
2397 ty::TraitContainer(_) => self.defaultness.has_value()
2399 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
2401 let constness = if cx.tcx.is_min_const_fn(self.def_id) {
2402 hir::Constness::Const
2404 hir::Constness::NotConst
2406 let asyncness = cx.tcx.asyncness(self.def_id);
2407 let defaultness = match self.container {
2408 ty::ImplContainer(_) => Some(self.defaultness),
2409 ty::TraitContainer(_) => None,
2414 header: hir::FnHeader {
2415 unsafety: sig.unsafety(),
2425 TyMethodItem(TyMethod {
2428 header: hir::FnHeader {
2429 unsafety: sig.unsafety(),
2431 constness: hir::Constness::NotConst,
2432 asyncness: hir::IsAsync::NotAsync,
2439 ty::AssocKind::Type => {
2440 let my_name = self.ident.name.clean(cx);
2442 if let ty::TraitContainer(did) = self.container {
2443 // When loading a cross-crate associated type, the bounds for this type
2444 // are actually located on the trait/impl itself, so we need to load
2445 // all of the generics from there and then look for bounds that are
2446 // applied to this associated type in question.
2447 let predicates = cx.tcx.explicit_predicates_of(did);
2448 let generics = (cx.tcx.generics_of(did), &predicates).clean(cx);
2449 let mut bounds = generics.where_predicates.iter().filter_map(|pred| {
2450 let (name, self_type, trait_, bounds) = match *pred {
2451 WherePredicate::BoundPredicate {
2452 ty: QPath { ref name, ref self_type, ref trait_ },
2454 } => (name, self_type, trait_, bounds),
2457 if *name != my_name { return None }
2459 ResolvedPath { did, .. } if did == self.container.id() => {}
2463 Generic(ref s) if *s == "Self" => {}
2467 }).flat_map(|i| i.iter().cloned()).collect::<Vec<_>>();
2468 // Our Sized/?Sized bound didn't get handled when creating the generics
2469 // because we didn't actually get our whole set of bounds until just now
2470 // (some of them may have come from the trait). If we do have a sized
2471 // bound, we remove it, and if we don't then we add the `?Sized` bound
2473 match bounds.iter().position(|b| b.is_sized_bound(cx)) {
2474 Some(i) => { bounds.remove(i); }
2475 None => bounds.push(GenericBound::maybe_sized(cx)),
2478 let ty = if self.defaultness.has_value() {
2479 Some(cx.tcx.type_of(self.def_id))
2484 AssocTypeItem(bounds, ty.clean(cx))
2486 TypedefItem(Typedef {
2487 type_: cx.tcx.type_of(self.def_id).clean(cx),
2488 generics: Generics {
2490 where_predicates: Vec::new(),
2495 ty::AssocKind::OpaqueTy => unimplemented!(),
2498 let visibility = match self.container {
2499 ty::ImplContainer(_) => self.vis.clean(cx),
2500 ty::TraitContainer(_) => Inherited,
2504 name: Some(self.ident.name.clean(cx)),
2506 stability: get_stability(cx, self.def_id),
2507 deprecation: get_deprecation(cx, self.def_id),
2508 def_id: self.def_id,
2509 attrs: inline::load_attrs(cx, self.def_id).clean(cx),
2510 source: cx.tcx.def_span(self.def_id).clean(cx),
2516 /// A trait reference, which may have higher ranked lifetimes.
2517 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
2518 pub struct PolyTrait {
2520 pub generic_params: Vec<GenericParamDef>,
2523 /// A representation of a type suitable for hyperlinking purposes. Ideally, one can get the original
2524 /// type out of the AST/`TyCtxt` given one of these, if more information is needed. Most
2525 /// importantly, it does not preserve mutability or boxes.
2526 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
2528 /// Structs/enums/traits (most that would be an `hir::TyKind::Path`).
2531 param_names: Option<Vec<GenericBound>>,
2533 /// `true` if is a `T::Name` path for associated types.
2536 /// For parameterized types, so the consumer of the JSON don't go
2537 /// looking for types which don't exist anywhere.
2539 /// Primitives are the fixed-size numeric types (plus int/usize/float), char,
2540 /// arrays, slices, and tuples.
2541 Primitive(PrimitiveType),
2542 /// `extern "ABI" fn`
2543 BareFunction(Box<BareFunctionDecl>),
2546 Array(Box<Type>, String),
2549 RawPointer(Mutability, Box<Type>),
2551 lifetime: Option<Lifetime>,
2552 mutability: Mutability,
2556 // `<Type as Trait>::Name`
2559 self_type: Box<Type>,
2566 // `impl TraitA + TraitB + ...`
2567 ImplTrait(Vec<GenericBound>),
2570 #[derive(Clone, PartialEq, Eq, Hash, Copy, Debug)]
2571 pub enum PrimitiveType {
2572 Isize, I8, I16, I32, I64, I128,
2573 Usize, U8, U16, U32, U64, U128,
2589 #[derive(Clone, Copy, Debug)]
2607 pub trait GetDefId {
2608 fn def_id(&self) -> Option<DefId>;
2611 impl<T: GetDefId> GetDefId for Option<T> {
2612 fn def_id(&self) -> Option<DefId> {
2613 self.as_ref().and_then(|d| d.def_id())
2618 pub fn primitive_type(&self) -> Option<PrimitiveType> {
2620 Primitive(p) | BorrowedRef { type_: box Primitive(p), ..} => Some(p),
2621 Slice(..) | BorrowedRef { type_: box Slice(..), .. } => Some(PrimitiveType::Slice),
2622 Array(..) | BorrowedRef { type_: box Array(..), .. } => Some(PrimitiveType::Array),
2623 Tuple(ref tys) => if tys.is_empty() {
2624 Some(PrimitiveType::Unit)
2626 Some(PrimitiveType::Tuple)
2628 RawPointer(..) => Some(PrimitiveType::RawPointer),
2629 BorrowedRef { type_: box Generic(..), .. } => Some(PrimitiveType::Reference),
2630 BareFunction(..) => Some(PrimitiveType::Fn),
2631 Never => Some(PrimitiveType::Never),
2636 pub fn is_generic(&self) -> bool {
2638 ResolvedPath { is_generic, .. } => is_generic,
2643 pub fn is_self_type(&self) -> bool {
2645 Generic(ref name) => name == "Self",
2650 pub fn generics(&self) -> Option<Vec<Type>> {
2652 ResolvedPath { ref path, .. } => {
2653 path.segments.last().and_then(|seg| {
2654 if let GenericArgs::AngleBracketed { ref args, .. } = seg.args {
2655 Some(args.iter().filter_map(|arg| match arg {
2656 GenericArg::Type(ty) => Some(ty.clone()),
2668 pub fn bindings(&self) -> Option<&[TypeBinding]> {
2670 ResolvedPath { ref path, .. } => {
2671 path.segments.last().and_then(|seg| {
2672 if let GenericArgs::AngleBracketed { ref bindings, .. } = seg.args {
2683 pub fn is_full_generic(&self) -> bool {
2685 Type::Generic(_) => true,
2690 pub fn projection(&self) -> Option<(&Type, DefId, &str)> {
2691 let (self_, trait_, name) = match self {
2692 QPath { ref self_type, ref trait_, ref name } => {
2693 (self_type, trait_, name)
2697 let trait_did = match **trait_ {
2698 ResolvedPath { did, .. } => did,
2701 Some((&self_, trait_did, name))
2706 impl GetDefId for Type {
2707 fn def_id(&self) -> Option<DefId> {
2709 ResolvedPath { did, .. } => Some(did),
2710 Primitive(p) => crate::html::render::cache().primitive_locations.get(&p).cloned(),
2711 BorrowedRef { type_: box Generic(..), .. } =>
2712 Primitive(PrimitiveType::Reference).def_id(),
2713 BorrowedRef { ref type_, .. } => type_.def_id(),
2714 Tuple(ref tys) => if tys.is_empty() {
2715 Primitive(PrimitiveType::Unit).def_id()
2717 Primitive(PrimitiveType::Tuple).def_id()
2719 BareFunction(..) => Primitive(PrimitiveType::Fn).def_id(),
2720 Never => Primitive(PrimitiveType::Never).def_id(),
2721 Slice(..) => Primitive(PrimitiveType::Slice).def_id(),
2722 Array(..) => Primitive(PrimitiveType::Array).def_id(),
2723 RawPointer(..) => Primitive(PrimitiveType::RawPointer).def_id(),
2724 QPath { ref self_type, .. } => self_type.def_id(),
2730 impl PrimitiveType {
2731 fn from_str(s: &str) -> Option<PrimitiveType> {
2733 "isize" => Some(PrimitiveType::Isize),
2734 "i8" => Some(PrimitiveType::I8),
2735 "i16" => Some(PrimitiveType::I16),
2736 "i32" => Some(PrimitiveType::I32),
2737 "i64" => Some(PrimitiveType::I64),
2738 "i128" => Some(PrimitiveType::I128),
2739 "usize" => Some(PrimitiveType::Usize),
2740 "u8" => Some(PrimitiveType::U8),
2741 "u16" => Some(PrimitiveType::U16),
2742 "u32" => Some(PrimitiveType::U32),
2743 "u64" => Some(PrimitiveType::U64),
2744 "u128" => Some(PrimitiveType::U128),
2745 "bool" => Some(PrimitiveType::Bool),
2746 "char" => Some(PrimitiveType::Char),
2747 "str" => Some(PrimitiveType::Str),
2748 "f32" => Some(PrimitiveType::F32),
2749 "f64" => Some(PrimitiveType::F64),
2750 "array" => Some(PrimitiveType::Array),
2751 "slice" => Some(PrimitiveType::Slice),
2752 "tuple" => Some(PrimitiveType::Tuple),
2753 "unit" => Some(PrimitiveType::Unit),
2754 "pointer" => Some(PrimitiveType::RawPointer),
2755 "reference" => Some(PrimitiveType::Reference),
2756 "fn" => Some(PrimitiveType::Fn),
2757 "never" => Some(PrimitiveType::Never),
2762 pub fn as_str(&self) -> &'static str {
2763 use self::PrimitiveType::*;
2786 RawPointer => "pointer",
2787 Reference => "reference",
2794 pub fn to_url_str(&self) -> &'static str {
2799 impl From<ast::IntTy> for PrimitiveType {
2800 fn from(int_ty: ast::IntTy) -> PrimitiveType {
2802 ast::IntTy::Isize => PrimitiveType::Isize,
2803 ast::IntTy::I8 => PrimitiveType::I8,
2804 ast::IntTy::I16 => PrimitiveType::I16,
2805 ast::IntTy::I32 => PrimitiveType::I32,
2806 ast::IntTy::I64 => PrimitiveType::I64,
2807 ast::IntTy::I128 => PrimitiveType::I128,
2812 impl From<ast::UintTy> for PrimitiveType {
2813 fn from(uint_ty: ast::UintTy) -> PrimitiveType {
2815 ast::UintTy::Usize => PrimitiveType::Usize,
2816 ast::UintTy::U8 => PrimitiveType::U8,
2817 ast::UintTy::U16 => PrimitiveType::U16,
2818 ast::UintTy::U32 => PrimitiveType::U32,
2819 ast::UintTy::U64 => PrimitiveType::U64,
2820 ast::UintTy::U128 => PrimitiveType::U128,
2825 impl From<ast::FloatTy> for PrimitiveType {
2826 fn from(float_ty: ast::FloatTy) -> PrimitiveType {
2828 ast::FloatTy::F32 => PrimitiveType::F32,
2829 ast::FloatTy::F64 => PrimitiveType::F64,
2834 impl Clean<Type> for hir::Ty {
2835 fn clean(&self, cx: &DocContext<'_>) -> Type {
2839 TyKind::Never => Never,
2840 TyKind::Ptr(ref m) => RawPointer(m.mutbl.clean(cx), box m.ty.clean(cx)),
2841 TyKind::Rptr(ref l, ref m) => {
2842 let lifetime = if l.is_elided() {
2847 BorrowedRef {lifetime: lifetime, mutability: m.mutbl.clean(cx),
2848 type_: box m.ty.clean(cx)}
2850 TyKind::Slice(ref ty) => Slice(box ty.clean(cx)),
2851 TyKind::Array(ref ty, ref length) => {
2852 let def_id = cx.tcx.hir().local_def_id(length.hir_id);
2853 let param_env = cx.tcx.param_env(def_id);
2854 let substs = InternalSubsts::identity_for_item(cx.tcx, def_id);
2855 let cid = GlobalId {
2856 instance: ty::Instance::new(def_id, substs),
2859 let length = match cx.tcx.const_eval(param_env.and(cid)) {
2860 Ok(length) => print_const(cx, length),
2863 .span_to_snippet(cx.tcx.def_span(def_id))
2864 .unwrap_or_else(|_| "_".to_string()),
2866 Array(box ty.clean(cx), length)
2868 TyKind::Tup(ref tys) => Tuple(tys.clean(cx)),
2869 TyKind::Def(item_id, _) => {
2870 let item = cx.tcx.hir().expect_item(item_id.id);
2871 if let hir::ItemKind::OpaqueTy(ref ty) = item.node {
2872 ImplTrait(ty.bounds.clean(cx))
2877 TyKind::Path(hir::QPath::Resolved(None, ref path)) => {
2878 if let Res::Def(DefKind::TyParam, did) = path.res {
2879 if let Some(new_ty) = cx.ty_substs.borrow().get(&did).cloned() {
2882 if let Some(bounds) = cx.impl_trait_bounds.borrow_mut().remove(&did.into()) {
2883 return ImplTrait(bounds);
2887 let mut alias = None;
2888 if let Res::Def(DefKind::TyAlias, def_id) = path.res {
2889 // Substitute private type aliases
2890 if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(def_id) {
2891 if !cx.renderinfo.borrow().access_levels.is_exported(def_id) {
2892 alias = Some(&cx.tcx.hir().expect_item(hir_id).node);
2897 if let Some(&hir::ItemKind::TyAlias(ref ty, ref generics)) = alias {
2898 let provided_params = &path.segments.last().expect("segments were empty");
2899 let mut ty_substs = FxHashMap::default();
2900 let mut lt_substs = FxHashMap::default();
2901 let mut ct_substs = FxHashMap::default();
2902 let generic_args = provided_params.generic_args();
2904 let mut indices: GenericParamCount = Default::default();
2905 for param in generics.params.iter() {
2907 hir::GenericParamKind::Lifetime { .. } => {
2909 let lifetime = generic_args.args.iter().find_map(|arg| {
2911 hir::GenericArg::Lifetime(lt) => {
2912 if indices.lifetimes == j {
2921 if let Some(lt) = lifetime.cloned() {
2922 if !lt.is_elided() {
2924 cx.tcx.hir().local_def_id(param.hir_id);
2925 lt_substs.insert(lt_def_id, lt.clean(cx));
2928 indices.lifetimes += 1;
2930 hir::GenericParamKind::Type { ref default, .. } => {
2931 let ty_param_def_id =
2932 cx.tcx.hir().local_def_id(param.hir_id);
2934 let type_ = generic_args.args.iter().find_map(|arg| {
2936 hir::GenericArg::Type(ty) => {
2937 if indices.types == j {
2946 if let Some(ty) = type_ {
2947 ty_substs.insert(ty_param_def_id, ty.clean(cx));
2948 } else if let Some(default) = default.clone() {
2949 ty_substs.insert(ty_param_def_id,
2954 hir::GenericParamKind::Const { .. } => {
2955 let const_param_def_id =
2956 cx.tcx.hir().local_def_id(param.hir_id);
2958 let const_ = generic_args.args.iter().find_map(|arg| {
2960 hir::GenericArg::Const(ct) => {
2961 if indices.consts == j {
2970 if let Some(ct) = const_ {
2971 ct_substs.insert(const_param_def_id, ct.clean(cx));
2973 // FIXME(const_generics:defaults)
2974 indices.consts += 1;
2979 return cx.enter_alias(ty_substs, lt_substs, ct_substs, || ty.clean(cx));
2981 resolve_type(cx, path.clean(cx), self.hir_id)
2983 TyKind::Path(hir::QPath::Resolved(Some(ref qself), ref p)) => {
2984 let segments = if p.is_global() { &p.segments[1..] } else { &p.segments };
2985 let trait_segments = &segments[..segments.len() - 1];
2986 let trait_path = self::Path {
2987 global: p.is_global(),
2990 cx.tcx.associated_item(p.res.def_id()).container.id(),
2992 segments: trait_segments.clean(cx),
2995 name: p.segments.last().expect("segments were empty").ident.name.clean(cx),
2996 self_type: box qself.clean(cx),
2997 trait_: box resolve_type(cx, trait_path, self.hir_id)
3000 TyKind::Path(hir::QPath::TypeRelative(ref qself, ref segment)) => {
3001 let mut res = Res::Err;
3002 let ty = hir_ty_to_ty(cx.tcx, self);
3003 if let ty::Projection(proj) = ty.kind {
3004 res = Res::Def(DefKind::Trait, proj.trait_ref(cx.tcx).def_id);
3006 let trait_path = hir::Path {
3009 segments: vec![].into(),
3012 name: segment.ident.name.clean(cx),
3013 self_type: box qself.clean(cx),
3014 trait_: box resolve_type(cx, trait_path.clean(cx), self.hir_id)
3017 TyKind::TraitObject(ref bounds, ref lifetime) => {
3018 match bounds[0].clean(cx).trait_ {
3019 ResolvedPath { path, param_names: None, did, is_generic } => {
3020 let mut bounds: Vec<self::GenericBound> = bounds[1..].iter().map(|bound| {
3021 self::GenericBound::TraitBound(bound.clean(cx),
3022 hir::TraitBoundModifier::None)
3024 if !lifetime.is_elided() {
3025 bounds.push(self::GenericBound::Outlives(lifetime.clean(cx)));
3027 ResolvedPath { path, param_names: Some(bounds), did, is_generic, }
3029 _ => Infer, // shouldn't happen
3032 TyKind::BareFn(ref barefn) => BareFunction(box barefn.clean(cx)),
3033 TyKind::Infer | TyKind::Err => Infer,
3034 TyKind::Typeof(..) => panic!("unimplemented type {:?}", self.node),
3035 TyKind::CVarArgs(_) => CVarArgs,
3040 impl<'tcx> Clean<Type> for Ty<'tcx> {
3041 fn clean(&self, cx: &DocContext<'_>) -> Type {
3042 debug!("cleaning type: {:?}", self);
3045 ty::Bool => Primitive(PrimitiveType::Bool),
3046 ty::Char => Primitive(PrimitiveType::Char),
3047 ty::Int(int_ty) => Primitive(int_ty.into()),
3048 ty::Uint(uint_ty) => Primitive(uint_ty.into()),
3049 ty::Float(float_ty) => Primitive(float_ty.into()),
3050 ty::Str => Primitive(PrimitiveType::Str),
3051 ty::Slice(ty) => Slice(box ty.clean(cx)),
3052 ty::Array(ty, n) => {
3053 let mut n = cx.tcx.lift(&n).expect("array lift failed");
3054 if let ConstValue::Unevaluated(def_id, substs) = n.val {
3055 let param_env = cx.tcx.param_env(def_id);
3056 let cid = GlobalId {
3057 instance: ty::Instance::new(def_id, substs),
3060 if let Ok(new_n) = cx.tcx.const_eval(param_env.and(cid)) {
3064 let n = print_const(cx, n);
3065 Array(box ty.clean(cx), n)
3067 ty::RawPtr(mt) => RawPointer(mt.mutbl.clean(cx), box mt.ty.clean(cx)),
3068 ty::Ref(r, ty, mutbl) => BorrowedRef {
3069 lifetime: r.clean(cx),
3070 mutability: mutbl.clean(cx),
3071 type_: box ty.clean(cx),
3075 let ty = cx.tcx.lift(self).expect("FnPtr lift failed");
3076 let sig = ty.fn_sig(cx.tcx);
3077 let local_def_id = cx.tcx.hir().local_def_id_from_node_id(ast::CRATE_NODE_ID);
3078 BareFunction(box BareFunctionDecl {
3079 unsafety: sig.unsafety(),
3080 generic_params: Vec::new(),
3081 decl: (local_def_id, sig).clean(cx),
3085 ty::Adt(def, substs) => {
3087 let kind = match def.adt_kind() {
3088 AdtKind::Struct => TypeKind::Struct,
3089 AdtKind::Union => TypeKind::Union,
3090 AdtKind::Enum => TypeKind::Enum,
3092 inline::record_extern_fqn(cx, did, kind);
3093 let path = external_path(cx, cx.tcx.item_name(did), None, false, vec![], substs);
3101 ty::Foreign(did) => {
3102 inline::record_extern_fqn(cx, did, TypeKind::Foreign);
3103 let path = external_path(cx, cx.tcx.item_name(did),
3104 None, false, vec![], InternalSubsts::empty());
3112 ty::Dynamic(ref obj, ref reg) => {
3113 // HACK: pick the first `did` as the `did` of the trait object. Someone
3114 // might want to implement "native" support for marker-trait-only
3116 let mut dids = obj.principal_def_id().into_iter().chain(obj.auto_traits());
3117 let did = dids.next().unwrap_or_else(|| {
3118 panic!("found trait object `{:?}` with no traits?", self)
3120 let substs = match obj.principal() {
3121 Some(principal) => principal.skip_binder().substs,
3122 // marker traits have no substs.
3123 _ => cx.tcx.intern_substs(&[])
3126 inline::record_extern_fqn(cx, did, TypeKind::Trait);
3128 let mut param_names = vec![];
3129 reg.clean(cx).map(|b| param_names.push(GenericBound::Outlives(b)));
3131 let empty = cx.tcx.intern_substs(&[]);
3132 let path = external_path(cx, cx.tcx.item_name(did),
3133 Some(did), false, vec![], empty);
3134 inline::record_extern_fqn(cx, did, TypeKind::Trait);
3135 let bound = GenericBound::TraitBound(PolyTrait {
3136 trait_: ResolvedPath {
3142 generic_params: Vec::new(),
3143 }, hir::TraitBoundModifier::None);
3144 param_names.push(bound);
3147 let mut bindings = vec![];
3148 for pb in obj.projection_bounds() {
3149 bindings.push(TypeBinding {
3150 name: cx.tcx.associated_item(pb.item_def_id()).ident.name.clean(cx),
3151 kind: TypeBindingKind::Equality {
3152 ty: pb.skip_binder().ty.clean(cx)
3157 let path = external_path(cx, cx.tcx.item_name(did), Some(did),
3158 false, bindings, substs);
3161 param_names: Some(param_names),
3166 ty::Tuple(ref t) => {
3167 Tuple(t.iter().map(|t| t.expect_ty()).collect::<Vec<_>>().clean(cx))
3170 ty::Projection(ref data) => data.clean(cx),
3172 ty::Param(ref p) => {
3173 if let Some(bounds) = cx.impl_trait_bounds.borrow_mut().remove(&p.index.into()) {
3176 Generic(p.name.to_string())
3180 ty::Opaque(def_id, substs) => {
3181 // Grab the "TraitA + TraitB" from `impl TraitA + TraitB`,
3182 // by looking up the projections associated with the def_id.
3183 let predicates_of = cx.tcx.explicit_predicates_of(def_id);
3184 let substs = cx.tcx.lift(&substs).expect("Opaque lift failed");
3185 let bounds = predicates_of.instantiate(cx.tcx, substs);
3186 let mut regions = vec![];
3187 let mut has_sized = false;
3188 let mut bounds = bounds.predicates.iter().filter_map(|predicate| {
3189 let trait_ref = if let Some(tr) = predicate.to_opt_poly_trait_ref() {
3191 } else if let ty::Predicate::TypeOutlives(pred) = *predicate {
3192 // these should turn up at the end
3193 pred.skip_binder().1.clean(cx).map(|r| {
3194 regions.push(GenericBound::Outlives(r))
3201 if let Some(sized) = cx.tcx.lang_items().sized_trait() {
3202 if trait_ref.def_id() == sized {
3208 let bounds = bounds.predicates.iter().filter_map(|pred|
3209 if let ty::Predicate::Projection(proj) = *pred {
3210 let proj = proj.skip_binder();
3211 if proj.projection_ty.trait_ref(cx.tcx) == *trait_ref.skip_binder() {
3213 name: cx.tcx.associated_item(proj.projection_ty.item_def_id)
3214 .ident.name.clean(cx),
3215 kind: TypeBindingKind::Equality {
3216 ty: proj.ty.clean(cx),
3227 Some((trait_ref.skip_binder(), bounds).clean(cx))
3228 }).collect::<Vec<_>>();
3229 bounds.extend(regions);
3230 if !has_sized && !bounds.is_empty() {
3231 bounds.insert(0, GenericBound::maybe_sized(cx));
3236 ty::Closure(..) | ty::Generator(..) => Tuple(vec![]), // FIXME(pcwalton)
3238 ty::Bound(..) => panic!("Bound"),
3239 ty::Placeholder(..) => panic!("Placeholder"),
3240 ty::UnnormalizedProjection(..) => panic!("UnnormalizedProjection"),
3241 ty::GeneratorWitness(..) => panic!("GeneratorWitness"),
3242 ty::Infer(..) => panic!("Infer"),
3243 ty::Error => panic!("Error"),
3248 impl<'tcx> Clean<Constant> for ty::Const<'tcx> {
3249 fn clean(&self, cx: &DocContext<'_>) -> Constant {
3251 type_: self.ty.clean(cx),
3252 expr: format!("{}", self),
3257 impl Clean<Item> for hir::StructField {
3258 fn clean(&self, cx: &DocContext<'_>) -> Item {
3259 let local_did = cx.tcx.hir().local_def_id(self.hir_id);
3262 name: Some(self.ident.name).clean(cx),
3263 attrs: self.attrs.clean(cx),
3264 source: self.span.clean(cx),
3265 visibility: self.vis.clean(cx),
3266 stability: get_stability(cx, local_did),
3267 deprecation: get_deprecation(cx, local_did),
3269 inner: StructFieldItem(self.ty.clean(cx)),
3274 impl Clean<Item> for ty::FieldDef {
3275 fn clean(&self, cx: &DocContext<'_>) -> Item {
3277 name: Some(self.ident.name).clean(cx),
3278 attrs: cx.tcx.get_attrs(self.did).clean(cx),
3279 source: cx.tcx.def_span(self.did).clean(cx),
3280 visibility: self.vis.clean(cx),
3281 stability: get_stability(cx, self.did),
3282 deprecation: get_deprecation(cx, self.did),
3284 inner: StructFieldItem(cx.tcx.type_of(self.did).clean(cx)),
3289 #[derive(Clone, PartialEq, Eq, Debug)]
3290 pub enum Visibility {
3294 Restricted(DefId, Path),
3297 impl Clean<Visibility> for hir::Visibility {
3298 fn clean(&self, cx: &DocContext<'_>) -> Visibility {
3300 hir::VisibilityKind::Public => Visibility::Public,
3301 hir::VisibilityKind::Inherited => Visibility::Inherited,
3302 hir::VisibilityKind::Crate(_) => Visibility::Crate,
3303 hir::VisibilityKind::Restricted { ref path, .. } => {
3304 let path = path.clean(cx);
3305 let did = register_res(cx, path.res);
3306 Visibility::Restricted(did, path)
3312 impl Clean<Visibility> for ty::Visibility {
3313 fn clean(&self, _: &DocContext<'_>) -> Visibility {
3314 if *self == ty::Visibility::Public { Public } else { Inherited }
3318 #[derive(Clone, Debug)]
3320 pub struct_type: doctree::StructType,
3321 pub generics: Generics,
3322 pub fields: Vec<Item>,
3323 pub fields_stripped: bool,
3326 #[derive(Clone, Debug)]
3328 pub struct_type: doctree::StructType,
3329 pub generics: Generics,
3330 pub fields: Vec<Item>,
3331 pub fields_stripped: bool,
3334 impl Clean<Item> for doctree::Struct<'_> {
3335 fn clean(&self, cx: &DocContext<'_>) -> Item {
3337 name: Some(self.name.clean(cx)),
3338 attrs: self.attrs.clean(cx),
3339 source: self.whence.clean(cx),
3340 def_id: cx.tcx.hir().local_def_id(self.id),
3341 visibility: self.vis.clean(cx),
3342 stability: cx.stability(self.id).clean(cx),
3343 deprecation: cx.deprecation(self.id).clean(cx),
3344 inner: StructItem(Struct {
3345 struct_type: self.struct_type,
3346 generics: self.generics.clean(cx),
3347 fields: self.fields.clean(cx),
3348 fields_stripped: false,
3354 impl Clean<Item> for doctree::Union<'_> {
3355 fn clean(&self, cx: &DocContext<'_>) -> Item {
3357 name: Some(self.name.clean(cx)),
3358 attrs: self.attrs.clean(cx),
3359 source: self.whence.clean(cx),
3360 def_id: cx.tcx.hir().local_def_id(self.id),
3361 visibility: self.vis.clean(cx),
3362 stability: cx.stability(self.id).clean(cx),
3363 deprecation: cx.deprecation(self.id).clean(cx),
3364 inner: UnionItem(Union {
3365 struct_type: self.struct_type,
3366 generics: self.generics.clean(cx),
3367 fields: self.fields.clean(cx),
3368 fields_stripped: false,
3374 /// This is a more limited form of the standard Struct, different in that
3375 /// it lacks the things most items have (name, id, parameterization). Found
3376 /// only as a variant in an enum.
3377 #[derive(Clone, Debug)]
3378 pub struct VariantStruct {
3379 pub struct_type: doctree::StructType,
3380 pub fields: Vec<Item>,
3381 pub fields_stripped: bool,
3384 impl Clean<VariantStruct> for ::rustc::hir::VariantData {
3385 fn clean(&self, cx: &DocContext<'_>) -> VariantStruct {
3387 struct_type: doctree::struct_type_from_def(self),
3388 fields: self.fields().iter().map(|x| x.clean(cx)).collect(),
3389 fields_stripped: false,
3394 #[derive(Clone, Debug)]
3396 pub variants: IndexVec<VariantIdx, Item>,
3397 pub generics: Generics,
3398 pub variants_stripped: bool,
3401 impl Clean<Item> for doctree::Enum<'_> {
3402 fn clean(&self, cx: &DocContext<'_>) -> Item {
3404 name: Some(self.name.clean(cx)),
3405 attrs: self.attrs.clean(cx),
3406 source: self.whence.clean(cx),
3407 def_id: cx.tcx.hir().local_def_id(self.id),
3408 visibility: self.vis.clean(cx),
3409 stability: cx.stability(self.id).clean(cx),
3410 deprecation: cx.deprecation(self.id).clean(cx),
3411 inner: EnumItem(Enum {
3412 variants: self.variants.iter().map(|v| v.clean(cx)).collect(),
3413 generics: self.generics.clean(cx),
3414 variants_stripped: false,
3420 #[derive(Clone, Debug)]
3421 pub struct Variant {
3422 pub kind: VariantKind,
3425 impl Clean<Item> for doctree::Variant<'_> {
3426 fn clean(&self, cx: &DocContext<'_>) -> Item {
3428 name: Some(self.name.clean(cx)),
3429 attrs: self.attrs.clean(cx),
3430 source: self.whence.clean(cx),
3431 visibility: Inherited,
3432 stability: cx.stability(self.id).clean(cx),
3433 deprecation: cx.deprecation(self.id).clean(cx),
3434 def_id: cx.tcx.hir().local_def_id(self.id),
3435 inner: VariantItem(Variant {
3436 kind: self.def.clean(cx),
3442 impl Clean<Item> for ty::VariantDef {
3443 fn clean(&self, cx: &DocContext<'_>) -> Item {
3444 let kind = match self.ctor_kind {
3445 CtorKind::Const => VariantKind::CLike,
3448 self.fields.iter().map(|f| cx.tcx.type_of(f.did).clean(cx)).collect()
3451 CtorKind::Fictive => {
3452 VariantKind::Struct(VariantStruct {
3453 struct_type: doctree::Plain,
3454 fields_stripped: false,
3455 fields: self.fields.iter().map(|field| {
3457 source: cx.tcx.def_span(field.did).clean(cx),
3458 name: Some(field.ident.name.clean(cx)),
3459 attrs: cx.tcx.get_attrs(field.did).clean(cx),
3460 visibility: field.vis.clean(cx),
3462 stability: get_stability(cx, field.did),
3463 deprecation: get_deprecation(cx, field.did),
3464 inner: StructFieldItem(cx.tcx.type_of(field.did).clean(cx))
3471 name: Some(self.ident.clean(cx)),
3472 attrs: inline::load_attrs(cx, self.def_id).clean(cx),
3473 source: cx.tcx.def_span(self.def_id).clean(cx),
3474 visibility: Inherited,
3475 def_id: self.def_id,
3476 inner: VariantItem(Variant { kind }),
3477 stability: get_stability(cx, self.def_id),
3478 deprecation: get_deprecation(cx, self.def_id),
3483 #[derive(Clone, Debug)]
3484 pub enum VariantKind {
3487 Struct(VariantStruct),
3490 impl Clean<VariantKind> for hir::VariantData {
3491 fn clean(&self, cx: &DocContext<'_>) -> VariantKind {
3493 hir::VariantData::Struct(..) => VariantKind::Struct(self.clean(cx)),
3494 hir::VariantData::Tuple(..) =>
3495 VariantKind::Tuple(self.fields().iter().map(|x| x.ty.clean(cx)).collect()),
3496 hir::VariantData::Unit(..) => VariantKind::CLike,
3501 #[derive(Clone, Debug)]
3503 pub filename: FileName,
3508 pub original: syntax_pos::Span,
3512 pub fn empty() -> Span {
3514 filename: FileName::Anon(0),
3515 loline: 0, locol: 0,
3516 hiline: 0, hicol: 0,
3517 original: syntax_pos::DUMMY_SP,
3521 pub fn span(&self) -> syntax_pos::Span {
3526 impl Clean<Span> for syntax_pos::Span {
3527 fn clean(&self, cx: &DocContext<'_>) -> Span {
3528 if self.is_dummy() {
3529 return Span::empty();
3532 let cm = cx.sess().source_map();
3533 let filename = cm.span_to_filename(*self);
3534 let lo = cm.lookup_char_pos(self.lo());
3535 let hi = cm.lookup_char_pos(self.hi());
3539 locol: lo.col.to_usize(),
3541 hicol: hi.col.to_usize(),
3547 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
3551 pub segments: Vec<PathSegment>,
3555 pub fn last_name(&self) -> &str {
3556 self.segments.last().expect("segments were empty").name.as_str()
3560 impl Clean<Path> for hir::Path {
3561 fn clean(&self, cx: &DocContext<'_>) -> Path {
3563 global: self.is_global(),
3565 segments: if self.is_global() { &self.segments[1..] } else { &self.segments }.clean(cx),
3570 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
3571 pub enum GenericArg {
3577 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
3578 pub enum GenericArgs {
3580 args: Vec<GenericArg>,
3581 bindings: Vec<TypeBinding>,
3585 output: Option<Type>,
3589 impl Clean<GenericArgs> for hir::GenericArgs {
3590 fn clean(&self, cx: &DocContext<'_>) -> GenericArgs {
3591 if self.parenthesized {
3592 let output = self.bindings[0].ty().clean(cx);
3593 GenericArgs::Parenthesized {
3594 inputs: self.inputs().clean(cx),
3595 output: if output != Type::Tuple(Vec::new()) { Some(output) } else { None }
3598 let elide_lifetimes = self.args.iter().all(|arg| match arg {
3599 hir::GenericArg::Lifetime(lt) => lt.is_elided(),
3602 GenericArgs::AngleBracketed {
3603 args: self.args.iter().filter_map(|arg| match arg {
3604 hir::GenericArg::Lifetime(lt) if !elide_lifetimes => {
3605 Some(GenericArg::Lifetime(lt.clean(cx)))
3607 hir::GenericArg::Lifetime(_) => None,
3608 hir::GenericArg::Type(ty) => Some(GenericArg::Type(ty.clean(cx))),
3609 hir::GenericArg::Const(ct) => Some(GenericArg::Const(ct.clean(cx))),
3611 bindings: self.bindings.clean(cx),
3617 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
3618 pub struct PathSegment {
3620 pub args: GenericArgs,
3623 impl Clean<PathSegment> for hir::PathSegment {
3624 fn clean(&self, cx: &DocContext<'_>) -> PathSegment {
3626 name: self.ident.name.clean(cx),
3627 args: self.generic_args().clean(cx),
3632 fn strip_type(ty: Type) -> Type {
3634 Type::ResolvedPath { path, param_names, did, is_generic } => {
3635 Type::ResolvedPath { path: strip_path(&path), param_names, did, is_generic }
3637 Type::Tuple(inner_tys) => {
3638 Type::Tuple(inner_tys.iter().map(|t| strip_type(t.clone())).collect())
3640 Type::Slice(inner_ty) => Type::Slice(Box::new(strip_type(*inner_ty))),
3641 Type::Array(inner_ty, s) => Type::Array(Box::new(strip_type(*inner_ty)), s),
3642 Type::RawPointer(m, inner_ty) => Type::RawPointer(m, Box::new(strip_type(*inner_ty))),
3643 Type::BorrowedRef { lifetime, mutability, type_ } => {
3644 Type::BorrowedRef { lifetime, mutability, type_: Box::new(strip_type(*type_)) }
3646 Type::QPath { name, self_type, trait_ } => {
3649 self_type: Box::new(strip_type(*self_type)), trait_: Box::new(strip_type(*trait_))
3656 fn strip_path(path: &Path) -> Path {
3657 let segments = path.segments.iter().map(|s| {
3659 name: s.name.clone(),
3660 args: GenericArgs::AngleBracketed {
3668 global: path.global,
3669 res: path.res.clone(),
3674 fn qpath_to_string(p: &hir::QPath) -> String {
3675 let segments = match *p {
3676 hir::QPath::Resolved(_, ref path) => &path.segments,
3677 hir::QPath::TypeRelative(_, ref segment) => return segment.ident.to_string(),
3680 let mut s = String::new();
3681 for (i, seg) in segments.iter().enumerate() {
3685 if seg.ident.name != kw::PathRoot {
3686 s.push_str(&*seg.ident.as_str());
3692 impl Clean<String> for Ident {
3694 fn clean(&self, cx: &DocContext<'_>) -> String {
3699 impl Clean<String> for ast::Name {
3701 fn clean(&self, _: &DocContext<'_>) -> String {
3706 impl Clean<String> for InternedString {
3708 fn clean(&self, _: &DocContext<'_>) -> String {
3713 #[derive(Clone, Debug)]
3714 pub struct Typedef {
3716 pub generics: Generics,
3719 impl Clean<Item> for doctree::Typedef<'_> {
3720 fn clean(&self, cx: &DocContext<'_>) -> Item {
3722 name: Some(self.name.clean(cx)),
3723 attrs: self.attrs.clean(cx),
3724 source: self.whence.clean(cx),
3725 def_id: cx.tcx.hir().local_def_id(self.id),
3726 visibility: self.vis.clean(cx),
3727 stability: cx.stability(self.id).clean(cx),
3728 deprecation: cx.deprecation(self.id).clean(cx),
3729 inner: TypedefItem(Typedef {
3730 type_: self.ty.clean(cx),
3731 generics: self.gen.clean(cx),
3737 #[derive(Clone, Debug)]
3738 pub struct OpaqueTy {
3739 pub bounds: Vec<GenericBound>,
3740 pub generics: Generics,
3743 impl Clean<Item> for doctree::OpaqueTy<'_> {
3744 fn clean(&self, cx: &DocContext<'_>) -> Item {
3746 name: Some(self.name.clean(cx)),
3747 attrs: self.attrs.clean(cx),
3748 source: self.whence.clean(cx),
3749 def_id: cx.tcx.hir().local_def_id(self.id),
3750 visibility: self.vis.clean(cx),
3751 stability: cx.stability(self.id).clean(cx),
3752 deprecation: cx.deprecation(self.id).clean(cx),
3753 inner: OpaqueTyItem(OpaqueTy {
3754 bounds: self.opaque_ty.bounds.clean(cx),
3755 generics: self.opaque_ty.generics.clean(cx),
3761 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
3762 pub struct BareFunctionDecl {
3763 pub unsafety: hir::Unsafety,
3764 pub generic_params: Vec<GenericParamDef>,
3769 impl Clean<BareFunctionDecl> for hir::BareFnTy {
3770 fn clean(&self, cx: &DocContext<'_>) -> BareFunctionDecl {
3771 let (generic_params, decl) = enter_impl_trait(cx, || {
3772 (self.generic_params.clean(cx), (&*self.decl, &self.param_names[..]).clean(cx))
3775 unsafety: self.unsafety,
3783 #[derive(Clone, Debug)]
3786 pub mutability: Mutability,
3787 /// It's useful to have the value of a static documented, but I have no
3788 /// desire to represent expressions (that'd basically be all of the AST,
3789 /// which is huge!). So, have a string.
3793 impl Clean<Item> for doctree::Static<'_> {
3794 fn clean(&self, cx: &DocContext<'_>) -> Item {
3795 debug!("cleaning static {}: {:?}", self.name.clean(cx), self);
3797 name: Some(self.name.clean(cx)),
3798 attrs: self.attrs.clean(cx),
3799 source: self.whence.clean(cx),
3800 def_id: cx.tcx.hir().local_def_id(self.id),
3801 visibility: self.vis.clean(cx),
3802 stability: cx.stability(self.id).clean(cx),
3803 deprecation: cx.deprecation(self.id).clean(cx),
3804 inner: StaticItem(Static {
3805 type_: self.type_.clean(cx),
3806 mutability: self.mutability.clean(cx),
3807 expr: print_const_expr(cx, self.expr),
3813 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
3814 pub struct Constant {
3819 impl Clean<Item> for doctree::Constant<'_> {
3820 fn clean(&self, cx: &DocContext<'_>) -> Item {
3822 name: Some(self.name.clean(cx)),
3823 attrs: self.attrs.clean(cx),
3824 source: self.whence.clean(cx),
3825 def_id: cx.tcx.hir().local_def_id(self.id),
3826 visibility: self.vis.clean(cx),
3827 stability: cx.stability(self.id).clean(cx),
3828 deprecation: cx.deprecation(self.id).clean(cx),
3829 inner: ConstantItem(Constant {
3830 type_: self.type_.clean(cx),
3831 expr: print_const_expr(cx, self.expr),
3837 #[derive(Debug, Clone, PartialEq, Eq, Copy, Hash)]
3838 pub enum Mutability {
3843 impl Clean<Mutability> for hir::Mutability {
3844 fn clean(&self, _: &DocContext<'_>) -> Mutability {
3846 &hir::MutMutable => Mutable,
3847 &hir::MutImmutable => Immutable,
3852 #[derive(Clone, PartialEq, Eq, Copy, Debug, Hash)]
3853 pub enum ImplPolarity {
3858 impl Clean<ImplPolarity> for ty::ImplPolarity {
3859 fn clean(&self, _: &DocContext<'_>) -> ImplPolarity {
3861 &ty::ImplPolarity::Positive |
3862 // FIXME: do we want to do something else here?
3863 &ty::ImplPolarity::Reservation => ImplPolarity::Positive,
3864 &ty::ImplPolarity::Negative => ImplPolarity::Negative,
3869 #[derive(Clone, Debug)]
3871 pub unsafety: hir::Unsafety,
3872 pub generics: Generics,
3873 pub provided_trait_methods: FxHashSet<String>,
3874 pub trait_: Option<Type>,
3876 pub items: Vec<Item>,
3877 pub polarity: Option<ImplPolarity>,
3878 pub synthetic: bool,
3879 pub blanket_impl: Option<Type>,
3882 pub fn get_auto_trait_and_blanket_impls(
3883 cx: &DocContext<'tcx>,
3885 param_env_def_id: DefId,
3886 ) -> impl Iterator<Item = Item> {
3887 AutoTraitFinder::new(cx).get_auto_trait_impls(ty, param_env_def_id).into_iter()
3888 .chain(BlanketImplFinder::new(cx).get_blanket_impls(ty, param_env_def_id))
3891 impl Clean<Vec<Item>> for doctree::Impl<'_> {
3892 fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
3893 let mut ret = Vec::new();
3894 let trait_ = self.trait_.clean(cx);
3895 let items = self.items.iter().map(|ii| ii.clean(cx)).collect::<Vec<_>>();
3896 let def_id = cx.tcx.hir().local_def_id(self.id);
3898 // If this impl block is an implementation of the Deref trait, then we
3899 // need to try inlining the target's inherent impl blocks as well.
3900 if trait_.def_id() == cx.tcx.lang_items().deref_trait() {
3901 build_deref_target_impls(cx, &items, &mut ret);
3904 let provided = trait_.def_id().map(|did| {
3905 cx.tcx.provided_trait_methods(did)
3907 .map(|meth| meth.ident.to_string())
3909 }).unwrap_or_default();
3913 attrs: self.attrs.clean(cx),
3914 source: self.whence.clean(cx),
3916 visibility: self.vis.clean(cx),
3917 stability: cx.stability(self.id).clean(cx),
3918 deprecation: cx.deprecation(self.id).clean(cx),
3919 inner: ImplItem(Impl {
3920 unsafety: self.unsafety,
3921 generics: self.generics.clean(cx),
3922 provided_trait_methods: provided,
3924 for_: self.for_.clean(cx),
3926 polarity: Some(cx.tcx.impl_polarity(def_id).clean(cx)),
3935 fn build_deref_target_impls(cx: &DocContext<'_>,
3937 ret: &mut Vec<Item>) {
3938 use self::PrimitiveType::*;
3942 let target = match item.inner {
3943 TypedefItem(ref t, true) => &t.type_,
3946 let primitive = match *target {
3947 ResolvedPath { did, .. } if did.is_local() => continue,
3948 ResolvedPath { did, .. } => {
3949 ret.extend(inline::build_impls(cx, did, None));
3952 _ => match target.primitive_type() {
3957 let did = match primitive {
3958 Isize => tcx.lang_items().isize_impl(),
3959 I8 => tcx.lang_items().i8_impl(),
3960 I16 => tcx.lang_items().i16_impl(),
3961 I32 => tcx.lang_items().i32_impl(),
3962 I64 => tcx.lang_items().i64_impl(),
3963 I128 => tcx.lang_items().i128_impl(),
3964 Usize => tcx.lang_items().usize_impl(),
3965 U8 => tcx.lang_items().u8_impl(),
3966 U16 => tcx.lang_items().u16_impl(),
3967 U32 => tcx.lang_items().u32_impl(),
3968 U64 => tcx.lang_items().u64_impl(),
3969 U128 => tcx.lang_items().u128_impl(),
3970 F32 => tcx.lang_items().f32_impl(),
3971 F64 => tcx.lang_items().f64_impl(),
3972 Char => tcx.lang_items().char_impl(),
3973 Bool => tcx.lang_items().bool_impl(),
3974 Str => tcx.lang_items().str_impl(),
3975 Slice => tcx.lang_items().slice_impl(),
3976 Array => tcx.lang_items().slice_impl(),
3979 RawPointer => tcx.lang_items().const_ptr_impl(),
3983 CVarArgs => tcx.lang_items().va_list(),
3985 if let Some(did) = did {
3986 if !did.is_local() {
3987 inline::build_impl(cx, did, None, ret);
3993 impl Clean<Vec<Item>> for doctree::ExternCrate<'_> {
3994 fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
3996 let please_inline = self.vis.node.is_pub() && self.attrs.iter().any(|a| {
3997 a.check_name(sym::doc) && match a.meta_item_list() {
3998 Some(l) => attr::list_contains_name(&l, sym::inline),
4004 let mut visited = FxHashSet::default();
4010 index: CRATE_DEF_INDEX,
4014 if let Some(items) = inline::try_inline(
4016 Some(rustc::ty::Attributes::Borrowed(self.attrs)),
4025 attrs: self.attrs.clean(cx),
4026 source: self.whence.clean(cx),
4027 def_id: DefId { krate: self.cnum, index: CRATE_DEF_INDEX },
4028 visibility: self.vis.clean(cx),
4031 inner: ExternCrateItem(self.name.clean(cx), self.path.clone())
4036 impl Clean<Vec<Item>> for doctree::Import<'_> {
4037 fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
4038 // We consider inlining the documentation of `pub use` statements, but we
4039 // forcefully don't inline if this is not public or if the
4040 // #[doc(no_inline)] attribute is present.
4041 // Don't inline doc(hidden) imports so they can be stripped at a later stage.
4042 let mut denied = !self.vis.node.is_pub() || self.attrs.iter().any(|a| {
4043 a.check_name(sym::doc) && match a.meta_item_list() {
4044 Some(l) => attr::list_contains_name(&l, sym::no_inline) ||
4045 attr::list_contains_name(&l, sym::hidden),
4049 // Also check whether imports were asked to be inlined, in case we're trying to re-export a
4050 // crate in Rust 2018+
4051 let please_inline = self.attrs.lists(sym::doc).has_word(sym::inline);
4052 let path = self.path.clean(cx);
4053 let inner = if self.glob {
4055 let mut visited = FxHashSet::default();
4056 if let Some(items) = inline::try_inline_glob(cx, path.res, &mut visited) {
4061 Import::Glob(resolve_use_source(cx, path))
4063 let name = self.name;
4066 Res::Def(DefKind::Mod, did) => {
4067 if !did.is_local() && did.index == CRATE_DEF_INDEX {
4068 // if we're `pub use`ing an extern crate root, don't inline it unless we
4069 // were specifically asked for it
4077 let mut visited = FxHashSet::default();
4078 if let Some(items) = inline::try_inline(
4080 Some(rustc::ty::Attributes::Borrowed(self.attrs)),
4086 Import::Simple(name.clean(cx), resolve_use_source(cx, path))
4091 attrs: self.attrs.clean(cx),
4092 source: self.whence.clean(cx),
4093 def_id: cx.tcx.hir().local_def_id_from_node_id(ast::CRATE_NODE_ID),
4094 visibility: self.vis.clean(cx),
4097 inner: ImportItem(inner)
4102 #[derive(Clone, Debug)]
4104 // use source as str;
4105 Simple(String, ImportSource),
4110 #[derive(Clone, Debug)]
4111 pub struct ImportSource {
4113 pub did: Option<DefId>,
4116 impl Clean<Item> for doctree::ForeignItem<'_> {
4117 fn clean(&self, cx: &DocContext<'_>) -> Item {
4118 let inner = match self.kind {
4119 hir::ForeignItemKind::Fn(ref decl, ref names, ref generics) => {
4120 let abi = cx.tcx.hir().get_foreign_abi(self.id);
4121 let (generics, decl) = enter_impl_trait(cx, || {
4122 (generics.clean(cx), (&**decl, &names[..]).clean(cx))
4124 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
4125 ForeignFunctionItem(Function {
4128 header: hir::FnHeader {
4129 unsafety: hir::Unsafety::Unsafe,
4131 constness: hir::Constness::NotConst,
4132 asyncness: hir::IsAsync::NotAsync,
4138 hir::ForeignItemKind::Static(ref ty, mutbl) => {
4139 ForeignStaticItem(Static {
4140 type_: ty.clean(cx),
4141 mutability: mutbl.clean(cx),
4142 expr: String::new(),
4145 hir::ForeignItemKind::Type => {
4151 name: Some(self.name.clean(cx)),
4152 attrs: self.attrs.clean(cx),
4153 source: self.whence.clean(cx),
4154 def_id: cx.tcx.hir().local_def_id(self.id),
4155 visibility: self.vis.clean(cx),
4156 stability: cx.stability(self.id).clean(cx),
4157 deprecation: cx.deprecation(self.id).clean(cx),
4165 pub trait ToSource {
4166 fn to_src(&self, cx: &DocContext<'_>) -> String;
4169 impl ToSource for syntax_pos::Span {
4170 fn to_src(&self, cx: &DocContext<'_>) -> String {
4171 debug!("converting span {:?} to snippet", self.clean(cx));
4172 let sn = match cx.sess().source_map().span_to_snippet(*self) {
4174 Err(_) => String::new()
4176 debug!("got snippet {}", sn);
4181 fn name_from_pat(p: &hir::Pat) -> String {
4183 debug!("trying to get a name from pattern: {:?}", p);
4186 PatKind::Wild => "_".to_string(),
4187 PatKind::Binding(_, _, ident, _) => ident.to_string(),
4188 PatKind::TupleStruct(ref p, ..) | PatKind::Path(ref p) => qpath_to_string(p),
4189 PatKind::Struct(ref name, ref fields, etc) => {
4190 format!("{} {{ {}{} }}", qpath_to_string(name),
4191 fields.iter().map(|fp| format!("{}: {}", fp.ident, name_from_pat(&fp.pat)))
4192 .collect::<Vec<String>>().join(", "),
4193 if etc { ", .." } else { "" }
4196 PatKind::Or(ref pats) => {
4197 pats.iter().map(|p| name_from_pat(&**p)).collect::<Vec<String>>().join(" | ")
4199 PatKind::Tuple(ref elts, _) => format!("({})", elts.iter().map(|p| name_from_pat(&**p))
4200 .collect::<Vec<String>>().join(", ")),
4201 PatKind::Box(ref p) => name_from_pat(&**p),
4202 PatKind::Ref(ref p, _) => name_from_pat(&**p),
4203 PatKind::Lit(..) => {
4204 warn!("tried to get argument name from PatKind::Lit, \
4205 which is silly in function arguments");
4208 PatKind::Range(..) => panic!("tried to get argument name from PatKind::Range, \
4209 which is not allowed in function arguments"),
4210 PatKind::Slice(ref begin, ref mid, ref end) => {
4211 let begin = begin.iter().map(|p| name_from_pat(&**p));
4212 let mid = mid.as_ref().map(|p| format!("..{}", name_from_pat(&**p))).into_iter();
4213 let end = end.iter().map(|p| name_from_pat(&**p));
4214 format!("[{}]", begin.chain(mid).chain(end).collect::<Vec<_>>().join(", "))
4219 fn print_const(cx: &DocContext<'_>, n: &ty::Const<'_>) -> String {
4221 ConstValue::Unevaluated(def_id, _) => {
4222 if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(def_id) {
4223 print_const_expr(cx, cx.tcx.hir().body_owned_by(hir_id))
4225 inline::print_inlined_const(cx, def_id)
4229 let mut s = n.to_string();
4230 // array lengths are obviously usize
4231 if s.ends_with("usize") {
4232 let n = s.len() - "usize".len();
4234 if s.ends_with(": ") {
4235 let n = s.len() - ": ".len();
4244 fn print_const_expr(cx: &DocContext<'_>, body: hir::BodyId) -> String {
4245 cx.tcx.hir().hir_to_pretty_string(body.hir_id)
4248 /// Given a type Path, resolve it to a Type using the TyCtxt
4249 fn resolve_type(cx: &DocContext<'_>,
4251 id: hir::HirId) -> Type {
4252 if id == hir::DUMMY_HIR_ID {
4253 debug!("resolve_type({:?})", path);
4255 debug!("resolve_type({:?},{:?})", path, id);
4258 let is_generic = match path.res {
4259 Res::PrimTy(p) => match p {
4260 hir::Str => return Primitive(PrimitiveType::Str),
4261 hir::Bool => return Primitive(PrimitiveType::Bool),
4262 hir::Char => return Primitive(PrimitiveType::Char),
4263 hir::Int(int_ty) => return Primitive(int_ty.into()),
4264 hir::Uint(uint_ty) => return Primitive(uint_ty.into()),
4265 hir::Float(float_ty) => return Primitive(float_ty.into()),
4267 Res::SelfTy(..) if path.segments.len() == 1 => {
4268 return Generic(kw::SelfUpper.to_string());
4270 Res::Def(DefKind::TyParam, _) if path.segments.len() == 1 => {
4271 return Generic(format!("{:#}", path.print()));
4274 | Res::Def(DefKind::TyParam, _)
4275 | Res::Def(DefKind::AssocTy, _) => true,
4278 let did = register_res(&*cx, path.res);
4279 ResolvedPath { path: path, param_names: None, did: did, is_generic: is_generic }
4282 pub fn register_res(cx: &DocContext<'_>, res: Res) -> DefId {
4283 debug!("register_res({:?})", res);
4285 let (did, kind) = match res {
4286 Res::Def(DefKind::Fn, i) => (i, TypeKind::Function),
4287 Res::Def(DefKind::TyAlias, i) => (i, TypeKind::Typedef),
4288 Res::Def(DefKind::Enum, i) => (i, TypeKind::Enum),
4289 Res::Def(DefKind::Trait, i) => (i, TypeKind::Trait),
4290 Res::Def(DefKind::Struct, i) => (i, TypeKind::Struct),
4291 Res::Def(DefKind::Union, i) => (i, TypeKind::Union),
4292 Res::Def(DefKind::Mod, i) => (i, TypeKind::Module),
4293 Res::Def(DefKind::ForeignTy, i) => (i, TypeKind::Foreign),
4294 Res::Def(DefKind::Const, i) => (i, TypeKind::Const),
4295 Res::Def(DefKind::Static, i) => (i, TypeKind::Static),
4296 Res::Def(DefKind::Variant, i) => (cx.tcx.parent(i).expect("cannot get parent def id"),
4298 Res::Def(DefKind::Macro(mac_kind), i) => match mac_kind {
4299 MacroKind::Bang => (i, TypeKind::Macro),
4300 MacroKind::Attr => (i, TypeKind::Attr),
4301 MacroKind::Derive => (i, TypeKind::Derive),
4303 Res::Def(DefKind::TraitAlias, i) => (i, TypeKind::TraitAlias),
4304 Res::SelfTy(Some(def_id), _) => (def_id, TypeKind::Trait),
4305 Res::SelfTy(_, Some(impl_def_id)) => return impl_def_id,
4306 _ => return res.def_id()
4308 if did.is_local() { return did }
4309 inline::record_extern_fqn(cx, did, kind);
4310 if let TypeKind::Trait = kind {
4311 inline::record_extern_trait(cx, did);
4316 fn resolve_use_source(cx: &DocContext<'_>, path: Path) -> ImportSource {
4318 did: if path.res.opt_def_id().is_none() {
4321 Some(register_res(cx, path.res))
4327 #[derive(Clone, Debug)]
4330 pub imported_from: Option<String>,
4333 impl Clean<Item> for doctree::Macro<'_> {
4334 fn clean(&self, cx: &DocContext<'_>) -> Item {
4335 let name = self.name.clean(cx);
4337 name: Some(name.clone()),
4338 attrs: self.attrs.clean(cx),
4339 source: self.whence.clean(cx),
4341 stability: cx.stability(self.hid).clean(cx),
4342 deprecation: cx.deprecation(self.hid).clean(cx),
4343 def_id: self.def_id,
4344 inner: MacroItem(Macro {
4345 source: format!("macro_rules! {} {{\n{}}}",
4347 self.matchers.iter().map(|span| {
4348 format!(" {} => {{ ... }};\n", span.to_src(cx))
4349 }).collect::<String>()),
4350 imported_from: self.imported_from.clean(cx),
4356 #[derive(Clone, Debug)]
4357 pub struct ProcMacro {
4358 pub kind: MacroKind,
4359 pub helpers: Vec<String>,
4362 impl Clean<Item> for doctree::ProcMacro<'_> {
4363 fn clean(&self, cx: &DocContext<'_>) -> Item {
4365 name: Some(self.name.clean(cx)),
4366 attrs: self.attrs.clean(cx),
4367 source: self.whence.clean(cx),
4369 stability: cx.stability(self.id).clean(cx),
4370 deprecation: cx.deprecation(self.id).clean(cx),
4371 def_id: cx.tcx.hir().local_def_id(self.id),
4372 inner: ProcMacroItem(ProcMacro {
4374 helpers: self.helpers.clean(cx),
4380 #[derive(Clone, Debug)]
4381 pub struct Stability {
4382 pub level: stability::StabilityLevel,
4383 pub feature: Option<String>,
4385 pub deprecation: Option<Deprecation>,
4386 pub unstable_reason: Option<String>,
4387 pub issue: Option<u32>,
4390 #[derive(Clone, Debug)]
4391 pub struct Deprecation {
4392 pub since: Option<String>,
4393 pub note: Option<String>,
4396 impl Clean<Stability> for attr::Stability {
4397 fn clean(&self, _: &DocContext<'_>) -> Stability {
4399 level: stability::StabilityLevel::from_attr_level(&self.level),
4400 feature: Some(self.feature.to_string()).filter(|f| !f.is_empty()),
4401 since: match self.level {
4402 attr::Stable {ref since} => since.to_string(),
4405 deprecation: self.rustc_depr.as_ref().map(|d| {
4407 note: Some(d.reason.to_string()).filter(|r| !r.is_empty()),
4408 since: Some(d.since.to_string()).filter(|d| !d.is_empty()),
4411 unstable_reason: match self.level {
4412 attr::Unstable { reason: Some(ref reason), .. } => Some(reason.to_string()),
4415 issue: match self.level {
4416 attr::Unstable {issue, ..} => Some(issue),
4423 impl<'a> Clean<Stability> for &'a attr::Stability {
4424 fn clean(&self, dc: &DocContext<'_>) -> Stability {
4429 impl Clean<Deprecation> for attr::Deprecation {
4430 fn clean(&self, _: &DocContext<'_>) -> Deprecation {
4432 since: self.since.map(|s| s.to_string()).filter(|s| !s.is_empty()),
4433 note: self.note.map(|n| n.to_string()).filter(|n| !n.is_empty()),
4438 /// An type binding on an associated type (e.g., `A = Bar` in `Foo<A = Bar>` or
4439 /// `A: Send + Sync` in `Foo<A: Send + Sync>`).
4440 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
4441 pub struct TypeBinding {
4443 pub kind: TypeBindingKind,
4446 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
4447 pub enum TypeBindingKind {
4452 bounds: Vec<GenericBound>,
4457 pub fn ty(&self) -> &Type {
4459 TypeBindingKind::Equality { ref ty } => ty,
4460 _ => panic!("expected equality type binding for parenthesized generic args"),
4465 impl Clean<TypeBinding> for hir::TypeBinding {
4466 fn clean(&self, cx: &DocContext<'_>) -> TypeBinding {
4468 name: self.ident.name.clean(cx),
4469 kind: self.kind.clean(cx),
4474 impl Clean<TypeBindingKind> for hir::TypeBindingKind {
4475 fn clean(&self, cx: &DocContext<'_>) -> TypeBindingKind {
4477 hir::TypeBindingKind::Equality { ref ty } =>
4478 TypeBindingKind::Equality {
4481 hir::TypeBindingKind::Constraint { ref bounds } =>
4482 TypeBindingKind::Constraint {
4483 bounds: bounds.into_iter().map(|b| b.clean(cx)).collect(),
4489 pub fn enter_impl_trait<F, R>(cx: &DocContext<'_>, f: F) -> R
4493 let old_bounds = mem::take(&mut *cx.impl_trait_bounds.borrow_mut());
4495 assert!(cx.impl_trait_bounds.borrow().is_empty());
4496 *cx.impl_trait_bounds.borrow_mut() = old_bounds;
4500 #[derive(Eq, PartialEq, Hash, Copy, Clone, Debug)]
4501 enum RegionTarget<'tcx> {
4502 Region(Region<'tcx>),
4503 RegionVid(RegionVid)
4506 #[derive(Default, Debug, Clone)]
4507 struct RegionDeps<'tcx> {
4508 larger: FxHashSet<RegionTarget<'tcx>>,
4509 smaller: FxHashSet<RegionTarget<'tcx>>
4512 #[derive(Eq, PartialEq, Hash, Debug)]
4514 TraitBound(Vec<PathSegment>, Vec<SimpleBound>, Vec<GenericParamDef>, hir::TraitBoundModifier),
4518 impl From<GenericBound> for SimpleBound {
4519 fn from(bound: GenericBound) -> Self {
4520 match bound.clone() {
4521 GenericBound::Outlives(l) => SimpleBound::Outlives(l),
4522 GenericBound::TraitBound(t, mod_) => match t.trait_ {
4523 Type::ResolvedPath { path, param_names, .. } => {
4524 SimpleBound::TraitBound(path.segments,
4526 .map_or_else(|| Vec::new(), |v| v.iter()
4527 .map(|p| SimpleBound::from(p.clone()))
4532 _ => panic!("Unexpected bound {:?}", bound),