1 //! This module contains the "cleaned" pieces of the AST, and the functions
12 use rustc_index::vec::{IndexVec, Idx};
13 use rustc_typeck::hir_ty_to_ty;
14 use rustc::infer::region_constraints::{RegionConstraintData, Constraint};
15 use rustc::middle::resolve_lifetime as rl;
16 use rustc::middle::lang_items;
17 use rustc::middle::stability;
18 use rustc::mir::interpret::GlobalId;
20 use rustc::hir::def::{CtorKind, DefKind, Res};
21 use rustc::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX};
22 use rustc::hir::ptr::P;
23 use rustc::ty::subst::InternalSubsts;
24 use rustc::ty::{self, TyCtxt, Ty, AdtKind};
25 use rustc::ty::fold::TypeFolder;
26 use rustc::util::nodemap::{FxHashMap, FxHashSet};
27 use syntax::ast::{self, Ident};
29 use syntax_pos::symbol::{kw, sym};
30 use syntax_pos::hygiene::MacroKind;
31 use syntax_pos::{self, Pos};
33 use std::collections::hash_map::Entry;
35 use std::default::Default;
40 use crate::core::{self, DocContext, ImplTraitParam};
45 pub use utils::{get_auto_trait_and_blanket_impls, krate, register_res};
47 pub use self::types::*;
48 pub use self::types::Type::*;
49 pub use self::types::Mutability::*;
50 pub use self::types::ItemEnum::*;
51 pub use self::types::SelfTy::*;
52 pub use self::types::FunctionRetTy::*;
53 pub use self::types::Visibility::{Public, Inherited};
55 const FN_OUTPUT_NAME: &'static str = "Output";
58 fn clean(&self, cx: &DocContext<'_>) -> T;
61 impl<T: Clean<U>, U> Clean<Vec<U>> for [T] {
62 fn clean(&self, cx: &DocContext<'_>) -> Vec<U> {
63 self.iter().map(|x| x.clean(cx)).collect()
67 impl<T: Clean<U>, U, V: Idx> Clean<IndexVec<V, U>> for IndexVec<V, T> {
68 fn clean(&self, cx: &DocContext<'_>) -> IndexVec<V, U> {
69 self.iter().map(|x| x.clean(cx)).collect()
73 impl<T: Clean<U>, U> Clean<U> for P<T> {
74 fn clean(&self, cx: &DocContext<'_>) -> U {
79 impl<T: Clean<U>, U> Clean<U> for Rc<T> {
80 fn clean(&self, cx: &DocContext<'_>) -> U {
85 impl<T: Clean<U>, U> Clean<Option<U>> for Option<T> {
86 fn clean(&self, cx: &DocContext<'_>) -> Option<U> {
87 self.as_ref().map(|v| v.clean(cx))
91 impl<T, U> Clean<U> for ty::Binder<T> where T: Clean<U> {
92 fn clean(&self, cx: &DocContext<'_>) -> U {
93 self.skip_binder().clean(cx)
97 impl<T: Clean<U>, U> Clean<Vec<U>> for P<[T]> {
98 fn clean(&self, cx: &DocContext<'_>) -> Vec<U> {
99 self.iter().map(|x| x.clean(cx)).collect()
103 impl Clean<ExternalCrate> for CrateNum {
104 fn clean(&self, cx: &DocContext<'_>) -> ExternalCrate {
105 let root = DefId { krate: *self, index: CRATE_DEF_INDEX };
106 let krate_span = cx.tcx.def_span(root);
107 let krate_src = cx.sess().source_map().span_to_filename(krate_span);
109 // Collect all inner modules which are tagged as implementations of
112 // Note that this loop only searches the top-level items of the crate,
113 // and this is intentional. If we were to search the entire crate for an
114 // item tagged with `#[doc(primitive)]` then we would also have to
115 // search the entirety of external modules for items tagged
116 // `#[doc(primitive)]`, which is a pretty inefficient process (decoding
117 // all that metadata unconditionally).
119 // In order to keep the metadata load under control, the
120 // `#[doc(primitive)]` feature is explicitly designed to only allow the
121 // primitive tags to show up as the top level items in a crate.
123 // Also note that this does not attempt to deal with modules tagged
124 // duplicately for the same primitive. This is handled later on when
125 // rendering by delegating everything to a hash map.
126 let as_primitive = |res: Res| {
127 if let Res::Def(DefKind::Mod, def_id) = res {
128 let attrs = cx.tcx.get_attrs(def_id).clean(cx);
130 for attr in attrs.lists(sym::doc) {
131 if let Some(v) = attr.value_str() {
132 if attr.check_name(sym::primitive) {
133 prim = PrimitiveType::from_str(&v.as_str());
137 // FIXME: should warn on unknown primitives?
141 return prim.map(|p| (def_id, p, attrs));
145 let primitives = if root.is_local() {
146 cx.tcx.hir().krate().module.item_ids.iter().filter_map(|&id| {
147 let item = cx.tcx.hir().expect_item(id.id);
149 hir::ItemKind::Mod(_) => {
150 as_primitive(Res::Def(
152 cx.tcx.hir().local_def_id(id.id),
155 hir::ItemKind::Use(ref path, hir::UseKind::Single)
156 if item.vis.node.is_pub() => {
157 as_primitive(path.res).map(|(_, prim, attrs)| {
158 // Pretend the primitive is local.
159 (cx.tcx.hir().local_def_id(id.id), prim, attrs)
166 cx.tcx.item_children(root).iter().map(|item| item.res)
167 .filter_map(as_primitive).collect()
170 let as_keyword = |res: Res| {
171 if let Res::Def(DefKind::Mod, def_id) = res {
172 let attrs = cx.tcx.get_attrs(def_id).clean(cx);
173 let mut keyword = None;
174 for attr in attrs.lists(sym::doc) {
175 if let Some(v) = attr.value_str() {
176 if attr.check_name(sym::keyword) {
177 if v.is_doc_keyword() {
178 keyword = Some(v.to_string());
181 // FIXME: should warn on unknown keywords?
185 return keyword.map(|p| (def_id, p, attrs));
189 let keywords = if root.is_local() {
190 cx.tcx.hir().krate().module.item_ids.iter().filter_map(|&id| {
191 let item = cx.tcx.hir().expect_item(id.id);
193 hir::ItemKind::Mod(_) => {
196 cx.tcx.hir().local_def_id(id.id),
199 hir::ItemKind::Use(ref path, hir::UseKind::Single)
200 if item.vis.node.is_pub() => {
201 as_keyword(path.res).map(|(_, prim, attrs)| {
202 (cx.tcx.hir().local_def_id(id.id), prim, attrs)
209 cx.tcx.item_children(root).iter().map(|item| item.res)
210 .filter_map(as_keyword).collect()
214 name: cx.tcx.crate_name(*self).to_string(),
216 attrs: cx.tcx.get_attrs(root).clean(cx),
223 impl Clean<Item> for doctree::Module<'_> {
224 fn clean(&self, cx: &DocContext<'_>) -> Item {
225 let name = if self.name.is_some() {
226 self.name.expect("No name provided").clean(cx)
231 // maintain a stack of mod ids, for doc comment path resolution
232 // but we also need to resolve the module's own docs based on whether its docs were written
233 // inside or outside the module, so check for that
234 let attrs = self.attrs.clean(cx);
236 let mut items: Vec<Item> = vec![];
237 items.extend(self.extern_crates.iter().flat_map(|x| x.clean(cx)));
238 items.extend(self.imports.iter().flat_map(|x| x.clean(cx)));
239 items.extend(self.structs.iter().map(|x| x.clean(cx)));
240 items.extend(self.unions.iter().map(|x| x.clean(cx)));
241 items.extend(self.enums.iter().map(|x| x.clean(cx)));
242 items.extend(self.fns.iter().map(|x| x.clean(cx)));
243 items.extend(self.foreigns.iter().map(|x| x.clean(cx)));
244 items.extend(self.mods.iter().map(|x| x.clean(cx)));
245 items.extend(self.typedefs.iter().map(|x| x.clean(cx)));
246 items.extend(self.opaque_tys.iter().map(|x| x.clean(cx)));
247 items.extend(self.statics.iter().map(|x| x.clean(cx)));
248 items.extend(self.constants.iter().map(|x| x.clean(cx)));
249 items.extend(self.traits.iter().map(|x| x.clean(cx)));
250 items.extend(self.impls.iter().flat_map(|x| x.clean(cx)));
251 items.extend(self.macros.iter().map(|x| x.clean(cx)));
252 items.extend(self.proc_macros.iter().map(|x| x.clean(cx)));
253 items.extend(self.trait_aliases.iter().map(|x| x.clean(cx)));
255 // determine if we should display the inner contents or
256 // the outer `mod` item for the source code.
258 let cm = cx.sess().source_map();
259 let outer = cm.lookup_char_pos(self.where_outer.lo());
260 let inner = cm.lookup_char_pos(self.where_inner.lo());
261 if outer.file.start_pos == inner.file.start_pos {
265 // mod foo; (and a separate SourceFile for the contents)
273 source: whence.clean(cx),
274 visibility: self.vis.clean(cx),
275 stability: cx.stability(self.id).clean(cx),
276 deprecation: cx.deprecation(self.id).clean(cx),
277 def_id: cx.tcx.hir().local_def_id(self.id),
278 inner: ModuleItem(Module {
279 is_crate: self.is_crate,
286 impl Clean<Attributes> for [ast::Attribute] {
287 fn clean(&self, cx: &DocContext<'_>) -> Attributes {
288 Attributes::from_ast(cx.sess().diagnostic(), self)
292 impl Clean<GenericBound> for hir::GenericBound {
293 fn clean(&self, cx: &DocContext<'_>) -> GenericBound {
295 hir::GenericBound::Outlives(lt) => GenericBound::Outlives(lt.clean(cx)),
296 hir::GenericBound::Trait(ref t, modifier) => {
297 GenericBound::TraitBound(t.clean(cx), modifier)
303 impl<'a, 'tcx> Clean<GenericBound> for (&'a ty::TraitRef<'tcx>, Vec<TypeBinding>) {
304 fn clean(&self, cx: &DocContext<'_>) -> GenericBound {
305 let (trait_ref, ref bounds) = *self;
306 inline::record_extern_fqn(cx, trait_ref.def_id, TypeKind::Trait);
307 let path = external_path(cx, cx.tcx.item_name(trait_ref.def_id),
308 Some(trait_ref.def_id), true, bounds.clone(), trait_ref.substs);
310 debug!("ty::TraitRef\n subst: {:?}\n", trait_ref.substs);
312 // collect any late bound regions
313 let mut late_bounds = vec![];
314 for ty_s in trait_ref.input_types().skip(1) {
315 if let ty::Tuple(ts) = ty_s.kind {
317 if let ty::Ref(ref reg, _, _) = ty_s.expect_ty().kind {
318 if let &ty::RegionKind::ReLateBound(..) = *reg {
319 debug!(" hit an ReLateBound {:?}", reg);
320 if let Some(Lifetime(name)) = reg.clean(cx) {
321 late_bounds.push(GenericParamDef {
323 kind: GenericParamDefKind::Lifetime,
332 GenericBound::TraitBound(
334 trait_: ResolvedPath {
337 did: trait_ref.def_id,
340 generic_params: late_bounds,
342 hir::TraitBoundModifier::None
347 impl<'tcx> Clean<GenericBound> for ty::TraitRef<'tcx> {
348 fn clean(&self, cx: &DocContext<'_>) -> GenericBound {
349 (self, vec![]).clean(cx)
353 impl<'tcx> Clean<Option<Vec<GenericBound>>> for InternalSubsts<'tcx> {
354 fn clean(&self, cx: &DocContext<'_>) -> Option<Vec<GenericBound>> {
355 let mut v = Vec::new();
356 v.extend(self.regions().filter_map(|r| r.clean(cx)).map(GenericBound::Outlives));
357 v.extend(self.types().map(|t| GenericBound::TraitBound(PolyTrait {
359 generic_params: Vec::new(),
360 }, hir::TraitBoundModifier::None)));
361 if !v.is_empty() {Some(v)} else {None}
365 impl Clean<Lifetime> for hir::Lifetime {
366 fn clean(&self, cx: &DocContext<'_>) -> Lifetime {
367 if self.hir_id != hir::DUMMY_HIR_ID {
368 let def = cx.tcx.named_region(self.hir_id);
370 Some(rl::Region::EarlyBound(_, node_id, _)) |
371 Some(rl::Region::LateBound(_, node_id, _)) |
372 Some(rl::Region::Free(_, node_id)) => {
373 if let Some(lt) = cx.lt_substs.borrow().get(&node_id).cloned() {
380 Lifetime(self.name.ident().to_string())
384 impl Clean<Lifetime> for hir::GenericParam {
385 fn clean(&self, _: &DocContext<'_>) -> Lifetime {
387 hir::GenericParamKind::Lifetime { .. } => {
388 if self.bounds.len() > 0 {
389 let mut bounds = self.bounds.iter().map(|bound| match bound {
390 hir::GenericBound::Outlives(lt) => lt,
393 let name = bounds.next().expect("no more bounds").name.ident();
394 let mut s = format!("{}: {}", self.name.ident(), name);
395 for bound in bounds {
396 s.push_str(&format!(" + {}", bound.name.ident()));
400 Lifetime(self.name.ident().to_string())
408 impl Clean<Constant> for hir::ConstArg {
409 fn clean(&self, cx: &DocContext<'_>) -> Constant {
411 type_: cx.tcx.type_of(cx.tcx.hir().body_owner_def_id(self.value.body)).clean(cx),
412 expr: print_const_expr(cx, self.value.body),
417 impl Clean<Lifetime> for ty::GenericParamDef {
418 fn clean(&self, _cx: &DocContext<'_>) -> Lifetime {
419 Lifetime(self.name.to_string())
423 impl Clean<Option<Lifetime>> for ty::RegionKind {
424 fn clean(&self, cx: &DocContext<'_>) -> Option<Lifetime> {
426 ty::ReStatic => Some(Lifetime::statik()),
427 ty::ReLateBound(_, ty::BrNamed(_, name)) => Some(Lifetime(name.to_string())),
428 ty::ReEarlyBound(ref data) => Some(Lifetime(data.name.clean(cx))),
430 ty::ReLateBound(..) |
434 ty::RePlaceholder(..) |
436 ty::ReClosureBound(_) |
438 debug!("cannot clean region {:?}", self);
445 impl Clean<WherePredicate> for hir::WherePredicate {
446 fn clean(&self, cx: &DocContext<'_>) -> WherePredicate {
448 hir::WherePredicate::BoundPredicate(ref wbp) => {
449 WherePredicate::BoundPredicate {
450 ty: wbp.bounded_ty.clean(cx),
451 bounds: wbp.bounds.clean(cx)
455 hir::WherePredicate::RegionPredicate(ref wrp) => {
456 WherePredicate::RegionPredicate {
457 lifetime: wrp.lifetime.clean(cx),
458 bounds: wrp.bounds.clean(cx)
462 hir::WherePredicate::EqPredicate(ref wrp) => {
463 WherePredicate::EqPredicate {
464 lhs: wrp.lhs_ty.clean(cx),
465 rhs: wrp.rhs_ty.clean(cx)
472 impl<'a> Clean<Option<WherePredicate>> for ty::Predicate<'a> {
473 fn clean(&self, cx: &DocContext<'_>) -> Option<WherePredicate> {
474 use rustc::ty::Predicate;
477 Predicate::Trait(ref pred) => Some(pred.clean(cx)),
478 Predicate::Subtype(ref pred) => Some(pred.clean(cx)),
479 Predicate::RegionOutlives(ref pred) => pred.clean(cx),
480 Predicate::TypeOutlives(ref pred) => pred.clean(cx),
481 Predicate::Projection(ref pred) => Some(pred.clean(cx)),
483 Predicate::WellFormed(..) |
484 Predicate::ObjectSafe(..) |
485 Predicate::ClosureKind(..) |
486 Predicate::ConstEvaluatable(..) => panic!("not user writable"),
491 impl<'a> Clean<WherePredicate> for ty::TraitPredicate<'a> {
492 fn clean(&self, cx: &DocContext<'_>) -> WherePredicate {
493 WherePredicate::BoundPredicate {
494 ty: self.trait_ref.self_ty().clean(cx),
495 bounds: vec![self.trait_ref.clean(cx)]
500 impl<'tcx> Clean<WherePredicate> for ty::SubtypePredicate<'tcx> {
501 fn clean(&self, _cx: &DocContext<'_>) -> WherePredicate {
502 panic!("subtype predicates are an internal rustc artifact \
503 and should not be seen by rustdoc")
507 impl<'tcx> Clean<Option<WherePredicate>> for
508 ty::OutlivesPredicate<ty::Region<'tcx>,ty::Region<'tcx>> {
510 fn clean(&self, cx: &DocContext<'_>) -> Option<WherePredicate> {
511 let ty::OutlivesPredicate(ref a, ref b) = *self;
514 (ty::ReEmpty, ty::ReEmpty) => {
520 Some(WherePredicate::RegionPredicate {
521 lifetime: a.clean(cx).expect("failed to clean lifetime"),
522 bounds: vec![GenericBound::Outlives(b.clean(cx).expect("failed to clean bounds"))]
527 impl<'tcx> Clean<Option<WherePredicate>> for ty::OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>> {
528 fn clean(&self, cx: &DocContext<'_>) -> Option<WherePredicate> {
529 let ty::OutlivesPredicate(ref ty, ref lt) = *self;
532 ty::ReEmpty => return None,
536 Some(WherePredicate::BoundPredicate {
538 bounds: vec![GenericBound::Outlives(lt.clean(cx).expect("failed to clean lifetimes"))]
543 impl<'tcx> Clean<WherePredicate> for ty::ProjectionPredicate<'tcx> {
544 fn clean(&self, cx: &DocContext<'_>) -> WherePredicate {
545 WherePredicate::EqPredicate {
546 lhs: self.projection_ty.clean(cx),
547 rhs: self.ty.clean(cx)
552 impl<'tcx> Clean<Type> for ty::ProjectionTy<'tcx> {
553 fn clean(&self, cx: &DocContext<'_>) -> Type {
554 let trait_ = match self.trait_ref(cx.tcx).clean(cx) {
555 GenericBound::TraitBound(t, _) => t.trait_,
556 GenericBound::Outlives(_) => panic!("cleaning a trait got a lifetime"),
559 name: cx.tcx.associated_item(self.item_def_id).ident.name.clean(cx),
560 self_type: box self.self_ty().clean(cx),
566 impl Clean<GenericParamDef> for ty::GenericParamDef {
567 fn clean(&self, cx: &DocContext<'_>) -> GenericParamDef {
568 let (name, kind) = match self.kind {
569 ty::GenericParamDefKind::Lifetime => {
570 (self.name.to_string(), GenericParamDefKind::Lifetime)
572 ty::GenericParamDefKind::Type { has_default, synthetic, .. } => {
573 let default = if has_default {
574 Some(cx.tcx.type_of(self.def_id).clean(cx))
578 (self.name.clean(cx), GenericParamDefKind::Type {
580 bounds: vec![], // These are filled in from the where-clauses.
585 ty::GenericParamDefKind::Const { .. } => {
586 (self.name.clean(cx), GenericParamDefKind::Const {
588 ty: cx.tcx.type_of(self.def_id).clean(cx),
600 impl Clean<GenericParamDef> for hir::GenericParam {
601 fn clean(&self, cx: &DocContext<'_>) -> GenericParamDef {
602 let (name, kind) = match self.kind {
603 hir::GenericParamKind::Lifetime { .. } => {
604 let name = if self.bounds.len() > 0 {
605 let mut bounds = self.bounds.iter().map(|bound| match bound {
606 hir::GenericBound::Outlives(lt) => lt,
609 let name = bounds.next().expect("no more bounds").name.ident();
610 let mut s = format!("{}: {}", self.name.ident(), name);
611 for bound in bounds {
612 s.push_str(&format!(" + {}", bound.name.ident()));
616 self.name.ident().to_string()
618 (name, GenericParamDefKind::Lifetime)
620 hir::GenericParamKind::Type { ref default, synthetic } => {
621 (self.name.ident().name.clean(cx), GenericParamDefKind::Type {
622 did: cx.tcx.hir().local_def_id(self.hir_id),
623 bounds: self.bounds.clean(cx),
624 default: default.clean(cx),
628 hir::GenericParamKind::Const { ref ty } => {
629 (self.name.ident().name.clean(cx), GenericParamDefKind::Const {
630 did: cx.tcx.hir().local_def_id(self.hir_id),
643 impl Clean<Generics> for hir::Generics {
644 fn clean(&self, cx: &DocContext<'_>) -> Generics {
645 // Synthetic type-parameters are inserted after normal ones.
646 // In order for normal parameters to be able to refer to synthetic ones,
648 fn is_impl_trait(param: &hir::GenericParam) -> bool {
650 hir::GenericParamKind::Type { synthetic, .. } => {
651 synthetic == Some(hir::SyntheticTyParamKind::ImplTrait)
656 let impl_trait_params = self.params
658 .filter(|param| is_impl_trait(param))
660 let param: GenericParamDef = param.clean(cx);
662 GenericParamDefKind::Lifetime => unreachable!(),
663 GenericParamDefKind::Type { did, ref bounds, .. } => {
664 cx.impl_trait_bounds.borrow_mut().insert(did.into(), bounds.clone());
666 GenericParamDefKind::Const { .. } => unreachable!(),
670 .collect::<Vec<_>>();
672 let mut params = Vec::with_capacity(self.params.len());
673 for p in self.params.iter().filter(|p| !is_impl_trait(p)) {
677 params.extend(impl_trait_params);
679 let mut generics = Generics {
681 where_predicates: self.where_clause.predicates.clean(cx),
684 // Some duplicates are generated for ?Sized bounds between type params and where
685 // predicates. The point in here is to move the bounds definitions from type params
686 // to where predicates when such cases occur.
687 for where_pred in &mut generics.where_predicates {
689 WherePredicate::BoundPredicate { ty: Generic(ref name), ref mut bounds } => {
690 if bounds.is_empty() {
691 for param in &mut generics.params {
693 GenericParamDefKind::Lifetime => {}
694 GenericParamDefKind::Type { bounds: ref mut ty_bounds, .. } => {
695 if ¶m.name == name {
696 mem::swap(bounds, ty_bounds);
700 GenericParamDefKind::Const { .. } => {}
712 impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics, ty::GenericPredicates<'tcx>) {
713 fn clean(&self, cx: &DocContext<'_>) -> Generics {
714 use self::WherePredicate as WP;
715 use std::collections::BTreeMap;
717 let (gens, preds) = *self;
719 // Don't populate `cx.impl_trait_bounds` before `clean`ning `where` clauses,
720 // since `Clean for ty::Predicate` would consume them.
721 let mut impl_trait = BTreeMap::<ImplTraitParam, Vec<GenericBound>>::default();
723 // Bounds in the type_params and lifetimes fields are repeated in the
724 // predicates field (see rustc_typeck::collect::ty_generics), so remove
726 let stripped_typarams = gens.params.iter()
727 .filter_map(|param| match param.kind {
728 ty::GenericParamDefKind::Lifetime => None,
729 ty::GenericParamDefKind::Type { synthetic, .. } => {
730 if param.name == kw::SelfUpper {
731 assert_eq!(param.index, 0);
734 if synthetic == Some(hir::SyntheticTyParamKind::ImplTrait) {
735 impl_trait.insert(param.index.into(), vec![]);
738 Some(param.clean(cx))
740 ty::GenericParamDefKind::Const { .. } => None,
741 }).collect::<Vec<GenericParamDef>>();
743 // param index -> [(DefId of trait, associated type name, type)]
744 let mut impl_trait_proj =
745 FxHashMap::<u32, Vec<(DefId, String, Ty<'tcx>)>>::default();
747 let where_predicates = preds.predicates.iter()
749 let mut projection = None;
750 let param_idx = (|| {
751 if let Some(trait_ref) = p.to_opt_poly_trait_ref() {
752 if let ty::Param(param) = trait_ref.self_ty().kind {
753 return Some(param.index);
755 } else if let Some(outlives) = p.to_opt_type_outlives() {
756 if let ty::Param(param) = outlives.skip_binder().0.kind {
757 return Some(param.index);
759 } else if let ty::Predicate::Projection(p) = p {
760 if let ty::Param(param) = p.skip_binder().projection_ty.self_ty().kind {
761 projection = Some(p);
762 return Some(param.index);
769 if let Some(param_idx) = param_idx {
770 if let Some(b) = impl_trait.get_mut(¶m_idx.into()) {
771 let p = p.clean(cx)?;
778 .filter(|b| !b.is_sized_bound(cx))
781 let proj = projection
782 .map(|p| (p.skip_binder().projection_ty.clean(cx), p.skip_binder().ty));
783 if let Some(((_, trait_did, name), rhs)) =
784 proj.as_ref().and_then(|(lhs, rhs)| Some((lhs.projection()?, rhs)))
789 .push((trait_did, name.to_string(), rhs));
798 .collect::<Vec<_>>();
800 for (param, mut bounds) in impl_trait {
801 // Move trait bounds to the front.
802 bounds.sort_by_key(|b| if let GenericBound::TraitBound(..) = b {
808 if let crate::core::ImplTraitParam::ParamIndex(idx) = param {
809 if let Some(proj) = impl_trait_proj.remove(&idx) {
810 for (trait_did, name, rhs) in proj {
811 simplify::merge_bounds(
824 cx.impl_trait_bounds.borrow_mut().insert(param, bounds);
827 // Now that `cx.impl_trait_bounds` is populated, we can process
828 // remaining predicates which could contain `impl Trait`.
829 let mut where_predicates = where_predicates
831 .flat_map(|p| p.clean(cx))
832 .collect::<Vec<_>>();
834 // Type parameters and have a Sized bound by default unless removed with
835 // ?Sized. Scan through the predicates and mark any type parameter with
836 // a Sized bound, removing the bounds as we find them.
838 // Note that associated types also have a sized bound by default, but we
839 // don't actually know the set of associated types right here so that's
840 // handled in cleaning associated types
841 let mut sized_params = FxHashSet::default();
842 where_predicates.retain(|pred| {
844 WP::BoundPredicate { ty: Generic(ref g), ref bounds } => {
845 if bounds.iter().any(|b| b.is_sized_bound(cx)) {
846 sized_params.insert(g.clone());
856 // Run through the type parameters again and insert a ?Sized
857 // unbound for any we didn't find to be Sized.
858 for tp in &stripped_typarams {
859 if !sized_params.contains(&tp.name) {
860 where_predicates.push(WP::BoundPredicate {
861 ty: Type::Generic(tp.name.clone()),
862 bounds: vec![GenericBound::maybe_sized(cx)],
867 // It would be nice to collect all of the bounds on a type and recombine
868 // them if possible, to avoid e.g., `where T: Foo, T: Bar, T: Sized, T: 'a`
869 // and instead see `where T: Foo + Bar + Sized + 'a`
874 .flat_map(|param| match param.kind {
875 ty::GenericParamDefKind::Lifetime => Some(param.clean(cx)),
876 ty::GenericParamDefKind::Type { .. } => None,
877 ty::GenericParamDefKind::Const { .. } => Some(param.clean(cx)),
878 }).chain(simplify::ty_params(stripped_typarams).into_iter())
880 where_predicates: simplify::where_clauses(cx, where_predicates),
885 impl<'a> Clean<Method> for (&'a hir::FnSig, &'a hir::Generics, hir::BodyId,
886 Option<hir::Defaultness>) {
887 fn clean(&self, cx: &DocContext<'_>) -> Method {
888 let (generics, decl) = enter_impl_trait(cx, || {
889 (self.1.clean(cx), (&*self.0.decl, self.2).clean(cx))
891 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
895 header: self.0.header,
903 impl Clean<Item> for doctree::Function<'_> {
904 fn clean(&self, cx: &DocContext<'_>) -> Item {
905 let (generics, decl) = enter_impl_trait(cx, || {
906 (self.generics.clean(cx), (self.decl, self.body).clean(cx))
909 let did = cx.tcx.hir().local_def_id(self.id);
910 let constness = if cx.tcx.is_min_const_fn(did) {
911 hir::Constness::Const
913 hir::Constness::NotConst
915 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
917 name: Some(self.name.clean(cx)),
918 attrs: self.attrs.clean(cx),
919 source: self.whence.clean(cx),
920 visibility: self.vis.clean(cx),
921 stability: cx.stability(self.id).clean(cx),
922 deprecation: cx.deprecation(self.id).clean(cx),
924 inner: FunctionItem(Function {
927 header: hir::FnHeader { constness, ..self.header },
935 impl<'a> Clean<Arguments> for (&'a [hir::Ty], &'a [ast::Ident]) {
936 fn clean(&self, cx: &DocContext<'_>) -> Arguments {
938 values: self.0.iter().enumerate().map(|(i, ty)| {
939 let mut name = self.1.get(i).map(|ident| ident.to_string())
940 .unwrap_or(String::new());
942 name = "_".to_string();
953 impl<'a> Clean<Arguments> for (&'a [hir::Ty], hir::BodyId) {
954 fn clean(&self, cx: &DocContext<'_>) -> Arguments {
955 let body = cx.tcx.hir().body(self.1);
958 values: self.0.iter().enumerate().map(|(i, ty)| {
960 name: name_from_pat(&body.params[i].pat),
968 impl<'a, A: Copy> Clean<FnDecl> for (&'a hir::FnDecl, A)
969 where (&'a [hir::Ty], A): Clean<Arguments>
971 fn clean(&self, cx: &DocContext<'_>) -> FnDecl {
973 inputs: (&self.0.inputs[..], self.1).clean(cx),
974 output: self.0.output.clean(cx),
975 c_variadic: self.0.c_variadic,
976 attrs: Attributes::default(),
981 impl<'tcx> Clean<FnDecl> for (DefId, ty::PolyFnSig<'tcx>) {
982 fn clean(&self, cx: &DocContext<'_>) -> FnDecl {
983 let (did, sig) = *self;
984 let mut names = if cx.tcx.hir().as_local_hir_id(did).is_some() {
987 cx.tcx.fn_arg_names(did).into_iter()
991 output: Return(sig.skip_binder().output().clean(cx)),
992 attrs: Attributes::default(),
993 c_variadic: sig.skip_binder().c_variadic,
995 values: sig.skip_binder().inputs().iter().map(|t| {
998 name: names.next().map_or(String::new(), |name| name.to_string()),
1006 impl Clean<FunctionRetTy> for hir::FunctionRetTy {
1007 fn clean(&self, cx: &DocContext<'_>) -> FunctionRetTy {
1009 hir::Return(ref typ) => Return(typ.clean(cx)),
1010 hir::DefaultReturn(..) => DefaultReturn,
1015 impl Clean<Item> for doctree::Trait<'_> {
1016 fn clean(&self, cx: &DocContext<'_>) -> Item {
1017 let attrs = self.attrs.clean(cx);
1018 let is_spotlight = attrs.has_doc_flag(sym::spotlight);
1020 name: Some(self.name.clean(cx)),
1022 source: self.whence.clean(cx),
1023 def_id: cx.tcx.hir().local_def_id(self.id),
1024 visibility: self.vis.clean(cx),
1025 stability: cx.stability(self.id).clean(cx),
1026 deprecation: cx.deprecation(self.id).clean(cx),
1027 inner: TraitItem(Trait {
1028 auto: self.is_auto.clean(cx),
1029 unsafety: self.unsafety,
1030 items: self.items.iter().map(|ti| ti.clean(cx)).collect(),
1031 generics: self.generics.clean(cx),
1032 bounds: self.bounds.clean(cx),
1034 is_auto: self.is_auto.clean(cx),
1040 impl Clean<Item> for doctree::TraitAlias<'_> {
1041 fn clean(&self, cx: &DocContext<'_>) -> Item {
1042 let attrs = self.attrs.clean(cx);
1044 name: Some(self.name.clean(cx)),
1046 source: self.whence.clean(cx),
1047 def_id: cx.tcx.hir().local_def_id(self.id),
1048 visibility: self.vis.clean(cx),
1049 stability: cx.stability(self.id).clean(cx),
1050 deprecation: cx.deprecation(self.id).clean(cx),
1051 inner: TraitAliasItem(TraitAlias {
1052 generics: self.generics.clean(cx),
1053 bounds: self.bounds.clean(cx),
1059 impl Clean<bool> for hir::IsAuto {
1060 fn clean(&self, _: &DocContext<'_>) -> bool {
1062 hir::IsAuto::Yes => true,
1063 hir::IsAuto::No => false,
1068 impl Clean<Type> for hir::TraitRef {
1069 fn clean(&self, cx: &DocContext<'_>) -> Type {
1070 resolve_type(cx, self.path.clean(cx), self.hir_ref_id)
1074 impl Clean<PolyTrait> for hir::PolyTraitRef {
1075 fn clean(&self, cx: &DocContext<'_>) -> PolyTrait {
1077 trait_: self.trait_ref.clean(cx),
1078 generic_params: self.bound_generic_params.clean(cx)
1083 impl Clean<Item> for hir::TraitItem {
1084 fn clean(&self, cx: &DocContext<'_>) -> Item {
1085 let inner = match self.kind {
1086 hir::TraitItemKind::Const(ref ty, default) => {
1087 AssocConstItem(ty.clean(cx),
1088 default.map(|e| print_const_expr(cx, e)))
1090 hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Provided(body)) => {
1091 MethodItem((sig, &self.generics, body, None).clean(cx))
1093 hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Required(ref names)) => {
1094 let (generics, decl) = enter_impl_trait(cx, || {
1095 (self.generics.clean(cx), (&*sig.decl, &names[..]).clean(cx))
1097 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
1098 TyMethodItem(TyMethod {
1106 hir::TraitItemKind::Type(ref bounds, ref default) => {
1107 AssocTypeItem(bounds.clean(cx), default.clean(cx))
1110 let local_did = cx.tcx.hir().local_def_id(self.hir_id);
1112 name: Some(self.ident.name.clean(cx)),
1113 attrs: self.attrs.clean(cx),
1114 source: self.span.clean(cx),
1116 visibility: Visibility::Inherited,
1117 stability: get_stability(cx, local_did),
1118 deprecation: get_deprecation(cx, local_did),
1124 impl Clean<Item> for hir::ImplItem {
1125 fn clean(&self, cx: &DocContext<'_>) -> Item {
1126 let inner = match self.kind {
1127 hir::ImplItemKind::Const(ref ty, expr) => {
1128 AssocConstItem(ty.clean(cx),
1129 Some(print_const_expr(cx, expr)))
1131 hir::ImplItemKind::Method(ref sig, body) => {
1132 MethodItem((sig, &self.generics, body, Some(self.defaultness)).clean(cx))
1134 hir::ImplItemKind::TyAlias(ref ty) => TypedefItem(Typedef {
1135 type_: ty.clean(cx),
1136 generics: Generics::default(),
1138 hir::ImplItemKind::OpaqueTy(ref bounds) => OpaqueTyItem(OpaqueTy {
1139 bounds: bounds.clean(cx),
1140 generics: Generics::default(),
1143 let local_did = cx.tcx.hir().local_def_id(self.hir_id);
1145 name: Some(self.ident.name.clean(cx)),
1146 source: self.span.clean(cx),
1147 attrs: self.attrs.clean(cx),
1149 visibility: self.vis.clean(cx),
1150 stability: get_stability(cx, local_did),
1151 deprecation: get_deprecation(cx, local_did),
1157 impl Clean<Item> for ty::AssocItem {
1158 fn clean(&self, cx: &DocContext<'_>) -> Item {
1159 let inner = match self.kind {
1160 ty::AssocKind::Const => {
1161 let ty = cx.tcx.type_of(self.def_id);
1162 let default = if self.defaultness.has_value() {
1163 Some(inline::print_inlined_const(cx, self.def_id))
1167 AssocConstItem(ty.clean(cx), default)
1169 ty::AssocKind::Method => {
1170 let generics = (cx.tcx.generics_of(self.def_id),
1171 cx.tcx.explicit_predicates_of(self.def_id)).clean(cx);
1172 let sig = cx.tcx.fn_sig(self.def_id);
1173 let mut decl = (self.def_id, sig).clean(cx);
1175 if self.method_has_self_argument {
1176 let self_ty = match self.container {
1177 ty::ImplContainer(def_id) => {
1178 cx.tcx.type_of(def_id)
1180 ty::TraitContainer(_) => cx.tcx.types.self_param,
1182 let self_arg_ty = *sig.input(0).skip_binder();
1183 if self_arg_ty == self_ty {
1184 decl.inputs.values[0].type_ = Generic(String::from("Self"));
1185 } else if let ty::Ref(_, ty, _) = self_arg_ty.kind {
1187 match decl.inputs.values[0].type_ {
1188 BorrowedRef{ref mut type_, ..} => {
1189 **type_ = Generic(String::from("Self"))
1191 _ => unreachable!(),
1197 let provided = match self.container {
1198 ty::ImplContainer(_) => true,
1199 ty::TraitContainer(_) => self.defaultness.has_value()
1201 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
1203 let constness = if cx.tcx.is_min_const_fn(self.def_id) {
1204 hir::Constness::Const
1206 hir::Constness::NotConst
1208 let asyncness = cx.tcx.asyncness(self.def_id);
1209 let defaultness = match self.container {
1210 ty::ImplContainer(_) => Some(self.defaultness),
1211 ty::TraitContainer(_) => None,
1216 header: hir::FnHeader {
1217 unsafety: sig.unsafety(),
1227 TyMethodItem(TyMethod {
1230 header: hir::FnHeader {
1231 unsafety: sig.unsafety(),
1233 constness: hir::Constness::NotConst,
1234 asyncness: hir::IsAsync::NotAsync,
1241 ty::AssocKind::Type => {
1242 let my_name = self.ident.name.clean(cx);
1244 if let ty::TraitContainer(did) = self.container {
1245 // When loading a cross-crate associated type, the bounds for this type
1246 // are actually located on the trait/impl itself, so we need to load
1247 // all of the generics from there and then look for bounds that are
1248 // applied to this associated type in question.
1249 let predicates = cx.tcx.explicit_predicates_of(did);
1250 let generics = (cx.tcx.generics_of(did), predicates).clean(cx);
1251 let mut bounds = generics.where_predicates.iter().filter_map(|pred| {
1252 let (name, self_type, trait_, bounds) = match *pred {
1253 WherePredicate::BoundPredicate {
1254 ty: QPath { ref name, ref self_type, ref trait_ },
1256 } => (name, self_type, trait_, bounds),
1259 if *name != my_name { return None }
1261 ResolvedPath { did, .. } if did == self.container.id() => {}
1265 Generic(ref s) if *s == "Self" => {}
1269 }).flat_map(|i| i.iter().cloned()).collect::<Vec<_>>();
1270 // Our Sized/?Sized bound didn't get handled when creating the generics
1271 // because we didn't actually get our whole set of bounds until just now
1272 // (some of them may have come from the trait). If we do have a sized
1273 // bound, we remove it, and if we don't then we add the `?Sized` bound
1275 match bounds.iter().position(|b| b.is_sized_bound(cx)) {
1276 Some(i) => { bounds.remove(i); }
1277 None => bounds.push(GenericBound::maybe_sized(cx)),
1280 let ty = if self.defaultness.has_value() {
1281 Some(cx.tcx.type_of(self.def_id))
1286 AssocTypeItem(bounds, ty.clean(cx))
1288 TypedefItem(Typedef {
1289 type_: cx.tcx.type_of(self.def_id).clean(cx),
1290 generics: Generics {
1292 where_predicates: Vec::new(),
1297 ty::AssocKind::OpaqueTy => unimplemented!(),
1300 let visibility = match self.container {
1301 ty::ImplContainer(_) => self.vis.clean(cx),
1302 ty::TraitContainer(_) => Inherited,
1306 name: Some(self.ident.name.clean(cx)),
1308 stability: get_stability(cx, self.def_id),
1309 deprecation: get_deprecation(cx, self.def_id),
1310 def_id: self.def_id,
1311 attrs: inline::load_attrs(cx, self.def_id).clean(cx),
1312 source: cx.tcx.def_span(self.def_id).clean(cx),
1318 impl Clean<Type> for hir::Ty {
1319 fn clean(&self, cx: &DocContext<'_>) -> Type {
1323 TyKind::Never => Never,
1324 TyKind::Ptr(ref m) => RawPointer(m.mutbl.clean(cx), box m.ty.clean(cx)),
1325 TyKind::Rptr(ref l, ref m) => {
1326 let lifetime = if l.is_elided() {
1331 BorrowedRef {lifetime, mutability: m.mutbl.clean(cx),
1332 type_: box m.ty.clean(cx)}
1334 TyKind::Slice(ref ty) => Slice(box ty.clean(cx)),
1335 TyKind::Array(ref ty, ref length) => {
1336 let def_id = cx.tcx.hir().local_def_id(length.hir_id);
1337 let param_env = cx.tcx.param_env(def_id);
1338 let substs = InternalSubsts::identity_for_item(cx.tcx, def_id);
1339 let cid = GlobalId {
1340 instance: ty::Instance::new(def_id, substs),
1343 let length = match cx.tcx.const_eval(param_env.and(cid)) {
1344 Ok(length) => print_const(cx, length),
1347 .span_to_snippet(cx.tcx.def_span(def_id))
1348 .unwrap_or_else(|_| "_".to_string()),
1350 Array(box ty.clean(cx), length)
1352 TyKind::Tup(ref tys) => Tuple(tys.clean(cx)),
1353 TyKind::Def(item_id, _) => {
1354 let item = cx.tcx.hir().expect_item(item_id.id);
1355 if let hir::ItemKind::OpaqueTy(ref ty) = item.kind {
1356 ImplTrait(ty.bounds.clean(cx))
1361 TyKind::Path(hir::QPath::Resolved(None, ref path)) => {
1362 if let Res::Def(DefKind::TyParam, did) = path.res {
1363 if let Some(new_ty) = cx.ty_substs.borrow().get(&did).cloned() {
1366 if let Some(bounds) = cx.impl_trait_bounds.borrow_mut().remove(&did.into()) {
1367 return ImplTrait(bounds);
1371 let mut alias = None;
1372 if let Res::Def(DefKind::TyAlias, def_id) = path.res {
1373 // Substitute private type aliases
1374 if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(def_id) {
1375 if !cx.renderinfo.borrow().access_levels.is_exported(def_id) {
1376 alias = Some(&cx.tcx.hir().expect_item(hir_id).kind);
1381 if let Some(&hir::ItemKind::TyAlias(ref ty, ref generics)) = alias {
1382 let provided_params = &path.segments.last().expect("segments were empty");
1383 let mut ty_substs = FxHashMap::default();
1384 let mut lt_substs = FxHashMap::default();
1385 let mut ct_substs = FxHashMap::default();
1386 let generic_args = provided_params.generic_args();
1388 let mut indices: GenericParamCount = Default::default();
1389 for param in generics.params.iter() {
1391 hir::GenericParamKind::Lifetime { .. } => {
1393 let lifetime = generic_args.args.iter().find_map(|arg| {
1395 hir::GenericArg::Lifetime(lt) => {
1396 if indices.lifetimes == j {
1405 if let Some(lt) = lifetime.cloned() {
1406 if !lt.is_elided() {
1408 cx.tcx.hir().local_def_id(param.hir_id);
1409 lt_substs.insert(lt_def_id, lt.clean(cx));
1412 indices.lifetimes += 1;
1414 hir::GenericParamKind::Type { ref default, .. } => {
1415 let ty_param_def_id =
1416 cx.tcx.hir().local_def_id(param.hir_id);
1418 let type_ = generic_args.args.iter().find_map(|arg| {
1420 hir::GenericArg::Type(ty) => {
1421 if indices.types == j {
1430 if let Some(ty) = type_ {
1431 ty_substs.insert(ty_param_def_id, ty.clean(cx));
1432 } else if let Some(default) = default.clone() {
1433 ty_substs.insert(ty_param_def_id,
1438 hir::GenericParamKind::Const { .. } => {
1439 let const_param_def_id =
1440 cx.tcx.hir().local_def_id(param.hir_id);
1442 let const_ = generic_args.args.iter().find_map(|arg| {
1444 hir::GenericArg::Const(ct) => {
1445 if indices.consts == j {
1454 if let Some(ct) = const_ {
1455 ct_substs.insert(const_param_def_id, ct.clean(cx));
1457 // FIXME(const_generics:defaults)
1458 indices.consts += 1;
1463 return cx.enter_alias(ty_substs, lt_substs, ct_substs, || ty.clean(cx));
1465 resolve_type(cx, path.clean(cx), self.hir_id)
1467 TyKind::Path(hir::QPath::Resolved(Some(ref qself), ref p)) => {
1468 let segments = if p.is_global() { &p.segments[1..] } else { &p.segments };
1469 let trait_segments = &segments[..segments.len() - 1];
1470 let trait_path = self::Path {
1471 global: p.is_global(),
1474 cx.tcx.associated_item(p.res.def_id()).container.id(),
1476 segments: trait_segments.clean(cx),
1479 name: p.segments.last().expect("segments were empty").ident.name.clean(cx),
1480 self_type: box qself.clean(cx),
1481 trait_: box resolve_type(cx, trait_path, self.hir_id)
1484 TyKind::Path(hir::QPath::TypeRelative(ref qself, ref segment)) => {
1485 let mut res = Res::Err;
1486 let ty = hir_ty_to_ty(cx.tcx, self);
1487 if let ty::Projection(proj) = ty.kind {
1488 res = Res::Def(DefKind::Trait, proj.trait_ref(cx.tcx).def_id);
1490 let trait_path = hir::Path {
1493 segments: vec![].into(),
1496 name: segment.ident.name.clean(cx),
1497 self_type: box qself.clean(cx),
1498 trait_: box resolve_type(cx, trait_path.clean(cx), self.hir_id)
1501 TyKind::TraitObject(ref bounds, ref lifetime) => {
1502 match bounds[0].clean(cx).trait_ {
1503 ResolvedPath { path, param_names: None, did, is_generic } => {
1504 let mut bounds: Vec<self::GenericBound> = bounds[1..].iter().map(|bound| {
1505 self::GenericBound::TraitBound(bound.clean(cx),
1506 hir::TraitBoundModifier::None)
1508 if !lifetime.is_elided() {
1509 bounds.push(self::GenericBound::Outlives(lifetime.clean(cx)));
1511 ResolvedPath { path, param_names: Some(bounds), did, is_generic, }
1513 _ => Infer, // shouldn't happen
1516 TyKind::BareFn(ref barefn) => BareFunction(box barefn.clean(cx)),
1517 TyKind::Infer | TyKind::Err => Infer,
1518 TyKind::Typeof(..) => panic!("unimplemented type {:?}", self.kind),
1523 impl<'tcx> Clean<Type> for Ty<'tcx> {
1524 fn clean(&self, cx: &DocContext<'_>) -> Type {
1525 debug!("cleaning type: {:?}", self);
1528 ty::Bool => Primitive(PrimitiveType::Bool),
1529 ty::Char => Primitive(PrimitiveType::Char),
1530 ty::Int(int_ty) => Primitive(int_ty.into()),
1531 ty::Uint(uint_ty) => Primitive(uint_ty.into()),
1532 ty::Float(float_ty) => Primitive(float_ty.into()),
1533 ty::Str => Primitive(PrimitiveType::Str),
1534 ty::Slice(ty) => Slice(box ty.clean(cx)),
1535 ty::Array(ty, n) => {
1536 let mut n = cx.tcx.lift(&n).expect("array lift failed");
1537 if let ty::ConstKind::Unevaluated(def_id, substs) = n.val {
1538 let param_env = cx.tcx.param_env(def_id);
1539 let cid = GlobalId {
1540 instance: ty::Instance::new(def_id, substs),
1543 if let Ok(new_n) = cx.tcx.const_eval(param_env.and(cid)) {
1547 let n = print_const(cx, n);
1548 Array(box ty.clean(cx), n)
1550 ty::RawPtr(mt) => RawPointer(mt.mutbl.clean(cx), box mt.ty.clean(cx)),
1551 ty::Ref(r, ty, mutbl) => BorrowedRef {
1552 lifetime: r.clean(cx),
1553 mutability: mutbl.clean(cx),
1554 type_: box ty.clean(cx),
1558 let ty = cx.tcx.lift(self).expect("FnPtr lift failed");
1559 let sig = ty.fn_sig(cx.tcx);
1560 let local_def_id = cx.tcx.hir().local_def_id_from_node_id(ast::CRATE_NODE_ID);
1561 BareFunction(box BareFunctionDecl {
1562 unsafety: sig.unsafety(),
1563 generic_params: Vec::new(),
1564 decl: (local_def_id, sig).clean(cx),
1568 ty::Adt(def, substs) => {
1570 let kind = match def.adt_kind() {
1571 AdtKind::Struct => TypeKind::Struct,
1572 AdtKind::Union => TypeKind::Union,
1573 AdtKind::Enum => TypeKind::Enum,
1575 inline::record_extern_fqn(cx, did, kind);
1576 let path = external_path(cx, cx.tcx.item_name(did), None, false, vec![], substs);
1584 ty::Foreign(did) => {
1585 inline::record_extern_fqn(cx, did, TypeKind::Foreign);
1586 let path = external_path(cx, cx.tcx.item_name(did),
1587 None, false, vec![], InternalSubsts::empty());
1595 ty::Dynamic(ref obj, ref reg) => {
1596 // HACK: pick the first `did` as the `did` of the trait object. Someone
1597 // might want to implement "native" support for marker-trait-only
1599 let mut dids = obj.principal_def_id().into_iter().chain(obj.auto_traits());
1600 let did = dids.next().unwrap_or_else(|| {
1601 panic!("found trait object `{:?}` with no traits?", self)
1603 let substs = match obj.principal() {
1604 Some(principal) => principal.skip_binder().substs,
1605 // marker traits have no substs.
1606 _ => cx.tcx.intern_substs(&[])
1609 inline::record_extern_fqn(cx, did, TypeKind::Trait);
1611 let mut param_names = vec![];
1612 reg.clean(cx).map(|b| param_names.push(GenericBound::Outlives(b)));
1614 let empty = cx.tcx.intern_substs(&[]);
1615 let path = external_path(cx, cx.tcx.item_name(did),
1616 Some(did), false, vec![], empty);
1617 inline::record_extern_fqn(cx, did, TypeKind::Trait);
1618 let bound = GenericBound::TraitBound(PolyTrait {
1619 trait_: ResolvedPath {
1625 generic_params: Vec::new(),
1626 }, hir::TraitBoundModifier::None);
1627 param_names.push(bound);
1630 let mut bindings = vec![];
1631 for pb in obj.projection_bounds() {
1632 bindings.push(TypeBinding {
1633 name: cx.tcx.associated_item(pb.item_def_id()).ident.name.clean(cx),
1634 kind: TypeBindingKind::Equality {
1635 ty: pb.skip_binder().ty.clean(cx)
1640 let path = external_path(cx, cx.tcx.item_name(did), Some(did),
1641 false, bindings, substs);
1644 param_names: Some(param_names),
1649 ty::Tuple(ref t) => {
1650 Tuple(t.iter().map(|t| t.expect_ty()).collect::<Vec<_>>().clean(cx))
1653 ty::Projection(ref data) => data.clean(cx),
1655 ty::Param(ref p) => {
1656 if let Some(bounds) = cx.impl_trait_bounds.borrow_mut().remove(&p.index.into()) {
1659 Generic(p.name.to_string())
1663 ty::Opaque(def_id, substs) => {
1664 // Grab the "TraitA + TraitB" from `impl TraitA + TraitB`,
1665 // by looking up the projections associated with the def_id.
1666 let predicates_of = cx.tcx.explicit_predicates_of(def_id);
1667 let substs = cx.tcx.lift(&substs).expect("Opaque lift failed");
1668 let bounds = predicates_of.instantiate(cx.tcx, substs);
1669 let mut regions = vec![];
1670 let mut has_sized = false;
1671 let mut bounds = bounds.predicates.iter().filter_map(|predicate| {
1672 let trait_ref = if let Some(tr) = predicate.to_opt_poly_trait_ref() {
1674 } else if let ty::Predicate::TypeOutlives(pred) = *predicate {
1675 // these should turn up at the end
1676 pred.skip_binder().1.clean(cx).map(|r| {
1677 regions.push(GenericBound::Outlives(r))
1684 if let Some(sized) = cx.tcx.lang_items().sized_trait() {
1685 if trait_ref.def_id() == sized {
1691 let bounds = bounds.predicates.iter().filter_map(|pred|
1692 if let ty::Predicate::Projection(proj) = *pred {
1693 let proj = proj.skip_binder();
1694 if proj.projection_ty.trait_ref(cx.tcx) == *trait_ref.skip_binder() {
1696 name: cx.tcx.associated_item(proj.projection_ty.item_def_id)
1697 .ident.name.clean(cx),
1698 kind: TypeBindingKind::Equality {
1699 ty: proj.ty.clean(cx),
1710 Some((trait_ref.skip_binder(), bounds).clean(cx))
1711 }).collect::<Vec<_>>();
1712 bounds.extend(regions);
1713 if !has_sized && !bounds.is_empty() {
1714 bounds.insert(0, GenericBound::maybe_sized(cx));
1719 ty::Closure(..) | ty::Generator(..) => Tuple(vec![]), // FIXME(pcwalton)
1721 ty::Bound(..) => panic!("Bound"),
1722 ty::Placeholder(..) => panic!("Placeholder"),
1723 ty::UnnormalizedProjection(..) => panic!("UnnormalizedProjection"),
1724 ty::GeneratorWitness(..) => panic!("GeneratorWitness"),
1725 ty::Infer(..) => panic!("Infer"),
1726 ty::Error => panic!("Error"),
1731 impl<'tcx> Clean<Constant> for ty::Const<'tcx> {
1732 fn clean(&self, cx: &DocContext<'_>) -> Constant {
1734 type_: self.ty.clean(cx),
1735 expr: format!("{}", self),
1740 impl Clean<Item> for hir::StructField {
1741 fn clean(&self, cx: &DocContext<'_>) -> Item {
1742 let local_did = cx.tcx.hir().local_def_id(self.hir_id);
1745 name: Some(self.ident.name).clean(cx),
1746 attrs: self.attrs.clean(cx),
1747 source: self.span.clean(cx),
1748 visibility: self.vis.clean(cx),
1749 stability: get_stability(cx, local_did),
1750 deprecation: get_deprecation(cx, local_did),
1752 inner: StructFieldItem(self.ty.clean(cx)),
1757 impl Clean<Item> for ty::FieldDef {
1758 fn clean(&self, cx: &DocContext<'_>) -> Item {
1760 name: Some(self.ident.name).clean(cx),
1761 attrs: cx.tcx.get_attrs(self.did).clean(cx),
1762 source: cx.tcx.def_span(self.did).clean(cx),
1763 visibility: self.vis.clean(cx),
1764 stability: get_stability(cx, self.did),
1765 deprecation: get_deprecation(cx, self.did),
1767 inner: StructFieldItem(cx.tcx.type_of(self.did).clean(cx)),
1772 impl Clean<Visibility> for hir::Visibility {
1773 fn clean(&self, cx: &DocContext<'_>) -> Visibility {
1775 hir::VisibilityKind::Public => Visibility::Public,
1776 hir::VisibilityKind::Inherited => Visibility::Inherited,
1777 hir::VisibilityKind::Crate(_) => Visibility::Crate,
1778 hir::VisibilityKind::Restricted { ref path, .. } => {
1779 let path = path.clean(cx);
1780 let did = register_res(cx, path.res);
1781 Visibility::Restricted(did, path)
1787 impl Clean<Visibility> for ty::Visibility {
1788 fn clean(&self, _: &DocContext<'_>) -> Visibility {
1789 if *self == ty::Visibility::Public { Public } else { Inherited }
1793 impl Clean<Item> for doctree::Struct<'_> {
1794 fn clean(&self, cx: &DocContext<'_>) -> Item {
1796 name: Some(self.name.clean(cx)),
1797 attrs: self.attrs.clean(cx),
1798 source: self.whence.clean(cx),
1799 def_id: cx.tcx.hir().local_def_id(self.id),
1800 visibility: self.vis.clean(cx),
1801 stability: cx.stability(self.id).clean(cx),
1802 deprecation: cx.deprecation(self.id).clean(cx),
1803 inner: StructItem(Struct {
1804 struct_type: self.struct_type,
1805 generics: self.generics.clean(cx),
1806 fields: self.fields.clean(cx),
1807 fields_stripped: false,
1813 impl Clean<Item> for doctree::Union<'_> {
1814 fn clean(&self, cx: &DocContext<'_>) -> Item {
1816 name: Some(self.name.clean(cx)),
1817 attrs: self.attrs.clean(cx),
1818 source: self.whence.clean(cx),
1819 def_id: cx.tcx.hir().local_def_id(self.id),
1820 visibility: self.vis.clean(cx),
1821 stability: cx.stability(self.id).clean(cx),
1822 deprecation: cx.deprecation(self.id).clean(cx),
1823 inner: UnionItem(Union {
1824 struct_type: self.struct_type,
1825 generics: self.generics.clean(cx),
1826 fields: self.fields.clean(cx),
1827 fields_stripped: false,
1833 impl Clean<VariantStruct> for ::rustc::hir::VariantData {
1834 fn clean(&self, cx: &DocContext<'_>) -> VariantStruct {
1836 struct_type: doctree::struct_type_from_def(self),
1837 fields: self.fields().iter().map(|x| x.clean(cx)).collect(),
1838 fields_stripped: false,
1843 impl Clean<Item> for doctree::Enum<'_> {
1844 fn clean(&self, cx: &DocContext<'_>) -> Item {
1846 name: Some(self.name.clean(cx)),
1847 attrs: self.attrs.clean(cx),
1848 source: self.whence.clean(cx),
1849 def_id: cx.tcx.hir().local_def_id(self.id),
1850 visibility: self.vis.clean(cx),
1851 stability: cx.stability(self.id).clean(cx),
1852 deprecation: cx.deprecation(self.id).clean(cx),
1853 inner: EnumItem(Enum {
1854 variants: self.variants.iter().map(|v| v.clean(cx)).collect(),
1855 generics: self.generics.clean(cx),
1856 variants_stripped: false,
1862 impl Clean<Item> for doctree::Variant<'_> {
1863 fn clean(&self, cx: &DocContext<'_>) -> Item {
1865 name: Some(self.name.clean(cx)),
1866 attrs: self.attrs.clean(cx),
1867 source: self.whence.clean(cx),
1868 visibility: Inherited,
1869 stability: cx.stability(self.id).clean(cx),
1870 deprecation: cx.deprecation(self.id).clean(cx),
1871 def_id: cx.tcx.hir().local_def_id(self.id),
1872 inner: VariantItem(Variant {
1873 kind: self.def.clean(cx),
1879 impl Clean<Item> for ty::VariantDef {
1880 fn clean(&self, cx: &DocContext<'_>) -> Item {
1881 let kind = match self.ctor_kind {
1882 CtorKind::Const => VariantKind::CLike,
1885 self.fields.iter().map(|f| cx.tcx.type_of(f.did).clean(cx)).collect()
1888 CtorKind::Fictive => {
1889 VariantKind::Struct(VariantStruct {
1890 struct_type: doctree::Plain,
1891 fields_stripped: false,
1892 fields: self.fields.iter().map(|field| {
1894 source: cx.tcx.def_span(field.did).clean(cx),
1895 name: Some(field.ident.name.clean(cx)),
1896 attrs: cx.tcx.get_attrs(field.did).clean(cx),
1897 visibility: field.vis.clean(cx),
1899 stability: get_stability(cx, field.did),
1900 deprecation: get_deprecation(cx, field.did),
1901 inner: StructFieldItem(cx.tcx.type_of(field.did).clean(cx))
1908 name: Some(self.ident.clean(cx)),
1909 attrs: inline::load_attrs(cx, self.def_id).clean(cx),
1910 source: cx.tcx.def_span(self.def_id).clean(cx),
1911 visibility: Inherited,
1912 def_id: self.def_id,
1913 inner: VariantItem(Variant { kind }),
1914 stability: get_stability(cx, self.def_id),
1915 deprecation: get_deprecation(cx, self.def_id),
1920 impl Clean<VariantKind> for hir::VariantData {
1921 fn clean(&self, cx: &DocContext<'_>) -> VariantKind {
1923 hir::VariantData::Struct(..) => VariantKind::Struct(self.clean(cx)),
1924 hir::VariantData::Tuple(..) =>
1925 VariantKind::Tuple(self.fields().iter().map(|x| x.ty.clean(cx)).collect()),
1926 hir::VariantData::Unit(..) => VariantKind::CLike,
1931 impl Clean<Span> for syntax_pos::Span {
1932 fn clean(&self, cx: &DocContext<'_>) -> Span {
1933 if self.is_dummy() {
1934 return Span::empty();
1937 let cm = cx.sess().source_map();
1938 let filename = cm.span_to_filename(*self);
1939 let lo = cm.lookup_char_pos(self.lo());
1940 let hi = cm.lookup_char_pos(self.hi());
1944 locol: lo.col.to_usize(),
1946 hicol: hi.col.to_usize(),
1952 impl Clean<Path> for hir::Path {
1953 fn clean(&self, cx: &DocContext<'_>) -> Path {
1955 global: self.is_global(),
1957 segments: if self.is_global() { &self.segments[1..] } else { &self.segments }.clean(cx),
1962 impl Clean<GenericArgs> for hir::GenericArgs {
1963 fn clean(&self, cx: &DocContext<'_>) -> GenericArgs {
1964 if self.parenthesized {
1965 let output = self.bindings[0].ty().clean(cx);
1966 GenericArgs::Parenthesized {
1967 inputs: self.inputs().clean(cx),
1968 output: if output != Type::Tuple(Vec::new()) { Some(output) } else { None }
1971 let elide_lifetimes = self.args.iter().all(|arg| match arg {
1972 hir::GenericArg::Lifetime(lt) => lt.is_elided(),
1975 GenericArgs::AngleBracketed {
1976 args: self.args.iter().filter_map(|arg| match arg {
1977 hir::GenericArg::Lifetime(lt) if !elide_lifetimes => {
1978 Some(GenericArg::Lifetime(lt.clean(cx)))
1980 hir::GenericArg::Lifetime(_) => None,
1981 hir::GenericArg::Type(ty) => Some(GenericArg::Type(ty.clean(cx))),
1982 hir::GenericArg::Const(ct) => Some(GenericArg::Const(ct.clean(cx))),
1984 bindings: self.bindings.clean(cx),
1990 impl Clean<PathSegment> for hir::PathSegment {
1991 fn clean(&self, cx: &DocContext<'_>) -> PathSegment {
1993 name: self.ident.name.clean(cx),
1994 args: self.generic_args().clean(cx),
1999 impl Clean<String> for Ident {
2001 fn clean(&self, cx: &DocContext<'_>) -> String {
2006 impl Clean<String> for ast::Name {
2008 fn clean(&self, _: &DocContext<'_>) -> String {
2013 impl Clean<Item> for doctree::Typedef<'_> {
2014 fn clean(&self, cx: &DocContext<'_>) -> Item {
2016 name: Some(self.name.clean(cx)),
2017 attrs: self.attrs.clean(cx),
2018 source: self.whence.clean(cx),
2019 def_id: cx.tcx.hir().local_def_id(self.id),
2020 visibility: self.vis.clean(cx),
2021 stability: cx.stability(self.id).clean(cx),
2022 deprecation: cx.deprecation(self.id).clean(cx),
2023 inner: TypedefItem(Typedef {
2024 type_: self.ty.clean(cx),
2025 generics: self.gen.clean(cx),
2031 impl Clean<Item> for doctree::OpaqueTy<'_> {
2032 fn clean(&self, cx: &DocContext<'_>) -> Item {
2034 name: Some(self.name.clean(cx)),
2035 attrs: self.attrs.clean(cx),
2036 source: self.whence.clean(cx),
2037 def_id: cx.tcx.hir().local_def_id(self.id),
2038 visibility: self.vis.clean(cx),
2039 stability: cx.stability(self.id).clean(cx),
2040 deprecation: cx.deprecation(self.id).clean(cx),
2041 inner: OpaqueTyItem(OpaqueTy {
2042 bounds: self.opaque_ty.bounds.clean(cx),
2043 generics: self.opaque_ty.generics.clean(cx),
2049 impl Clean<BareFunctionDecl> for hir::BareFnTy {
2050 fn clean(&self, cx: &DocContext<'_>) -> BareFunctionDecl {
2051 let (generic_params, decl) = enter_impl_trait(cx, || {
2052 (self.generic_params.clean(cx), (&*self.decl, &self.param_names[..]).clean(cx))
2055 unsafety: self.unsafety,
2063 impl Clean<Item> for doctree::Static<'_> {
2064 fn clean(&self, cx: &DocContext<'_>) -> Item {
2065 debug!("cleaning static {}: {:?}", self.name.clean(cx), self);
2067 name: Some(self.name.clean(cx)),
2068 attrs: self.attrs.clean(cx),
2069 source: self.whence.clean(cx),
2070 def_id: cx.tcx.hir().local_def_id(self.id),
2071 visibility: self.vis.clean(cx),
2072 stability: cx.stability(self.id).clean(cx),
2073 deprecation: cx.deprecation(self.id).clean(cx),
2074 inner: StaticItem(Static {
2075 type_: self.type_.clean(cx),
2076 mutability: self.mutability.clean(cx),
2077 expr: print_const_expr(cx, self.expr),
2083 impl Clean<Item> for doctree::Constant<'_> {
2084 fn clean(&self, cx: &DocContext<'_>) -> Item {
2086 name: Some(self.name.clean(cx)),
2087 attrs: self.attrs.clean(cx),
2088 source: self.whence.clean(cx),
2089 def_id: cx.tcx.hir().local_def_id(self.id),
2090 visibility: self.vis.clean(cx),
2091 stability: cx.stability(self.id).clean(cx),
2092 deprecation: cx.deprecation(self.id).clean(cx),
2093 inner: ConstantItem(Constant {
2094 type_: self.type_.clean(cx),
2095 expr: print_const_expr(cx, self.expr),
2101 impl Clean<Mutability> for hir::Mutability {
2102 fn clean(&self, _: &DocContext<'_>) -> Mutability {
2104 &hir::Mutability::Mut => Mutable,
2105 &hir::Mutability::Not => Immutable,
2110 impl Clean<ImplPolarity> for ty::ImplPolarity {
2111 fn clean(&self, _: &DocContext<'_>) -> ImplPolarity {
2113 &ty::ImplPolarity::Positive |
2114 // FIXME: do we want to do something else here?
2115 &ty::ImplPolarity::Reservation => ImplPolarity::Positive,
2116 &ty::ImplPolarity::Negative => ImplPolarity::Negative,
2121 impl Clean<Vec<Item>> for doctree::Impl<'_> {
2122 fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
2123 let mut ret = Vec::new();
2124 let trait_ = self.trait_.clean(cx);
2125 let items = self.items.iter().map(|ii| ii.clean(cx)).collect::<Vec<_>>();
2126 let def_id = cx.tcx.hir().local_def_id(self.id);
2128 // If this impl block is an implementation of the Deref trait, then we
2129 // need to try inlining the target's inherent impl blocks as well.
2130 if trait_.def_id() == cx.tcx.lang_items().deref_trait() {
2131 build_deref_target_impls(cx, &items, &mut ret);
2134 let provided = trait_.def_id().map(|did| {
2135 cx.tcx.provided_trait_methods(did)
2137 .map(|meth| meth.ident.to_string())
2139 }).unwrap_or_default();
2143 attrs: self.attrs.clean(cx),
2144 source: self.whence.clean(cx),
2146 visibility: self.vis.clean(cx),
2147 stability: cx.stability(self.id).clean(cx),
2148 deprecation: cx.deprecation(self.id).clean(cx),
2149 inner: ImplItem(Impl {
2150 unsafety: self.unsafety,
2151 generics: self.generics.clean(cx),
2152 provided_trait_methods: provided,
2154 for_: self.for_.clean(cx),
2156 polarity: Some(cx.tcx.impl_polarity(def_id).clean(cx)),
2165 impl Clean<Vec<Item>> for doctree::ExternCrate<'_> {
2166 fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
2168 let please_inline = self.vis.node.is_pub() && self.attrs.iter().any(|a| {
2169 a.check_name(sym::doc) && match a.meta_item_list() {
2170 Some(l) => attr::list_contains_name(&l, sym::inline),
2176 let mut visited = FxHashSet::default();
2182 index: CRATE_DEF_INDEX,
2186 if let Some(items) = inline::try_inline(
2188 Some(rustc::ty::Attributes::Borrowed(self.attrs)),
2197 attrs: self.attrs.clean(cx),
2198 source: self.whence.clean(cx),
2199 def_id: DefId { krate: self.cnum, index: CRATE_DEF_INDEX },
2200 visibility: self.vis.clean(cx),
2203 inner: ExternCrateItem(self.name.clean(cx), self.path.clone())
2208 impl Clean<Vec<Item>> for doctree::Import<'_> {
2209 fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
2210 // We consider inlining the documentation of `pub use` statements, but we
2211 // forcefully don't inline if this is not public or if the
2212 // #[doc(no_inline)] attribute is present.
2213 // Don't inline doc(hidden) imports so they can be stripped at a later stage.
2214 let mut denied = !self.vis.node.is_pub() || self.attrs.iter().any(|a| {
2215 a.check_name(sym::doc) && match a.meta_item_list() {
2216 Some(l) => attr::list_contains_name(&l, sym::no_inline) ||
2217 attr::list_contains_name(&l, sym::hidden),
2221 // Also check whether imports were asked to be inlined, in case we're trying to re-export a
2222 // crate in Rust 2018+
2223 let please_inline = self.attrs.lists(sym::doc).has_word(sym::inline);
2224 let path = self.path.clean(cx);
2225 let inner = if self.glob {
2227 let mut visited = FxHashSet::default();
2228 if let Some(items) = inline::try_inline_glob(cx, path.res, &mut visited) {
2233 Import::Glob(resolve_use_source(cx, path))
2235 let name = self.name;
2238 Res::Def(DefKind::Mod, did) => {
2239 if !did.is_local() && did.index == CRATE_DEF_INDEX {
2240 // if we're `pub use`ing an extern crate root, don't inline it unless we
2241 // were specifically asked for it
2249 let mut visited = FxHashSet::default();
2250 if let Some(items) = inline::try_inline(
2252 Some(rustc::ty::Attributes::Borrowed(self.attrs)),
2258 Import::Simple(name.clean(cx), resolve_use_source(cx, path))
2263 attrs: self.attrs.clean(cx),
2264 source: self.whence.clean(cx),
2265 def_id: cx.tcx.hir().local_def_id_from_node_id(ast::CRATE_NODE_ID),
2266 visibility: self.vis.clean(cx),
2269 inner: ImportItem(inner)
2274 impl Clean<Item> for doctree::ForeignItem<'_> {
2275 fn clean(&self, cx: &DocContext<'_>) -> Item {
2276 let inner = match self.kind {
2277 hir::ForeignItemKind::Fn(ref decl, ref names, ref generics) => {
2278 let abi = cx.tcx.hir().get_foreign_abi(self.id);
2279 let (generics, decl) = enter_impl_trait(cx, || {
2280 (generics.clean(cx), (&**decl, &names[..]).clean(cx))
2282 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
2283 ForeignFunctionItem(Function {
2286 header: hir::FnHeader {
2287 unsafety: hir::Unsafety::Unsafe,
2289 constness: hir::Constness::NotConst,
2290 asyncness: hir::IsAsync::NotAsync,
2296 hir::ForeignItemKind::Static(ref ty, mutbl) => {
2297 ForeignStaticItem(Static {
2298 type_: ty.clean(cx),
2299 mutability: mutbl.clean(cx),
2300 expr: String::new(),
2303 hir::ForeignItemKind::Type => {
2309 name: Some(self.name.clean(cx)),
2310 attrs: self.attrs.clean(cx),
2311 source: self.whence.clean(cx),
2312 def_id: cx.tcx.hir().local_def_id(self.id),
2313 visibility: self.vis.clean(cx),
2314 stability: cx.stability(self.id).clean(cx),
2315 deprecation: cx.deprecation(self.id).clean(cx),
2321 impl Clean<Item> for doctree::Macro<'_> {
2322 fn clean(&self, cx: &DocContext<'_>) -> Item {
2323 let name = self.name.clean(cx);
2325 name: Some(name.clone()),
2326 attrs: self.attrs.clean(cx),
2327 source: self.whence.clean(cx),
2329 stability: cx.stability(self.hid).clean(cx),
2330 deprecation: cx.deprecation(self.hid).clean(cx),
2331 def_id: self.def_id,
2332 inner: MacroItem(Macro {
2333 source: format!("macro_rules! {} {{\n{}}}",
2335 self.matchers.iter().map(|span| {
2336 format!(" {} => {{ ... }};\n", span.to_src(cx))
2337 }).collect::<String>()),
2338 imported_from: self.imported_from.clean(cx),
2344 impl Clean<Item> for doctree::ProcMacro<'_> {
2345 fn clean(&self, cx: &DocContext<'_>) -> Item {
2347 name: Some(self.name.clean(cx)),
2348 attrs: self.attrs.clean(cx),
2349 source: self.whence.clean(cx),
2351 stability: cx.stability(self.id).clean(cx),
2352 deprecation: cx.deprecation(self.id).clean(cx),
2353 def_id: cx.tcx.hir().local_def_id(self.id),
2354 inner: ProcMacroItem(ProcMacro {
2356 helpers: self.helpers.clean(cx),
2362 impl Clean<Stability> for attr::Stability {
2363 fn clean(&self, _: &DocContext<'_>) -> Stability {
2365 level: stability::StabilityLevel::from_attr_level(&self.level),
2366 feature: Some(self.feature.to_string()).filter(|f| !f.is_empty()),
2367 since: match self.level {
2368 attr::Stable {ref since} => since.to_string(),
2371 deprecation: self.rustc_depr.as_ref().map(|d| {
2373 note: Some(d.reason.to_string()).filter(|r| !r.is_empty()),
2374 since: Some(d.since.to_string()).filter(|d| !d.is_empty()),
2377 unstable_reason: match self.level {
2378 attr::Unstable { reason: Some(ref reason), .. } => Some(reason.to_string()),
2381 issue: match self.level {
2382 attr::Unstable {issue, ..} => issue,
2389 impl<'a> Clean<Stability> for &'a attr::Stability {
2390 fn clean(&self, dc: &DocContext<'_>) -> Stability {
2395 impl Clean<Deprecation> for attr::Deprecation {
2396 fn clean(&self, _: &DocContext<'_>) -> Deprecation {
2398 since: self.since.map(|s| s.to_string()).filter(|s| !s.is_empty()),
2399 note: self.note.map(|n| n.to_string()).filter(|n| !n.is_empty()),
2404 impl Clean<TypeBinding> for hir::TypeBinding {
2405 fn clean(&self, cx: &DocContext<'_>) -> TypeBinding {
2407 name: self.ident.name.clean(cx),
2408 kind: self.kind.clean(cx),
2413 impl Clean<TypeBindingKind> for hir::TypeBindingKind {
2414 fn clean(&self, cx: &DocContext<'_>) -> TypeBindingKind {
2416 hir::TypeBindingKind::Equality { ref ty } =>
2417 TypeBindingKind::Equality {
2420 hir::TypeBindingKind::Constraint { ref bounds } =>
2421 TypeBindingKind::Constraint {
2422 bounds: bounds.into_iter().map(|b| b.clean(cx)).collect(),
2429 TraitBound(Vec<PathSegment>, Vec<SimpleBound>, Vec<GenericParamDef>, hir::TraitBoundModifier),
2433 impl From<GenericBound> for SimpleBound {
2434 fn from(bound: GenericBound) -> Self {
2435 match bound.clone() {
2436 GenericBound::Outlives(l) => SimpleBound::Outlives(l),
2437 GenericBound::TraitBound(t, mod_) => match t.trait_ {
2438 Type::ResolvedPath { path, param_names, .. } => {
2439 SimpleBound::TraitBound(path.segments,
2441 .map_or_else(|| Vec::new(), |v| v.iter()
2442 .map(|p| SimpleBound::from(p.clone()))
2447 _ => panic!("Unexpected bound {:?}", bound),