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>> =
79 RefCell::new(FxHashMap()));
81 const FN_OUTPUT_NAME: &'static str = "Output";
83 // extract the stability index for a node from tcx, if possible
84 fn get_stability(cx: &DocContext, def_id: DefId) -> Option<Stability> {
85 cx.tcx.lookup_stability(def_id).clean(cx)
88 fn get_deprecation(cx: &DocContext, def_id: DefId) -> Option<Deprecation> {
89 cx.tcx.lookup_deprecation(def_id).clean(cx)
93 fn clean(&self, cx: &DocContext) -> T;
96 impl<T: Clean<U>, U> Clean<Vec<U>> for [T] {
97 fn clean(&self, cx: &DocContext) -> Vec<U> {
98 self.iter().map(|x| x.clean(cx)).collect()
102 impl<T: Clean<U>, U> Clean<U> for P<T> {
103 fn clean(&self, cx: &DocContext) -> U {
108 impl<T: Clean<U>, U> Clean<U> for Rc<T> {
109 fn clean(&self, cx: &DocContext) -> U {
114 impl<T: Clean<U>, U> Clean<Option<U>> for Option<T> {
115 fn clean(&self, cx: &DocContext) -> Option<U> {
116 self.as_ref().map(|v| v.clean(cx))
120 impl<T, U> Clean<U> for ty::Binder<T> where T: Clean<U> {
121 fn clean(&self, cx: &DocContext) -> U {
122 self.skip_binder().clean(cx)
126 impl<T: Clean<U>, U> Clean<Vec<U>> for P<[T]> {
127 fn clean(&self, cx: &DocContext) -> Vec<U> {
128 self.iter().map(|x| x.clean(cx)).collect()
132 #[derive(Clone, Debug)]
135 pub version: Option<String>,
137 pub module: Option<Item>,
138 pub externs: Vec<(CrateNum, ExternalCrate)>,
139 pub primitives: Vec<(DefId, PrimitiveType, Attributes)>,
140 // These are later on moved into `CACHEKEY`, leaving the map empty.
141 // Only here so that they can be filtered through the rustdoc passes.
142 pub external_traits: Arc<ReentrantMutex<RefCell<FxHashMap<DefId, Trait>>>>,
143 pub masked_crates: FxHashSet<CrateNum>,
146 impl<'a, 'tcx, 'rcx, 'cstore> Clean<Crate> for visit_ast::RustdocVisitor<'a, 'tcx, 'rcx, 'cstore> {
147 fn clean(&self, cx: &DocContext) -> Crate {
148 use ::visit_lib::LibEmbargoVisitor;
151 let mut r = cx.renderinfo.borrow_mut();
152 r.deref_trait_did = cx.tcx.lang_items().deref_trait();
153 r.deref_mut_trait_did = cx.tcx.lang_items().deref_mut_trait();
154 r.owned_box_did = cx.tcx.lang_items().owned_box();
157 let mut externs = Vec::new();
158 for &cnum in cx.tcx.crates().iter() {
159 externs.push((cnum, cnum.clean(cx)));
160 // Analyze doc-reachability for extern items
161 LibEmbargoVisitor::new(cx).visit_lib(cnum);
163 externs.sort_by(|&(a, _), &(b, _)| a.cmp(&b));
165 // Clean the crate, translating the entire libsyntax AST to one that is
166 // understood by rustdoc.
167 let mut module = self.module.clean(cx);
168 let mut masked_crates = FxHashSet();
171 ModuleItem(ref module) => {
172 for it in &module.items {
173 if it.is_extern_crate() && it.attrs.has_doc_flag("masked") {
174 masked_crates.insert(it.def_id.krate);
181 let ExternalCrate { name, src, primitives, keywords, .. } = LOCAL_CRATE.clean(cx);
183 let m = match module.inner {
184 ModuleItem(ref mut m) => m,
187 m.items.extend(primitives.iter().map(|&(def_id, prim, ref attrs)| {
189 source: Span::empty(),
190 name: Some(prim.to_url_str().to_string()),
191 attrs: attrs.clone(),
192 visibility: Some(Public),
193 stability: get_stability(cx, def_id),
194 deprecation: get_deprecation(cx, def_id),
196 inner: PrimitiveItem(prim),
199 m.items.extend(keywords.into_iter().map(|(def_id, kw, attrs)| {
201 source: Span::empty(),
202 name: Some(kw.clone()),
204 visibility: Some(Public),
205 stability: get_stability(cx, def_id),
206 deprecation: get_deprecation(cx, def_id),
208 inner: KeywordItem(kw),
217 module: Some(module),
220 external_traits: cx.external_traits.clone(),
226 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
227 pub struct ExternalCrate {
230 pub attrs: Attributes,
231 pub primitives: Vec<(DefId, PrimitiveType, Attributes)>,
232 pub keywords: Vec<(DefId, String, Attributes)>,
235 impl Clean<ExternalCrate> for CrateNum {
236 fn clean(&self, cx: &DocContext) -> ExternalCrate {
237 let root = DefId { krate: *self, index: CRATE_DEF_INDEX };
238 let krate_span = cx.tcx.def_span(root);
239 let krate_src = cx.sess().source_map().span_to_filename(krate_span);
241 // Collect all inner modules which are tagged as implementations of
244 // Note that this loop only searches the top-level items of the crate,
245 // and this is intentional. If we were to search the entire crate for an
246 // item tagged with `#[doc(primitive)]` then we would also have to
247 // search the entirety of external modules for items tagged
248 // `#[doc(primitive)]`, which is a pretty inefficient process (decoding
249 // all that metadata unconditionally).
251 // In order to keep the metadata load under control, the
252 // `#[doc(primitive)]` feature is explicitly designed to only allow the
253 // primitive tags to show up as the top level items in a crate.
255 // Also note that this does not attempt to deal with modules tagged
256 // duplicately for the same primitive. This is handled later on when
257 // rendering by delegating everything to a hash map.
258 let as_primitive = |def: Def| {
259 if let Def::Mod(def_id) = def {
260 let attrs = cx.tcx.get_attrs(def_id).clean(cx);
262 for attr in attrs.lists("doc") {
263 if let Some(v) = attr.value_str() {
264 if attr.check_name("primitive") {
265 prim = PrimitiveType::from_str(&v.as_str());
269 // FIXME: should warn on unknown primitives?
273 return prim.map(|p| (def_id, p, attrs));
277 let primitives = if root.is_local() {
278 cx.tcx.hir.krate().module.item_ids.iter().filter_map(|&id| {
279 let item = cx.tcx.hir.expect_item(id.id);
281 hir::ItemKind::Mod(_) => {
282 as_primitive(Def::Mod(cx.tcx.hir.local_def_id(id.id)))
284 hir::ItemKind::Use(ref path, hir::UseKind::Single)
285 if item.vis.node.is_pub() => {
286 as_primitive(path.def).map(|(_, prim, attrs)| {
287 // Pretend the primitive is local.
288 (cx.tcx.hir.local_def_id(id.id), prim, attrs)
295 cx.tcx.item_children(root).iter().map(|item| item.def)
296 .filter_map(as_primitive).collect()
299 let as_keyword = |def: Def| {
300 if let Def::Mod(def_id) = def {
301 let attrs = cx.tcx.get_attrs(def_id).clean(cx);
302 let mut keyword = None;
303 for attr in attrs.lists("doc") {
304 if let Some(v) = attr.value_str() {
305 if attr.check_name("keyword") {
306 keyword = Keyword::from_str(&v.as_str()).ok()
307 .map(|x| x.name().to_string());
308 if keyword.is_some() {
311 // FIXME: should warn on unknown keywords?
315 return keyword.map(|p| (def_id, p, attrs));
319 let keywords = if root.is_local() {
320 cx.tcx.hir.krate().module.item_ids.iter().filter_map(|&id| {
321 let item = cx.tcx.hir.expect_item(id.id);
323 hir::ItemKind::Mod(_) => {
324 as_keyword(Def::Mod(cx.tcx.hir.local_def_id(id.id)))
326 hir::ItemKind::Use(ref path, hir::UseKind::Single)
327 if item.vis.node.is_pub() => {
328 as_keyword(path.def).map(|(_, prim, attrs)| {
329 (cx.tcx.hir.local_def_id(id.id), prim, attrs)
336 cx.tcx.item_children(root).iter().map(|item| item.def)
337 .filter_map(as_keyword).collect()
341 name: cx.tcx.crate_name(*self).to_string(),
343 attrs: cx.tcx.get_attrs(root).clean(cx),
350 /// Anything with a source location and set of attributes and, optionally, a
351 /// name. That is, anything that can be documented. This doesn't correspond
352 /// directly to the AST's concept of an item; it's a strict superset.
353 #[derive(Clone, RustcEncodable, RustcDecodable)]
357 /// Not everything has a name. E.g., impls
358 pub name: Option<String>,
359 pub attrs: Attributes,
361 pub visibility: Option<Visibility>,
363 pub stability: Option<Stability>,
364 pub deprecation: Option<Deprecation>,
367 impl fmt::Debug for Item {
368 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
370 let fake = MAX_DEF_ID.with(|m| m.borrow().get(&self.def_id.krate)
371 .map(|id| self.def_id >= *id).unwrap_or(false));
372 let def_id: &dyn fmt::Debug = if fake { &"**FAKE**" } else { &self.def_id };
374 fmt.debug_struct("Item")
375 .field("source", &self.source)
376 .field("name", &self.name)
377 .field("attrs", &self.attrs)
378 .field("inner", &self.inner)
379 .field("visibility", &self.visibility)
380 .field("def_id", def_id)
381 .field("stability", &self.stability)
382 .field("deprecation", &self.deprecation)
388 /// Finds the `doc` attribute as a NameValue and returns the corresponding
390 pub fn doc_value<'a>(&'a self) -> Option<&'a str> {
391 self.attrs.doc_value()
393 /// Finds all `doc` attributes as NameValues and returns their corresponding values, joined
395 pub fn collapsed_doc_value(&self) -> Option<String> {
396 self.attrs.collapsed_doc_value()
399 pub fn links(&self) -> Vec<(String, String)> {
400 self.attrs.links(&self.def_id.krate)
403 pub fn is_crate(&self) -> bool {
405 StrippedItem(box ModuleItem(Module { is_crate: true, ..})) |
406 ModuleItem(Module { is_crate: true, ..}) => true,
410 pub fn is_mod(&self) -> bool {
411 self.type_() == ItemType::Module
413 pub fn is_trait(&self) -> bool {
414 self.type_() == ItemType::Trait
416 pub fn is_struct(&self) -> bool {
417 self.type_() == ItemType::Struct
419 pub fn is_enum(&self) -> bool {
420 self.type_() == ItemType::Enum
422 pub fn is_associated_type(&self) -> bool {
423 self.type_() == ItemType::AssociatedType
425 pub fn is_associated_const(&self) -> bool {
426 self.type_() == ItemType::AssociatedConst
428 pub fn is_method(&self) -> bool {
429 self.type_() == ItemType::Method
431 pub fn is_ty_method(&self) -> bool {
432 self.type_() == ItemType::TyMethod
434 pub fn is_typedef(&self) -> bool {
435 self.type_() == ItemType::Typedef
437 pub fn is_primitive(&self) -> bool {
438 self.type_() == ItemType::Primitive
440 pub fn is_union(&self) -> bool {
441 self.type_() == ItemType::Union
443 pub fn is_import(&self) -> bool {
444 self.type_() == ItemType::Import
446 pub fn is_extern_crate(&self) -> bool {
447 self.type_() == ItemType::ExternCrate
449 pub fn is_keyword(&self) -> bool {
450 self.type_() == ItemType::Keyword
453 pub fn is_stripped(&self) -> bool {
454 match self.inner { StrippedItem(..) => true, _ => false }
456 pub fn has_stripped_fields(&self) -> Option<bool> {
458 StructItem(ref _struct) => Some(_struct.fields_stripped),
459 UnionItem(ref union) => Some(union.fields_stripped),
460 VariantItem(Variant { kind: VariantKind::Struct(ref vstruct)} ) => {
461 Some(vstruct.fields_stripped)
467 pub fn stability_class(&self) -> Option<String> {
468 self.stability.as_ref().and_then(|ref s| {
469 let mut classes = Vec::with_capacity(2);
471 if s.level == stability::Unstable {
472 classes.push("unstable");
475 if !s.deprecated_since.is_empty() {
476 classes.push("deprecated");
479 if classes.len() != 0 {
480 Some(classes.join(" "))
487 pub fn stable_since(&self) -> Option<&str> {
488 self.stability.as_ref().map(|s| &s.since[..])
491 pub fn is_non_exhaustive(&self) -> bool {
492 self.attrs.other_attrs.iter()
493 .any(|a| a.name().as_str() == "non_exhaustive")
496 /// Returns a documentation-level item type from the item.
497 pub fn type_(&self) -> ItemType {
502 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
504 ExternCrateItem(String, Option<String>),
509 FunctionItem(Function),
511 TypedefItem(Typedef, bool /* is associated type */),
512 ExistentialItem(Existential, bool /* is associated type */),
514 ConstantItem(Constant),
517 /// A method signature only. Used for required methods in traits (ie,
518 /// non-default-methods).
519 TyMethodItem(TyMethod),
520 /// A method with a body.
522 StructFieldItem(Type),
523 VariantItem(Variant),
524 /// `fn`s from an extern block
525 ForeignFunctionItem(Function),
526 /// `static`s from an extern block
527 ForeignStaticItem(Static),
528 /// `type`s from an extern block
531 ProcMacroItem(ProcMacro),
532 PrimitiveItem(PrimitiveType),
533 AssociatedConstItem(Type, Option<String>),
534 AssociatedTypeItem(Vec<GenericBound>, Option<Type>),
535 /// An item that has been stripped by a rustdoc pass
536 StrippedItem(Box<ItemEnum>),
541 pub fn generics(&self) -> Option<&Generics> {
543 ItemEnum::StructItem(ref s) => &s.generics,
544 ItemEnum::EnumItem(ref e) => &e.generics,
545 ItemEnum::FunctionItem(ref f) => &f.generics,
546 ItemEnum::TypedefItem(ref t, _) => &t.generics,
547 ItemEnum::ExistentialItem(ref t, _) => &t.generics,
548 ItemEnum::TraitItem(ref t) => &t.generics,
549 ItemEnum::ImplItem(ref i) => &i.generics,
550 ItemEnum::TyMethodItem(ref i) => &i.generics,
551 ItemEnum::MethodItem(ref i) => &i.generics,
552 ItemEnum::ForeignFunctionItem(ref f) => &f.generics,
558 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
560 pub items: Vec<Item>,
564 impl Clean<Item> for doctree::Module {
565 fn clean(&self, cx: &DocContext) -> Item {
566 let name = if self.name.is_some() {
567 self.name.expect("No name provided").clean(cx)
572 // maintain a stack of mod ids, for doc comment path resolution
573 // but we also need to resolve the module's own docs based on whether its docs were written
574 // inside or outside the module, so check for that
575 let attrs = self.attrs.clean(cx);
577 let mut items: Vec<Item> = vec![];
578 items.extend(self.extern_crates.iter().map(|x| x.clean(cx)));
579 items.extend(self.imports.iter().flat_map(|x| x.clean(cx)));
580 items.extend(self.structs.iter().map(|x| x.clean(cx)));
581 items.extend(self.unions.iter().map(|x| x.clean(cx)));
582 items.extend(self.enums.iter().map(|x| x.clean(cx)));
583 items.extend(self.fns.iter().map(|x| x.clean(cx)));
584 items.extend(self.foreigns.iter().flat_map(|x| x.clean(cx)));
585 items.extend(self.mods.iter().map(|x| x.clean(cx)));
586 items.extend(self.typedefs.iter().map(|x| x.clean(cx)));
587 items.extend(self.existentials.iter().map(|x| x.clean(cx)));
588 items.extend(self.statics.iter().map(|x| x.clean(cx)));
589 items.extend(self.constants.iter().map(|x| x.clean(cx)));
590 items.extend(self.traits.iter().map(|x| x.clean(cx)));
591 items.extend(self.impls.iter().flat_map(|x| x.clean(cx)));
592 items.extend(self.macros.iter().map(|x| x.clean(cx)));
593 items.extend(self.proc_macros.iter().map(|x| x.clean(cx)));
595 // determine if we should display the inner contents or
596 // the outer `mod` item for the source code.
598 let cm = cx.sess().source_map();
599 let outer = cm.lookup_char_pos(self.where_outer.lo());
600 let inner = cm.lookup_char_pos(self.where_inner.lo());
601 if outer.file.start_pos == inner.file.start_pos {
605 // mod foo; (and a separate SourceFile for the contents)
613 source: whence.clean(cx),
614 visibility: self.vis.clean(cx),
615 stability: self.stab.clean(cx),
616 deprecation: self.depr.clean(cx),
617 def_id: cx.tcx.hir.local_def_id(self.id),
618 inner: ModuleItem(Module {
619 is_crate: self.is_crate,
626 pub struct ListAttributesIter<'a> {
627 attrs: slice::Iter<'a, ast::Attribute>,
628 current_list: vec::IntoIter<ast::NestedMetaItem>,
632 impl<'a> Iterator for ListAttributesIter<'a> {
633 type Item = ast::NestedMetaItem;
635 fn next(&mut self) -> Option<Self::Item> {
636 if let Some(nested) = self.current_list.next() {
640 for attr in &mut self.attrs {
641 if let Some(list) = attr.meta_item_list() {
642 if attr.check_name(self.name) {
643 self.current_list = list.into_iter();
644 if let Some(nested) = self.current_list.next() {
654 fn size_hint(&self) -> (usize, Option<usize>) {
655 let lower = self.current_list.len();
660 pub trait AttributesExt {
661 /// Finds an attribute as List and returns the list of attributes nested inside.
662 fn lists<'a>(&'a self, name: &'a str) -> ListAttributesIter<'a>;
665 impl AttributesExt for [ast::Attribute] {
666 fn lists<'a>(&'a self, name: &'a str) -> ListAttributesIter<'a> {
669 current_list: Vec::new().into_iter(),
675 pub trait NestedAttributesExt {
676 /// Returns whether the attribute list contains a specific `Word`
677 fn has_word(self, word: &str) -> bool;
680 impl<I: IntoIterator<Item=ast::NestedMetaItem>> NestedAttributesExt for I {
681 fn has_word(self, word: &str) -> bool {
682 self.into_iter().any(|attr| attr.is_word() && attr.check_name(word))
686 /// A portion of documentation, extracted from a `#[doc]` attribute.
688 /// Each variant contains the line number within the complete doc-comment where the fragment
689 /// starts, as well as the Span where the corresponding doc comment or attribute is located.
691 /// Included files are kept separate from inline doc comments so that proper line-number
692 /// information can be given when a doctest fails. Sugared doc comments and "raw" doc comments are
693 /// kept separate because of issue #42760.
694 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
695 pub enum DocFragment {
696 // FIXME #44229 (misdreavus): sugared and raw doc comments can be brought back together once
697 // hoedown is completely removed from rustdoc.
698 /// A doc fragment created from a `///` or `//!` doc comment.
699 SugaredDoc(usize, syntax_pos::Span, String),
700 /// A doc fragment created from a "raw" `#[doc=""]` attribute.
701 RawDoc(usize, syntax_pos::Span, String),
702 /// A doc fragment created from a `#[doc(include="filename")]` attribute. Contains both the
703 /// given filename and the file contents.
704 Include(usize, syntax_pos::Span, String, String),
708 pub fn as_str(&self) -> &str {
710 DocFragment::SugaredDoc(_, _, ref s) => &s[..],
711 DocFragment::RawDoc(_, _, ref s) => &s[..],
712 DocFragment::Include(_, _, _, ref s) => &s[..],
716 pub fn span(&self) -> syntax_pos::Span {
718 DocFragment::SugaredDoc(_, span, _) |
719 DocFragment::RawDoc(_, span, _) |
720 DocFragment::Include(_, span, _, _) => span,
725 impl<'a> FromIterator<&'a DocFragment> for String {
726 fn from_iter<T>(iter: T) -> Self
728 T: IntoIterator<Item = &'a DocFragment>
730 iter.into_iter().fold(String::new(), |mut acc, frag| {
735 DocFragment::SugaredDoc(_, _, ref docs)
736 | DocFragment::RawDoc(_, _, ref docs)
737 | DocFragment::Include(_, _, _, ref docs) =>
746 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, Default)]
747 pub struct Attributes {
748 pub doc_strings: Vec<DocFragment>,
749 pub other_attrs: Vec<ast::Attribute>,
750 pub cfg: Option<Arc<Cfg>>,
751 pub span: Option<syntax_pos::Span>,
752 /// map from Rust paths to resolved defs and potential URL fragments
753 pub links: Vec<(String, Option<DefId>, Option<String>)>,
754 pub inner_docs: bool,
758 /// Extracts the content from an attribute `#[doc(cfg(content))]`.
759 fn extract_cfg(mi: &ast::MetaItem) -> Option<&ast::MetaItem> {
760 use syntax::ast::NestedMetaItemKind::MetaItem;
762 if let ast::MetaItemKind::List(ref nmis) = mi.node {
764 if let MetaItem(ref cfg_mi) = nmis[0].node {
765 if cfg_mi.check_name("cfg") {
766 if let ast::MetaItemKind::List(ref cfg_nmis) = cfg_mi.node {
767 if cfg_nmis.len() == 1 {
768 if let MetaItem(ref content_mi) = cfg_nmis[0].node {
769 return Some(content_mi);
781 /// Reads a `MetaItem` from within an attribute, looks for whether it is a
782 /// `#[doc(include="file")]`, and returns the filename and contents of the file as loaded from
784 fn extract_include(mi: &ast::MetaItem)
785 -> Option<(String, String)>
787 mi.meta_item_list().and_then(|list| {
789 if meta.check_name("include") {
790 // the actual compiled `#[doc(include="filename")]` gets expanded to
791 // `#[doc(include(file="filename", contents="file contents")]` so we need to
792 // look for that instead
793 return meta.meta_item_list().and_then(|list| {
794 let mut filename: Option<String> = None;
795 let mut contents: Option<String> = None;
798 if it.check_name("file") {
799 if let Some(name) = it.value_str() {
800 filename = Some(name.to_string());
802 } else if it.check_name("contents") {
803 if let Some(docs) = it.value_str() {
804 contents = Some(docs.to_string());
809 if let (Some(filename), Some(contents)) = (filename, contents) {
810 Some((filename, contents))
822 pub fn has_doc_flag(&self, flag: &str) -> bool {
823 for attr in &self.other_attrs {
824 if !attr.check_name("doc") { continue; }
826 if let Some(items) = attr.meta_item_list() {
827 if items.iter().filter_map(|i| i.meta_item()).any(|it| it.check_name(flag)) {
836 pub fn from_ast(diagnostic: &::errors::Handler,
837 attrs: &[ast::Attribute]) -> Attributes {
838 let mut doc_strings = vec![];
840 let mut cfg = Cfg::True;
841 let mut doc_line = 0;
843 let other_attrs = attrs.iter().filter_map(|attr| {
844 attr.with_desugared_doc(|attr| {
845 if attr.check_name("doc") {
846 if let Some(mi) = attr.meta() {
847 if let Some(value) = mi.value_str() {
848 // Extracted #[doc = "..."]
849 let value = value.to_string();
851 doc_line += value.lines().count();
853 if attr.is_sugared_doc {
854 doc_strings.push(DocFragment::SugaredDoc(line, attr.span, value));
856 doc_strings.push(DocFragment::RawDoc(line, attr.span, value));
860 sp = Some(attr.span);
863 } else if let Some(cfg_mi) = Attributes::extract_cfg(&mi) {
864 // Extracted #[doc(cfg(...))]
865 match Cfg::parse(cfg_mi) {
866 Ok(new_cfg) => cfg &= new_cfg,
867 Err(e) => diagnostic.span_err(e.span, e.msg),
870 } else if let Some((filename, contents)) = Attributes::extract_include(&mi)
873 doc_line += contents.lines().count();
874 doc_strings.push(DocFragment::Include(line,
885 // treat #[target_feature(enable = "feat")] attributes as if they were
886 // #[doc(cfg(target_feature = "feat"))] attributes as well
887 for attr in attrs.lists("target_feature") {
888 if attr.check_name("enable") {
889 if let Some(feat) = attr.value_str() {
890 let meta = attr::mk_name_value_item_str(Ident::from_str("target_feature"),
891 dummy_spanned(feat));
892 if let Ok(feat_cfg) = Cfg::parse(&meta) {
899 let inner_docs = attrs.iter()
900 .filter(|a| a.check_name("doc"))
902 .map_or(true, |a| a.style == AttrStyle::Inner);
907 cfg: if cfg == Cfg::True { None } else { Some(Arc::new(cfg)) },
914 /// Finds the `doc` attribute as a NameValue and returns the corresponding
916 pub fn doc_value<'a>(&'a self) -> Option<&'a str> {
917 self.doc_strings.first().map(|s| s.as_str())
920 /// Finds all `doc` attributes as NameValues and returns their corresponding values, joined
922 pub fn collapsed_doc_value(&self) -> Option<String> {
923 if !self.doc_strings.is_empty() {
924 Some(self.doc_strings.iter().collect())
930 /// Get links as a vector
932 /// Cache must be populated before call
933 pub fn links(&self, krate: &CrateNum) -> Vec<(String, String)> {
934 use html::format::href;
935 self.links.iter().filter_map(|&(ref s, did, ref fragment)| {
938 if let Some((mut href, ..)) = href(did) {
939 if let Some(ref fragment) = *fragment {
941 href.push_str(fragment);
943 Some((s.clone(), href))
949 if let Some(ref fragment) = *fragment {
951 let url = match cache.extern_locations.get(krate) {
952 Some(&(_, ref src, ExternalLocation::Local)) =>
953 src.to_str().expect("invalid file path"),
954 Some(&(_, _, ExternalLocation::Remote(ref s))) => s,
955 Some(&(_, _, ExternalLocation::Unknown)) | None =>
956 "https://doc.rust-lang.org/nightly",
958 // This is a primitive so the url is done "by hand".
960 format!("{}{}std/primitive.{}.html",
962 if !url.ends_with('/') { "/" } else { "" },
965 panic!("This isn't a primitive?!");
973 impl PartialEq for Attributes {
974 fn eq(&self, rhs: &Self) -> bool {
975 self.doc_strings == rhs.doc_strings &&
976 self.cfg == rhs.cfg &&
977 self.span == rhs.span &&
978 self.links == rhs.links &&
979 self.other_attrs.iter().map(|attr| attr.id).eq(rhs.other_attrs.iter().map(|attr| attr.id))
983 impl Eq for Attributes {}
985 impl Hash for Attributes {
986 fn hash<H: Hasher>(&self, hasher: &mut H) {
987 self.doc_strings.hash(hasher);
988 self.cfg.hash(hasher);
989 self.span.hash(hasher);
990 self.links.hash(hasher);
991 for attr in &self.other_attrs {
992 attr.id.hash(hasher);
997 impl AttributesExt for Attributes {
998 fn lists<'a>(&'a self, name: &'a str) -> ListAttributesIter<'a> {
999 self.other_attrs.lists(name)
1003 impl Clean<Attributes> for [ast::Attribute] {
1004 fn clean(&self, cx: &DocContext) -> Attributes {
1005 Attributes::from_ast(cx.sess().diagnostic(), self)
1009 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1010 pub enum GenericBound {
1011 TraitBound(PolyTrait, hir::TraitBoundModifier),
1016 fn maybe_sized(cx: &DocContext) -> GenericBound {
1017 let did = cx.tcx.require_lang_item(lang_items::SizedTraitLangItem);
1018 let empty = cx.tcx.intern_substs(&[]);
1019 let path = external_path(cx, &cx.tcx.item_name(did).as_str(),
1020 Some(did), false, vec![], empty);
1021 inline::record_extern_fqn(cx, did, TypeKind::Trait);
1022 GenericBound::TraitBound(PolyTrait {
1023 trait_: ResolvedPath {
1029 generic_params: Vec::new(),
1030 }, hir::TraitBoundModifier::Maybe)
1033 fn is_sized_bound(&self, cx: &DocContext) -> bool {
1034 use rustc::hir::TraitBoundModifier as TBM;
1035 if let GenericBound::TraitBound(PolyTrait { ref trait_, .. }, TBM::None) = *self {
1036 if trait_.def_id() == cx.tcx.lang_items().sized_trait() {
1043 fn get_poly_trait(&self) -> Option<PolyTrait> {
1044 if let GenericBound::TraitBound(ref p, _) = *self {
1045 return Some(p.clone())
1050 fn get_trait_type(&self) -> Option<Type> {
1051 if let GenericBound::TraitBound(PolyTrait { ref trait_, .. }, _) = *self {
1052 return Some(trait_.clone());
1058 impl Clean<GenericBound> for hir::GenericBound {
1059 fn clean(&self, cx: &DocContext) -> GenericBound {
1061 hir::GenericBound::Outlives(lt) => GenericBound::Outlives(lt.clean(cx)),
1062 hir::GenericBound::Trait(ref t, modifier) => {
1063 GenericBound::TraitBound(t.clean(cx), modifier)
1069 fn external_generic_args(cx: &DocContext, trait_did: Option<DefId>, has_self: bool,
1070 bindings: Vec<TypeBinding>, substs: &Substs) -> GenericArgs {
1071 let lifetimes = substs.regions().filter_map(|v| v.clean(cx)).collect();
1072 let types = substs.types().skip(has_self as usize).collect::<Vec<_>>();
1075 // Attempt to sugar an external path like Fn<(A, B,), C> to Fn(A, B) -> C
1076 Some(did) if cx.tcx.lang_items().fn_trait_kind(did).is_some() => {
1077 assert_eq!(types.len(), 1);
1078 let inputs = match types[0].sty {
1079 ty::Tuple(ref tys) => tys.iter().map(|t| t.clean(cx)).collect(),
1081 return GenericArgs::AngleBracketed {
1083 types: types.clean(cx),
1089 // FIXME(#20299) return type comes from a projection now
1090 // match types[1].sty {
1091 // ty::Tuple(ref v) if v.is_empty() => None, // -> ()
1092 // _ => Some(types[1].clean(cx))
1094 GenericArgs::Parenthesized {
1100 GenericArgs::AngleBracketed {
1102 types: types.clean(cx),
1109 // trait_did should be set to a trait's DefId if called on a TraitRef, in order to sugar
1110 // from Fn<(A, B,), C> to Fn(A, B) -> C
1111 fn external_path(cx: &DocContext, name: &str, trait_did: Option<DefId>, has_self: bool,
1112 bindings: Vec<TypeBinding>, substs: &Substs) -> Path {
1116 segments: vec![PathSegment {
1117 name: name.to_string(),
1118 args: external_generic_args(cx, trait_did, has_self, bindings, substs)
1123 impl<'a, 'tcx> Clean<GenericBound> for (&'a ty::TraitRef<'tcx>, Vec<TypeBinding>) {
1124 fn clean(&self, cx: &DocContext) -> GenericBound {
1125 let (trait_ref, ref bounds) = *self;
1126 inline::record_extern_fqn(cx, trait_ref.def_id, TypeKind::Trait);
1127 let path = external_path(cx, &cx.tcx.item_name(trait_ref.def_id).as_str(),
1128 Some(trait_ref.def_id), true, bounds.clone(), trait_ref.substs);
1130 debug!("ty::TraitRef\n subst: {:?}\n", trait_ref.substs);
1132 // collect any late bound regions
1133 let mut late_bounds = vec![];
1134 for ty_s in trait_ref.input_types().skip(1) {
1135 if let ty::Tuple(ts) = ty_s.sty {
1137 if let ty::Ref(ref reg, _, _) = ty_s.sty {
1138 if let &ty::RegionKind::ReLateBound(..) = *reg {
1139 debug!(" hit an ReLateBound {:?}", reg);
1140 if let Some(Lifetime(name)) = reg.clean(cx) {
1141 late_bounds.push(GenericParamDef {
1143 kind: GenericParamDefKind::Lifetime,
1152 GenericBound::TraitBound(
1154 trait_: ResolvedPath {
1157 did: trait_ref.def_id,
1160 generic_params: late_bounds,
1162 hir::TraitBoundModifier::None
1167 impl<'tcx> Clean<GenericBound> for ty::TraitRef<'tcx> {
1168 fn clean(&self, cx: &DocContext) -> GenericBound {
1169 (self, vec![]).clean(cx)
1173 impl<'tcx> Clean<Option<Vec<GenericBound>>> for Substs<'tcx> {
1174 fn clean(&self, cx: &DocContext) -> Option<Vec<GenericBound>> {
1175 let mut v = Vec::new();
1176 v.extend(self.regions().filter_map(|r| r.clean(cx)).map(GenericBound::Outlives));
1177 v.extend(self.types().map(|t| GenericBound::TraitBound(PolyTrait {
1178 trait_: t.clean(cx),
1179 generic_params: Vec::new(),
1180 }, hir::TraitBoundModifier::None)));
1181 if !v.is_empty() {Some(v)} else {None}
1185 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1186 pub struct Lifetime(String);
1189 pub fn get_ref<'a>(&'a self) -> &'a str {
1190 let Lifetime(ref s) = *self;
1195 pub fn statik() -> Lifetime {
1196 Lifetime("'static".to_string())
1200 impl Clean<Lifetime> for hir::Lifetime {
1201 fn clean(&self, cx: &DocContext) -> Lifetime {
1202 if self.id != ast::DUMMY_NODE_ID {
1203 let hir_id = cx.tcx.hir.node_to_hir_id(self.id);
1204 let def = cx.tcx.named_region(hir_id);
1206 Some(rl::Region::EarlyBound(_, node_id, _)) |
1207 Some(rl::Region::LateBound(_, node_id, _)) |
1208 Some(rl::Region::Free(_, node_id)) => {
1209 if let Some(lt) = cx.lt_substs.borrow().get(&node_id).cloned() {
1216 Lifetime(self.name.ident().to_string())
1220 impl Clean<Lifetime> for hir::GenericParam {
1221 fn clean(&self, _: &DocContext) -> Lifetime {
1223 hir::GenericParamKind::Lifetime { .. } => {
1224 if self.bounds.len() > 0 {
1225 let mut bounds = self.bounds.iter().map(|bound| match bound {
1226 hir::GenericBound::Outlives(lt) => lt,
1229 let name = bounds.next().expect("no more bounds").name.ident();
1230 let mut s = format!("{}: {}", self.name.ident(), name);
1231 for bound in bounds {
1232 s.push_str(&format!(" + {}", bound.name.ident()));
1236 Lifetime(self.name.ident().to_string())
1244 impl<'tcx> Clean<Lifetime> for ty::GenericParamDef {
1245 fn clean(&self, _cx: &DocContext) -> Lifetime {
1246 Lifetime(self.name.to_string())
1250 impl Clean<Option<Lifetime>> for ty::RegionKind {
1251 fn clean(&self, cx: &DocContext) -> Option<Lifetime> {
1253 ty::ReStatic => Some(Lifetime::statik()),
1254 ty::ReLateBound(_, ty::BrNamed(_, name)) => Some(Lifetime(name.to_string())),
1255 ty::ReEarlyBound(ref data) => Some(Lifetime(data.name.clean(cx))),
1257 ty::ReLateBound(..) |
1261 ty::RePlaceholder(..) |
1263 ty::ReClosureBound(_) |
1264 ty::ReCanonical(_) |
1265 ty::ReErased => None
1270 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1271 pub enum WherePredicate {
1272 BoundPredicate { ty: Type, bounds: Vec<GenericBound> },
1273 RegionPredicate { lifetime: Lifetime, bounds: Vec<GenericBound> },
1274 EqPredicate { lhs: Type, rhs: Type },
1277 impl Clean<WherePredicate> for hir::WherePredicate {
1278 fn clean(&self, cx: &DocContext) -> WherePredicate {
1280 hir::WherePredicate::BoundPredicate(ref wbp) => {
1281 WherePredicate::BoundPredicate {
1282 ty: wbp.bounded_ty.clean(cx),
1283 bounds: wbp.bounds.clean(cx)
1287 hir::WherePredicate::RegionPredicate(ref wrp) => {
1288 WherePredicate::RegionPredicate {
1289 lifetime: wrp.lifetime.clean(cx),
1290 bounds: wrp.bounds.clean(cx)
1294 hir::WherePredicate::EqPredicate(ref wrp) => {
1295 WherePredicate::EqPredicate {
1296 lhs: wrp.lhs_ty.clean(cx),
1297 rhs: wrp.rhs_ty.clean(cx)
1304 impl<'a> Clean<WherePredicate> for ty::Predicate<'a> {
1305 fn clean(&self, cx: &DocContext) -> WherePredicate {
1306 use rustc::ty::Predicate;
1309 Predicate::Trait(ref pred) => pred.clean(cx),
1310 Predicate::Subtype(ref pred) => pred.clean(cx),
1311 Predicate::RegionOutlives(ref pred) => pred.clean(cx),
1312 Predicate::TypeOutlives(ref pred) => pred.clean(cx),
1313 Predicate::Projection(ref pred) => pred.clean(cx),
1314 Predicate::WellFormed(ty) => {
1315 // This comes from `where Ty:` (i.e. no bounds) (see #53696).
1316 WherePredicate::BoundPredicate {
1321 Predicate::ObjectSafe(_) => panic!("not user writable"),
1322 Predicate::ClosureKind(..) => panic!("not user writable"),
1323 Predicate::ConstEvaluatable(..) => panic!("not user writable"),
1328 impl<'a> Clean<WherePredicate> for ty::TraitPredicate<'a> {
1329 fn clean(&self, cx: &DocContext) -> WherePredicate {
1330 WherePredicate::BoundPredicate {
1331 ty: self.trait_ref.self_ty().clean(cx),
1332 bounds: vec![self.trait_ref.clean(cx)]
1337 impl<'tcx> Clean<WherePredicate> for ty::SubtypePredicate<'tcx> {
1338 fn clean(&self, _cx: &DocContext) -> WherePredicate {
1339 panic!("subtype predicates are an internal rustc artifact \
1340 and should not be seen by rustdoc")
1344 impl<'tcx> Clean<WherePredicate> for ty::OutlivesPredicate<ty::Region<'tcx>, ty::Region<'tcx>> {
1345 fn clean(&self, cx: &DocContext) -> WherePredicate {
1346 let ty::OutlivesPredicate(ref a, ref b) = *self;
1347 WherePredicate::RegionPredicate {
1348 lifetime: a.clean(cx).expect("failed to clean lifetime"),
1349 bounds: vec![GenericBound::Outlives(b.clean(cx).expect("failed to clean bounds"))]
1354 impl<'tcx> Clean<WherePredicate> for ty::OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>> {
1355 fn clean(&self, cx: &DocContext) -> WherePredicate {
1356 let ty::OutlivesPredicate(ref ty, ref lt) = *self;
1358 WherePredicate::BoundPredicate {
1360 bounds: vec![GenericBound::Outlives(lt.clean(cx).expect("failed to clean lifetimes"))]
1365 impl<'tcx> Clean<WherePredicate> for ty::ProjectionPredicate<'tcx> {
1366 fn clean(&self, cx: &DocContext) -> WherePredicate {
1367 WherePredicate::EqPredicate {
1368 lhs: self.projection_ty.clean(cx),
1369 rhs: self.ty.clean(cx)
1374 impl<'tcx> Clean<Type> for ty::ProjectionTy<'tcx> {
1375 fn clean(&self, cx: &DocContext) -> Type {
1376 let trait_ = match self.trait_ref(cx.tcx).clean(cx) {
1377 GenericBound::TraitBound(t, _) => t.trait_,
1378 GenericBound::Outlives(_) => panic!("cleaning a trait got a lifetime"),
1381 name: cx.tcx.associated_item(self.item_def_id).ident.name.clean(cx),
1382 self_type: box self.self_ty().clean(cx),
1388 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1389 pub enum GenericParamDefKind {
1393 bounds: Vec<GenericBound>,
1394 default: Option<Type>,
1395 synthetic: Option<hir::SyntheticTyParamKind>,
1399 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1400 pub struct GenericParamDef {
1403 pub kind: GenericParamDefKind,
1406 impl GenericParamDef {
1407 pub fn is_synthetic_type_param(&self) -> bool {
1409 GenericParamDefKind::Lifetime => false,
1410 GenericParamDefKind::Type { ref synthetic, .. } => synthetic.is_some(),
1415 impl<'tcx> Clean<GenericParamDef> for ty::GenericParamDef {
1416 fn clean(&self, cx: &DocContext) -> GenericParamDef {
1417 let (name, kind) = match self.kind {
1418 ty::GenericParamDefKind::Lifetime => {
1419 (self.name.to_string(), GenericParamDefKind::Lifetime)
1421 ty::GenericParamDefKind::Type { has_default, .. } => {
1422 cx.renderinfo.borrow_mut().external_typarams
1423 .insert(self.def_id, self.name.clean(cx));
1424 let default = if has_default {
1425 Some(cx.tcx.type_of(self.def_id).clean(cx))
1429 (self.name.clean(cx), GenericParamDefKind::Type {
1431 bounds: vec![], // These are filled in from the where-clauses.
1445 impl Clean<GenericParamDef> for hir::GenericParam {
1446 fn clean(&self, cx: &DocContext) -> GenericParamDef {
1447 let (name, kind) = match self.kind {
1448 hir::GenericParamKind::Lifetime { .. } => {
1449 let name = if self.bounds.len() > 0 {
1450 let mut bounds = self.bounds.iter().map(|bound| match bound {
1451 hir::GenericBound::Outlives(lt) => lt,
1454 let name = bounds.next().expect("no more bounds").name.ident();
1455 let mut s = format!("{}: {}", self.name.ident(), name);
1456 for bound in bounds {
1457 s.push_str(&format!(" + {}", bound.name.ident()));
1461 self.name.ident().to_string()
1463 (name, GenericParamDefKind::Lifetime)
1465 hir::GenericParamKind::Type { ref default, synthetic, .. } => {
1466 (self.name.ident().name.clean(cx), GenericParamDefKind::Type {
1467 did: cx.tcx.hir.local_def_id(self.id),
1468 bounds: self.bounds.clean(cx),
1469 default: default.clean(cx),
1470 synthetic: synthetic,
1482 // maybe use a Generic enum and use Vec<Generic>?
1483 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Default, Hash)]
1484 pub struct Generics {
1485 pub params: Vec<GenericParamDef>,
1486 pub where_predicates: Vec<WherePredicate>,
1489 impl Clean<Generics> for hir::Generics {
1490 fn clean(&self, cx: &DocContext) -> Generics {
1491 // Synthetic type-parameters are inserted after normal ones.
1492 // In order for normal parameters to be able to refer to synthetic ones,
1493 // scans them first.
1494 fn is_impl_trait(param: &hir::GenericParam) -> bool {
1496 hir::GenericParamKind::Type { synthetic, .. } => {
1497 synthetic == Some(hir::SyntheticTyParamKind::ImplTrait)
1502 let impl_trait_params = self.params
1504 .filter(|param| is_impl_trait(param))
1506 let param: GenericParamDef = param.clean(cx);
1508 GenericParamDefKind::Lifetime => unreachable!(),
1509 GenericParamDefKind::Type { did, ref bounds, .. } => {
1510 cx.impl_trait_bounds.borrow_mut().insert(did, bounds.clone());
1515 .collect::<Vec<_>>();
1517 let mut params = Vec::with_capacity(self.params.len());
1518 for p in self.params.iter().filter(|p| !is_impl_trait(p)) {
1519 let p = p.clean(cx);
1522 params.extend(impl_trait_params);
1524 let mut generics = Generics {
1526 where_predicates: self.where_clause.predicates.clean(cx),
1529 // Some duplicates are generated for ?Sized bounds between type params and where
1530 // predicates. The point in here is to move the bounds definitions from type params
1531 // to where predicates when such cases occur.
1532 for where_pred in &mut generics.where_predicates {
1534 WherePredicate::BoundPredicate { ty: Generic(ref name), ref mut bounds } => {
1535 if bounds.is_empty() {
1536 for param in &mut generics.params {
1538 GenericParamDefKind::Lifetime => {}
1539 GenericParamDefKind::Type { bounds: ref mut ty_bounds, .. } => {
1540 if ¶m.name == name {
1541 mem::swap(bounds, ty_bounds);
1556 impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics,
1557 &'a ty::GenericPredicates<'tcx>) {
1558 fn clean(&self, cx: &DocContext) -> Generics {
1559 use self::WherePredicate as WP;
1561 let (gens, preds) = *self;
1563 // Bounds in the type_params and lifetimes fields are repeated in the
1564 // predicates field (see rustc_typeck::collect::ty_generics), so remove
1566 let stripped_typarams = gens.params.iter().filter_map(|param| match param.kind {
1567 ty::GenericParamDefKind::Lifetime => None,
1568 ty::GenericParamDefKind::Type { .. } => {
1569 if param.name == keywords::SelfType.name().as_str() {
1570 assert_eq!(param.index, 0);
1573 Some(param.clean(cx))
1575 }).collect::<Vec<GenericParamDef>>();
1577 let mut where_predicates = preds.predicates.iter()
1578 .map(|(p, _)| p.clean(cx))
1579 .collect::<Vec<_>>();
1581 // Type parameters and have a Sized bound by default unless removed with
1582 // ?Sized. Scan through the predicates and mark any type parameter with
1583 // a Sized bound, removing the bounds as we find them.
1585 // Note that associated types also have a sized bound by default, but we
1586 // don't actually know the set of associated types right here so that's
1587 // handled in cleaning associated types
1588 let mut sized_params = FxHashSet();
1589 where_predicates.retain(|pred| {
1591 WP::BoundPredicate { ty: Generic(ref g), ref bounds } => {
1592 if bounds.iter().any(|b| b.is_sized_bound(cx)) {
1593 sized_params.insert(g.clone());
1603 // Run through the type parameters again and insert a ?Sized
1604 // unbound for any we didn't find to be Sized.
1605 for tp in &stripped_typarams {
1606 if !sized_params.contains(&tp.name) {
1607 where_predicates.push(WP::BoundPredicate {
1608 ty: Type::Generic(tp.name.clone()),
1609 bounds: vec![GenericBound::maybe_sized(cx)],
1614 // It would be nice to collect all of the bounds on a type and recombine
1615 // them if possible, to avoid e.g. `where T: Foo, T: Bar, T: Sized, T: 'a`
1616 // and instead see `where T: Foo + Bar + Sized + 'a`
1621 .flat_map(|param| match param.kind {
1622 ty::GenericParamDefKind::Lifetime => Some(param.clean(cx)),
1623 ty::GenericParamDefKind::Type { .. } => None,
1624 }).chain(simplify::ty_params(stripped_typarams).into_iter())
1626 where_predicates: simplify::where_clauses(cx, where_predicates),
1631 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1633 pub generics: Generics,
1635 pub header: hir::FnHeader,
1638 impl<'a> Clean<Method> for (&'a hir::MethodSig, &'a hir::Generics, hir::BodyId) {
1639 fn clean(&self, cx: &DocContext) -> Method {
1640 let (generics, decl) = enter_impl_trait(cx, || {
1641 (self.1.clean(cx), (&*self.0.decl, self.2).clean(cx))
1646 header: self.0.header,
1651 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1652 pub struct TyMethod {
1653 pub header: hir::FnHeader,
1655 pub generics: Generics,
1658 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1659 pub struct Function {
1661 pub generics: Generics,
1662 pub header: hir::FnHeader,
1665 impl Clean<Item> for doctree::Function {
1666 fn clean(&self, cx: &DocContext) -> Item {
1667 let (generics, decl) = enter_impl_trait(cx, || {
1668 (self.generics.clean(cx), (&self.decl, self.body).clean(cx))
1672 name: Some(self.name.clean(cx)),
1673 attrs: self.attrs.clean(cx),
1674 source: self.whence.clean(cx),
1675 visibility: self.vis.clean(cx),
1676 stability: self.stab.clean(cx),
1677 deprecation: self.depr.clean(cx),
1678 def_id: cx.tcx.hir.local_def_id(self.id),
1679 inner: FunctionItem(Function {
1682 header: self.header,
1688 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1690 pub inputs: Arguments,
1691 pub output: FunctionRetTy,
1693 pub attrs: Attributes,
1697 pub fn self_type(&self) -> Option<SelfTy> {
1698 self.inputs.values.get(0).and_then(|v| v.to_self())
1702 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1703 pub struct Arguments {
1704 pub values: Vec<Argument>,
1707 impl<'a> Clean<Arguments> for (&'a [hir::Ty], &'a [ast::Ident]) {
1708 fn clean(&self, cx: &DocContext) -> Arguments {
1710 values: self.0.iter().enumerate().map(|(i, ty)| {
1711 let mut name = self.1.get(i).map(|ident| ident.to_string())
1712 .unwrap_or(String::new());
1713 if name.is_empty() {
1714 name = "_".to_string();
1718 type_: ty.clean(cx),
1725 impl<'a> Clean<Arguments> for (&'a [hir::Ty], hir::BodyId) {
1726 fn clean(&self, cx: &DocContext) -> Arguments {
1727 let body = cx.tcx.hir.body(self.1);
1730 values: self.0.iter().enumerate().map(|(i, ty)| {
1732 name: name_from_pat(&body.arguments[i].pat),
1733 type_: ty.clean(cx),
1740 impl<'a, A: Copy> Clean<FnDecl> for (&'a hir::FnDecl, A)
1741 where (&'a [hir::Ty], A): Clean<Arguments>
1743 fn clean(&self, cx: &DocContext) -> FnDecl {
1745 inputs: (&self.0.inputs[..], self.1).clean(cx),
1746 output: self.0.output.clean(cx),
1747 variadic: self.0.variadic,
1748 attrs: Attributes::default()
1753 impl<'a, 'tcx> Clean<FnDecl> for (DefId, ty::PolyFnSig<'tcx>) {
1754 fn clean(&self, cx: &DocContext) -> FnDecl {
1755 let (did, sig) = *self;
1756 let mut names = if cx.tcx.hir.as_local_node_id(did).is_some() {
1759 cx.tcx.fn_arg_names(did).into_iter()
1763 output: Return(sig.skip_binder().output().clean(cx)),
1764 attrs: Attributes::default(),
1765 variadic: sig.skip_binder().variadic,
1767 values: sig.skip_binder().inputs().iter().map(|t| {
1770 name: names.next().map_or(String::new(), |name| name.to_string()),
1778 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1779 pub struct Argument {
1784 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug)]
1787 SelfBorrowed(Option<Lifetime>, Mutability),
1792 pub fn to_self(&self) -> Option<SelfTy> {
1793 if self.name != "self" {
1796 if self.type_.is_self_type() {
1797 return Some(SelfValue);
1800 BorrowedRef{ref lifetime, mutability, ref type_} if type_.is_self_type() => {
1801 Some(SelfBorrowed(lifetime.clone(), mutability))
1803 _ => Some(SelfExplicit(self.type_.clone()))
1808 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
1809 pub enum FunctionRetTy {
1814 impl Clean<FunctionRetTy> for hir::FunctionRetTy {
1815 fn clean(&self, cx: &DocContext) -> FunctionRetTy {
1817 hir::Return(ref typ) => Return(typ.clean(cx)),
1818 hir::DefaultReturn(..) => DefaultReturn,
1823 impl GetDefId for FunctionRetTy {
1824 fn def_id(&self) -> Option<DefId> {
1826 Return(ref ty) => ty.def_id(),
1827 DefaultReturn => None,
1832 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1835 pub unsafety: hir::Unsafety,
1836 pub items: Vec<Item>,
1837 pub generics: Generics,
1838 pub bounds: Vec<GenericBound>,
1839 pub is_spotlight: bool,
1843 impl Clean<Item> for doctree::Trait {
1844 fn clean(&self, cx: &DocContext) -> Item {
1845 let attrs = self.attrs.clean(cx);
1846 let is_spotlight = attrs.has_doc_flag("spotlight");
1848 name: Some(self.name.clean(cx)),
1850 source: self.whence.clean(cx),
1851 def_id: cx.tcx.hir.local_def_id(self.id),
1852 visibility: self.vis.clean(cx),
1853 stability: self.stab.clean(cx),
1854 deprecation: self.depr.clean(cx),
1855 inner: TraitItem(Trait {
1856 auto: self.is_auto.clean(cx),
1857 unsafety: self.unsafety,
1858 items: self.items.clean(cx),
1859 generics: self.generics.clean(cx),
1860 bounds: self.bounds.clean(cx),
1861 is_spotlight: is_spotlight,
1862 is_auto: self.is_auto.clean(cx),
1868 impl Clean<bool> for hir::IsAuto {
1869 fn clean(&self, _: &DocContext) -> bool {
1871 hir::IsAuto::Yes => true,
1872 hir::IsAuto::No => false,
1877 impl Clean<Type> for hir::TraitRef {
1878 fn clean(&self, cx: &DocContext) -> Type {
1879 resolve_type(cx, self.path.clean(cx), self.ref_id)
1883 impl Clean<PolyTrait> for hir::PolyTraitRef {
1884 fn clean(&self, cx: &DocContext) -> PolyTrait {
1886 trait_: self.trait_ref.clean(cx),
1887 generic_params: self.bound_generic_params.clean(cx)
1892 impl Clean<Item> for hir::TraitItem {
1893 fn clean(&self, cx: &DocContext) -> Item {
1894 let inner = match self.node {
1895 hir::TraitItemKind::Const(ref ty, default) => {
1896 AssociatedConstItem(ty.clean(cx),
1897 default.map(|e| print_const_expr(cx, e)))
1899 hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Provided(body)) => {
1900 MethodItem((sig, &self.generics, body).clean(cx))
1902 hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Required(ref names)) => {
1903 let (generics, decl) = enter_impl_trait(cx, || {
1904 (self.generics.clean(cx), (&*sig.decl, &names[..]).clean(cx))
1906 TyMethodItem(TyMethod {
1912 hir::TraitItemKind::Type(ref bounds, ref default) => {
1913 AssociatedTypeItem(bounds.clean(cx), default.clean(cx))
1917 name: Some(self.ident.name.clean(cx)),
1918 attrs: self.attrs.clean(cx),
1919 source: self.span.clean(cx),
1920 def_id: cx.tcx.hir.local_def_id(self.id),
1922 stability: get_stability(cx, cx.tcx.hir.local_def_id(self.id)),
1923 deprecation: get_deprecation(cx, cx.tcx.hir.local_def_id(self.id)),
1929 impl Clean<Item> for hir::ImplItem {
1930 fn clean(&self, cx: &DocContext) -> Item {
1931 let inner = match self.node {
1932 hir::ImplItemKind::Const(ref ty, expr) => {
1933 AssociatedConstItem(ty.clean(cx),
1934 Some(print_const_expr(cx, expr)))
1936 hir::ImplItemKind::Method(ref sig, body) => {
1937 MethodItem((sig, &self.generics, body).clean(cx))
1939 hir::ImplItemKind::Type(ref ty) => TypedefItem(Typedef {
1940 type_: ty.clean(cx),
1941 generics: Generics::default(),
1943 hir::ImplItemKind::Existential(ref bounds) => ExistentialItem(Existential {
1944 bounds: bounds.clean(cx),
1945 generics: Generics::default(),
1949 name: Some(self.ident.name.clean(cx)),
1950 source: self.span.clean(cx),
1951 attrs: self.attrs.clean(cx),
1952 def_id: cx.tcx.hir.local_def_id(self.id),
1953 visibility: self.vis.clean(cx),
1954 stability: get_stability(cx, cx.tcx.hir.local_def_id(self.id)),
1955 deprecation: get_deprecation(cx, cx.tcx.hir.local_def_id(self.id)),
1961 impl<'tcx> Clean<Item> for ty::AssociatedItem {
1962 fn clean(&self, cx: &DocContext) -> Item {
1963 let inner = match self.kind {
1964 ty::AssociatedKind::Const => {
1965 let ty = cx.tcx.type_of(self.def_id);
1966 let default = if self.defaultness.has_value() {
1967 Some(inline::print_inlined_const(cx, self.def_id))
1971 AssociatedConstItem(ty.clean(cx), default)
1973 ty::AssociatedKind::Method => {
1974 let generics = (cx.tcx.generics_of(self.def_id),
1975 &cx.tcx.predicates_of(self.def_id)).clean(cx);
1976 let sig = cx.tcx.fn_sig(self.def_id);
1977 let mut decl = (self.def_id, sig).clean(cx);
1979 if self.method_has_self_argument {
1980 let self_ty = match self.container {
1981 ty::ImplContainer(def_id) => {
1982 cx.tcx.type_of(def_id)
1984 ty::TraitContainer(_) => cx.tcx.mk_self_type()
1986 let self_arg_ty = *sig.input(0).skip_binder();
1987 if self_arg_ty == self_ty {
1988 decl.inputs.values[0].type_ = Generic(String::from("Self"));
1989 } else if let ty::Ref(_, ty, _) = self_arg_ty.sty {
1991 match decl.inputs.values[0].type_ {
1992 BorrowedRef{ref mut type_, ..} => {
1993 **type_ = Generic(String::from("Self"))
1995 _ => unreachable!(),
2001 let provided = match self.container {
2002 ty::ImplContainer(_) => true,
2003 ty::TraitContainer(_) => self.defaultness.has_value()
2006 let constness = if cx.tcx.is_const_fn(self.def_id) {
2007 hir::Constness::Const
2009 hir::Constness::NotConst
2014 header: hir::FnHeader {
2015 unsafety: sig.unsafety(),
2018 asyncness: hir::IsAsync::NotAsync,
2022 TyMethodItem(TyMethod {
2025 header: hir::FnHeader {
2026 unsafety: sig.unsafety(),
2028 constness: hir::Constness::NotConst,
2029 asyncness: hir::IsAsync::NotAsync,
2034 ty::AssociatedKind::Type => {
2035 let my_name = self.ident.name.clean(cx);
2037 if let ty::TraitContainer(did) = self.container {
2038 // When loading a cross-crate associated type, the bounds for this type
2039 // are actually located on the trait/impl itself, so we need to load
2040 // all of the generics from there and then look for bounds that are
2041 // applied to this associated type in question.
2042 let predicates = cx.tcx.predicates_of(did);
2043 let generics = (cx.tcx.generics_of(did), &predicates).clean(cx);
2044 let mut bounds = generics.where_predicates.iter().filter_map(|pred| {
2045 let (name, self_type, trait_, bounds) = match *pred {
2046 WherePredicate::BoundPredicate {
2047 ty: QPath { ref name, ref self_type, ref trait_ },
2049 } => (name, self_type, trait_, bounds),
2052 if *name != my_name { return None }
2054 ResolvedPath { did, .. } if did == self.container.id() => {}
2058 Generic(ref s) if *s == "Self" => {}
2062 }).flat_map(|i| i.iter().cloned()).collect::<Vec<_>>();
2063 // Our Sized/?Sized bound didn't get handled when creating the generics
2064 // because we didn't actually get our whole set of bounds until just now
2065 // (some of them may have come from the trait). If we do have a sized
2066 // bound, we remove it, and if we don't then we add the `?Sized` bound
2068 match bounds.iter().position(|b| b.is_sized_bound(cx)) {
2069 Some(i) => { bounds.remove(i); }
2070 None => bounds.push(GenericBound::maybe_sized(cx)),
2073 let ty = if self.defaultness.has_value() {
2074 Some(cx.tcx.type_of(self.def_id))
2079 AssociatedTypeItem(bounds, ty.clean(cx))
2081 TypedefItem(Typedef {
2082 type_: cx.tcx.type_of(self.def_id).clean(cx),
2083 generics: Generics {
2085 where_predicates: Vec::new(),
2090 ty::AssociatedKind::Existential => unimplemented!(),
2093 let visibility = match self.container {
2094 ty::ImplContainer(_) => self.vis.clean(cx),
2095 ty::TraitContainer(_) => None,
2099 name: Some(self.ident.name.clean(cx)),
2101 stability: get_stability(cx, self.def_id),
2102 deprecation: get_deprecation(cx, self.def_id),
2103 def_id: self.def_id,
2104 attrs: inline::load_attrs(cx, self.def_id),
2105 source: cx.tcx.def_span(self.def_id).clean(cx),
2111 /// A trait reference, which may have higher ranked lifetimes.
2112 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
2113 pub struct PolyTrait {
2115 pub generic_params: Vec<GenericParamDef>,
2118 /// A representation of a Type suitable for hyperlinking purposes. Ideally one can get the original
2119 /// type out of the AST/TyCtxt given one of these, if more information is needed. Most importantly
2120 /// it does not preserve mutability or boxes.
2121 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
2123 /// structs/enums/traits (most that'd be an hir::TyKind::Path)
2126 typarams: Option<Vec<GenericBound>>,
2128 /// true if is a `T::Name` path for associated types
2131 /// For parameterized types, so the consumer of the JSON don't go
2132 /// looking for types which don't exist anywhere.
2134 /// Primitives are the fixed-size numeric types (plus int/usize/float), char,
2135 /// arrays, slices, and tuples.
2136 Primitive(PrimitiveType),
2138 BareFunction(Box<BareFunctionDecl>),
2141 Array(Box<Type>, String),
2144 RawPointer(Mutability, Box<Type>),
2146 lifetime: Option<Lifetime>,
2147 mutability: Mutability,
2151 // <Type as Trait>::Name
2154 self_type: Box<Type>,
2161 // impl TraitA+TraitB
2162 ImplTrait(Vec<GenericBound>),
2165 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Copy, Debug)]
2166 pub enum PrimitiveType {
2167 Isize, I8, I16, I32, I64, I128,
2168 Usize, U8, U16, U32, U64, U128,
2183 #[derive(Clone, RustcEncodable, RustcDecodable, Copy, Debug)]
2201 pub trait GetDefId {
2202 fn def_id(&self) -> Option<DefId>;
2205 impl<T: GetDefId> GetDefId for Option<T> {
2206 fn def_id(&self) -> Option<DefId> {
2207 self.as_ref().and_then(|d| d.def_id())
2212 pub fn primitive_type(&self) -> Option<PrimitiveType> {
2214 Primitive(p) | BorrowedRef { type_: box Primitive(p), ..} => Some(p),
2215 Slice(..) | BorrowedRef { type_: box Slice(..), .. } => Some(PrimitiveType::Slice),
2216 Array(..) | BorrowedRef { type_: box Array(..), .. } => Some(PrimitiveType::Array),
2217 Tuple(ref tys) => if tys.is_empty() {
2218 Some(PrimitiveType::Unit)
2220 Some(PrimitiveType::Tuple)
2222 RawPointer(..) => Some(PrimitiveType::RawPointer),
2223 BorrowedRef { type_: box Generic(..), .. } => Some(PrimitiveType::Reference),
2224 BareFunction(..) => Some(PrimitiveType::Fn),
2225 Never => Some(PrimitiveType::Never),
2230 pub fn is_generic(&self) -> bool {
2232 ResolvedPath { is_generic, .. } => is_generic,
2237 pub fn is_self_type(&self) -> bool {
2239 Generic(ref name) => name == "Self",
2244 pub fn generics(&self) -> Option<&[Type]> {
2246 ResolvedPath { ref path, .. } => {
2247 path.segments.last().and_then(|seg| {
2248 if let GenericArgs::AngleBracketed { ref types, .. } = seg.args {
2260 impl GetDefId for Type {
2261 fn def_id(&self) -> Option<DefId> {
2263 ResolvedPath { did, .. } => Some(did),
2264 Primitive(p) => ::html::render::cache().primitive_locations.get(&p).cloned(),
2265 BorrowedRef { type_: box Generic(..), .. } =>
2266 Primitive(PrimitiveType::Reference).def_id(),
2267 BorrowedRef { ref type_, .. } => type_.def_id(),
2268 Tuple(ref tys) => if tys.is_empty() {
2269 Primitive(PrimitiveType::Unit).def_id()
2271 Primitive(PrimitiveType::Tuple).def_id()
2273 BareFunction(..) => Primitive(PrimitiveType::Fn).def_id(),
2274 Never => Primitive(PrimitiveType::Never).def_id(),
2275 Slice(..) => Primitive(PrimitiveType::Slice).def_id(),
2276 Array(..) => Primitive(PrimitiveType::Array).def_id(),
2277 RawPointer(..) => Primitive(PrimitiveType::RawPointer).def_id(),
2278 QPath { ref self_type, .. } => self_type.def_id(),
2284 impl PrimitiveType {
2285 fn from_str(s: &str) -> Option<PrimitiveType> {
2287 "isize" => Some(PrimitiveType::Isize),
2288 "i8" => Some(PrimitiveType::I8),
2289 "i16" => Some(PrimitiveType::I16),
2290 "i32" => Some(PrimitiveType::I32),
2291 "i64" => Some(PrimitiveType::I64),
2292 "i128" => Some(PrimitiveType::I128),
2293 "usize" => Some(PrimitiveType::Usize),
2294 "u8" => Some(PrimitiveType::U8),
2295 "u16" => Some(PrimitiveType::U16),
2296 "u32" => Some(PrimitiveType::U32),
2297 "u64" => Some(PrimitiveType::U64),
2298 "u128" => Some(PrimitiveType::U128),
2299 "bool" => Some(PrimitiveType::Bool),
2300 "char" => Some(PrimitiveType::Char),
2301 "str" => Some(PrimitiveType::Str),
2302 "f32" => Some(PrimitiveType::F32),
2303 "f64" => Some(PrimitiveType::F64),
2304 "array" => Some(PrimitiveType::Array),
2305 "slice" => Some(PrimitiveType::Slice),
2306 "tuple" => Some(PrimitiveType::Tuple),
2307 "unit" => Some(PrimitiveType::Unit),
2308 "pointer" => Some(PrimitiveType::RawPointer),
2309 "reference" => Some(PrimitiveType::Reference),
2310 "fn" => Some(PrimitiveType::Fn),
2311 "never" => Some(PrimitiveType::Never),
2316 pub fn as_str(&self) -> &'static str {
2317 use self::PrimitiveType::*;
2340 RawPointer => "pointer",
2341 Reference => "reference",
2347 pub fn to_url_str(&self) -> &'static str {
2352 impl From<ast::IntTy> for PrimitiveType {
2353 fn from(int_ty: ast::IntTy) -> PrimitiveType {
2355 ast::IntTy::Isize => PrimitiveType::Isize,
2356 ast::IntTy::I8 => PrimitiveType::I8,
2357 ast::IntTy::I16 => PrimitiveType::I16,
2358 ast::IntTy::I32 => PrimitiveType::I32,
2359 ast::IntTy::I64 => PrimitiveType::I64,
2360 ast::IntTy::I128 => PrimitiveType::I128,
2365 impl From<ast::UintTy> for PrimitiveType {
2366 fn from(uint_ty: ast::UintTy) -> PrimitiveType {
2368 ast::UintTy::Usize => PrimitiveType::Usize,
2369 ast::UintTy::U8 => PrimitiveType::U8,
2370 ast::UintTy::U16 => PrimitiveType::U16,
2371 ast::UintTy::U32 => PrimitiveType::U32,
2372 ast::UintTy::U64 => PrimitiveType::U64,
2373 ast::UintTy::U128 => PrimitiveType::U128,
2378 impl From<ast::FloatTy> for PrimitiveType {
2379 fn from(float_ty: ast::FloatTy) -> PrimitiveType {
2381 ast::FloatTy::F32 => PrimitiveType::F32,
2382 ast::FloatTy::F64 => PrimitiveType::F64,
2387 impl Clean<Type> for hir::Ty {
2388 fn clean(&self, cx: &DocContext) -> Type {
2392 TyKind::Never => Never,
2393 TyKind::Ptr(ref m) => RawPointer(m.mutbl.clean(cx), box m.ty.clean(cx)),
2394 TyKind::Rptr(ref l, ref m) => {
2395 let lifetime = if l.is_elided() {
2400 BorrowedRef {lifetime: lifetime, mutability: m.mutbl.clean(cx),
2401 type_: box m.ty.clean(cx)}
2403 TyKind::Slice(ref ty) => Slice(box ty.clean(cx)),
2404 TyKind::Array(ref ty, ref length) => {
2405 let def_id = cx.tcx.hir.local_def_id(length.id);
2406 let param_env = cx.tcx.param_env(def_id);
2407 let substs = Substs::identity_for_item(cx.tcx, def_id);
2408 let cid = GlobalId {
2409 instance: ty::Instance::new(def_id, substs),
2412 let length = cx.tcx.const_eval(param_env.and(cid)).unwrap_or_else(|_| {
2413 ty::Const::unevaluated(cx.tcx, def_id, substs, cx.tcx.types.usize)
2415 let length = print_const(cx, length);
2416 Array(box ty.clean(cx), length)
2418 TyKind::Tup(ref tys) => Tuple(tys.clean(cx)),
2419 TyKind::Def(item_id, _) => {
2420 let item = cx.tcx.hir.expect_item(item_id.id);
2421 if let hir::ItemKind::Existential(ref ty) = item.node {
2422 ImplTrait(ty.bounds.clean(cx))
2427 TyKind::Path(hir::QPath::Resolved(None, ref path)) => {
2428 if let Some(new_ty) = cx.ty_substs.borrow().get(&path.def).cloned() {
2432 if let Def::TyParam(did) = path.def {
2433 if let Some(bounds) = cx.impl_trait_bounds.borrow_mut().remove(&did) {
2434 return ImplTrait(bounds);
2438 let mut alias = None;
2439 if let Def::TyAlias(def_id) = path.def {
2440 // Substitute private type aliases
2441 if let Some(node_id) = cx.tcx.hir.as_local_node_id(def_id) {
2442 if !cx.renderinfo.borrow().access_levels.is_exported(def_id) {
2443 alias = Some(&cx.tcx.hir.expect_item(node_id).node);
2448 if let Some(&hir::ItemKind::Ty(ref ty, ref generics)) = alias {
2449 let provided_params = &path.segments.last().expect("segments were empty");
2450 let mut ty_substs = FxHashMap();
2451 let mut lt_substs = FxHashMap();
2452 provided_params.with_generic_args(|generic_args| {
2453 let mut indices: GenericParamCount = Default::default();
2454 for param in generics.params.iter() {
2456 hir::GenericParamKind::Lifetime { .. } => {
2458 let lifetime = generic_args.args.iter().find_map(|arg| {
2460 GenericArg::Lifetime(lt) => {
2461 if indices.lifetimes == j {
2470 if let Some(lt) = lifetime.cloned() {
2471 if !lt.is_elided() {
2473 cx.tcx.hir.local_def_id(param.id);
2474 lt_substs.insert(lt_def_id, lt.clean(cx));
2477 indices.lifetimes += 1;
2479 hir::GenericParamKind::Type { ref default, .. } => {
2481 Def::TyParam(cx.tcx.hir.local_def_id(param.id));
2483 let type_ = generic_args.args.iter().find_map(|arg| {
2485 GenericArg::Type(ty) => {
2486 if indices.types == j {
2495 if let Some(ty) = type_.cloned() {
2496 ty_substs.insert(ty_param_def, ty.clean(cx));
2497 } else if let Some(default) = default.clone() {
2498 ty_substs.insert(ty_param_def,
2499 default.into_inner().clean(cx));
2506 return cx.enter_alias(ty_substs, lt_substs, || ty.clean(cx));
2508 resolve_type(cx, path.clean(cx), self.id)
2510 TyKind::Path(hir::QPath::Resolved(Some(ref qself), ref p)) => {
2511 let mut segments: Vec<_> = p.segments.clone().into();
2513 let trait_path = hir::Path {
2515 def: Def::Trait(cx.tcx.associated_item(p.def.def_id()).container.id()),
2516 segments: segments.into(),
2519 name: p.segments.last().expect("segments were empty").ident.name.clean(cx),
2520 self_type: box qself.clean(cx),
2521 trait_: box resolve_type(cx, trait_path.clean(cx), self.id)
2524 TyKind::Path(hir::QPath::TypeRelative(ref qself, ref segment)) => {
2525 let mut def = Def::Err;
2526 let ty = hir_ty_to_ty(cx.tcx, self);
2527 if let ty::Projection(proj) = ty.sty {
2528 def = Def::Trait(proj.trait_ref(cx.tcx).def_id);
2530 let trait_path = hir::Path {
2533 segments: vec![].into(),
2536 name: segment.ident.name.clean(cx),
2537 self_type: box qself.clean(cx),
2538 trait_: box resolve_type(cx, trait_path.clean(cx), self.id)
2541 TyKind::TraitObject(ref bounds, ref lifetime) => {
2542 match bounds[0].clean(cx).trait_ {
2543 ResolvedPath { path, typarams: None, did, is_generic } => {
2544 let mut bounds: Vec<self::GenericBound> = bounds[1..].iter().map(|bound| {
2545 self::GenericBound::TraitBound(bound.clean(cx),
2546 hir::TraitBoundModifier::None)
2548 if !lifetime.is_elided() {
2549 bounds.push(self::GenericBound::Outlives(lifetime.clean(cx)));
2551 ResolvedPath { path, typarams: Some(bounds), did, is_generic, }
2553 _ => Infer // shouldn't happen
2556 TyKind::BareFn(ref barefn) => BareFunction(box barefn.clean(cx)),
2557 TyKind::Infer | TyKind::Err => Infer,
2558 TyKind::Typeof(..) => panic!("Unimplemented type {:?}", self.node),
2563 impl<'tcx> Clean<Type> for Ty<'tcx> {
2564 fn clean(&self, cx: &DocContext) -> Type {
2567 ty::Bool => Primitive(PrimitiveType::Bool),
2568 ty::Char => Primitive(PrimitiveType::Char),
2569 ty::Int(int_ty) => Primitive(int_ty.into()),
2570 ty::Uint(uint_ty) => Primitive(uint_ty.into()),
2571 ty::Float(float_ty) => Primitive(float_ty.into()),
2572 ty::Str => Primitive(PrimitiveType::Str),
2573 ty::Slice(ty) => Slice(box ty.clean(cx)),
2574 ty::Array(ty, n) => {
2575 let mut n = cx.tcx.lift(&n).expect("array lift failed");
2576 if let ConstValue::Unevaluated(def_id, substs) = n.val {
2577 let param_env = cx.tcx.param_env(def_id);
2578 let cid = GlobalId {
2579 instance: ty::Instance::new(def_id, substs),
2582 if let Ok(new_n) = cx.tcx.const_eval(param_env.and(cid)) {
2586 let n = print_const(cx, n);
2587 Array(box ty.clean(cx), n)
2589 ty::RawPtr(mt) => RawPointer(mt.mutbl.clean(cx), box mt.ty.clean(cx)),
2590 ty::Ref(r, ty, mutbl) => BorrowedRef {
2591 lifetime: r.clean(cx),
2592 mutability: mutbl.clean(cx),
2593 type_: box ty.clean(cx),
2597 let ty = cx.tcx.lift(self).expect("FnPtr lift failed");
2598 let sig = ty.fn_sig(cx.tcx);
2599 BareFunction(box BareFunctionDecl {
2600 unsafety: sig.unsafety(),
2601 generic_params: Vec::new(),
2602 decl: (cx.tcx.hir.local_def_id(ast::CRATE_NODE_ID), sig).clean(cx),
2606 ty::Adt(def, substs) => {
2608 let kind = match def.adt_kind() {
2609 AdtKind::Struct => TypeKind::Struct,
2610 AdtKind::Union => TypeKind::Union,
2611 AdtKind::Enum => TypeKind::Enum,
2613 inline::record_extern_fqn(cx, did, kind);
2614 let path = external_path(cx, &cx.tcx.item_name(did).as_str(),
2615 None, false, vec![], substs);
2623 ty::Foreign(did) => {
2624 inline::record_extern_fqn(cx, did, TypeKind::Foreign);
2625 let path = external_path(cx, &cx.tcx.item_name(did).as_str(),
2626 None, false, vec![], Substs::empty());
2634 ty::Dynamic(ref obj, ref reg) => {
2635 if let Some(principal) = obj.principal() {
2636 let did = principal.def_id();
2637 inline::record_extern_fqn(cx, did, TypeKind::Trait);
2639 let mut typarams = vec![];
2640 reg.clean(cx).map(|b| typarams.push(GenericBound::Outlives(b)));
2641 for did in obj.auto_traits() {
2642 let empty = cx.tcx.intern_substs(&[]);
2643 let path = external_path(cx, &cx.tcx.item_name(did).as_str(),
2644 Some(did), false, vec![], empty);
2645 inline::record_extern_fqn(cx, did, TypeKind::Trait);
2646 let bound = GenericBound::TraitBound(PolyTrait {
2647 trait_: ResolvedPath {
2653 generic_params: Vec::new(),
2654 }, hir::TraitBoundModifier::None);
2655 typarams.push(bound);
2658 let mut bindings = vec![];
2659 for pb in obj.projection_bounds() {
2660 bindings.push(TypeBinding {
2661 name: cx.tcx.associated_item(pb.item_def_id()).ident.name.clean(cx),
2662 ty: pb.skip_binder().ty.clean(cx)
2666 let path = external_path(cx, &cx.tcx.item_name(did).as_str(), Some(did),
2667 false, bindings, principal.skip_binder().substs);
2670 typarams: Some(typarams),
2678 ty::Tuple(ref t) => Tuple(t.clean(cx)),
2680 ty::Projection(ref data) => data.clean(cx),
2682 ty::Param(ref p) => Generic(p.name.to_string()),
2684 ty::Opaque(def_id, substs) => {
2685 // Grab the "TraitA + TraitB" from `impl TraitA + TraitB`,
2686 // by looking up the projections associated with the def_id.
2687 let predicates_of = cx.tcx.predicates_of(def_id);
2688 let substs = cx.tcx.lift(&substs).expect("Opaque lift failed");
2689 let bounds = predicates_of.instantiate(cx.tcx, substs);
2690 let mut regions = vec![];
2691 let mut has_sized = false;
2692 let mut bounds = bounds.predicates.iter().filter_map(|predicate| {
2693 let trait_ref = if let Some(tr) = predicate.to_opt_poly_trait_ref() {
2695 } else if let ty::Predicate::TypeOutlives(pred) = *predicate {
2696 // these should turn up at the end
2697 pred.skip_binder().1.clean(cx).map(|r| {
2698 regions.push(GenericBound::Outlives(r))
2705 if let Some(sized) = cx.tcx.lang_items().sized_trait() {
2706 if trait_ref.def_id() == sized {
2712 let bounds = bounds.predicates.iter().filter_map(|pred|
2713 if let ty::Predicate::Projection(proj) = *pred {
2714 let proj = proj.skip_binder();
2715 if proj.projection_ty.trait_ref(cx.tcx) == *trait_ref.skip_binder() {
2717 name: cx.tcx.associated_item(proj.projection_ty.item_def_id)
2718 .ident.name.clean(cx),
2719 ty: proj.ty.clean(cx),
2729 Some((trait_ref.skip_binder(), bounds).clean(cx))
2730 }).collect::<Vec<_>>();
2731 bounds.extend(regions);
2732 if !has_sized && !bounds.is_empty() {
2733 bounds.insert(0, GenericBound::maybe_sized(cx));
2738 ty::Closure(..) | ty::Generator(..) => Tuple(vec![]), // FIXME(pcwalton)
2740 ty::UnnormalizedProjection(..) => panic!("UnnormalizedProjection"),
2741 ty::GeneratorWitness(..) => panic!("GeneratorWitness"),
2742 ty::Infer(..) => panic!("Infer"),
2743 ty::Error => panic!("Error"),
2748 impl Clean<Item> for hir::StructField {
2749 fn clean(&self, cx: &DocContext) -> Item {
2751 name: Some(self.ident.name).clean(cx),
2752 attrs: self.attrs.clean(cx),
2753 source: self.span.clean(cx),
2754 visibility: self.vis.clean(cx),
2755 stability: get_stability(cx, cx.tcx.hir.local_def_id(self.id)),
2756 deprecation: get_deprecation(cx, cx.tcx.hir.local_def_id(self.id)),
2757 def_id: cx.tcx.hir.local_def_id(self.id),
2758 inner: StructFieldItem(self.ty.clean(cx)),
2763 impl<'tcx> Clean<Item> for ty::FieldDef {
2764 fn clean(&self, cx: &DocContext) -> Item {
2766 name: Some(self.ident.name).clean(cx),
2767 attrs: cx.tcx.get_attrs(self.did).clean(cx),
2768 source: cx.tcx.def_span(self.did).clean(cx),
2769 visibility: self.vis.clean(cx),
2770 stability: get_stability(cx, self.did),
2771 deprecation: get_deprecation(cx, self.did),
2773 inner: StructFieldItem(cx.tcx.type_of(self.did).clean(cx)),
2778 #[derive(Clone, PartialEq, Eq, RustcDecodable, RustcEncodable, Debug)]
2779 pub enum Visibility {
2783 Restricted(DefId, Path),
2786 impl Clean<Option<Visibility>> for hir::Visibility {
2787 fn clean(&self, cx: &DocContext) -> Option<Visibility> {
2788 Some(match self.node {
2789 hir::VisibilityKind::Public => Visibility::Public,
2790 hir::VisibilityKind::Inherited => Visibility::Inherited,
2791 hir::VisibilityKind::Crate(_) => Visibility::Crate,
2792 hir::VisibilityKind::Restricted { ref path, .. } => {
2793 let path = path.clean(cx);
2794 let did = register_def(cx, path.def);
2795 Visibility::Restricted(did, path)
2801 impl Clean<Option<Visibility>> for ty::Visibility {
2802 fn clean(&self, _: &DocContext) -> Option<Visibility> {
2803 Some(if *self == ty::Visibility::Public { Public } else { Inherited })
2807 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2809 pub struct_type: doctree::StructType,
2810 pub generics: Generics,
2811 pub fields: Vec<Item>,
2812 pub fields_stripped: bool,
2815 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2817 pub struct_type: doctree::StructType,
2818 pub generics: Generics,
2819 pub fields: Vec<Item>,
2820 pub fields_stripped: bool,
2823 impl Clean<Item> for doctree::Struct {
2824 fn clean(&self, cx: &DocContext) -> Item {
2826 name: Some(self.name.clean(cx)),
2827 attrs: self.attrs.clean(cx),
2828 source: self.whence.clean(cx),
2829 def_id: cx.tcx.hir.local_def_id(self.id),
2830 visibility: self.vis.clean(cx),
2831 stability: self.stab.clean(cx),
2832 deprecation: self.depr.clean(cx),
2833 inner: StructItem(Struct {
2834 struct_type: self.struct_type,
2835 generics: self.generics.clean(cx),
2836 fields: self.fields.clean(cx),
2837 fields_stripped: false,
2843 impl Clean<Item> for doctree::Union {
2844 fn clean(&self, cx: &DocContext) -> Item {
2846 name: Some(self.name.clean(cx)),
2847 attrs: self.attrs.clean(cx),
2848 source: self.whence.clean(cx),
2849 def_id: cx.tcx.hir.local_def_id(self.id),
2850 visibility: self.vis.clean(cx),
2851 stability: self.stab.clean(cx),
2852 deprecation: self.depr.clean(cx),
2853 inner: UnionItem(Union {
2854 struct_type: self.struct_type,
2855 generics: self.generics.clean(cx),
2856 fields: self.fields.clean(cx),
2857 fields_stripped: false,
2863 /// This is a more limited form of the standard Struct, different in that
2864 /// it lacks the things most items have (name, id, parameterization). Found
2865 /// only as a variant in an enum.
2866 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2867 pub struct VariantStruct {
2868 pub struct_type: doctree::StructType,
2869 pub fields: Vec<Item>,
2870 pub fields_stripped: bool,
2873 impl Clean<VariantStruct> for ::rustc::hir::VariantData {
2874 fn clean(&self, cx: &DocContext) -> VariantStruct {
2876 struct_type: doctree::struct_type_from_def(self),
2877 fields: self.fields().iter().map(|x| x.clean(cx)).collect(),
2878 fields_stripped: false,
2883 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2885 pub variants: Vec<Item>,
2886 pub generics: Generics,
2887 pub variants_stripped: bool,
2890 impl Clean<Item> for doctree::Enum {
2891 fn clean(&self, cx: &DocContext) -> Item {
2893 name: Some(self.name.clean(cx)),
2894 attrs: self.attrs.clean(cx),
2895 source: self.whence.clean(cx),
2896 def_id: cx.tcx.hir.local_def_id(self.id),
2897 visibility: self.vis.clean(cx),
2898 stability: self.stab.clean(cx),
2899 deprecation: self.depr.clean(cx),
2900 inner: EnumItem(Enum {
2901 variants: self.variants.clean(cx),
2902 generics: self.generics.clean(cx),
2903 variants_stripped: false,
2909 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2910 pub struct Variant {
2911 pub kind: VariantKind,
2914 impl Clean<Item> for doctree::Variant {
2915 fn clean(&self, cx: &DocContext) -> Item {
2917 name: Some(self.name.clean(cx)),
2918 attrs: self.attrs.clean(cx),
2919 source: self.whence.clean(cx),
2921 stability: self.stab.clean(cx),
2922 deprecation: self.depr.clean(cx),
2923 def_id: cx.tcx.hir.local_def_id(self.def.id()),
2924 inner: VariantItem(Variant {
2925 kind: self.def.clean(cx),
2931 impl<'tcx> Clean<Item> for ty::VariantDef {
2932 fn clean(&self, cx: &DocContext) -> Item {
2933 let kind = match self.ctor_kind {
2934 CtorKind::Const => VariantKind::CLike,
2937 self.fields.iter().map(|f| cx.tcx.type_of(f.did).clean(cx)).collect()
2940 CtorKind::Fictive => {
2941 VariantKind::Struct(VariantStruct {
2942 struct_type: doctree::Plain,
2943 fields_stripped: false,
2944 fields: self.fields.iter().map(|field| {
2946 source: cx.tcx.def_span(field.did).clean(cx),
2947 name: Some(field.ident.name.clean(cx)),
2948 attrs: cx.tcx.get_attrs(field.did).clean(cx),
2949 visibility: field.vis.clean(cx),
2951 stability: get_stability(cx, field.did),
2952 deprecation: get_deprecation(cx, field.did),
2953 inner: StructFieldItem(cx.tcx.type_of(field.did).clean(cx))
2960 name: Some(self.name.clean(cx)),
2961 attrs: inline::load_attrs(cx, self.did),
2962 source: cx.tcx.def_span(self.did).clean(cx),
2963 visibility: Some(Inherited),
2965 inner: VariantItem(Variant { kind: kind }),
2966 stability: get_stability(cx, self.did),
2967 deprecation: get_deprecation(cx, self.did),
2972 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2973 pub enum VariantKind {
2976 Struct(VariantStruct),
2979 impl Clean<VariantKind> for hir::VariantData {
2980 fn clean(&self, cx: &DocContext) -> VariantKind {
2981 if self.is_struct() {
2982 VariantKind::Struct(self.clean(cx))
2983 } else if self.is_unit() {
2986 VariantKind::Tuple(self.fields().iter().map(|x| x.ty.clean(cx)).collect())
2991 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2993 pub filename: FileName,
3001 pub fn empty() -> Span {
3003 filename: FileName::Anon,
3004 loline: 0, locol: 0,
3005 hiline: 0, hicol: 0,
3010 impl Clean<Span> for syntax_pos::Span {
3011 fn clean(&self, cx: &DocContext) -> Span {
3012 if self.is_dummy() {
3013 return Span::empty();
3016 let cm = cx.sess().source_map();
3017 let filename = cm.span_to_filename(*self);
3018 let lo = cm.lookup_char_pos(self.lo());
3019 let hi = cm.lookup_char_pos(self.hi());
3023 locol: lo.col.to_usize(),
3025 hicol: hi.col.to_usize(),
3030 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
3034 pub segments: Vec<PathSegment>,
3038 pub fn last_name(&self) -> &str {
3039 self.segments.last().expect("segments were empty").name.as_str()
3043 impl Clean<Path> for hir::Path {
3044 fn clean(&self, cx: &DocContext) -> Path {
3046 global: self.is_global(),
3048 segments: if self.is_global() { &self.segments[1..] } else { &self.segments }.clean(cx),
3053 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
3054 pub enum GenericArgs {
3056 lifetimes: Vec<Lifetime>,
3058 bindings: Vec<TypeBinding>,
3062 output: Option<Type>,
3066 impl Clean<GenericArgs> for hir::GenericArgs {
3067 fn clean(&self, cx: &DocContext) -> GenericArgs {
3068 if self.parenthesized {
3069 let output = self.bindings[0].ty.clean(cx);
3070 GenericArgs::Parenthesized {
3071 inputs: self.inputs().clean(cx),
3072 output: if output != Type::Tuple(Vec::new()) { Some(output) } else { None }
3075 let (mut lifetimes, mut types) = (vec![], vec![]);
3076 let mut elided_lifetimes = true;
3077 for arg in &self.args {
3079 GenericArg::Lifetime(lt) => {
3080 if !lt.is_elided() {
3081 elided_lifetimes = false;
3083 lifetimes.push(lt.clean(cx));
3085 GenericArg::Type(ty) => {
3086 types.push(ty.clean(cx));
3090 GenericArgs::AngleBracketed {
3091 lifetimes: if elided_lifetimes { vec![] } else { lifetimes },
3093 bindings: self.bindings.clean(cx),
3099 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
3100 pub struct PathSegment {
3102 pub args: GenericArgs,
3105 impl Clean<PathSegment> for hir::PathSegment {
3106 fn clean(&self, cx: &DocContext) -> PathSegment {
3108 name: self.ident.name.clean(cx),
3109 args: self.with_generic_args(|generic_args| generic_args.clean(cx))
3114 fn strip_type(ty: Type) -> Type {
3116 Type::ResolvedPath { path, typarams, did, is_generic } => {
3117 Type::ResolvedPath { path: strip_path(&path), typarams, did, is_generic }
3119 Type::Tuple(inner_tys) => {
3120 Type::Tuple(inner_tys.iter().map(|t| strip_type(t.clone())).collect())
3122 Type::Slice(inner_ty) => Type::Slice(Box::new(strip_type(*inner_ty))),
3123 Type::Array(inner_ty, s) => Type::Array(Box::new(strip_type(*inner_ty)), s),
3124 Type::Unique(inner_ty) => Type::Unique(Box::new(strip_type(*inner_ty))),
3125 Type::RawPointer(m, inner_ty) => Type::RawPointer(m, Box::new(strip_type(*inner_ty))),
3126 Type::BorrowedRef { lifetime, mutability, type_ } => {
3127 Type::BorrowedRef { lifetime, mutability, type_: Box::new(strip_type(*type_)) }
3129 Type::QPath { name, self_type, trait_ } => {
3132 self_type: Box::new(strip_type(*self_type)), trait_: Box::new(strip_type(*trait_))
3139 fn strip_path(path: &Path) -> Path {
3140 let segments = path.segments.iter().map(|s| {
3142 name: s.name.clone(),
3143 args: GenericArgs::AngleBracketed {
3144 lifetimes: Vec::new(),
3146 bindings: Vec::new(),
3152 global: path.global,
3153 def: path.def.clone(),
3158 fn qpath_to_string(p: &hir::QPath) -> String {
3159 let segments = match *p {
3160 hir::QPath::Resolved(_, ref path) => &path.segments,
3161 hir::QPath::TypeRelative(_, ref segment) => return segment.ident.to_string(),
3164 let mut s = String::new();
3165 for (i, seg) in segments.iter().enumerate() {
3169 if seg.ident.name != keywords::CrateRoot.name() {
3170 s.push_str(&*seg.ident.as_str());
3176 impl Clean<String> for ast::Name {
3177 fn clean(&self, _: &DocContext) -> String {
3182 impl Clean<String> for InternedString {
3183 fn clean(&self, _: &DocContext) -> String {
3188 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3189 pub struct Typedef {
3191 pub generics: Generics,
3194 impl Clean<Item> for doctree::Typedef {
3195 fn clean(&self, cx: &DocContext) -> Item {
3197 name: Some(self.name.clean(cx)),
3198 attrs: self.attrs.clean(cx),
3199 source: self.whence.clean(cx),
3200 def_id: cx.tcx.hir.local_def_id(self.id.clone()),
3201 visibility: self.vis.clean(cx),
3202 stability: self.stab.clean(cx),
3203 deprecation: self.depr.clean(cx),
3204 inner: TypedefItem(Typedef {
3205 type_: self.ty.clean(cx),
3206 generics: self.gen.clean(cx),
3212 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3213 pub struct Existential {
3214 pub bounds: Vec<GenericBound>,
3215 pub generics: Generics,
3218 impl Clean<Item> for doctree::Existential {
3219 fn clean(&self, cx: &DocContext) -> Item {
3221 name: Some(self.name.clean(cx)),
3222 attrs: self.attrs.clean(cx),
3223 source: self.whence.clean(cx),
3224 def_id: cx.tcx.hir.local_def_id(self.id.clone()),
3225 visibility: self.vis.clean(cx),
3226 stability: self.stab.clean(cx),
3227 deprecation: self.depr.clean(cx),
3228 inner: ExistentialItem(Existential {
3229 bounds: self.exist_ty.bounds.clean(cx),
3230 generics: self.exist_ty.generics.clean(cx),
3236 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
3237 pub struct BareFunctionDecl {
3238 pub unsafety: hir::Unsafety,
3239 pub generic_params: Vec<GenericParamDef>,
3244 impl Clean<BareFunctionDecl> for hir::BareFnTy {
3245 fn clean(&self, cx: &DocContext) -> BareFunctionDecl {
3246 let (generic_params, decl) = enter_impl_trait(cx, || {
3247 (self.generic_params.clean(cx), (&*self.decl, &self.arg_names[..]).clean(cx))
3250 unsafety: self.unsafety,
3258 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3261 pub mutability: Mutability,
3262 /// It's useful to have the value of a static documented, but I have no
3263 /// desire to represent expressions (that'd basically be all of the AST,
3264 /// which is huge!). So, have a string.
3268 impl Clean<Item> for doctree::Static {
3269 fn clean(&self, cx: &DocContext) -> Item {
3270 debug!("cleaning static {}: {:?}", self.name.clean(cx), self);
3272 name: Some(self.name.clean(cx)),
3273 attrs: self.attrs.clean(cx),
3274 source: self.whence.clean(cx),
3275 def_id: cx.tcx.hir.local_def_id(self.id),
3276 visibility: self.vis.clean(cx),
3277 stability: self.stab.clean(cx),
3278 deprecation: self.depr.clean(cx),
3279 inner: StaticItem(Static {
3280 type_: self.type_.clean(cx),
3281 mutability: self.mutability.clean(cx),
3282 expr: print_const_expr(cx, self.expr),
3288 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3289 pub struct Constant {
3294 impl Clean<Item> for doctree::Constant {
3295 fn clean(&self, cx: &DocContext) -> Item {
3297 name: Some(self.name.clean(cx)),
3298 attrs: self.attrs.clean(cx),
3299 source: self.whence.clean(cx),
3300 def_id: cx.tcx.hir.local_def_id(self.id),
3301 visibility: self.vis.clean(cx),
3302 stability: self.stab.clean(cx),
3303 deprecation: self.depr.clean(cx),
3304 inner: ConstantItem(Constant {
3305 type_: self.type_.clean(cx),
3306 expr: print_const_expr(cx, self.expr),
3312 #[derive(Debug, Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Copy, Hash)]
3313 pub enum Mutability {
3318 impl Clean<Mutability> for hir::Mutability {
3319 fn clean(&self, _: &DocContext) -> Mutability {
3321 &hir::MutMutable => Mutable,
3322 &hir::MutImmutable => Immutable,
3327 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Copy, Debug, Hash)]
3328 pub enum ImplPolarity {
3333 impl Clean<ImplPolarity> for hir::ImplPolarity {
3334 fn clean(&self, _: &DocContext) -> ImplPolarity {
3336 &hir::ImplPolarity::Positive => ImplPolarity::Positive,
3337 &hir::ImplPolarity::Negative => ImplPolarity::Negative,
3342 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3344 pub unsafety: hir::Unsafety,
3345 pub generics: Generics,
3346 pub provided_trait_methods: FxHashSet<String>,
3347 pub trait_: Option<Type>,
3349 pub items: Vec<Item>,
3350 pub polarity: Option<ImplPolarity>,
3351 pub synthetic: bool,
3352 pub blanket_impl: Option<Type>,
3355 pub fn get_auto_traits_with_node_id(cx: &DocContext, id: ast::NodeId, name: String) -> Vec<Item> {
3356 let finder = AutoTraitFinder::new(cx);
3357 finder.get_with_node_id(id, name)
3360 pub fn get_auto_traits_with_def_id(cx: &DocContext, id: DefId) -> Vec<Item> {
3361 let finder = AutoTraitFinder::new(cx);
3363 finder.get_with_def_id(id)
3366 pub fn get_blanket_impls_with_node_id(cx: &DocContext, id: ast::NodeId, name: String) -> Vec<Item> {
3367 let finder = BlanketImplFinder::new(cx);
3368 finder.get_with_node_id(id, name)
3371 pub fn get_blanket_impls_with_def_id(cx: &DocContext, id: DefId) -> Vec<Item> {
3372 let finder = BlanketImplFinder::new(cx);
3374 finder.get_with_def_id(id)
3377 impl Clean<Vec<Item>> for doctree::Impl {
3378 fn clean(&self, cx: &DocContext) -> Vec<Item> {
3379 let mut ret = Vec::new();
3380 let trait_ = self.trait_.clean(cx);
3381 let items = self.items.clean(cx);
3383 // If this impl block is an implementation of the Deref trait, then we
3384 // need to try inlining the target's inherent impl blocks as well.
3385 if trait_.def_id() == cx.tcx.lang_items().deref_trait() {
3386 build_deref_target_impls(cx, &items, &mut ret);
3389 let provided = trait_.def_id().map(|did| {
3390 cx.tcx.provided_trait_methods(did)
3392 .map(|meth| meth.ident.to_string())
3394 }).unwrap_or(FxHashSet());
3398 attrs: self.attrs.clean(cx),
3399 source: self.whence.clean(cx),
3400 def_id: cx.tcx.hir.local_def_id(self.id),
3401 visibility: self.vis.clean(cx),
3402 stability: self.stab.clean(cx),
3403 deprecation: self.depr.clean(cx),
3404 inner: ImplItem(Impl {
3405 unsafety: self.unsafety,
3406 generics: self.generics.clean(cx),
3407 provided_trait_methods: provided,
3409 for_: self.for_.clean(cx),
3411 polarity: Some(self.polarity.clean(cx)),
3420 fn build_deref_target_impls(cx: &DocContext,
3422 ret: &mut Vec<Item>) {
3423 use self::PrimitiveType::*;
3427 let target = match item.inner {
3428 TypedefItem(ref t, true) => &t.type_,
3431 let primitive = match *target {
3432 ResolvedPath { did, .. } if did.is_local() => continue,
3433 ResolvedPath { did, .. } => {
3434 ret.extend(inline::build_impls(cx, did));
3437 _ => match target.primitive_type() {
3442 let did = match primitive {
3443 Isize => tcx.lang_items().isize_impl(),
3444 I8 => tcx.lang_items().i8_impl(),
3445 I16 => tcx.lang_items().i16_impl(),
3446 I32 => tcx.lang_items().i32_impl(),
3447 I64 => tcx.lang_items().i64_impl(),
3448 I128 => tcx.lang_items().i128_impl(),
3449 Usize => tcx.lang_items().usize_impl(),
3450 U8 => tcx.lang_items().u8_impl(),
3451 U16 => tcx.lang_items().u16_impl(),
3452 U32 => tcx.lang_items().u32_impl(),
3453 U64 => tcx.lang_items().u64_impl(),
3454 U128 => tcx.lang_items().u128_impl(),
3455 F32 => tcx.lang_items().f32_impl(),
3456 F64 => tcx.lang_items().f64_impl(),
3457 Char => tcx.lang_items().char_impl(),
3459 Str => tcx.lang_items().str_impl(),
3460 Slice => tcx.lang_items().slice_impl(),
3461 Array => tcx.lang_items().slice_impl(),
3464 RawPointer => tcx.lang_items().const_ptr_impl(),
3469 if let Some(did) = did {
3470 if !did.is_local() {
3471 inline::build_impl(cx, did, ret);
3477 impl Clean<Item> for doctree::ExternCrate {
3478 fn clean(&self, cx: &DocContext) -> Item {
3481 attrs: self.attrs.clean(cx),
3482 source: self.whence.clean(cx),
3483 def_id: DefId { krate: self.cnum, index: CRATE_DEF_INDEX },
3484 visibility: self.vis.clean(cx),
3487 inner: ExternCrateItem(self.name.clean(cx), self.path.clone())
3492 impl Clean<Vec<Item>> for doctree::Import {
3493 fn clean(&self, cx: &DocContext) -> Vec<Item> {
3494 // We consider inlining the documentation of `pub use` statements, but we
3495 // forcefully don't inline if this is not public or if the
3496 // #[doc(no_inline)] attribute is present.
3497 // Don't inline doc(hidden) imports so they can be stripped at a later stage.
3498 let denied = !self.vis.node.is_pub() || self.attrs.iter().any(|a| {
3499 a.name() == "doc" && match a.meta_item_list() {
3500 Some(l) => attr::list_contains_name(&l, "no_inline") ||
3501 attr::list_contains_name(&l, "hidden"),
3505 let path = self.path.clean(cx);
3506 let inner = if self.glob {
3508 let mut visited = FxHashSet();
3509 if let Some(items) = inline::try_inline_glob(cx, path.def, &mut visited) {
3514 Import::Glob(resolve_use_source(cx, path))
3516 let name = self.name;
3518 let mut visited = FxHashSet();
3519 if let Some(items) = inline::try_inline(cx, path.def, name, &mut visited) {
3523 Import::Simple(name.clean(cx), resolve_use_source(cx, path))
3528 attrs: self.attrs.clean(cx),
3529 source: self.whence.clean(cx),
3530 def_id: cx.tcx.hir.local_def_id(ast::CRATE_NODE_ID),
3531 visibility: self.vis.clean(cx),
3534 inner: ImportItem(inner)
3539 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3541 // use source as str;
3542 Simple(String, ImportSource),
3547 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3548 pub struct ImportSource {
3550 pub did: Option<DefId>,
3553 impl Clean<Vec<Item>> for hir::ForeignMod {
3554 fn clean(&self, cx: &DocContext) -> Vec<Item> {
3555 let mut items = self.items.clean(cx);
3556 for item in &mut items {
3557 if let ForeignFunctionItem(ref mut f) = item.inner {
3558 f.header.abi = self.abi;
3565 impl Clean<Item> for hir::ForeignItem {
3566 fn clean(&self, cx: &DocContext) -> Item {
3567 let inner = match self.node {
3568 hir::ForeignItemKind::Fn(ref decl, ref names, ref generics) => {
3569 let (generics, decl) = enter_impl_trait(cx, || {
3570 (generics.clean(cx), (&**decl, &names[..]).clean(cx))
3572 ForeignFunctionItem(Function {
3575 header: hir::FnHeader {
3576 unsafety: hir::Unsafety::Unsafe,
3578 constness: hir::Constness::NotConst,
3579 asyncness: hir::IsAsync::NotAsync,
3583 hir::ForeignItemKind::Static(ref ty, mutbl) => {
3584 ForeignStaticItem(Static {
3585 type_: ty.clean(cx),
3586 mutability: if mutbl {Mutable} else {Immutable},
3587 expr: String::new(),
3590 hir::ForeignItemKind::Type => {
3596 name: Some(self.name.clean(cx)),
3597 attrs: self.attrs.clean(cx),
3598 source: self.span.clean(cx),
3599 def_id: cx.tcx.hir.local_def_id(self.id),
3600 visibility: self.vis.clean(cx),
3601 stability: get_stability(cx, cx.tcx.hir.local_def_id(self.id)),
3602 deprecation: get_deprecation(cx, cx.tcx.hir.local_def_id(self.id)),
3610 pub trait ToSource {
3611 fn to_src(&self, cx: &DocContext) -> String;
3614 impl ToSource for syntax_pos::Span {
3615 fn to_src(&self, cx: &DocContext) -> String {
3616 debug!("converting span {:?} to snippet", self.clean(cx));
3617 let sn = match cx.sess().source_map().span_to_snippet(*self) {
3618 Ok(x) => x.to_string(),
3619 Err(_) => String::new()
3621 debug!("got snippet {}", sn);
3626 fn name_from_pat(p: &hir::Pat) -> String {
3628 debug!("Trying to get a name from pattern: {:?}", p);
3631 PatKind::Wild => "_".to_string(),
3632 PatKind::Binding(_, _, ident, _) => ident.to_string(),
3633 PatKind::TupleStruct(ref p, ..) | PatKind::Path(ref p) => qpath_to_string(p),
3634 PatKind::Struct(ref name, ref fields, etc) => {
3635 format!("{} {{ {}{} }}", qpath_to_string(name),
3636 fields.iter().map(|&Spanned { node: ref fp, .. }|
3637 format!("{}: {}", fp.ident, name_from_pat(&*fp.pat)))
3638 .collect::<Vec<String>>().join(", "),
3639 if etc { ", ..." } else { "" }
3642 PatKind::Tuple(ref elts, _) => format!("({})", elts.iter().map(|p| name_from_pat(&**p))
3643 .collect::<Vec<String>>().join(", ")),
3644 PatKind::Box(ref p) => name_from_pat(&**p),
3645 PatKind::Ref(ref p, _) => name_from_pat(&**p),
3646 PatKind::Lit(..) => {
3647 warn!("tried to get argument name from PatKind::Lit, \
3648 which is silly in function arguments");
3651 PatKind::Range(..) => panic!("tried to get argument name from PatKind::Range, \
3652 which is not allowed in function arguments"),
3653 PatKind::Slice(ref begin, ref mid, ref end) => {
3654 let begin = begin.iter().map(|p| name_from_pat(&**p));
3655 let mid = mid.as_ref().map(|p| format!("..{}", name_from_pat(&**p))).into_iter();
3656 let end = end.iter().map(|p| name_from_pat(&**p));
3657 format!("[{}]", begin.chain(mid).chain(end).collect::<Vec<_>>().join(", "))
3662 fn print_const(cx: &DocContext, n: &ty::Const) -> String {
3664 ConstValue::Unevaluated(def_id, _) => {
3665 if let Some(node_id) = cx.tcx.hir.as_local_node_id(def_id) {
3666 print_const_expr(cx, cx.tcx.hir.body_owned_by(node_id))
3668 inline::print_inlined_const(cx, def_id)
3672 let mut s = String::new();
3673 ::rustc::mir::fmt_const_val(&mut s, n).expect("fmt_const_val failed");
3674 // array lengths are obviously usize
3675 if s.ends_with("usize") {
3676 let n = s.len() - "usize".len();
3684 fn print_const_expr(cx: &DocContext, body: hir::BodyId) -> String {
3685 cx.tcx.hir.node_to_pretty_string(body.node_id)
3688 /// Given a type Path, resolve it to a Type using the TyCtxt
3689 fn resolve_type(cx: &DocContext,
3691 id: ast::NodeId) -> Type {
3692 if id == ast::DUMMY_NODE_ID {
3693 debug!("resolve_type({:?})", path);
3695 debug!("resolve_type({:?},{:?})", path, id);
3698 let is_generic = match path.def {
3699 Def::PrimTy(p) => match p {
3700 hir::Str => return Primitive(PrimitiveType::Str),
3701 hir::Bool => return Primitive(PrimitiveType::Bool),
3702 hir::Char => return Primitive(PrimitiveType::Char),
3703 hir::Int(int_ty) => return Primitive(int_ty.into()),
3704 hir::Uint(uint_ty) => return Primitive(uint_ty.into()),
3705 hir::Float(float_ty) => return Primitive(float_ty.into()),
3707 Def::SelfTy(..) if path.segments.len() == 1 => {
3708 return Generic(keywords::SelfType.name().to_string());
3710 Def::TyParam(..) if path.segments.len() == 1 => {
3711 return Generic(format!("{:#}", path));
3713 Def::SelfTy(..) | Def::TyParam(..) | Def::AssociatedTy(..) => true,
3716 let did = register_def(&*cx, path.def);
3717 ResolvedPath { path: path, typarams: None, did: did, is_generic: is_generic }
3720 pub fn register_def(cx: &DocContext, def: Def) -> DefId {
3721 debug!("register_def({:?})", def);
3723 let (did, kind) = match def {
3724 Def::Fn(i) => (i, TypeKind::Function),
3725 Def::TyAlias(i) => (i, TypeKind::Typedef),
3726 Def::Enum(i) => (i, TypeKind::Enum),
3727 Def::Trait(i) => (i, TypeKind::Trait),
3728 Def::Struct(i) => (i, TypeKind::Struct),
3729 Def::Union(i) => (i, TypeKind::Union),
3730 Def::Mod(i) => (i, TypeKind::Module),
3731 Def::ForeignTy(i) => (i, TypeKind::Foreign),
3732 Def::Const(i) => (i, TypeKind::Const),
3733 Def::Static(i, _) => (i, TypeKind::Static),
3734 Def::Variant(i) => (cx.tcx.parent_def_id(i).expect("cannot get parent def id"),
3736 Def::Macro(i, mac_kind) => match mac_kind {
3737 MacroKind::Bang => (i, TypeKind::Macro),
3738 MacroKind::Attr => (i, TypeKind::Attr),
3739 MacroKind::Derive => (i, TypeKind::Derive),
3740 MacroKind::ProcMacroStub => unreachable!(),
3742 Def::SelfTy(Some(def_id), _) => (def_id, TypeKind::Trait),
3743 Def::SelfTy(_, Some(impl_def_id)) => {
3746 _ => return def.def_id()
3748 if did.is_local() { return did }
3749 inline::record_extern_fqn(cx, did, kind);
3750 if let TypeKind::Trait = kind {
3751 inline::record_extern_trait(cx, did);
3756 fn resolve_use_source(cx: &DocContext, path: Path) -> ImportSource {
3758 did: if path.def == Def::Err {
3761 Some(register_def(cx, path.def))
3767 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3770 pub imported_from: Option<String>,
3773 impl Clean<Item> for doctree::Macro {
3774 fn clean(&self, cx: &DocContext) -> Item {
3775 let name = self.name.clean(cx);
3777 name: Some(name.clone()),
3778 attrs: self.attrs.clean(cx),
3779 source: self.whence.clean(cx),
3780 visibility: Some(Public),
3781 stability: self.stab.clean(cx),
3782 deprecation: self.depr.clean(cx),
3783 def_id: self.def_id,
3784 inner: MacroItem(Macro {
3785 source: format!("macro_rules! {} {{\n{}}}",
3787 self.matchers.iter().map(|span| {
3788 format!(" {} => {{ ... }};\n", span.to_src(cx))
3789 }).collect::<String>()),
3790 imported_from: self.imported_from.clean(cx),
3796 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3797 pub struct ProcMacro {
3798 pub kind: MacroKind,
3799 pub helpers: Vec<String>,
3802 impl Clean<Item> for doctree::ProcMacro {
3803 fn clean(&self, cx: &DocContext) -> Item {
3805 name: Some(self.name.clean(cx)),
3806 attrs: self.attrs.clean(cx),
3807 source: self.whence.clean(cx),
3808 visibility: Some(Public),
3809 stability: self.stab.clean(cx),
3810 deprecation: self.depr.clean(cx),
3811 def_id: cx.tcx.hir.local_def_id(self.id),
3812 inner: ProcMacroItem(ProcMacro {
3814 helpers: self.helpers.clean(cx),
3820 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3821 pub struct Stability {
3822 pub level: stability::StabilityLevel,
3823 pub feature: String,
3825 pub deprecated_since: String,
3826 pub deprecated_reason: String,
3827 pub unstable_reason: String,
3828 pub issue: Option<u32>
3831 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3832 pub struct Deprecation {
3837 impl Clean<Stability> for attr::Stability {
3838 fn clean(&self, _: &DocContext) -> Stability {
3840 level: stability::StabilityLevel::from_attr_level(&self.level),
3841 feature: self.feature.to_string(),
3842 since: match self.level {
3843 attr::Stable {ref since} => since.to_string(),
3846 deprecated_since: match self.rustc_depr {
3847 Some(attr::RustcDeprecation {ref since, ..}) => since.to_string(),
3850 deprecated_reason: match self.rustc_depr {
3851 Some(ref depr) => depr.reason.to_string(),
3854 unstable_reason: match self.level {
3855 attr::Unstable { reason: Some(ref reason), .. } => reason.to_string(),
3858 issue: match self.level {
3859 attr::Unstable {issue, ..} => Some(issue),
3866 impl<'a> Clean<Stability> for &'a attr::Stability {
3867 fn clean(&self, dc: &DocContext) -> Stability {
3872 impl Clean<Deprecation> for attr::Deprecation {
3873 fn clean(&self, _: &DocContext) -> Deprecation {
3875 since: self.since.as_ref().map_or(String::new(), |s| s.to_string()),
3876 note: self.note.as_ref().map_or(String::new(), |s| s.to_string()),
3881 /// An equality constraint on an associated type, e.g. `A=Bar` in `Foo<A=Bar>`
3882 #[derive(Clone, PartialEq, Eq, RustcDecodable, RustcEncodable, Debug, Hash)]
3883 pub struct TypeBinding {
3888 impl Clean<TypeBinding> for hir::TypeBinding {
3889 fn clean(&self, cx: &DocContext) -> TypeBinding {
3891 name: self.ident.name.clean(cx),
3892 ty: self.ty.clean(cx)
3897 pub fn def_id_to_path(cx: &DocContext, did: DefId, name: Option<String>) -> Vec<String> {
3898 let crate_name = name.unwrap_or_else(|| cx.tcx.crate_name(did.krate).to_string());
3899 let relative = cx.tcx.def_path(did).data.into_iter().filter_map(|elem| {
3900 // extern blocks have an empty name
3901 let s = elem.data.to_string();
3908 once(crate_name).chain(relative).collect()
3911 pub fn enter_impl_trait<F, R>(cx: &DocContext, f: F) -> R
3915 let old_bounds = mem::replace(&mut *cx.impl_trait_bounds.borrow_mut(), Default::default());
3917 assert!(cx.impl_trait_bounds.borrow().is_empty());
3918 *cx.impl_trait_bounds.borrow_mut() = old_bounds;
3922 // Start of code copied from rust-clippy
3924 pub fn path_to_def_local(tcx: &TyCtxt, path: &[&str]) -> Option<DefId> {
3925 let krate = tcx.hir.krate();
3926 let mut items = krate.module.item_ids.clone();
3927 let mut path_it = path.iter().peekable();
3930 let segment = match path_it.next() {
3931 Some(segment) => segment,
3932 None => return None,
3935 for item_id in mem::replace(&mut items, HirVec::new()).iter() {
3936 let item = tcx.hir.expect_item(item_id.id);
3937 if item.name == *segment {
3938 if path_it.peek().is_none() {
3939 return Some(tcx.hir.local_def_id(item_id.id))
3942 items = match &item.node {
3943 &hir::ItemKind::Mod(ref m) => m.item_ids.clone(),
3944 _ => panic!("Unexpected item {:?} in path {:?} path")
3952 pub fn path_to_def(tcx: &TyCtxt, path: &[&str]) -> Option<DefId> {
3953 let crates = tcx.crates();
3957 .find(|&&krate| tcx.crate_name(krate) == path[0]);
3959 if let Some(krate) = krate {
3962 index: CRATE_DEF_INDEX,
3964 let mut items = tcx.item_children(krate);
3965 let mut path_it = path.iter().skip(1).peekable();
3968 let segment = match path_it.next() {
3969 Some(segment) => segment,
3970 None => return None,
3973 for item in mem::replace(&mut items, Lrc::new(vec![])).iter() {
3974 if item.ident.name == *segment {
3975 if path_it.peek().is_none() {
3976 return match item.def {
3977 def::Def::Trait(did) => Some(did),
3982 items = tcx.item_children(item.def.def_id());
3992 pub fn get_path_for_type<F>(tcx: TyCtxt, def_id: DefId, def_ctor: F) -> hir::Path
3993 where F: Fn(DefId) -> Def {
3995 struct AbsolutePathBuffer {
3999 impl ty::item_path::ItemPathBuffer for AbsolutePathBuffer {
4000 fn root_mode(&self) -> &ty::item_path::RootMode {
4001 const ABSOLUTE: &'static ty::item_path::RootMode = &ty::item_path::RootMode::Absolute;
4005 fn push(&mut self, text: &str) {
4006 self.names.push(text.to_owned());
4010 let mut apb = AbsolutePathBuffer { names: vec![] };
4012 tcx.push_item_path(&mut apb, def_id, false);
4016 def: def_ctor(def_id),
4017 segments: hir::HirVec::from_vec(apb.names.iter().map(|s| hir::PathSegment {
4018 ident: ast::Ident::from_str(&s),
4025 // End of code copied from rust-clippy
4028 #[derive(Eq, PartialEq, Hash, Copy, Clone, Debug)]
4029 enum RegionTarget<'tcx> {
4030 Region(Region<'tcx>),
4031 RegionVid(RegionVid)
4034 #[derive(Default, Debug, Clone)]
4035 struct RegionDeps<'tcx> {
4036 larger: FxHashSet<RegionTarget<'tcx>>,
4037 smaller: FxHashSet<RegionTarget<'tcx>>
4040 #[derive(Eq, PartialEq, Hash, Debug)]
4042 TraitBound(Vec<PathSegment>, Vec<SimpleBound>, Vec<GenericParamDef>, hir::TraitBoundModifier),
4046 enum AutoTraitResult {
4048 PositiveImpl(Generics),
4052 impl AutoTraitResult {
4053 fn is_auto(&self) -> bool {
4055 AutoTraitResult::PositiveImpl(_) | AutoTraitResult::NegativeImpl => true,
4061 impl From<GenericBound> for SimpleBound {
4062 fn from(bound: GenericBound) -> Self {
4063 match bound.clone() {
4064 GenericBound::Outlives(l) => SimpleBound::Outlives(l),
4065 GenericBound::TraitBound(t, mod_) => match t.trait_ {
4066 Type::ResolvedPath { path, typarams, .. } => {
4067 SimpleBound::TraitBound(path.segments,
4069 .map_or_else(|| Vec::new(), |v| v.iter()
4070 .map(|p| SimpleBound::from(p.clone()))
4075 _ => panic!("Unexpected bound {:?}", bound),