1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! This module contains the "cleaned" pieces of the AST, and the functions
14 pub use self::Type::*;
15 pub use self::Mutability::*;
16 pub use self::ItemEnum::*;
17 pub use self::SelfTy::*;
18 pub use self::FunctionRetTy::*;
19 pub use self::Visibility::{Public, Inherited};
21 use rustc_target::spec::abi::Abi;
22 use syntax::ast::{self, AttrStyle, Ident};
24 use syntax::ext::base::MacroKind;
25 use syntax::source_map::{dummy_spanned, Spanned};
27 use syntax::symbol::keywords::{self, Keyword};
28 use syntax::symbol::InternedString;
29 use syntax_pos::{self, DUMMY_SP, Pos, FileName};
31 use rustc::mir::interpret::ConstValue;
32 use rustc::middle::resolve_lifetime as rl;
33 use rustc::ty::fold::TypeFolder;
34 use rustc::middle::lang_items;
35 use rustc::mir::interpret::GlobalId;
36 use rustc::hir::{self, GenericArg, HirVec};
37 use rustc::hir::def::{self, Def, CtorKind};
38 use rustc::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
39 use rustc::ty::subst::Substs;
40 use rustc::ty::{self, TyCtxt, Region, RegionVid, Ty, AdtKind};
41 use rustc::middle::stability;
42 use rustc::util::nodemap::{FxHashMap, FxHashSet};
43 use rustc_typeck::hir_ty_to_ty;
44 use rustc::infer::region_constraints::{RegionConstraintData, Constraint};
46 use std::collections::hash_map::Entry;
48 use std::hash::{Hash, Hasher};
49 use std::default::Default;
50 use std::{mem, slice, vec};
51 use std::iter::{FromIterator, once};
52 use rustc_data_structures::sync::Lrc;
54 use std::str::FromStr;
55 use std::cell::RefCell;
59 use parking_lot::ReentrantMutex;
61 use core::{self, DocContext};
64 use html::render::{cache, ExternalLocation};
65 use html::item_type::ItemType;
75 use self::auto_trait::AutoTraitFinder;
76 use self::blanket_impl::BlanketImplFinder;
78 thread_local!(pub static MAX_DEF_ID: RefCell<FxHashMap<CrateNum, DefId>> = Default::default());
80 const FN_OUTPUT_NAME: &'static str = "Output";
82 // extract the stability index for a node from tcx, if possible
83 fn get_stability(cx: &DocContext, def_id: DefId) -> Option<Stability> {
84 cx.tcx.lookup_stability(def_id).clean(cx)
87 fn get_deprecation(cx: &DocContext, def_id: DefId) -> Option<Deprecation> {
88 cx.tcx.lookup_deprecation(def_id).clean(cx)
92 fn clean(&self, cx: &DocContext) -> T;
95 impl<T: Clean<U>, U> Clean<Vec<U>> for [T] {
96 fn clean(&self, cx: &DocContext) -> Vec<U> {
97 self.iter().map(|x| x.clean(cx)).collect()
101 impl<T: Clean<U>, U> Clean<U> for P<T> {
102 fn clean(&self, cx: &DocContext) -> U {
107 impl<T: Clean<U>, U> Clean<U> for Rc<T> {
108 fn clean(&self, cx: &DocContext) -> U {
113 impl<T: Clean<U>, U> Clean<Option<U>> for Option<T> {
114 fn clean(&self, cx: &DocContext) -> Option<U> {
115 self.as_ref().map(|v| v.clean(cx))
119 impl<T, U> Clean<U> for ty::Binder<T> where T: Clean<U> {
120 fn clean(&self, cx: &DocContext) -> U {
121 self.skip_binder().clean(cx)
125 impl<T: Clean<U>, U> Clean<Vec<U>> for P<[T]> {
126 fn clean(&self, cx: &DocContext) -> Vec<U> {
127 self.iter().map(|x| x.clean(cx)).collect()
131 #[derive(Clone, Debug)]
134 pub version: Option<String>,
136 pub module: Option<Item>,
137 pub externs: Vec<(CrateNum, ExternalCrate)>,
138 pub primitives: Vec<(DefId, PrimitiveType, Attributes)>,
139 // These are later on moved into `CACHEKEY`, leaving the map empty.
140 // Only here so that they can be filtered through the rustdoc passes.
141 pub external_traits: Arc<ReentrantMutex<RefCell<FxHashMap<DefId, Trait>>>>,
142 pub masked_crates: FxHashSet<CrateNum>,
145 impl<'a, 'tcx, 'rcx, 'cstore> Clean<Crate> for visit_ast::RustdocVisitor<'a, 'tcx, 'rcx, 'cstore> {
146 fn clean(&self, cx: &DocContext) -> Crate {
147 use ::visit_lib::LibEmbargoVisitor;
150 let mut r = cx.renderinfo.borrow_mut();
151 r.deref_trait_did = cx.tcx.lang_items().deref_trait();
152 r.deref_mut_trait_did = cx.tcx.lang_items().deref_mut_trait();
153 r.owned_box_did = cx.tcx.lang_items().owned_box();
156 let mut externs = Vec::new();
157 for &cnum in cx.tcx.crates().iter() {
158 externs.push((cnum, cnum.clean(cx)));
159 // Analyze doc-reachability for extern items
160 LibEmbargoVisitor::new(cx).visit_lib(cnum);
162 externs.sort_by(|&(a, _), &(b, _)| a.cmp(&b));
164 // Clean the crate, translating the entire libsyntax AST to one that is
165 // understood by rustdoc.
166 let mut module = self.module.clean(cx);
167 let mut masked_crates = FxHashSet::default();
170 ModuleItem(ref module) => {
171 for it in &module.items {
172 if it.is_extern_crate() && it.attrs.has_doc_flag("masked") {
173 masked_crates.insert(it.def_id.krate);
180 let ExternalCrate { name, src, primitives, keywords, .. } = LOCAL_CRATE.clean(cx);
182 let m = match module.inner {
183 ModuleItem(ref mut m) => m,
186 m.items.extend(primitives.iter().map(|&(def_id, prim, ref attrs)| {
188 source: Span::empty(),
189 name: Some(prim.to_url_str().to_string()),
190 attrs: attrs.clone(),
191 visibility: Some(Public),
192 stability: get_stability(cx, def_id),
193 deprecation: get_deprecation(cx, def_id),
195 inner: PrimitiveItem(prim),
198 m.items.extend(keywords.into_iter().map(|(def_id, kw, attrs)| {
200 source: Span::empty(),
201 name: Some(kw.clone()),
203 visibility: Some(Public),
204 stability: get_stability(cx, def_id),
205 deprecation: get_deprecation(cx, def_id),
207 inner: KeywordItem(kw),
216 module: Some(module),
219 external_traits: cx.external_traits.clone(),
225 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
226 pub struct ExternalCrate {
229 pub attrs: Attributes,
230 pub primitives: Vec<(DefId, PrimitiveType, Attributes)>,
231 pub keywords: Vec<(DefId, String, Attributes)>,
234 impl Clean<ExternalCrate> for CrateNum {
235 fn clean(&self, cx: &DocContext) -> ExternalCrate {
236 let root = DefId { krate: *self, index: CRATE_DEF_INDEX };
237 let krate_span = cx.tcx.def_span(root);
238 let krate_src = cx.sess().source_map().span_to_filename(krate_span);
240 // Collect all inner modules which are tagged as implementations of
243 // Note that this loop only searches the top-level items of the crate,
244 // and this is intentional. If we were to search the entire crate for an
245 // item tagged with `#[doc(primitive)]` then we would also have to
246 // search the entirety of external modules for items tagged
247 // `#[doc(primitive)]`, which is a pretty inefficient process (decoding
248 // all that metadata unconditionally).
250 // In order to keep the metadata load under control, the
251 // `#[doc(primitive)]` feature is explicitly designed to only allow the
252 // primitive tags to show up as the top level items in a crate.
254 // Also note that this does not attempt to deal with modules tagged
255 // duplicately for the same primitive. This is handled later on when
256 // rendering by delegating everything to a hash map.
257 let as_primitive = |def: Def| {
258 if let Def::Mod(def_id) = def {
259 let attrs = cx.tcx.get_attrs(def_id).clean(cx);
261 for attr in attrs.lists("doc") {
262 if let Some(v) = attr.value_str() {
263 if attr.check_name("primitive") {
264 prim = PrimitiveType::from_str(&v.as_str());
268 // FIXME: should warn on unknown primitives?
272 return prim.map(|p| (def_id, p, attrs));
276 let primitives = if root.is_local() {
277 cx.tcx.hir.krate().module.item_ids.iter().filter_map(|&id| {
278 let item = cx.tcx.hir.expect_item(id.id);
280 hir::ItemKind::Mod(_) => {
281 as_primitive(Def::Mod(cx.tcx.hir.local_def_id(id.id)))
283 hir::ItemKind::Use(ref path, hir::UseKind::Single)
284 if item.vis.node.is_pub() => {
285 as_primitive(path.def).map(|(_, prim, attrs)| {
286 // Pretend the primitive is local.
287 (cx.tcx.hir.local_def_id(id.id), prim, attrs)
294 cx.tcx.item_children(root).iter().map(|item| item.def)
295 .filter_map(as_primitive).collect()
298 let as_keyword = |def: Def| {
299 if let Def::Mod(def_id) = def {
300 let attrs = cx.tcx.get_attrs(def_id).clean(cx);
301 let mut keyword = None;
302 for attr in attrs.lists("doc") {
303 if let Some(v) = attr.value_str() {
304 if attr.check_name("keyword") {
305 keyword = Keyword::from_str(&v.as_str()).ok()
306 .map(|x| x.name().to_string());
307 if keyword.is_some() {
310 // FIXME: should warn on unknown keywords?
314 return keyword.map(|p| (def_id, p, attrs));
318 let keywords = if root.is_local() {
319 cx.tcx.hir.krate().module.item_ids.iter().filter_map(|&id| {
320 let item = cx.tcx.hir.expect_item(id.id);
322 hir::ItemKind::Mod(_) => {
323 as_keyword(Def::Mod(cx.tcx.hir.local_def_id(id.id)))
325 hir::ItemKind::Use(ref path, hir::UseKind::Single)
326 if item.vis.node.is_pub() => {
327 as_keyword(path.def).map(|(_, prim, attrs)| {
328 (cx.tcx.hir.local_def_id(id.id), prim, attrs)
335 cx.tcx.item_children(root).iter().map(|item| item.def)
336 .filter_map(as_keyword).collect()
340 name: cx.tcx.crate_name(*self).to_string(),
342 attrs: cx.tcx.get_attrs(root).clean(cx),
349 /// Anything with a source location and set of attributes and, optionally, a
350 /// name. That is, anything that can be documented. This doesn't correspond
351 /// directly to the AST's concept of an item; it's a strict superset.
352 #[derive(Clone, RustcEncodable, RustcDecodable)]
356 /// Not everything has a name. E.g., impls
357 pub name: Option<String>,
358 pub attrs: Attributes,
360 pub visibility: Option<Visibility>,
362 pub stability: Option<Stability>,
363 pub deprecation: Option<Deprecation>,
366 impl fmt::Debug for Item {
367 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
369 let fake = MAX_DEF_ID.with(|m| m.borrow().get(&self.def_id.krate)
370 .map(|id| self.def_id >= *id).unwrap_or(false));
371 let def_id: &dyn fmt::Debug = if fake { &"**FAKE**" } else { &self.def_id };
373 fmt.debug_struct("Item")
374 .field("source", &self.source)
375 .field("name", &self.name)
376 .field("attrs", &self.attrs)
377 .field("inner", &self.inner)
378 .field("visibility", &self.visibility)
379 .field("def_id", def_id)
380 .field("stability", &self.stability)
381 .field("deprecation", &self.deprecation)
387 /// Finds the `doc` attribute as a NameValue and returns the corresponding
389 pub fn doc_value<'a>(&'a self) -> Option<&'a str> {
390 self.attrs.doc_value()
392 /// Finds all `doc` attributes as NameValues and returns their corresponding values, joined
394 pub fn collapsed_doc_value(&self) -> Option<String> {
395 self.attrs.collapsed_doc_value()
398 pub fn links(&self) -> Vec<(String, String)> {
399 self.attrs.links(&self.def_id.krate)
402 pub fn is_crate(&self) -> bool {
404 StrippedItem(box ModuleItem(Module { is_crate: true, ..})) |
405 ModuleItem(Module { is_crate: true, ..}) => true,
409 pub fn is_mod(&self) -> bool {
410 self.type_() == ItemType::Module
412 pub fn is_trait(&self) -> bool {
413 self.type_() == ItemType::Trait
415 pub fn is_struct(&self) -> bool {
416 self.type_() == ItemType::Struct
418 pub fn is_enum(&self) -> bool {
419 self.type_() == ItemType::Enum
421 pub fn is_associated_type(&self) -> bool {
422 self.type_() == ItemType::AssociatedType
424 pub fn is_associated_const(&self) -> bool {
425 self.type_() == ItemType::AssociatedConst
427 pub fn is_method(&self) -> bool {
428 self.type_() == ItemType::Method
430 pub fn is_ty_method(&self) -> bool {
431 self.type_() == ItemType::TyMethod
433 pub fn is_typedef(&self) -> bool {
434 self.type_() == ItemType::Typedef
436 pub fn is_primitive(&self) -> bool {
437 self.type_() == ItemType::Primitive
439 pub fn is_union(&self) -> bool {
440 self.type_() == ItemType::Union
442 pub fn is_import(&self) -> bool {
443 self.type_() == ItemType::Import
445 pub fn is_extern_crate(&self) -> bool {
446 self.type_() == ItemType::ExternCrate
448 pub fn is_keyword(&self) -> bool {
449 self.type_() == ItemType::Keyword
452 pub fn is_stripped(&self) -> bool {
453 match self.inner { StrippedItem(..) => true, _ => false }
455 pub fn has_stripped_fields(&self) -> Option<bool> {
457 StructItem(ref _struct) => Some(_struct.fields_stripped),
458 UnionItem(ref union) => Some(union.fields_stripped),
459 VariantItem(Variant { kind: VariantKind::Struct(ref vstruct)} ) => {
460 Some(vstruct.fields_stripped)
466 pub fn stability_class(&self) -> Option<String> {
467 self.stability.as_ref().and_then(|ref s| {
468 let mut classes = Vec::with_capacity(2);
470 if s.level == stability::Unstable {
471 classes.push("unstable");
474 if !s.deprecated_since.is_empty() {
475 classes.push("deprecated");
478 if classes.len() != 0 {
479 Some(classes.join(" "))
486 pub fn stable_since(&self) -> Option<&str> {
487 self.stability.as_ref().map(|s| &s.since[..])
490 pub fn is_non_exhaustive(&self) -> bool {
491 self.attrs.other_attrs.iter()
492 .any(|a| a.name().as_str() == "non_exhaustive")
495 /// Returns a documentation-level item type from the item.
496 pub fn type_(&self) -> ItemType {
501 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
503 ExternCrateItem(String, Option<String>),
508 FunctionItem(Function),
510 TypedefItem(Typedef, bool /* is associated type */),
511 ExistentialItem(Existential, bool /* is associated type */),
513 ConstantItem(Constant),
516 /// A method signature only. Used for required methods in traits (ie,
517 /// non-default-methods).
518 TyMethodItem(TyMethod),
519 /// A method with a body.
521 StructFieldItem(Type),
522 VariantItem(Variant),
523 /// `fn`s from an extern block
524 ForeignFunctionItem(Function),
525 /// `static`s from an extern block
526 ForeignStaticItem(Static),
527 /// `type`s from an extern block
530 ProcMacroItem(ProcMacro),
531 PrimitiveItem(PrimitiveType),
532 AssociatedConstItem(Type, Option<String>),
533 AssociatedTypeItem(Vec<GenericBound>, Option<Type>),
534 /// An item that has been stripped by a rustdoc pass
535 StrippedItem(Box<ItemEnum>),
540 pub fn generics(&self) -> Option<&Generics> {
542 ItemEnum::StructItem(ref s) => &s.generics,
543 ItemEnum::EnumItem(ref e) => &e.generics,
544 ItemEnum::FunctionItem(ref f) => &f.generics,
545 ItemEnum::TypedefItem(ref t, _) => &t.generics,
546 ItemEnum::ExistentialItem(ref t, _) => &t.generics,
547 ItemEnum::TraitItem(ref t) => &t.generics,
548 ItemEnum::ImplItem(ref i) => &i.generics,
549 ItemEnum::TyMethodItem(ref i) => &i.generics,
550 ItemEnum::MethodItem(ref i) => &i.generics,
551 ItemEnum::ForeignFunctionItem(ref f) => &f.generics,
556 pub fn is_associated(&self) -> bool {
558 ItemEnum::TypedefItem(_, _) |
559 ItemEnum::AssociatedTypeItem(_, _) => true,
565 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
567 pub items: Vec<Item>,
571 impl Clean<Item> for doctree::Module {
572 fn clean(&self, cx: &DocContext) -> Item {
573 let name = if self.name.is_some() {
574 self.name.expect("No name provided").clean(cx)
579 // maintain a stack of mod ids, for doc comment path resolution
580 // but we also need to resolve the module's own docs based on whether its docs were written
581 // inside or outside the module, so check for that
582 let attrs = self.attrs.clean(cx);
584 let mut items: Vec<Item> = vec![];
585 items.extend(self.extern_crates.iter().map(|x| x.clean(cx)));
586 items.extend(self.imports.iter().flat_map(|x| x.clean(cx)));
587 items.extend(self.structs.iter().map(|x| x.clean(cx)));
588 items.extend(self.unions.iter().map(|x| x.clean(cx)));
589 items.extend(self.enums.iter().map(|x| x.clean(cx)));
590 items.extend(self.fns.iter().map(|x| x.clean(cx)));
591 items.extend(self.foreigns.iter().flat_map(|x| x.clean(cx)));
592 items.extend(self.mods.iter().map(|x| x.clean(cx)));
593 items.extend(self.typedefs.iter().map(|x| x.clean(cx)));
594 items.extend(self.existentials.iter().map(|x| x.clean(cx)));
595 items.extend(self.statics.iter().map(|x| x.clean(cx)));
596 items.extend(self.constants.iter().map(|x| x.clean(cx)));
597 items.extend(self.traits.iter().map(|x| x.clean(cx)));
598 items.extend(self.impls.iter().flat_map(|x| x.clean(cx)));
599 items.extend(self.macros.iter().map(|x| x.clean(cx)));
600 items.extend(self.proc_macros.iter().map(|x| x.clean(cx)));
602 // determine if we should display the inner contents or
603 // the outer `mod` item for the source code.
605 let cm = cx.sess().source_map();
606 let outer = cm.lookup_char_pos(self.where_outer.lo());
607 let inner = cm.lookup_char_pos(self.where_inner.lo());
608 if outer.file.start_pos == inner.file.start_pos {
612 // mod foo; (and a separate SourceFile for the contents)
620 source: whence.clean(cx),
621 visibility: self.vis.clean(cx),
622 stability: self.stab.clean(cx),
623 deprecation: self.depr.clean(cx),
624 def_id: cx.tcx.hir.local_def_id(self.id),
625 inner: ModuleItem(Module {
626 is_crate: self.is_crate,
633 pub struct ListAttributesIter<'a> {
634 attrs: slice::Iter<'a, ast::Attribute>,
635 current_list: vec::IntoIter<ast::NestedMetaItem>,
639 impl<'a> Iterator for ListAttributesIter<'a> {
640 type Item = ast::NestedMetaItem;
642 fn next(&mut self) -> Option<Self::Item> {
643 if let Some(nested) = self.current_list.next() {
647 for attr in &mut self.attrs {
648 if let Some(list) = attr.meta_item_list() {
649 if attr.check_name(self.name) {
650 self.current_list = list.into_iter();
651 if let Some(nested) = self.current_list.next() {
661 fn size_hint(&self) -> (usize, Option<usize>) {
662 let lower = self.current_list.len();
667 pub trait AttributesExt {
668 /// Finds an attribute as List and returns the list of attributes nested inside.
669 fn lists<'a>(&'a self, name: &'a str) -> ListAttributesIter<'a>;
672 impl AttributesExt for [ast::Attribute] {
673 fn lists<'a>(&'a self, name: &'a str) -> ListAttributesIter<'a> {
676 current_list: Vec::new().into_iter(),
682 pub trait NestedAttributesExt {
683 /// Returns whether the attribute list contains a specific `Word`
684 fn has_word(self, word: &str) -> bool;
687 impl<I: IntoIterator<Item=ast::NestedMetaItem>> NestedAttributesExt for I {
688 fn has_word(self, word: &str) -> bool {
689 self.into_iter().any(|attr| attr.is_word() && attr.check_name(word))
693 /// A portion of documentation, extracted from a `#[doc]` attribute.
695 /// Each variant contains the line number within the complete doc-comment where the fragment
696 /// starts, as well as the Span where the corresponding doc comment or attribute is located.
698 /// Included files are kept separate from inline doc comments so that proper line-number
699 /// information can be given when a doctest fails. Sugared doc comments and "raw" doc comments are
700 /// kept separate because of issue #42760.
701 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
702 pub enum DocFragment {
703 // FIXME #44229 (misdreavus): sugared and raw doc comments can be brought back together once
704 // hoedown is completely removed from rustdoc.
705 /// A doc fragment created from a `///` or `//!` doc comment.
706 SugaredDoc(usize, syntax_pos::Span, String),
707 /// A doc fragment created from a "raw" `#[doc=""]` attribute.
708 RawDoc(usize, syntax_pos::Span, String),
709 /// A doc fragment created from a `#[doc(include="filename")]` attribute. Contains both the
710 /// given filename and the file contents.
711 Include(usize, syntax_pos::Span, String, String),
715 pub fn as_str(&self) -> &str {
717 DocFragment::SugaredDoc(_, _, ref s) => &s[..],
718 DocFragment::RawDoc(_, _, ref s) => &s[..],
719 DocFragment::Include(_, _, _, ref s) => &s[..],
723 pub fn span(&self) -> syntax_pos::Span {
725 DocFragment::SugaredDoc(_, span, _) |
726 DocFragment::RawDoc(_, span, _) |
727 DocFragment::Include(_, span, _, _) => span,
732 impl<'a> FromIterator<&'a DocFragment> for String {
733 fn from_iter<T>(iter: T) -> Self
735 T: IntoIterator<Item = &'a DocFragment>
737 iter.into_iter().fold(String::new(), |mut acc, frag| {
742 DocFragment::SugaredDoc(_, _, ref docs)
743 | DocFragment::RawDoc(_, _, ref docs)
744 | DocFragment::Include(_, _, _, ref docs) =>
753 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, Default)]
754 pub struct Attributes {
755 pub doc_strings: Vec<DocFragment>,
756 pub other_attrs: Vec<ast::Attribute>,
757 pub cfg: Option<Arc<Cfg>>,
758 pub span: Option<syntax_pos::Span>,
759 /// map from Rust paths to resolved defs and potential URL fragments
760 pub links: Vec<(String, Option<DefId>, Option<String>)>,
761 pub inner_docs: bool,
765 /// Extracts the content from an attribute `#[doc(cfg(content))]`.
766 fn extract_cfg(mi: &ast::MetaItem) -> Option<&ast::MetaItem> {
767 use syntax::ast::NestedMetaItemKind::MetaItem;
769 if let ast::MetaItemKind::List(ref nmis) = mi.node {
771 if let MetaItem(ref cfg_mi) = nmis[0].node {
772 if cfg_mi.check_name("cfg") {
773 if let ast::MetaItemKind::List(ref cfg_nmis) = cfg_mi.node {
774 if cfg_nmis.len() == 1 {
775 if let MetaItem(ref content_mi) = cfg_nmis[0].node {
776 return Some(content_mi);
788 /// Reads a `MetaItem` from within an attribute, looks for whether it is a
789 /// `#[doc(include="file")]`, and returns the filename and contents of the file as loaded from
791 fn extract_include(mi: &ast::MetaItem)
792 -> Option<(String, String)>
794 mi.meta_item_list().and_then(|list| {
796 if meta.check_name("include") {
797 // the actual compiled `#[doc(include="filename")]` gets expanded to
798 // `#[doc(include(file="filename", contents="file contents")]` so we need to
799 // look for that instead
800 return meta.meta_item_list().and_then(|list| {
801 let mut filename: Option<String> = None;
802 let mut contents: Option<String> = None;
805 if it.check_name("file") {
806 if let Some(name) = it.value_str() {
807 filename = Some(name.to_string());
809 } else if it.check_name("contents") {
810 if let Some(docs) = it.value_str() {
811 contents = Some(docs.to_string());
816 if let (Some(filename), Some(contents)) = (filename, contents) {
817 Some((filename, contents))
829 pub fn has_doc_flag(&self, flag: &str) -> bool {
830 for attr in &self.other_attrs {
831 if !attr.check_name("doc") { continue; }
833 if let Some(items) = attr.meta_item_list() {
834 if items.iter().filter_map(|i| i.meta_item()).any(|it| it.check_name(flag)) {
843 pub fn from_ast(diagnostic: &::errors::Handler,
844 attrs: &[ast::Attribute]) -> Attributes {
845 let mut doc_strings = vec![];
847 let mut cfg = Cfg::True;
848 let mut doc_line = 0;
850 let other_attrs = attrs.iter().filter_map(|attr| {
851 attr.with_desugared_doc(|attr| {
852 if attr.check_name("doc") {
853 if let Some(mi) = attr.meta() {
854 if let Some(value) = mi.value_str() {
855 // Extracted #[doc = "..."]
856 let value = value.to_string();
858 doc_line += value.lines().count();
860 if attr.is_sugared_doc {
861 doc_strings.push(DocFragment::SugaredDoc(line, attr.span, value));
863 doc_strings.push(DocFragment::RawDoc(line, attr.span, value));
867 sp = Some(attr.span);
870 } else if let Some(cfg_mi) = Attributes::extract_cfg(&mi) {
871 // Extracted #[doc(cfg(...))]
872 match Cfg::parse(cfg_mi) {
873 Ok(new_cfg) => cfg &= new_cfg,
874 Err(e) => diagnostic.span_err(e.span, e.msg),
877 } else if let Some((filename, contents)) = Attributes::extract_include(&mi)
880 doc_line += contents.lines().count();
881 doc_strings.push(DocFragment::Include(line,
892 // treat #[target_feature(enable = "feat")] attributes as if they were
893 // #[doc(cfg(target_feature = "feat"))] attributes as well
894 for attr in attrs.lists("target_feature") {
895 if attr.check_name("enable") {
896 if let Some(feat) = attr.value_str() {
897 let meta = attr::mk_name_value_item_str(Ident::from_str("target_feature"),
898 dummy_spanned(feat));
899 if let Ok(feat_cfg) = Cfg::parse(&meta) {
906 let inner_docs = attrs.iter()
907 .filter(|a| a.check_name("doc"))
909 .map_or(true, |a| a.style == AttrStyle::Inner);
914 cfg: if cfg == Cfg::True { None } else { Some(Arc::new(cfg)) },
921 /// Finds the `doc` attribute as a NameValue and returns the corresponding
923 pub fn doc_value<'a>(&'a self) -> Option<&'a str> {
924 self.doc_strings.first().map(|s| s.as_str())
927 /// Finds all `doc` attributes as NameValues and returns their corresponding values, joined
929 pub fn collapsed_doc_value(&self) -> Option<String> {
930 if !self.doc_strings.is_empty() {
931 Some(self.doc_strings.iter().collect())
937 /// Get links as a vector
939 /// Cache must be populated before call
940 pub fn links(&self, krate: &CrateNum) -> Vec<(String, String)> {
941 use html::format::href;
942 self.links.iter().filter_map(|&(ref s, did, ref fragment)| {
945 if let Some((mut href, ..)) = href(did) {
946 if let Some(ref fragment) = *fragment {
948 href.push_str(fragment);
950 Some((s.clone(), href))
956 if let Some(ref fragment) = *fragment {
958 let url = match cache.extern_locations.get(krate) {
959 Some(&(_, ref src, ExternalLocation::Local)) =>
960 src.to_str().expect("invalid file path"),
961 Some(&(_, _, ExternalLocation::Remote(ref s))) => s,
962 Some(&(_, _, ExternalLocation::Unknown)) | None =>
963 "https://doc.rust-lang.org/nightly",
965 // This is a primitive so the url is done "by hand".
967 format!("{}{}std/primitive.{}.html",
969 if !url.ends_with('/') { "/" } else { "" },
972 panic!("This isn't a primitive?!");
980 impl PartialEq for Attributes {
981 fn eq(&self, rhs: &Self) -> bool {
982 self.doc_strings == rhs.doc_strings &&
983 self.cfg == rhs.cfg &&
984 self.span == rhs.span &&
985 self.links == rhs.links &&
986 self.other_attrs.iter().map(|attr| attr.id).eq(rhs.other_attrs.iter().map(|attr| attr.id))
990 impl Eq for Attributes {}
992 impl Hash for Attributes {
993 fn hash<H: Hasher>(&self, hasher: &mut H) {
994 self.doc_strings.hash(hasher);
995 self.cfg.hash(hasher);
996 self.span.hash(hasher);
997 self.links.hash(hasher);
998 for attr in &self.other_attrs {
999 attr.id.hash(hasher);
1004 impl AttributesExt for Attributes {
1005 fn lists<'a>(&'a self, name: &'a str) -> ListAttributesIter<'a> {
1006 self.other_attrs.lists(name)
1010 impl Clean<Attributes> for [ast::Attribute] {
1011 fn clean(&self, cx: &DocContext) -> Attributes {
1012 Attributes::from_ast(cx.sess().diagnostic(), self)
1016 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1017 pub enum GenericBound {
1018 TraitBound(PolyTrait, hir::TraitBoundModifier),
1023 fn maybe_sized(cx: &DocContext) -> GenericBound {
1024 let did = cx.tcx.require_lang_item(lang_items::SizedTraitLangItem);
1025 let empty = cx.tcx.intern_substs(&[]);
1026 let path = external_path(cx, &cx.tcx.item_name(did).as_str(),
1027 Some(did), false, vec![], empty);
1028 inline::record_extern_fqn(cx, did, TypeKind::Trait);
1029 GenericBound::TraitBound(PolyTrait {
1030 trait_: ResolvedPath {
1036 generic_params: Vec::new(),
1037 }, hir::TraitBoundModifier::Maybe)
1040 fn is_sized_bound(&self, cx: &DocContext) -> bool {
1041 use rustc::hir::TraitBoundModifier as TBM;
1042 if let GenericBound::TraitBound(PolyTrait { ref trait_, .. }, TBM::None) = *self {
1043 if trait_.def_id() == cx.tcx.lang_items().sized_trait() {
1050 fn get_poly_trait(&self) -> Option<PolyTrait> {
1051 if let GenericBound::TraitBound(ref p, _) = *self {
1052 return Some(p.clone())
1057 fn get_trait_type(&self) -> Option<Type> {
1058 if let GenericBound::TraitBound(PolyTrait { ref trait_, .. }, _) = *self {
1059 return Some(trait_.clone());
1065 impl Clean<GenericBound> for hir::GenericBound {
1066 fn clean(&self, cx: &DocContext) -> GenericBound {
1068 hir::GenericBound::Outlives(lt) => GenericBound::Outlives(lt.clean(cx)),
1069 hir::GenericBound::Trait(ref t, modifier) => {
1070 GenericBound::TraitBound(t.clean(cx), modifier)
1076 fn external_generic_args(cx: &DocContext, trait_did: Option<DefId>, has_self: bool,
1077 bindings: Vec<TypeBinding>, substs: &Substs) -> GenericArgs {
1078 let lifetimes = substs.regions().filter_map(|v| v.clean(cx)).collect();
1079 let types = substs.types().skip(has_self as usize).collect::<Vec<_>>();
1082 // Attempt to sugar an external path like Fn<(A, B,), C> to Fn(A, B) -> C
1083 Some(did) if cx.tcx.lang_items().fn_trait_kind(did).is_some() => {
1084 assert_eq!(types.len(), 1);
1085 let inputs = match types[0].sty {
1086 ty::Tuple(ref tys) => tys.iter().map(|t| t.clean(cx)).collect(),
1088 return GenericArgs::AngleBracketed {
1090 types: types.clean(cx),
1096 // FIXME(#20299) return type comes from a projection now
1097 // match types[1].sty {
1098 // ty::Tuple(ref v) if v.is_empty() => None, // -> ()
1099 // _ => Some(types[1].clean(cx))
1101 GenericArgs::Parenthesized {
1107 GenericArgs::AngleBracketed {
1109 types: types.clean(cx),
1116 // trait_did should be set to a trait's DefId if called on a TraitRef, in order to sugar
1117 // from Fn<(A, B,), C> to Fn(A, B) -> C
1118 fn external_path(cx: &DocContext, name: &str, trait_did: Option<DefId>, has_self: bool,
1119 bindings: Vec<TypeBinding>, substs: &Substs) -> Path {
1123 segments: vec![PathSegment {
1124 name: name.to_string(),
1125 args: external_generic_args(cx, trait_did, has_self, bindings, substs)
1130 impl<'a, 'tcx> Clean<GenericBound> for (&'a ty::TraitRef<'tcx>, Vec<TypeBinding>) {
1131 fn clean(&self, cx: &DocContext) -> GenericBound {
1132 let (trait_ref, ref bounds) = *self;
1133 inline::record_extern_fqn(cx, trait_ref.def_id, TypeKind::Trait);
1134 let path = external_path(cx, &cx.tcx.item_name(trait_ref.def_id).as_str(),
1135 Some(trait_ref.def_id), true, bounds.clone(), trait_ref.substs);
1137 debug!("ty::TraitRef\n subst: {:?}\n", trait_ref.substs);
1139 // collect any late bound regions
1140 let mut late_bounds = vec![];
1141 for ty_s in trait_ref.input_types().skip(1) {
1142 if let ty::Tuple(ts) = ty_s.sty {
1144 if let ty::Ref(ref reg, _, _) = ty_s.sty {
1145 if let &ty::RegionKind::ReLateBound(..) = *reg {
1146 debug!(" hit an ReLateBound {:?}", reg);
1147 if let Some(Lifetime(name)) = reg.clean(cx) {
1148 late_bounds.push(GenericParamDef {
1150 kind: GenericParamDefKind::Lifetime,
1159 GenericBound::TraitBound(
1161 trait_: ResolvedPath {
1164 did: trait_ref.def_id,
1167 generic_params: late_bounds,
1169 hir::TraitBoundModifier::None
1174 impl<'tcx> Clean<GenericBound> for ty::TraitRef<'tcx> {
1175 fn clean(&self, cx: &DocContext) -> GenericBound {
1176 (self, vec![]).clean(cx)
1180 impl<'tcx> Clean<Option<Vec<GenericBound>>> for Substs<'tcx> {
1181 fn clean(&self, cx: &DocContext) -> Option<Vec<GenericBound>> {
1182 let mut v = Vec::new();
1183 v.extend(self.regions().filter_map(|r| r.clean(cx)).map(GenericBound::Outlives));
1184 v.extend(self.types().map(|t| GenericBound::TraitBound(PolyTrait {
1185 trait_: t.clean(cx),
1186 generic_params: Vec::new(),
1187 }, hir::TraitBoundModifier::None)));
1188 if !v.is_empty() {Some(v)} else {None}
1192 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1193 pub struct Lifetime(String);
1196 pub fn get_ref<'a>(&'a self) -> &'a str {
1197 let Lifetime(ref s) = *self;
1202 pub fn statik() -> Lifetime {
1203 Lifetime("'static".to_string())
1207 impl Clean<Lifetime> for hir::Lifetime {
1208 fn clean(&self, cx: &DocContext) -> Lifetime {
1209 if self.id != ast::DUMMY_NODE_ID {
1210 let hir_id = cx.tcx.hir.node_to_hir_id(self.id);
1211 let def = cx.tcx.named_region(hir_id);
1213 Some(rl::Region::EarlyBound(_, node_id, _)) |
1214 Some(rl::Region::LateBound(_, node_id, _)) |
1215 Some(rl::Region::Free(_, node_id)) => {
1216 if let Some(lt) = cx.lt_substs.borrow().get(&node_id).cloned() {
1223 Lifetime(self.name.ident().to_string())
1227 impl Clean<Lifetime> for hir::GenericParam {
1228 fn clean(&self, _: &DocContext) -> Lifetime {
1230 hir::GenericParamKind::Lifetime { .. } => {
1231 if self.bounds.len() > 0 {
1232 let mut bounds = self.bounds.iter().map(|bound| match bound {
1233 hir::GenericBound::Outlives(lt) => lt,
1236 let name = bounds.next().expect("no more bounds").name.ident();
1237 let mut s = format!("{}: {}", self.name.ident(), name);
1238 for bound in bounds {
1239 s.push_str(&format!(" + {}", bound.name.ident()));
1243 Lifetime(self.name.ident().to_string())
1251 impl<'tcx> Clean<Lifetime> for ty::GenericParamDef {
1252 fn clean(&self, _cx: &DocContext) -> Lifetime {
1253 Lifetime(self.name.to_string())
1257 impl Clean<Option<Lifetime>> for ty::RegionKind {
1258 fn clean(&self, cx: &DocContext) -> Option<Lifetime> {
1260 ty::ReStatic => Some(Lifetime::statik()),
1261 ty::ReLateBound(_, ty::BrNamed(_, name)) => Some(Lifetime(name.to_string())),
1262 ty::ReEarlyBound(ref data) => Some(Lifetime(data.name.clean(cx))),
1264 ty::ReLateBound(..) |
1268 ty::RePlaceholder(..) |
1270 ty::ReClosureBound(_) |
1271 ty::ReCanonical(_) |
1272 ty::ReErased => None
1277 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1278 pub enum WherePredicate {
1279 BoundPredicate { ty: Type, bounds: Vec<GenericBound> },
1280 RegionPredicate { lifetime: Lifetime, bounds: Vec<GenericBound> },
1281 EqPredicate { lhs: Type, rhs: Type },
1284 impl Clean<WherePredicate> for hir::WherePredicate {
1285 fn clean(&self, cx: &DocContext) -> WherePredicate {
1287 hir::WherePredicate::BoundPredicate(ref wbp) => {
1288 WherePredicate::BoundPredicate {
1289 ty: wbp.bounded_ty.clean(cx),
1290 bounds: wbp.bounds.clean(cx)
1294 hir::WherePredicate::RegionPredicate(ref wrp) => {
1295 WherePredicate::RegionPredicate {
1296 lifetime: wrp.lifetime.clean(cx),
1297 bounds: wrp.bounds.clean(cx)
1301 hir::WherePredicate::EqPredicate(ref wrp) => {
1302 WherePredicate::EqPredicate {
1303 lhs: wrp.lhs_ty.clean(cx),
1304 rhs: wrp.rhs_ty.clean(cx)
1311 impl<'a> Clean<WherePredicate> for ty::Predicate<'a> {
1312 fn clean(&self, cx: &DocContext) -> WherePredicate {
1313 use rustc::ty::Predicate;
1316 Predicate::Trait(ref pred) => pred.clean(cx),
1317 Predicate::Subtype(ref pred) => pred.clean(cx),
1318 Predicate::RegionOutlives(ref pred) => pred.clean(cx),
1319 Predicate::TypeOutlives(ref pred) => pred.clean(cx),
1320 Predicate::Projection(ref pred) => pred.clean(cx),
1321 Predicate::WellFormed(ty) => {
1322 // This comes from `where Ty:` (i.e. no bounds) (see #53696).
1323 WherePredicate::BoundPredicate {
1328 Predicate::ObjectSafe(_) => panic!("not user writable"),
1329 Predicate::ClosureKind(..) => panic!("not user writable"),
1330 Predicate::ConstEvaluatable(..) => panic!("not user writable"),
1335 impl<'a> Clean<WherePredicate> for ty::TraitPredicate<'a> {
1336 fn clean(&self, cx: &DocContext) -> WherePredicate {
1337 WherePredicate::BoundPredicate {
1338 ty: self.trait_ref.self_ty().clean(cx),
1339 bounds: vec![self.trait_ref.clean(cx)]
1344 impl<'tcx> Clean<WherePredicate> for ty::SubtypePredicate<'tcx> {
1345 fn clean(&self, _cx: &DocContext) -> WherePredicate {
1346 panic!("subtype predicates are an internal rustc artifact \
1347 and should not be seen by rustdoc")
1351 impl<'tcx> Clean<WherePredicate> for ty::OutlivesPredicate<ty::Region<'tcx>, ty::Region<'tcx>> {
1352 fn clean(&self, cx: &DocContext) -> WherePredicate {
1353 let ty::OutlivesPredicate(ref a, ref b) = *self;
1354 WherePredicate::RegionPredicate {
1355 lifetime: a.clean(cx).expect("failed to clean lifetime"),
1356 bounds: vec![GenericBound::Outlives(b.clean(cx).expect("failed to clean bounds"))]
1361 impl<'tcx> Clean<WherePredicate> for ty::OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>> {
1362 fn clean(&self, cx: &DocContext) -> WherePredicate {
1363 let ty::OutlivesPredicate(ref ty, ref lt) = *self;
1365 WherePredicate::BoundPredicate {
1367 bounds: vec![GenericBound::Outlives(lt.clean(cx).expect("failed to clean lifetimes"))]
1372 impl<'tcx> Clean<WherePredicate> for ty::ProjectionPredicate<'tcx> {
1373 fn clean(&self, cx: &DocContext) -> WherePredicate {
1374 WherePredicate::EqPredicate {
1375 lhs: self.projection_ty.clean(cx),
1376 rhs: self.ty.clean(cx)
1381 impl<'tcx> Clean<Type> for ty::ProjectionTy<'tcx> {
1382 fn clean(&self, cx: &DocContext) -> Type {
1383 let trait_ = match self.trait_ref(cx.tcx).clean(cx) {
1384 GenericBound::TraitBound(t, _) => t.trait_,
1385 GenericBound::Outlives(_) => panic!("cleaning a trait got a lifetime"),
1388 name: cx.tcx.associated_item(self.item_def_id).ident.name.clean(cx),
1389 self_type: box self.self_ty().clean(cx),
1395 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1396 pub enum GenericParamDefKind {
1400 bounds: Vec<GenericBound>,
1401 default: Option<Type>,
1402 synthetic: Option<hir::SyntheticTyParamKind>,
1406 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1407 pub struct GenericParamDef {
1410 pub kind: GenericParamDefKind,
1413 impl GenericParamDef {
1414 pub fn is_synthetic_type_param(&self) -> bool {
1416 GenericParamDefKind::Lifetime => false,
1417 GenericParamDefKind::Type { ref synthetic, .. } => synthetic.is_some(),
1422 impl<'tcx> Clean<GenericParamDef> for ty::GenericParamDef {
1423 fn clean(&self, cx: &DocContext) -> GenericParamDef {
1424 let (name, kind) = match self.kind {
1425 ty::GenericParamDefKind::Lifetime => {
1426 (self.name.to_string(), GenericParamDefKind::Lifetime)
1428 ty::GenericParamDefKind::Type { has_default, .. } => {
1429 cx.renderinfo.borrow_mut().external_typarams
1430 .insert(self.def_id, self.name.clean(cx));
1431 let default = if has_default {
1432 Some(cx.tcx.type_of(self.def_id).clean(cx))
1436 (self.name.clean(cx), GenericParamDefKind::Type {
1438 bounds: vec![], // These are filled in from the where-clauses.
1452 impl Clean<GenericParamDef> for hir::GenericParam {
1453 fn clean(&self, cx: &DocContext) -> GenericParamDef {
1454 let (name, kind) = match self.kind {
1455 hir::GenericParamKind::Lifetime { .. } => {
1456 let name = if self.bounds.len() > 0 {
1457 let mut bounds = self.bounds.iter().map(|bound| match bound {
1458 hir::GenericBound::Outlives(lt) => lt,
1461 let name = bounds.next().expect("no more bounds").name.ident();
1462 let mut s = format!("{}: {}", self.name.ident(), name);
1463 for bound in bounds {
1464 s.push_str(&format!(" + {}", bound.name.ident()));
1468 self.name.ident().to_string()
1470 (name, GenericParamDefKind::Lifetime)
1472 hir::GenericParamKind::Type { ref default, synthetic, .. } => {
1473 (self.name.ident().name.clean(cx), GenericParamDefKind::Type {
1474 did: cx.tcx.hir.local_def_id(self.id),
1475 bounds: self.bounds.clean(cx),
1476 default: default.clean(cx),
1477 synthetic: synthetic,
1489 // maybe use a Generic enum and use Vec<Generic>?
1490 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Default, Hash)]
1491 pub struct Generics {
1492 pub params: Vec<GenericParamDef>,
1493 pub where_predicates: Vec<WherePredicate>,
1496 impl Clean<Generics> for hir::Generics {
1497 fn clean(&self, cx: &DocContext) -> Generics {
1498 // Synthetic type-parameters are inserted after normal ones.
1499 // In order for normal parameters to be able to refer to synthetic ones,
1500 // scans them first.
1501 fn is_impl_trait(param: &hir::GenericParam) -> bool {
1503 hir::GenericParamKind::Type { synthetic, .. } => {
1504 synthetic == Some(hir::SyntheticTyParamKind::ImplTrait)
1509 let impl_trait_params = self.params
1511 .filter(|param| is_impl_trait(param))
1513 let param: GenericParamDef = param.clean(cx);
1515 GenericParamDefKind::Lifetime => unreachable!(),
1516 GenericParamDefKind::Type { did, ref bounds, .. } => {
1517 cx.impl_trait_bounds.borrow_mut().insert(did, bounds.clone());
1522 .collect::<Vec<_>>();
1524 let mut params = Vec::with_capacity(self.params.len());
1525 for p in self.params.iter().filter(|p| !is_impl_trait(p)) {
1526 let p = p.clean(cx);
1529 params.extend(impl_trait_params);
1531 let mut generics = Generics {
1533 where_predicates: self.where_clause.predicates.clean(cx),
1536 // Some duplicates are generated for ?Sized bounds between type params and where
1537 // predicates. The point in here is to move the bounds definitions from type params
1538 // to where predicates when such cases occur.
1539 for where_pred in &mut generics.where_predicates {
1541 WherePredicate::BoundPredicate { ty: Generic(ref name), ref mut bounds } => {
1542 if bounds.is_empty() {
1543 for param in &mut generics.params {
1545 GenericParamDefKind::Lifetime => {}
1546 GenericParamDefKind::Type { bounds: ref mut ty_bounds, .. } => {
1547 if ¶m.name == name {
1548 mem::swap(bounds, ty_bounds);
1563 impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics,
1564 &'a ty::GenericPredicates<'tcx>) {
1565 fn clean(&self, cx: &DocContext) -> Generics {
1566 use self::WherePredicate as WP;
1568 let (gens, preds) = *self;
1570 // Bounds in the type_params and lifetimes fields are repeated in the
1571 // predicates field (see rustc_typeck::collect::ty_generics), so remove
1573 let stripped_typarams = gens.params.iter().filter_map(|param| match param.kind {
1574 ty::GenericParamDefKind::Lifetime => None,
1575 ty::GenericParamDefKind::Type { .. } => {
1576 if param.name == keywords::SelfType.name().as_str() {
1577 assert_eq!(param.index, 0);
1580 Some(param.clean(cx))
1582 }).collect::<Vec<GenericParamDef>>();
1584 let mut where_predicates = preds.predicates.iter()
1585 .map(|(p, _)| p.clean(cx))
1586 .collect::<Vec<_>>();
1588 // Type parameters and have a Sized bound by default unless removed with
1589 // ?Sized. Scan through the predicates and mark any type parameter with
1590 // a Sized bound, removing the bounds as we find them.
1592 // Note that associated types also have a sized bound by default, but we
1593 // don't actually know the set of associated types right here so that's
1594 // handled in cleaning associated types
1595 let mut sized_params = FxHashSet::default();
1596 where_predicates.retain(|pred| {
1598 WP::BoundPredicate { ty: Generic(ref g), ref bounds } => {
1599 if bounds.iter().any(|b| b.is_sized_bound(cx)) {
1600 sized_params.insert(g.clone());
1610 // Run through the type parameters again and insert a ?Sized
1611 // unbound for any we didn't find to be Sized.
1612 for tp in &stripped_typarams {
1613 if !sized_params.contains(&tp.name) {
1614 where_predicates.push(WP::BoundPredicate {
1615 ty: Type::Generic(tp.name.clone()),
1616 bounds: vec![GenericBound::maybe_sized(cx)],
1621 // It would be nice to collect all of the bounds on a type and recombine
1622 // them if possible, to avoid e.g. `where T: Foo, T: Bar, T: Sized, T: 'a`
1623 // and instead see `where T: Foo + Bar + Sized + 'a`
1628 .flat_map(|param| match param.kind {
1629 ty::GenericParamDefKind::Lifetime => Some(param.clean(cx)),
1630 ty::GenericParamDefKind::Type { .. } => None,
1631 }).chain(simplify::ty_params(stripped_typarams).into_iter())
1633 where_predicates: simplify::where_clauses(cx, where_predicates),
1638 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1640 pub generics: Generics,
1642 pub header: hir::FnHeader,
1645 impl<'a> Clean<Method> for (&'a hir::MethodSig, &'a hir::Generics, hir::BodyId) {
1646 fn clean(&self, cx: &DocContext) -> Method {
1647 let (generics, decl) = enter_impl_trait(cx, || {
1648 (self.1.clean(cx), (&*self.0.decl, self.2).clean(cx))
1653 header: self.0.header,
1658 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1659 pub struct TyMethod {
1660 pub header: hir::FnHeader,
1662 pub generics: Generics,
1665 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1666 pub struct Function {
1668 pub generics: Generics,
1669 pub header: hir::FnHeader,
1672 impl Clean<Item> for doctree::Function {
1673 fn clean(&self, cx: &DocContext) -> Item {
1674 let (generics, decl) = enter_impl_trait(cx, || {
1675 (self.generics.clean(cx), (&self.decl, self.body).clean(cx))
1679 name: Some(self.name.clean(cx)),
1680 attrs: self.attrs.clean(cx),
1681 source: self.whence.clean(cx),
1682 visibility: self.vis.clean(cx),
1683 stability: self.stab.clean(cx),
1684 deprecation: self.depr.clean(cx),
1685 def_id: cx.tcx.hir.local_def_id(self.id),
1686 inner: FunctionItem(Function {
1689 header: self.header,
1695 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1697 pub inputs: Arguments,
1698 pub output: FunctionRetTy,
1700 pub attrs: Attributes,
1704 pub fn self_type(&self) -> Option<SelfTy> {
1705 self.inputs.values.get(0).and_then(|v| v.to_self())
1709 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1710 pub struct Arguments {
1711 pub values: Vec<Argument>,
1714 impl<'a> Clean<Arguments> for (&'a [hir::Ty], &'a [ast::Ident]) {
1715 fn clean(&self, cx: &DocContext) -> Arguments {
1717 values: self.0.iter().enumerate().map(|(i, ty)| {
1718 let mut name = self.1.get(i).map(|ident| ident.to_string())
1719 .unwrap_or(String::new());
1720 if name.is_empty() {
1721 name = "_".to_string();
1725 type_: ty.clean(cx),
1732 impl<'a> Clean<Arguments> for (&'a [hir::Ty], hir::BodyId) {
1733 fn clean(&self, cx: &DocContext) -> Arguments {
1734 let body = cx.tcx.hir.body(self.1);
1737 values: self.0.iter().enumerate().map(|(i, ty)| {
1739 name: name_from_pat(&body.arguments[i].pat),
1740 type_: ty.clean(cx),
1747 impl<'a, A: Copy> Clean<FnDecl> for (&'a hir::FnDecl, A)
1748 where (&'a [hir::Ty], A): Clean<Arguments>
1750 fn clean(&self, cx: &DocContext) -> FnDecl {
1752 inputs: (&self.0.inputs[..], self.1).clean(cx),
1753 output: self.0.output.clean(cx),
1754 variadic: self.0.variadic,
1755 attrs: Attributes::default()
1760 impl<'a, 'tcx> Clean<FnDecl> for (DefId, ty::PolyFnSig<'tcx>) {
1761 fn clean(&self, cx: &DocContext) -> FnDecl {
1762 let (did, sig) = *self;
1763 let mut names = if cx.tcx.hir.as_local_node_id(did).is_some() {
1766 cx.tcx.fn_arg_names(did).into_iter()
1770 output: Return(sig.skip_binder().output().clean(cx)),
1771 attrs: Attributes::default(),
1772 variadic: sig.skip_binder().variadic,
1774 values: sig.skip_binder().inputs().iter().map(|t| {
1777 name: names.next().map_or(String::new(), |name| name.to_string()),
1785 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1786 pub struct Argument {
1791 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug)]
1794 SelfBorrowed(Option<Lifetime>, Mutability),
1799 pub fn to_self(&self) -> Option<SelfTy> {
1800 if self.name != "self" {
1803 if self.type_.is_self_type() {
1804 return Some(SelfValue);
1807 BorrowedRef{ref lifetime, mutability, ref type_} if type_.is_self_type() => {
1808 Some(SelfBorrowed(lifetime.clone(), mutability))
1810 _ => Some(SelfExplicit(self.type_.clone()))
1815 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1816 pub enum FunctionRetTy {
1821 impl Clean<FunctionRetTy> for hir::FunctionRetTy {
1822 fn clean(&self, cx: &DocContext) -> FunctionRetTy {
1824 hir::Return(ref typ) => Return(typ.clean(cx)),
1825 hir::DefaultReturn(..) => DefaultReturn,
1830 impl GetDefId for FunctionRetTy {
1831 fn def_id(&self) -> Option<DefId> {
1833 Return(ref ty) => ty.def_id(),
1834 DefaultReturn => None,
1839 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1842 pub unsafety: hir::Unsafety,
1843 pub items: Vec<Item>,
1844 pub generics: Generics,
1845 pub bounds: Vec<GenericBound>,
1846 pub is_spotlight: bool,
1850 impl Clean<Item> for doctree::Trait {
1851 fn clean(&self, cx: &DocContext) -> Item {
1852 let attrs = self.attrs.clean(cx);
1853 let is_spotlight = attrs.has_doc_flag("spotlight");
1855 name: Some(self.name.clean(cx)),
1857 source: self.whence.clean(cx),
1858 def_id: cx.tcx.hir.local_def_id(self.id),
1859 visibility: self.vis.clean(cx),
1860 stability: self.stab.clean(cx),
1861 deprecation: self.depr.clean(cx),
1862 inner: TraitItem(Trait {
1863 auto: self.is_auto.clean(cx),
1864 unsafety: self.unsafety,
1865 items: self.items.clean(cx),
1866 generics: self.generics.clean(cx),
1867 bounds: self.bounds.clean(cx),
1868 is_spotlight: is_spotlight,
1869 is_auto: self.is_auto.clean(cx),
1875 impl Clean<bool> for hir::IsAuto {
1876 fn clean(&self, _: &DocContext) -> bool {
1878 hir::IsAuto::Yes => true,
1879 hir::IsAuto::No => false,
1884 impl Clean<Type> for hir::TraitRef {
1885 fn clean(&self, cx: &DocContext) -> Type {
1886 resolve_type(cx, self.path.clean(cx), self.ref_id)
1890 impl Clean<PolyTrait> for hir::PolyTraitRef {
1891 fn clean(&self, cx: &DocContext) -> PolyTrait {
1893 trait_: self.trait_ref.clean(cx),
1894 generic_params: self.bound_generic_params.clean(cx)
1899 impl Clean<Item> for hir::TraitItem {
1900 fn clean(&self, cx: &DocContext) -> Item {
1901 let inner = match self.node {
1902 hir::TraitItemKind::Const(ref ty, default) => {
1903 AssociatedConstItem(ty.clean(cx),
1904 default.map(|e| print_const_expr(cx, e)))
1906 hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Provided(body)) => {
1907 MethodItem((sig, &self.generics, body).clean(cx))
1909 hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Required(ref names)) => {
1910 let (generics, decl) = enter_impl_trait(cx, || {
1911 (self.generics.clean(cx), (&*sig.decl, &names[..]).clean(cx))
1913 TyMethodItem(TyMethod {
1919 hir::TraitItemKind::Type(ref bounds, ref default) => {
1920 AssociatedTypeItem(bounds.clean(cx), default.clean(cx))
1924 name: Some(self.ident.name.clean(cx)),
1925 attrs: self.attrs.clean(cx),
1926 source: self.span.clean(cx),
1927 def_id: cx.tcx.hir.local_def_id(self.id),
1929 stability: get_stability(cx, cx.tcx.hir.local_def_id(self.id)),
1930 deprecation: get_deprecation(cx, cx.tcx.hir.local_def_id(self.id)),
1936 impl Clean<Item> for hir::ImplItem {
1937 fn clean(&self, cx: &DocContext) -> Item {
1938 let inner = match self.node {
1939 hir::ImplItemKind::Const(ref ty, expr) => {
1940 AssociatedConstItem(ty.clean(cx),
1941 Some(print_const_expr(cx, expr)))
1943 hir::ImplItemKind::Method(ref sig, body) => {
1944 MethodItem((sig, &self.generics, body).clean(cx))
1946 hir::ImplItemKind::Type(ref ty) => TypedefItem(Typedef {
1947 type_: ty.clean(cx),
1948 generics: Generics::default(),
1950 hir::ImplItemKind::Existential(ref bounds) => ExistentialItem(Existential {
1951 bounds: bounds.clean(cx),
1952 generics: Generics::default(),
1956 name: Some(self.ident.name.clean(cx)),
1957 source: self.span.clean(cx),
1958 attrs: self.attrs.clean(cx),
1959 def_id: cx.tcx.hir.local_def_id(self.id),
1960 visibility: self.vis.clean(cx),
1961 stability: get_stability(cx, cx.tcx.hir.local_def_id(self.id)),
1962 deprecation: get_deprecation(cx, cx.tcx.hir.local_def_id(self.id)),
1968 impl<'tcx> Clean<Item> for ty::AssociatedItem {
1969 fn clean(&self, cx: &DocContext) -> Item {
1970 let inner = match self.kind {
1971 ty::AssociatedKind::Const => {
1972 let ty = cx.tcx.type_of(self.def_id);
1973 let default = if self.defaultness.has_value() {
1974 Some(inline::print_inlined_const(cx, self.def_id))
1978 AssociatedConstItem(ty.clean(cx), default)
1980 ty::AssociatedKind::Method => {
1981 let generics = (cx.tcx.generics_of(self.def_id),
1982 &cx.tcx.predicates_of(self.def_id)).clean(cx);
1983 let sig = cx.tcx.fn_sig(self.def_id);
1984 let mut decl = (self.def_id, sig).clean(cx);
1986 if self.method_has_self_argument {
1987 let self_ty = match self.container {
1988 ty::ImplContainer(def_id) => {
1989 cx.tcx.type_of(def_id)
1991 ty::TraitContainer(_) => cx.tcx.mk_self_type()
1993 let self_arg_ty = *sig.input(0).skip_binder();
1994 if self_arg_ty == self_ty {
1995 decl.inputs.values[0].type_ = Generic(String::from("Self"));
1996 } else if let ty::Ref(_, ty, _) = self_arg_ty.sty {
1998 match decl.inputs.values[0].type_ {
1999 BorrowedRef{ref mut type_, ..} => {
2000 **type_ = Generic(String::from("Self"))
2002 _ => unreachable!(),
2008 let provided = match self.container {
2009 ty::ImplContainer(_) => true,
2010 ty::TraitContainer(_) => self.defaultness.has_value()
2013 let constness = if cx.tcx.is_const_fn(self.def_id) {
2014 hir::Constness::Const
2016 hir::Constness::NotConst
2021 header: hir::FnHeader {
2022 unsafety: sig.unsafety(),
2025 asyncness: hir::IsAsync::NotAsync,
2029 TyMethodItem(TyMethod {
2032 header: hir::FnHeader {
2033 unsafety: sig.unsafety(),
2035 constness: hir::Constness::NotConst,
2036 asyncness: hir::IsAsync::NotAsync,
2041 ty::AssociatedKind::Type => {
2042 let my_name = self.ident.name.clean(cx);
2044 if let ty::TraitContainer(did) = self.container {
2045 // When loading a cross-crate associated type, the bounds for this type
2046 // are actually located on the trait/impl itself, so we need to load
2047 // all of the generics from there and then look for bounds that are
2048 // applied to this associated type in question.
2049 let predicates = cx.tcx.predicates_of(did);
2050 let generics = (cx.tcx.generics_of(did), &predicates).clean(cx);
2051 let mut bounds = generics.where_predicates.iter().filter_map(|pred| {
2052 let (name, self_type, trait_, bounds) = match *pred {
2053 WherePredicate::BoundPredicate {
2054 ty: QPath { ref name, ref self_type, ref trait_ },
2056 } => (name, self_type, trait_, bounds),
2059 if *name != my_name { return None }
2061 ResolvedPath { did, .. } if did == self.container.id() => {}
2065 Generic(ref s) if *s == "Self" => {}
2069 }).flat_map(|i| i.iter().cloned()).collect::<Vec<_>>();
2070 // Our Sized/?Sized bound didn't get handled when creating the generics
2071 // because we didn't actually get our whole set of bounds until just now
2072 // (some of them may have come from the trait). If we do have a sized
2073 // bound, we remove it, and if we don't then we add the `?Sized` bound
2075 match bounds.iter().position(|b| b.is_sized_bound(cx)) {
2076 Some(i) => { bounds.remove(i); }
2077 None => bounds.push(GenericBound::maybe_sized(cx)),
2080 let ty = if self.defaultness.has_value() {
2081 Some(cx.tcx.type_of(self.def_id))
2086 AssociatedTypeItem(bounds, ty.clean(cx))
2088 TypedefItem(Typedef {
2089 type_: cx.tcx.type_of(self.def_id).clean(cx),
2090 generics: Generics {
2092 where_predicates: Vec::new(),
2097 ty::AssociatedKind::Existential => unimplemented!(),
2100 let visibility = match self.container {
2101 ty::ImplContainer(_) => self.vis.clean(cx),
2102 ty::TraitContainer(_) => None,
2106 name: Some(self.ident.name.clean(cx)),
2108 stability: get_stability(cx, self.def_id),
2109 deprecation: get_deprecation(cx, self.def_id),
2110 def_id: self.def_id,
2111 attrs: inline::load_attrs(cx, self.def_id),
2112 source: cx.tcx.def_span(self.def_id).clean(cx),
2118 /// A trait reference, which may have higher ranked lifetimes.
2119 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
2120 pub struct PolyTrait {
2122 pub generic_params: Vec<GenericParamDef>,
2125 /// A representation of a Type suitable for hyperlinking purposes. Ideally one can get the original
2126 /// type out of the AST/TyCtxt given one of these, if more information is needed. Most importantly
2127 /// it does not preserve mutability or boxes.
2128 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
2130 /// structs/enums/traits (most that'd be an hir::TyKind::Path)
2133 typarams: Option<Vec<GenericBound>>,
2135 /// true if is a `T::Name` path for associated types
2138 /// For parameterized types, so the consumer of the JSON don't go
2139 /// looking for types which don't exist anywhere.
2141 /// Primitives are the fixed-size numeric types (plus int/usize/float), char,
2142 /// arrays, slices, and tuples.
2143 Primitive(PrimitiveType),
2145 BareFunction(Box<BareFunctionDecl>),
2148 Array(Box<Type>, String),
2151 RawPointer(Mutability, Box<Type>),
2153 lifetime: Option<Lifetime>,
2154 mutability: Mutability,
2158 // <Type as Trait>::Name
2161 self_type: Box<Type>,
2168 // impl TraitA+TraitB
2169 ImplTrait(Vec<GenericBound>),
2172 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Copy, Debug)]
2173 pub enum PrimitiveType {
2174 Isize, I8, I16, I32, I64, I128,
2175 Usize, U8, U16, U32, U64, U128,
2190 #[derive(Clone, RustcEncodable, RustcDecodable, Copy, Debug)]
2208 pub trait GetDefId {
2209 fn def_id(&self) -> Option<DefId>;
2212 impl<T: GetDefId> GetDefId for Option<T> {
2213 fn def_id(&self) -> Option<DefId> {
2214 self.as_ref().and_then(|d| d.def_id())
2219 pub fn primitive_type(&self) -> Option<PrimitiveType> {
2221 Primitive(p) | BorrowedRef { type_: box Primitive(p), ..} => Some(p),
2222 Slice(..) | BorrowedRef { type_: box Slice(..), .. } => Some(PrimitiveType::Slice),
2223 Array(..) | BorrowedRef { type_: box Array(..), .. } => Some(PrimitiveType::Array),
2224 Tuple(ref tys) => if tys.is_empty() {
2225 Some(PrimitiveType::Unit)
2227 Some(PrimitiveType::Tuple)
2229 RawPointer(..) => Some(PrimitiveType::RawPointer),
2230 BorrowedRef { type_: box Generic(..), .. } => Some(PrimitiveType::Reference),
2231 BareFunction(..) => Some(PrimitiveType::Fn),
2232 Never => Some(PrimitiveType::Never),
2237 pub fn is_generic(&self) -> bool {
2239 ResolvedPath { is_generic, .. } => is_generic,
2244 pub fn is_self_type(&self) -> bool {
2246 Generic(ref name) => name == "Self",
2251 pub fn generics(&self) -> Option<&[Type]> {
2253 ResolvedPath { ref path, .. } => {
2254 path.segments.last().and_then(|seg| {
2255 if let GenericArgs::AngleBracketed { ref types, .. } = seg.args {
2267 impl GetDefId for Type {
2268 fn def_id(&self) -> Option<DefId> {
2270 ResolvedPath { did, .. } => Some(did),
2271 Primitive(p) => ::html::render::cache().primitive_locations.get(&p).cloned(),
2272 BorrowedRef { type_: box Generic(..), .. } =>
2273 Primitive(PrimitiveType::Reference).def_id(),
2274 BorrowedRef { ref type_, .. } => type_.def_id(),
2275 Tuple(ref tys) => if tys.is_empty() {
2276 Primitive(PrimitiveType::Unit).def_id()
2278 Primitive(PrimitiveType::Tuple).def_id()
2280 BareFunction(..) => Primitive(PrimitiveType::Fn).def_id(),
2281 Never => Primitive(PrimitiveType::Never).def_id(),
2282 Slice(..) => Primitive(PrimitiveType::Slice).def_id(),
2283 Array(..) => Primitive(PrimitiveType::Array).def_id(),
2284 RawPointer(..) => Primitive(PrimitiveType::RawPointer).def_id(),
2285 QPath { ref self_type, .. } => self_type.def_id(),
2291 impl PrimitiveType {
2292 fn from_str(s: &str) -> Option<PrimitiveType> {
2294 "isize" => Some(PrimitiveType::Isize),
2295 "i8" => Some(PrimitiveType::I8),
2296 "i16" => Some(PrimitiveType::I16),
2297 "i32" => Some(PrimitiveType::I32),
2298 "i64" => Some(PrimitiveType::I64),
2299 "i128" => Some(PrimitiveType::I128),
2300 "usize" => Some(PrimitiveType::Usize),
2301 "u8" => Some(PrimitiveType::U8),
2302 "u16" => Some(PrimitiveType::U16),
2303 "u32" => Some(PrimitiveType::U32),
2304 "u64" => Some(PrimitiveType::U64),
2305 "u128" => Some(PrimitiveType::U128),
2306 "bool" => Some(PrimitiveType::Bool),
2307 "char" => Some(PrimitiveType::Char),
2308 "str" => Some(PrimitiveType::Str),
2309 "f32" => Some(PrimitiveType::F32),
2310 "f64" => Some(PrimitiveType::F64),
2311 "array" => Some(PrimitiveType::Array),
2312 "slice" => Some(PrimitiveType::Slice),
2313 "tuple" => Some(PrimitiveType::Tuple),
2314 "unit" => Some(PrimitiveType::Unit),
2315 "pointer" => Some(PrimitiveType::RawPointer),
2316 "reference" => Some(PrimitiveType::Reference),
2317 "fn" => Some(PrimitiveType::Fn),
2318 "never" => Some(PrimitiveType::Never),
2323 pub fn as_str(&self) -> &'static str {
2324 use self::PrimitiveType::*;
2347 RawPointer => "pointer",
2348 Reference => "reference",
2354 pub fn to_url_str(&self) -> &'static str {
2359 impl From<ast::IntTy> for PrimitiveType {
2360 fn from(int_ty: ast::IntTy) -> PrimitiveType {
2362 ast::IntTy::Isize => PrimitiveType::Isize,
2363 ast::IntTy::I8 => PrimitiveType::I8,
2364 ast::IntTy::I16 => PrimitiveType::I16,
2365 ast::IntTy::I32 => PrimitiveType::I32,
2366 ast::IntTy::I64 => PrimitiveType::I64,
2367 ast::IntTy::I128 => PrimitiveType::I128,
2372 impl From<ast::UintTy> for PrimitiveType {
2373 fn from(uint_ty: ast::UintTy) -> PrimitiveType {
2375 ast::UintTy::Usize => PrimitiveType::Usize,
2376 ast::UintTy::U8 => PrimitiveType::U8,
2377 ast::UintTy::U16 => PrimitiveType::U16,
2378 ast::UintTy::U32 => PrimitiveType::U32,
2379 ast::UintTy::U64 => PrimitiveType::U64,
2380 ast::UintTy::U128 => PrimitiveType::U128,
2385 impl From<ast::FloatTy> for PrimitiveType {
2386 fn from(float_ty: ast::FloatTy) -> PrimitiveType {
2388 ast::FloatTy::F32 => PrimitiveType::F32,
2389 ast::FloatTy::F64 => PrimitiveType::F64,
2394 impl Clean<Type> for hir::Ty {
2395 fn clean(&self, cx: &DocContext) -> Type {
2399 TyKind::Never => Never,
2400 TyKind::Ptr(ref m) => RawPointer(m.mutbl.clean(cx), box m.ty.clean(cx)),
2401 TyKind::Rptr(ref l, ref m) => {
2402 let lifetime = if l.is_elided() {
2407 BorrowedRef {lifetime: lifetime, mutability: m.mutbl.clean(cx),
2408 type_: box m.ty.clean(cx)}
2410 TyKind::Slice(ref ty) => Slice(box ty.clean(cx)),
2411 TyKind::Array(ref ty, ref length) => {
2412 let def_id = cx.tcx.hir.local_def_id(length.id);
2413 let param_env = cx.tcx.param_env(def_id);
2414 let substs = Substs::identity_for_item(cx.tcx, def_id);
2415 let cid = GlobalId {
2416 instance: ty::Instance::new(def_id, substs),
2419 let length = cx.tcx.const_eval(param_env.and(cid)).unwrap_or_else(|_| {
2420 ty::Const::unevaluated(cx.tcx, def_id, substs, cx.tcx.types.usize)
2422 let length = print_const(cx, length);
2423 Array(box ty.clean(cx), length)
2425 TyKind::Tup(ref tys) => Tuple(tys.clean(cx)),
2426 TyKind::Def(item_id, _) => {
2427 let item = cx.tcx.hir.expect_item(item_id.id);
2428 if let hir::ItemKind::Existential(ref ty) = item.node {
2429 ImplTrait(ty.bounds.clean(cx))
2434 TyKind::Path(hir::QPath::Resolved(None, ref path)) => {
2435 if let Some(new_ty) = cx.ty_substs.borrow().get(&path.def).cloned() {
2439 if let Def::TyParam(did) = path.def {
2440 if let Some(bounds) = cx.impl_trait_bounds.borrow_mut().remove(&did) {
2441 return ImplTrait(bounds);
2445 let mut alias = None;
2446 if let Def::TyAlias(def_id) = path.def {
2447 // Substitute private type aliases
2448 if let Some(node_id) = cx.tcx.hir.as_local_node_id(def_id) {
2449 if !cx.renderinfo.borrow().access_levels.is_exported(def_id) {
2450 alias = Some(&cx.tcx.hir.expect_item(node_id).node);
2455 if let Some(&hir::ItemKind::Ty(ref ty, ref generics)) = alias {
2456 let provided_params = &path.segments.last().expect("segments were empty");
2457 let mut ty_substs = FxHashMap::default();
2458 let mut lt_substs = FxHashMap::default();
2459 provided_params.with_generic_args(|generic_args| {
2460 let mut indices: GenericParamCount = Default::default();
2461 for param in generics.params.iter() {
2463 hir::GenericParamKind::Lifetime { .. } => {
2465 let lifetime = generic_args.args.iter().find_map(|arg| {
2467 GenericArg::Lifetime(lt) => {
2468 if indices.lifetimes == j {
2477 if let Some(lt) = lifetime.cloned() {
2478 if !lt.is_elided() {
2480 cx.tcx.hir.local_def_id(param.id);
2481 lt_substs.insert(lt_def_id, lt.clean(cx));
2484 indices.lifetimes += 1;
2486 hir::GenericParamKind::Type { ref default, .. } => {
2488 Def::TyParam(cx.tcx.hir.local_def_id(param.id));
2490 let type_ = generic_args.args.iter().find_map(|arg| {
2492 GenericArg::Type(ty) => {
2493 if indices.types == j {
2502 if let Some(ty) = type_.cloned() {
2503 ty_substs.insert(ty_param_def, ty.clean(cx));
2504 } else if let Some(default) = default.clone() {
2505 ty_substs.insert(ty_param_def,
2506 default.into_inner().clean(cx));
2513 return cx.enter_alias(ty_substs, lt_substs, || ty.clean(cx));
2515 resolve_type(cx, path.clean(cx), self.id)
2517 TyKind::Path(hir::QPath::Resolved(Some(ref qself), ref p)) => {
2518 let mut segments: Vec<_> = p.segments.clone().into();
2520 let trait_path = hir::Path {
2522 def: Def::Trait(cx.tcx.associated_item(p.def.def_id()).container.id()),
2523 segments: segments.into(),
2526 name: p.segments.last().expect("segments were empty").ident.name.clean(cx),
2527 self_type: box qself.clean(cx),
2528 trait_: box resolve_type(cx, trait_path.clean(cx), self.id)
2531 TyKind::Path(hir::QPath::TypeRelative(ref qself, ref segment)) => {
2532 let mut def = Def::Err;
2533 let ty = hir_ty_to_ty(cx.tcx, self);
2534 if let ty::Projection(proj) = ty.sty {
2535 def = Def::Trait(proj.trait_ref(cx.tcx).def_id);
2537 let trait_path = hir::Path {
2540 segments: vec![].into(),
2543 name: segment.ident.name.clean(cx),
2544 self_type: box qself.clean(cx),
2545 trait_: box resolve_type(cx, trait_path.clean(cx), self.id)
2548 TyKind::TraitObject(ref bounds, ref lifetime) => {
2549 match bounds[0].clean(cx).trait_ {
2550 ResolvedPath { path, typarams: None, did, is_generic } => {
2551 let mut bounds: Vec<self::GenericBound> = bounds[1..].iter().map(|bound| {
2552 self::GenericBound::TraitBound(bound.clean(cx),
2553 hir::TraitBoundModifier::None)
2555 if !lifetime.is_elided() {
2556 bounds.push(self::GenericBound::Outlives(lifetime.clean(cx)));
2558 ResolvedPath { path, typarams: Some(bounds), did, is_generic, }
2560 _ => Infer // shouldn't happen
2563 TyKind::BareFn(ref barefn) => BareFunction(box barefn.clean(cx)),
2564 TyKind::Infer | TyKind::Err => Infer,
2565 TyKind::Typeof(..) => panic!("Unimplemented type {:?}", self.node),
2570 impl<'tcx> Clean<Type> for Ty<'tcx> {
2571 fn clean(&self, cx: &DocContext) -> Type {
2574 ty::Bool => Primitive(PrimitiveType::Bool),
2575 ty::Char => Primitive(PrimitiveType::Char),
2576 ty::Int(int_ty) => Primitive(int_ty.into()),
2577 ty::Uint(uint_ty) => Primitive(uint_ty.into()),
2578 ty::Float(float_ty) => Primitive(float_ty.into()),
2579 ty::Str => Primitive(PrimitiveType::Str),
2580 ty::Slice(ty) => Slice(box ty.clean(cx)),
2581 ty::Array(ty, n) => {
2582 let mut n = cx.tcx.lift(&n).expect("array lift failed");
2583 if let ConstValue::Unevaluated(def_id, substs) = n.val {
2584 let param_env = cx.tcx.param_env(def_id);
2585 let cid = GlobalId {
2586 instance: ty::Instance::new(def_id, substs),
2589 if let Ok(new_n) = cx.tcx.const_eval(param_env.and(cid)) {
2593 let n = print_const(cx, n);
2594 Array(box ty.clean(cx), n)
2596 ty::RawPtr(mt) => RawPointer(mt.mutbl.clean(cx), box mt.ty.clean(cx)),
2597 ty::Ref(r, ty, mutbl) => BorrowedRef {
2598 lifetime: r.clean(cx),
2599 mutability: mutbl.clean(cx),
2600 type_: box ty.clean(cx),
2604 let ty = cx.tcx.lift(self).expect("FnPtr lift failed");
2605 let sig = ty.fn_sig(cx.tcx);
2606 BareFunction(box BareFunctionDecl {
2607 unsafety: sig.unsafety(),
2608 generic_params: Vec::new(),
2609 decl: (cx.tcx.hir.local_def_id(ast::CRATE_NODE_ID), sig).clean(cx),
2613 ty::Adt(def, substs) => {
2615 let kind = match def.adt_kind() {
2616 AdtKind::Struct => TypeKind::Struct,
2617 AdtKind::Union => TypeKind::Union,
2618 AdtKind::Enum => TypeKind::Enum,
2620 inline::record_extern_fqn(cx, did, kind);
2621 let path = external_path(cx, &cx.tcx.item_name(did).as_str(),
2622 None, false, vec![], substs);
2630 ty::Foreign(did) => {
2631 inline::record_extern_fqn(cx, did, TypeKind::Foreign);
2632 let path = external_path(cx, &cx.tcx.item_name(did).as_str(),
2633 None, false, vec![], Substs::empty());
2641 ty::Dynamic(ref obj, ref reg) => {
2642 let principal = obj.principal();
2643 let did = principal.def_id();
2644 inline::record_extern_fqn(cx, did, TypeKind::Trait);
2646 let mut typarams = vec![];
2647 reg.clean(cx).map(|b| typarams.push(GenericBound::Outlives(b)));
2648 for did in obj.auto_traits() {
2649 let empty = cx.tcx.intern_substs(&[]);
2650 let path = external_path(cx, &cx.tcx.item_name(did).as_str(),
2651 Some(did), false, vec![], empty);
2652 inline::record_extern_fqn(cx, did, TypeKind::Trait);
2653 let bound = GenericBound::TraitBound(PolyTrait {
2654 trait_: ResolvedPath {
2660 generic_params: Vec::new(),
2661 }, hir::TraitBoundModifier::None);
2662 typarams.push(bound);
2665 let mut bindings = vec![];
2666 for pb in obj.projection_bounds() {
2667 bindings.push(TypeBinding {
2668 name: cx.tcx.associated_item(pb.item_def_id()).ident.name.clean(cx),
2669 ty: pb.skip_binder().ty.clean(cx)
2673 let path = external_path(cx, &cx.tcx.item_name(did).as_str(), Some(did),
2674 false, bindings, principal.skip_binder().substs);
2677 typarams: Some(typarams),
2682 ty::Tuple(ref t) => Tuple(t.clean(cx)),
2684 ty::Projection(ref data) => data.clean(cx),
2686 ty::Param(ref p) => Generic(p.name.to_string()),
2688 ty::Opaque(def_id, substs) => {
2689 // Grab the "TraitA + TraitB" from `impl TraitA + TraitB`,
2690 // by looking up the projections associated with the def_id.
2691 let predicates_of = cx.tcx.predicates_of(def_id);
2692 let substs = cx.tcx.lift(&substs).expect("Opaque lift failed");
2693 let bounds = predicates_of.instantiate(cx.tcx, substs);
2694 let mut regions = vec![];
2695 let mut has_sized = false;
2696 let mut bounds = bounds.predicates.iter().filter_map(|predicate| {
2697 let trait_ref = if let Some(tr) = predicate.to_opt_poly_trait_ref() {
2699 } else if let ty::Predicate::TypeOutlives(pred) = *predicate {
2700 // these should turn up at the end
2701 pred.skip_binder().1.clean(cx).map(|r| {
2702 regions.push(GenericBound::Outlives(r))
2709 if let Some(sized) = cx.tcx.lang_items().sized_trait() {
2710 if trait_ref.def_id() == sized {
2716 let bounds = bounds.predicates.iter().filter_map(|pred|
2717 if let ty::Predicate::Projection(proj) = *pred {
2718 let proj = proj.skip_binder();
2719 if proj.projection_ty.trait_ref(cx.tcx) == *trait_ref.skip_binder() {
2721 name: cx.tcx.associated_item(proj.projection_ty.item_def_id)
2722 .ident.name.clean(cx),
2723 ty: proj.ty.clean(cx),
2733 Some((trait_ref.skip_binder(), bounds).clean(cx))
2734 }).collect::<Vec<_>>();
2735 bounds.extend(regions);
2736 if !has_sized && !bounds.is_empty() {
2737 bounds.insert(0, GenericBound::maybe_sized(cx));
2742 ty::Closure(..) | ty::Generator(..) => Tuple(vec![]), // FIXME(pcwalton)
2744 ty::UnnormalizedProjection(..) => panic!("UnnormalizedProjection"),
2745 ty::GeneratorWitness(..) => panic!("GeneratorWitness"),
2746 ty::Infer(..) => panic!("Infer"),
2747 ty::Error => panic!("Error"),
2752 impl Clean<Item> for hir::StructField {
2753 fn clean(&self, cx: &DocContext) -> Item {
2755 name: Some(self.ident.name).clean(cx),
2756 attrs: self.attrs.clean(cx),
2757 source: self.span.clean(cx),
2758 visibility: self.vis.clean(cx),
2759 stability: get_stability(cx, cx.tcx.hir.local_def_id(self.id)),
2760 deprecation: get_deprecation(cx, cx.tcx.hir.local_def_id(self.id)),
2761 def_id: cx.tcx.hir.local_def_id(self.id),
2762 inner: StructFieldItem(self.ty.clean(cx)),
2767 impl<'tcx> Clean<Item> for ty::FieldDef {
2768 fn clean(&self, cx: &DocContext) -> Item {
2770 name: Some(self.ident.name).clean(cx),
2771 attrs: cx.tcx.get_attrs(self.did).clean(cx),
2772 source: cx.tcx.def_span(self.did).clean(cx),
2773 visibility: self.vis.clean(cx),
2774 stability: get_stability(cx, self.did),
2775 deprecation: get_deprecation(cx, self.did),
2777 inner: StructFieldItem(cx.tcx.type_of(self.did).clean(cx)),
2782 #[derive(Clone, PartialEq, Eq, RustcDecodable, RustcEncodable, Debug)]
2783 pub enum Visibility {
2787 Restricted(DefId, Path),
2790 impl Clean<Option<Visibility>> for hir::Visibility {
2791 fn clean(&self, cx: &DocContext) -> Option<Visibility> {
2792 Some(match self.node {
2793 hir::VisibilityKind::Public => Visibility::Public,
2794 hir::VisibilityKind::Inherited => Visibility::Inherited,
2795 hir::VisibilityKind::Crate(_) => Visibility::Crate,
2796 hir::VisibilityKind::Restricted { ref path, .. } => {
2797 let path = path.clean(cx);
2798 let did = register_def(cx, path.def);
2799 Visibility::Restricted(did, path)
2805 impl Clean<Option<Visibility>> for ty::Visibility {
2806 fn clean(&self, _: &DocContext) -> Option<Visibility> {
2807 Some(if *self == ty::Visibility::Public { Public } else { Inherited })
2811 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2813 pub struct_type: doctree::StructType,
2814 pub generics: Generics,
2815 pub fields: Vec<Item>,
2816 pub fields_stripped: bool,
2819 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2821 pub struct_type: doctree::StructType,
2822 pub generics: Generics,
2823 pub fields: Vec<Item>,
2824 pub fields_stripped: bool,
2827 impl Clean<Item> for doctree::Struct {
2828 fn clean(&self, cx: &DocContext) -> Item {
2830 name: Some(self.name.clean(cx)),
2831 attrs: self.attrs.clean(cx),
2832 source: self.whence.clean(cx),
2833 def_id: cx.tcx.hir.local_def_id(self.id),
2834 visibility: self.vis.clean(cx),
2835 stability: self.stab.clean(cx),
2836 deprecation: self.depr.clean(cx),
2837 inner: StructItem(Struct {
2838 struct_type: self.struct_type,
2839 generics: self.generics.clean(cx),
2840 fields: self.fields.clean(cx),
2841 fields_stripped: false,
2847 impl Clean<Item> for doctree::Union {
2848 fn clean(&self, cx: &DocContext) -> Item {
2850 name: Some(self.name.clean(cx)),
2851 attrs: self.attrs.clean(cx),
2852 source: self.whence.clean(cx),
2853 def_id: cx.tcx.hir.local_def_id(self.id),
2854 visibility: self.vis.clean(cx),
2855 stability: self.stab.clean(cx),
2856 deprecation: self.depr.clean(cx),
2857 inner: UnionItem(Union {
2858 struct_type: self.struct_type,
2859 generics: self.generics.clean(cx),
2860 fields: self.fields.clean(cx),
2861 fields_stripped: false,
2867 /// This is a more limited form of the standard Struct, different in that
2868 /// it lacks the things most items have (name, id, parameterization). Found
2869 /// only as a variant in an enum.
2870 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2871 pub struct VariantStruct {
2872 pub struct_type: doctree::StructType,
2873 pub fields: Vec<Item>,
2874 pub fields_stripped: bool,
2877 impl Clean<VariantStruct> for ::rustc::hir::VariantData {
2878 fn clean(&self, cx: &DocContext) -> VariantStruct {
2880 struct_type: doctree::struct_type_from_def(self),
2881 fields: self.fields().iter().map(|x| x.clean(cx)).collect(),
2882 fields_stripped: false,
2887 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2889 pub variants: Vec<Item>,
2890 pub generics: Generics,
2891 pub variants_stripped: bool,
2894 impl Clean<Item> for doctree::Enum {
2895 fn clean(&self, cx: &DocContext) -> Item {
2897 name: Some(self.name.clean(cx)),
2898 attrs: self.attrs.clean(cx),
2899 source: self.whence.clean(cx),
2900 def_id: cx.tcx.hir.local_def_id(self.id),
2901 visibility: self.vis.clean(cx),
2902 stability: self.stab.clean(cx),
2903 deprecation: self.depr.clean(cx),
2904 inner: EnumItem(Enum {
2905 variants: self.variants.clean(cx),
2906 generics: self.generics.clean(cx),
2907 variants_stripped: false,
2913 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2914 pub struct Variant {
2915 pub kind: VariantKind,
2918 impl Clean<Item> for doctree::Variant {
2919 fn clean(&self, cx: &DocContext) -> Item {
2921 name: Some(self.name.clean(cx)),
2922 attrs: self.attrs.clean(cx),
2923 source: self.whence.clean(cx),
2925 stability: self.stab.clean(cx),
2926 deprecation: self.depr.clean(cx),
2927 def_id: cx.tcx.hir.local_def_id(self.def.id()),
2928 inner: VariantItem(Variant {
2929 kind: self.def.clean(cx),
2935 impl<'tcx> Clean<Item> for ty::VariantDef {
2936 fn clean(&self, cx: &DocContext) -> Item {
2937 let kind = match self.ctor_kind {
2938 CtorKind::Const => VariantKind::CLike,
2941 self.fields.iter().map(|f| cx.tcx.type_of(f.did).clean(cx)).collect()
2944 CtorKind::Fictive => {
2945 VariantKind::Struct(VariantStruct {
2946 struct_type: doctree::Plain,
2947 fields_stripped: false,
2948 fields: self.fields.iter().map(|field| {
2950 source: cx.tcx.def_span(field.did).clean(cx),
2951 name: Some(field.ident.name.clean(cx)),
2952 attrs: cx.tcx.get_attrs(field.did).clean(cx),
2953 visibility: field.vis.clean(cx),
2955 stability: get_stability(cx, field.did),
2956 deprecation: get_deprecation(cx, field.did),
2957 inner: StructFieldItem(cx.tcx.type_of(field.did).clean(cx))
2964 name: Some(self.name.clean(cx)),
2965 attrs: inline::load_attrs(cx, self.did),
2966 source: cx.tcx.def_span(self.did).clean(cx),
2967 visibility: Some(Inherited),
2969 inner: VariantItem(Variant { kind: kind }),
2970 stability: get_stability(cx, self.did),
2971 deprecation: get_deprecation(cx, self.did),
2976 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2977 pub enum VariantKind {
2980 Struct(VariantStruct),
2983 impl Clean<VariantKind> for hir::VariantData {
2984 fn clean(&self, cx: &DocContext) -> VariantKind {
2985 if self.is_struct() {
2986 VariantKind::Struct(self.clean(cx))
2987 } else if self.is_unit() {
2990 VariantKind::Tuple(self.fields().iter().map(|x| x.ty.clean(cx)).collect())
2995 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2997 pub filename: FileName,
3005 pub fn empty() -> Span {
3007 filename: FileName::Anon,
3008 loline: 0, locol: 0,
3009 hiline: 0, hicol: 0,
3014 impl Clean<Span> for syntax_pos::Span {
3015 fn clean(&self, cx: &DocContext) -> Span {
3016 if self.is_dummy() {
3017 return Span::empty();
3020 let cm = cx.sess().source_map();
3021 let filename = cm.span_to_filename(*self);
3022 let lo = cm.lookup_char_pos(self.lo());
3023 let hi = cm.lookup_char_pos(self.hi());
3027 locol: lo.col.to_usize(),
3029 hicol: hi.col.to_usize(),
3034 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
3038 pub segments: Vec<PathSegment>,
3042 pub fn last_name(&self) -> &str {
3043 self.segments.last().expect("segments were empty").name.as_str()
3047 impl Clean<Path> for hir::Path {
3048 fn clean(&self, cx: &DocContext) -> Path {
3050 global: self.is_global(),
3052 segments: if self.is_global() { &self.segments[1..] } else { &self.segments }.clean(cx),
3057 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
3058 pub enum GenericArgs {
3060 lifetimes: Vec<Lifetime>,
3062 bindings: Vec<TypeBinding>,
3066 output: Option<Type>,
3070 impl Clean<GenericArgs> for hir::GenericArgs {
3071 fn clean(&self, cx: &DocContext) -> GenericArgs {
3072 if self.parenthesized {
3073 let output = self.bindings[0].ty.clean(cx);
3074 GenericArgs::Parenthesized {
3075 inputs: self.inputs().clean(cx),
3076 output: if output != Type::Tuple(Vec::new()) { Some(output) } else { None }
3079 let (mut lifetimes, mut types) = (vec![], vec![]);
3080 let mut elided_lifetimes = true;
3081 for arg in &self.args {
3083 GenericArg::Lifetime(lt) => {
3084 if !lt.is_elided() {
3085 elided_lifetimes = false;
3087 lifetimes.push(lt.clean(cx));
3089 GenericArg::Type(ty) => {
3090 types.push(ty.clean(cx));
3094 GenericArgs::AngleBracketed {
3095 lifetimes: if elided_lifetimes { vec![] } else { lifetimes },
3097 bindings: self.bindings.clean(cx),
3103 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
3104 pub struct PathSegment {
3106 pub args: GenericArgs,
3109 impl Clean<PathSegment> for hir::PathSegment {
3110 fn clean(&self, cx: &DocContext) -> PathSegment {
3112 name: self.ident.name.clean(cx),
3113 args: self.with_generic_args(|generic_args| generic_args.clean(cx))
3118 fn strip_type(ty: Type) -> Type {
3120 Type::ResolvedPath { path, typarams, did, is_generic } => {
3121 Type::ResolvedPath { path: strip_path(&path), typarams, did, is_generic }
3123 Type::Tuple(inner_tys) => {
3124 Type::Tuple(inner_tys.iter().map(|t| strip_type(t.clone())).collect())
3126 Type::Slice(inner_ty) => Type::Slice(Box::new(strip_type(*inner_ty))),
3127 Type::Array(inner_ty, s) => Type::Array(Box::new(strip_type(*inner_ty)), s),
3128 Type::Unique(inner_ty) => Type::Unique(Box::new(strip_type(*inner_ty))),
3129 Type::RawPointer(m, inner_ty) => Type::RawPointer(m, Box::new(strip_type(*inner_ty))),
3130 Type::BorrowedRef { lifetime, mutability, type_ } => {
3131 Type::BorrowedRef { lifetime, mutability, type_: Box::new(strip_type(*type_)) }
3133 Type::QPath { name, self_type, trait_ } => {
3136 self_type: Box::new(strip_type(*self_type)), trait_: Box::new(strip_type(*trait_))
3143 fn strip_path(path: &Path) -> Path {
3144 let segments = path.segments.iter().map(|s| {
3146 name: s.name.clone(),
3147 args: GenericArgs::AngleBracketed {
3148 lifetimes: Vec::new(),
3150 bindings: Vec::new(),
3156 global: path.global,
3157 def: path.def.clone(),
3162 fn qpath_to_string(p: &hir::QPath) -> String {
3163 let segments = match *p {
3164 hir::QPath::Resolved(_, ref path) => &path.segments,
3165 hir::QPath::TypeRelative(_, ref segment) => return segment.ident.to_string(),
3168 let mut s = String::new();
3169 for (i, seg) in segments.iter().enumerate() {
3173 if seg.ident.name != keywords::CrateRoot.name() {
3174 s.push_str(&*seg.ident.as_str());
3180 impl Clean<String> for ast::Name {
3181 fn clean(&self, _: &DocContext) -> String {
3186 impl Clean<String> for InternedString {
3187 fn clean(&self, _: &DocContext) -> String {
3192 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3193 pub struct Typedef {
3195 pub generics: Generics,
3198 impl Clean<Item> for doctree::Typedef {
3199 fn clean(&self, cx: &DocContext) -> Item {
3201 name: Some(self.name.clean(cx)),
3202 attrs: self.attrs.clean(cx),
3203 source: self.whence.clean(cx),
3204 def_id: cx.tcx.hir.local_def_id(self.id.clone()),
3205 visibility: self.vis.clean(cx),
3206 stability: self.stab.clean(cx),
3207 deprecation: self.depr.clean(cx),
3208 inner: TypedefItem(Typedef {
3209 type_: self.ty.clean(cx),
3210 generics: self.gen.clean(cx),
3216 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3217 pub struct Existential {
3218 pub bounds: Vec<GenericBound>,
3219 pub generics: Generics,
3222 impl Clean<Item> for doctree::Existential {
3223 fn clean(&self, cx: &DocContext) -> Item {
3225 name: Some(self.name.clean(cx)),
3226 attrs: self.attrs.clean(cx),
3227 source: self.whence.clean(cx),
3228 def_id: cx.tcx.hir.local_def_id(self.id.clone()),
3229 visibility: self.vis.clean(cx),
3230 stability: self.stab.clean(cx),
3231 deprecation: self.depr.clean(cx),
3232 inner: ExistentialItem(Existential {
3233 bounds: self.exist_ty.bounds.clean(cx),
3234 generics: self.exist_ty.generics.clean(cx),
3240 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
3241 pub struct BareFunctionDecl {
3242 pub unsafety: hir::Unsafety,
3243 pub generic_params: Vec<GenericParamDef>,
3248 impl Clean<BareFunctionDecl> for hir::BareFnTy {
3249 fn clean(&self, cx: &DocContext) -> BareFunctionDecl {
3250 let (generic_params, decl) = enter_impl_trait(cx, || {
3251 (self.generic_params.clean(cx), (&*self.decl, &self.arg_names[..]).clean(cx))
3254 unsafety: self.unsafety,
3262 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3265 pub mutability: Mutability,
3266 /// It's useful to have the value of a static documented, but I have no
3267 /// desire to represent expressions (that'd basically be all of the AST,
3268 /// which is huge!). So, have a string.
3272 impl Clean<Item> for doctree::Static {
3273 fn clean(&self, cx: &DocContext) -> Item {
3274 debug!("cleaning static {}: {:?}", self.name.clean(cx), self);
3276 name: Some(self.name.clean(cx)),
3277 attrs: self.attrs.clean(cx),
3278 source: self.whence.clean(cx),
3279 def_id: cx.tcx.hir.local_def_id(self.id),
3280 visibility: self.vis.clean(cx),
3281 stability: self.stab.clean(cx),
3282 deprecation: self.depr.clean(cx),
3283 inner: StaticItem(Static {
3284 type_: self.type_.clean(cx),
3285 mutability: self.mutability.clean(cx),
3286 expr: print_const_expr(cx, self.expr),
3292 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3293 pub struct Constant {
3298 impl Clean<Item> for doctree::Constant {
3299 fn clean(&self, cx: &DocContext) -> Item {
3301 name: Some(self.name.clean(cx)),
3302 attrs: self.attrs.clean(cx),
3303 source: self.whence.clean(cx),
3304 def_id: cx.tcx.hir.local_def_id(self.id),
3305 visibility: self.vis.clean(cx),
3306 stability: self.stab.clean(cx),
3307 deprecation: self.depr.clean(cx),
3308 inner: ConstantItem(Constant {
3309 type_: self.type_.clean(cx),
3310 expr: print_const_expr(cx, self.expr),
3316 #[derive(Debug, Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Copy, Hash)]
3317 pub enum Mutability {
3322 impl Clean<Mutability> for hir::Mutability {
3323 fn clean(&self, _: &DocContext) -> Mutability {
3325 &hir::MutMutable => Mutable,
3326 &hir::MutImmutable => Immutable,
3331 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Copy, Debug, Hash)]
3332 pub enum ImplPolarity {
3337 impl Clean<ImplPolarity> for hir::ImplPolarity {
3338 fn clean(&self, _: &DocContext) -> ImplPolarity {
3340 &hir::ImplPolarity::Positive => ImplPolarity::Positive,
3341 &hir::ImplPolarity::Negative => ImplPolarity::Negative,
3346 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3348 pub unsafety: hir::Unsafety,
3349 pub generics: Generics,
3350 pub provided_trait_methods: FxHashSet<String>,
3351 pub trait_: Option<Type>,
3353 pub items: Vec<Item>,
3354 pub polarity: Option<ImplPolarity>,
3355 pub synthetic: bool,
3356 pub blanket_impl: Option<Type>,
3359 pub fn get_auto_traits_with_node_id(cx: &DocContext, id: ast::NodeId, name: String) -> Vec<Item> {
3360 let finder = AutoTraitFinder::new(cx);
3361 finder.get_with_node_id(id, name)
3364 pub fn get_auto_traits_with_def_id(cx: &DocContext, id: DefId) -> Vec<Item> {
3365 let finder = AutoTraitFinder::new(cx);
3367 finder.get_with_def_id(id)
3370 pub fn get_blanket_impls_with_node_id(cx: &DocContext, id: ast::NodeId, name: String) -> Vec<Item> {
3371 let finder = BlanketImplFinder::new(cx);
3372 finder.get_with_node_id(id, name)
3375 pub fn get_blanket_impls_with_def_id(cx: &DocContext, id: DefId) -> Vec<Item> {
3376 let finder = BlanketImplFinder::new(cx);
3378 finder.get_with_def_id(id)
3381 impl Clean<Vec<Item>> for doctree::Impl {
3382 fn clean(&self, cx: &DocContext) -> Vec<Item> {
3383 let mut ret = Vec::new();
3384 let trait_ = self.trait_.clean(cx);
3385 let items = self.items.clean(cx);
3387 // If this impl block is an implementation of the Deref trait, then we
3388 // need to try inlining the target's inherent impl blocks as well.
3389 if trait_.def_id() == cx.tcx.lang_items().deref_trait() {
3390 build_deref_target_impls(cx, &items, &mut ret);
3393 let provided = trait_.def_id().map(|did| {
3394 cx.tcx.provided_trait_methods(did)
3396 .map(|meth| meth.ident.to_string())
3398 }).unwrap_or_default();
3402 attrs: self.attrs.clean(cx),
3403 source: self.whence.clean(cx),
3404 def_id: cx.tcx.hir.local_def_id(self.id),
3405 visibility: self.vis.clean(cx),
3406 stability: self.stab.clean(cx),
3407 deprecation: self.depr.clean(cx),
3408 inner: ImplItem(Impl {
3409 unsafety: self.unsafety,
3410 generics: self.generics.clean(cx),
3411 provided_trait_methods: provided,
3413 for_: self.for_.clean(cx),
3415 polarity: Some(self.polarity.clean(cx)),
3424 fn build_deref_target_impls(cx: &DocContext,
3426 ret: &mut Vec<Item>) {
3427 use self::PrimitiveType::*;
3431 let target = match item.inner {
3432 TypedefItem(ref t, true) => &t.type_,
3435 let primitive = match *target {
3436 ResolvedPath { did, .. } if did.is_local() => continue,
3437 ResolvedPath { did, .. } => {
3438 ret.extend(inline::build_impls(cx, did));
3441 _ => match target.primitive_type() {
3446 let did = match primitive {
3447 Isize => tcx.lang_items().isize_impl(),
3448 I8 => tcx.lang_items().i8_impl(),
3449 I16 => tcx.lang_items().i16_impl(),
3450 I32 => tcx.lang_items().i32_impl(),
3451 I64 => tcx.lang_items().i64_impl(),
3452 I128 => tcx.lang_items().i128_impl(),
3453 Usize => tcx.lang_items().usize_impl(),
3454 U8 => tcx.lang_items().u8_impl(),
3455 U16 => tcx.lang_items().u16_impl(),
3456 U32 => tcx.lang_items().u32_impl(),
3457 U64 => tcx.lang_items().u64_impl(),
3458 U128 => tcx.lang_items().u128_impl(),
3459 F32 => tcx.lang_items().f32_impl(),
3460 F64 => tcx.lang_items().f64_impl(),
3461 Char => tcx.lang_items().char_impl(),
3463 Str => tcx.lang_items().str_impl(),
3464 Slice => tcx.lang_items().slice_impl(),
3465 Array => tcx.lang_items().slice_impl(),
3468 RawPointer => tcx.lang_items().const_ptr_impl(),
3473 if let Some(did) = did {
3474 if !did.is_local() {
3475 inline::build_impl(cx, did, ret);
3481 impl Clean<Item> for doctree::ExternCrate {
3482 fn clean(&self, cx: &DocContext) -> Item {
3485 attrs: self.attrs.clean(cx),
3486 source: self.whence.clean(cx),
3487 def_id: DefId { krate: self.cnum, index: CRATE_DEF_INDEX },
3488 visibility: self.vis.clean(cx),
3491 inner: ExternCrateItem(self.name.clean(cx), self.path.clone())
3496 impl Clean<Vec<Item>> for doctree::Import {
3497 fn clean(&self, cx: &DocContext) -> Vec<Item> {
3498 // We consider inlining the documentation of `pub use` statements, but we
3499 // forcefully don't inline if this is not public or if the
3500 // #[doc(no_inline)] attribute is present.
3501 // Don't inline doc(hidden) imports so they can be stripped at a later stage.
3502 let denied = !self.vis.node.is_pub() || self.attrs.iter().any(|a| {
3503 a.name() == "doc" && match a.meta_item_list() {
3504 Some(l) => attr::list_contains_name(&l, "no_inline") ||
3505 attr::list_contains_name(&l, "hidden"),
3509 let path = self.path.clean(cx);
3510 let inner = if self.glob {
3512 let mut visited = FxHashSet::default();
3513 if let Some(items) = inline::try_inline_glob(cx, path.def, &mut visited) {
3518 Import::Glob(resolve_use_source(cx, path))
3520 let name = self.name;
3522 let mut visited = FxHashSet::default();
3523 if let Some(items) = inline::try_inline(cx, path.def, name, &mut visited) {
3527 Import::Simple(name.clean(cx), resolve_use_source(cx, path))
3532 attrs: self.attrs.clean(cx),
3533 source: self.whence.clean(cx),
3534 def_id: cx.tcx.hir.local_def_id(ast::CRATE_NODE_ID),
3535 visibility: self.vis.clean(cx),
3538 inner: ImportItem(inner)
3543 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3545 // use source as str;
3546 Simple(String, ImportSource),
3551 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3552 pub struct ImportSource {
3554 pub did: Option<DefId>,
3557 impl Clean<Vec<Item>> for hir::ForeignMod {
3558 fn clean(&self, cx: &DocContext) -> Vec<Item> {
3559 let mut items = self.items.clean(cx);
3560 for item in &mut items {
3561 if let ForeignFunctionItem(ref mut f) = item.inner {
3562 f.header.abi = self.abi;
3569 impl Clean<Item> for hir::ForeignItem {
3570 fn clean(&self, cx: &DocContext) -> Item {
3571 let inner = match self.node {
3572 hir::ForeignItemKind::Fn(ref decl, ref names, ref generics) => {
3573 let (generics, decl) = enter_impl_trait(cx, || {
3574 (generics.clean(cx), (&**decl, &names[..]).clean(cx))
3576 ForeignFunctionItem(Function {
3579 header: hir::FnHeader {
3580 unsafety: hir::Unsafety::Unsafe,
3582 constness: hir::Constness::NotConst,
3583 asyncness: hir::IsAsync::NotAsync,
3587 hir::ForeignItemKind::Static(ref ty, mutbl) => {
3588 ForeignStaticItem(Static {
3589 type_: ty.clean(cx),
3590 mutability: if mutbl {Mutable} else {Immutable},
3591 expr: String::new(),
3594 hir::ForeignItemKind::Type => {
3600 name: Some(self.name.clean(cx)),
3601 attrs: self.attrs.clean(cx),
3602 source: self.span.clean(cx),
3603 def_id: cx.tcx.hir.local_def_id(self.id),
3604 visibility: self.vis.clean(cx),
3605 stability: get_stability(cx, cx.tcx.hir.local_def_id(self.id)),
3606 deprecation: get_deprecation(cx, cx.tcx.hir.local_def_id(self.id)),
3614 pub trait ToSource {
3615 fn to_src(&self, cx: &DocContext) -> String;
3618 impl ToSource for syntax_pos::Span {
3619 fn to_src(&self, cx: &DocContext) -> String {
3620 debug!("converting span {:?} to snippet", self.clean(cx));
3621 let sn = match cx.sess().source_map().span_to_snippet(*self) {
3622 Ok(x) => x.to_string(),
3623 Err(_) => String::new()
3625 debug!("got snippet {}", sn);
3630 fn name_from_pat(p: &hir::Pat) -> String {
3632 debug!("Trying to get a name from pattern: {:?}", p);
3635 PatKind::Wild => "_".to_string(),
3636 PatKind::Binding(_, _, ident, _) => ident.to_string(),
3637 PatKind::TupleStruct(ref p, ..) | PatKind::Path(ref p) => qpath_to_string(p),
3638 PatKind::Struct(ref name, ref fields, etc) => {
3639 format!("{} {{ {}{} }}", qpath_to_string(name),
3640 fields.iter().map(|&Spanned { node: ref fp, .. }|
3641 format!("{}: {}", fp.ident, name_from_pat(&*fp.pat)))
3642 .collect::<Vec<String>>().join(", "),
3643 if etc { ", ..." } else { "" }
3646 PatKind::Tuple(ref elts, _) => format!("({})", elts.iter().map(|p| name_from_pat(&**p))
3647 .collect::<Vec<String>>().join(", ")),
3648 PatKind::Box(ref p) => name_from_pat(&**p),
3649 PatKind::Ref(ref p, _) => name_from_pat(&**p),
3650 PatKind::Lit(..) => {
3651 warn!("tried to get argument name from PatKind::Lit, \
3652 which is silly in function arguments");
3655 PatKind::Range(..) => panic!("tried to get argument name from PatKind::Range, \
3656 which is not allowed in function arguments"),
3657 PatKind::Slice(ref begin, ref mid, ref end) => {
3658 let begin = begin.iter().map(|p| name_from_pat(&**p));
3659 let mid = mid.as_ref().map(|p| format!("..{}", name_from_pat(&**p))).into_iter();
3660 let end = end.iter().map(|p| name_from_pat(&**p));
3661 format!("[{}]", begin.chain(mid).chain(end).collect::<Vec<_>>().join(", "))
3666 fn print_const(cx: &DocContext, n: &ty::Const) -> String {
3668 ConstValue::Unevaluated(def_id, _) => {
3669 if let Some(node_id) = cx.tcx.hir.as_local_node_id(def_id) {
3670 print_const_expr(cx, cx.tcx.hir.body_owned_by(node_id))
3672 inline::print_inlined_const(cx, def_id)
3676 let mut s = String::new();
3677 ::rustc::mir::fmt_const_val(&mut s, n).expect("fmt_const_val failed");
3678 // array lengths are obviously usize
3679 if s.ends_with("usize") {
3680 let n = s.len() - "usize".len();
3688 fn print_const_expr(cx: &DocContext, body: hir::BodyId) -> String {
3689 cx.tcx.hir.node_to_pretty_string(body.node_id)
3692 /// Given a type Path, resolve it to a Type using the TyCtxt
3693 fn resolve_type(cx: &DocContext,
3695 id: ast::NodeId) -> Type {
3696 if id == ast::DUMMY_NODE_ID {
3697 debug!("resolve_type({:?})", path);
3699 debug!("resolve_type({:?},{:?})", path, id);
3702 let is_generic = match path.def {
3703 Def::PrimTy(p) => match p {
3704 hir::Str => return Primitive(PrimitiveType::Str),
3705 hir::Bool => return Primitive(PrimitiveType::Bool),
3706 hir::Char => return Primitive(PrimitiveType::Char),
3707 hir::Int(int_ty) => return Primitive(int_ty.into()),
3708 hir::Uint(uint_ty) => return Primitive(uint_ty.into()),
3709 hir::Float(float_ty) => return Primitive(float_ty.into()),
3711 Def::SelfTy(..) if path.segments.len() == 1 => {
3712 return Generic(keywords::SelfType.name().to_string());
3714 Def::TyParam(..) if path.segments.len() == 1 => {
3715 return Generic(format!("{:#}", path));
3717 Def::SelfTy(..) | Def::TyParam(..) | Def::AssociatedTy(..) => true,
3720 let did = register_def(&*cx, path.def);
3721 ResolvedPath { path: path, typarams: None, did: did, is_generic: is_generic }
3724 pub fn register_def(cx: &DocContext, def: Def) -> DefId {
3725 debug!("register_def({:?})", def);
3727 let (did, kind) = match def {
3728 Def::Fn(i) => (i, TypeKind::Function),
3729 Def::TyAlias(i) => (i, TypeKind::Typedef),
3730 Def::Enum(i) => (i, TypeKind::Enum),
3731 Def::Trait(i) => (i, TypeKind::Trait),
3732 Def::Struct(i) => (i, TypeKind::Struct),
3733 Def::Union(i) => (i, TypeKind::Union),
3734 Def::Mod(i) => (i, TypeKind::Module),
3735 Def::ForeignTy(i) => (i, TypeKind::Foreign),
3736 Def::Const(i) => (i, TypeKind::Const),
3737 Def::Static(i, _) => (i, TypeKind::Static),
3738 Def::Variant(i) => (cx.tcx.parent_def_id(i).expect("cannot get parent def id"),
3740 Def::Macro(i, mac_kind) => match mac_kind {
3741 MacroKind::Bang => (i, TypeKind::Macro),
3742 MacroKind::Attr => (i, TypeKind::Attr),
3743 MacroKind::Derive => (i, TypeKind::Derive),
3744 MacroKind::ProcMacroStub => unreachable!(),
3746 Def::SelfTy(Some(def_id), _) => (def_id, TypeKind::Trait),
3747 Def::SelfTy(_, Some(impl_def_id)) => {
3750 _ => return def.def_id()
3752 if did.is_local() { return did }
3753 inline::record_extern_fqn(cx, did, kind);
3754 if let TypeKind::Trait = kind {
3755 inline::record_extern_trait(cx, did);
3760 fn resolve_use_source(cx: &DocContext, path: Path) -> ImportSource {
3762 did: if path.def == Def::Err {
3765 Some(register_def(cx, path.def))
3771 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3774 pub imported_from: Option<String>,
3777 impl Clean<Item> for doctree::Macro {
3778 fn clean(&self, cx: &DocContext) -> Item {
3779 let name = self.name.clean(cx);
3781 name: Some(name.clone()),
3782 attrs: self.attrs.clean(cx),
3783 source: self.whence.clean(cx),
3784 visibility: Some(Public),
3785 stability: self.stab.clean(cx),
3786 deprecation: self.depr.clean(cx),
3787 def_id: self.def_id,
3788 inner: MacroItem(Macro {
3789 source: format!("macro_rules! {} {{\n{}}}",
3791 self.matchers.iter().map(|span| {
3792 format!(" {} => {{ ... }};\n", span.to_src(cx))
3793 }).collect::<String>()),
3794 imported_from: self.imported_from.clean(cx),
3800 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3801 pub struct ProcMacro {
3802 pub kind: MacroKind,
3803 pub helpers: Vec<String>,
3806 impl Clean<Item> for doctree::ProcMacro {
3807 fn clean(&self, cx: &DocContext) -> Item {
3809 name: Some(self.name.clean(cx)),
3810 attrs: self.attrs.clean(cx),
3811 source: self.whence.clean(cx),
3812 visibility: Some(Public),
3813 stability: self.stab.clean(cx),
3814 deprecation: self.depr.clean(cx),
3815 def_id: cx.tcx.hir.local_def_id(self.id),
3816 inner: ProcMacroItem(ProcMacro {
3818 helpers: self.helpers.clean(cx),
3824 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3825 pub struct Stability {
3826 pub level: stability::StabilityLevel,
3827 pub feature: String,
3829 pub deprecated_since: String,
3830 pub deprecated_reason: String,
3831 pub unstable_reason: String,
3832 pub issue: Option<u32>
3835 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3836 pub struct Deprecation {
3841 impl Clean<Stability> for attr::Stability {
3842 fn clean(&self, _: &DocContext) -> Stability {
3844 level: stability::StabilityLevel::from_attr_level(&self.level),
3845 feature: self.feature.to_string(),
3846 since: match self.level {
3847 attr::Stable {ref since} => since.to_string(),
3850 deprecated_since: match self.rustc_depr {
3851 Some(attr::RustcDeprecation {ref since, ..}) => since.to_string(),
3854 deprecated_reason: match self.rustc_depr {
3855 Some(ref depr) => depr.reason.to_string(),
3858 unstable_reason: match self.level {
3859 attr::Unstable { reason: Some(ref reason), .. } => reason.to_string(),
3862 issue: match self.level {
3863 attr::Unstable {issue, ..} => Some(issue),
3870 impl<'a> Clean<Stability> for &'a attr::Stability {
3871 fn clean(&self, dc: &DocContext) -> Stability {
3876 impl Clean<Deprecation> for attr::Deprecation {
3877 fn clean(&self, _: &DocContext) -> Deprecation {
3879 since: self.since.as_ref().map_or(String::new(), |s| s.to_string()),
3880 note: self.note.as_ref().map_or(String::new(), |s| s.to_string()),
3885 /// An equality constraint on an associated type, e.g. `A=Bar` in `Foo<A=Bar>`
3886 #[derive(Clone, PartialEq, Eq, RustcDecodable, RustcEncodable, Debug, Hash)]
3887 pub struct TypeBinding {
3892 impl Clean<TypeBinding> for hir::TypeBinding {
3893 fn clean(&self, cx: &DocContext) -> TypeBinding {
3895 name: self.ident.name.clean(cx),
3896 ty: self.ty.clean(cx)
3901 pub fn def_id_to_path(cx: &DocContext, did: DefId, name: Option<String>) -> Vec<String> {
3902 let crate_name = name.unwrap_or_else(|| cx.tcx.crate_name(did.krate).to_string());
3903 let relative = cx.tcx.def_path(did).data.into_iter().filter_map(|elem| {
3904 // extern blocks have an empty name
3905 let s = elem.data.to_string();
3912 once(crate_name).chain(relative).collect()
3915 pub fn enter_impl_trait<F, R>(cx: &DocContext, f: F) -> R
3919 let old_bounds = mem::replace(&mut *cx.impl_trait_bounds.borrow_mut(), Default::default());
3921 assert!(cx.impl_trait_bounds.borrow().is_empty());
3922 *cx.impl_trait_bounds.borrow_mut() = old_bounds;
3926 // Start of code copied from rust-clippy
3928 pub fn path_to_def_local(tcx: &TyCtxt, path: &[&str]) -> Option<DefId> {
3929 let krate = tcx.hir.krate();
3930 let mut items = krate.module.item_ids.clone();
3931 let mut path_it = path.iter().peekable();
3934 let segment = match path_it.next() {
3935 Some(segment) => segment,
3936 None => return None,
3939 for item_id in mem::replace(&mut items, HirVec::new()).iter() {
3940 let item = tcx.hir.expect_item(item_id.id);
3941 if item.name == *segment {
3942 if path_it.peek().is_none() {
3943 return Some(tcx.hir.local_def_id(item_id.id))
3946 items = match &item.node {
3947 &hir::ItemKind::Mod(ref m) => m.item_ids.clone(),
3948 _ => panic!("Unexpected item {:?} in path {:?} path")
3956 pub fn path_to_def(tcx: &TyCtxt, path: &[&str]) -> Option<DefId> {
3957 let crates = tcx.crates();
3961 .find(|&&krate| tcx.crate_name(krate) == path[0]);
3963 if let Some(krate) = krate {
3966 index: CRATE_DEF_INDEX,
3968 let mut items = tcx.item_children(krate);
3969 let mut path_it = path.iter().skip(1).peekable();
3972 let segment = match path_it.next() {
3973 Some(segment) => segment,
3974 None => return None,
3977 for item in mem::replace(&mut items, Lrc::new(vec![])).iter() {
3978 if item.ident.name == *segment {
3979 if path_it.peek().is_none() {
3980 return match item.def {
3981 def::Def::Trait(did) => Some(did),
3986 items = tcx.item_children(item.def.def_id());
3996 pub fn get_path_for_type<F>(tcx: TyCtxt, def_id: DefId, def_ctor: F) -> hir::Path
3997 where F: Fn(DefId) -> Def {
3999 struct AbsolutePathBuffer {
4003 impl ty::item_path::ItemPathBuffer for AbsolutePathBuffer {
4004 fn root_mode(&self) -> &ty::item_path::RootMode {
4005 const ABSOLUTE: &'static ty::item_path::RootMode = &ty::item_path::RootMode::Absolute;
4009 fn push(&mut self, text: &str) {
4010 self.names.push(text.to_owned());
4014 let mut apb = AbsolutePathBuffer { names: vec![] };
4016 tcx.push_item_path(&mut apb, def_id, false);
4020 def: def_ctor(def_id),
4021 segments: hir::HirVec::from_vec(apb.names.iter().map(|s| hir::PathSegment {
4022 ident: ast::Ident::from_str(&s),
4029 // End of code copied from rust-clippy
4032 #[derive(Eq, PartialEq, Hash, Copy, Clone, Debug)]
4033 enum RegionTarget<'tcx> {
4034 Region(Region<'tcx>),
4035 RegionVid(RegionVid)
4038 #[derive(Default, Debug, Clone)]
4039 struct RegionDeps<'tcx> {
4040 larger: FxHashSet<RegionTarget<'tcx>>,
4041 smaller: FxHashSet<RegionTarget<'tcx>>
4044 #[derive(Eq, PartialEq, Hash, Debug)]
4046 TraitBound(Vec<PathSegment>, Vec<SimpleBound>, Vec<GenericParamDef>, hir::TraitBoundModifier),
4050 enum AutoTraitResult {
4052 PositiveImpl(Generics),
4056 impl AutoTraitResult {
4057 fn is_auto(&self) -> bool {
4059 AutoTraitResult::PositiveImpl(_) | AutoTraitResult::NegativeImpl => true,
4065 impl From<GenericBound> for SimpleBound {
4066 fn from(bound: GenericBound) -> Self {
4067 match bound.clone() {
4068 GenericBound::Outlives(l) => SimpleBound::Outlives(l),
4069 GenericBound::TraitBound(t, mod_) => match t.trait_ {
4070 Type::ResolvedPath { path, typarams, .. } => {
4071 SimpleBound::TraitBound(path.segments,
4073 .map_or_else(|| Vec::new(), |v| v.iter()
4074 .map(|p| SimpleBound::from(p.clone()))
4079 _ => panic!("Unexpected bound {:?}", bound),