1 //! This module contains the "cleaned" pieces of the AST, and the functions
13 use rustc_attr as attr;
14 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
16 use rustc_hir::def::{CtorKind, DefKind, Res};
17 use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX};
18 use rustc_index::vec::{Idx, IndexVec};
19 use rustc_infer::infer::region_constraints::{Constraint, RegionConstraintData};
20 use rustc_middle::middle::resolve_lifetime as rl;
21 use rustc_middle::middle::stability;
22 use rustc_middle::ty::fold::TypeFolder;
23 use rustc_middle::ty::subst::InternalSubsts;
24 use rustc_middle::ty::{self, AdtKind, Lift, Ty, TyCtxt};
25 use rustc_mir::const_eval::is_min_const_fn;
26 use rustc_span::hygiene::MacroKind;
27 use rustc_span::symbol::{kw, sym, Ident, Symbol};
28 use rustc_span::{self, Pos};
29 use rustc_typeck::hir_ty_to_ty;
31 use std::collections::hash_map::Entry;
32 use std::default::Default;
37 use crate::core::{self, DocContext, ImplTraitParam};
42 pub use utils::{get_auto_trait_and_blanket_impls, krate, register_res};
44 pub use self::types::FnRetTy::*;
45 pub use self::types::ItemEnum::*;
46 pub use self::types::SelfTy::*;
47 pub use self::types::Type::*;
48 pub use self::types::Visibility::{Inherited, Public};
49 pub use self::types::*;
51 const FN_OUTPUT_NAME: &str = "Output";
54 fn clean(&self, cx: &DocContext<'_>) -> T;
57 impl<T: Clean<U>, U> Clean<Vec<U>> for [T] {
58 fn clean(&self, cx: &DocContext<'_>) -> Vec<U> {
59 self.iter().map(|x| x.clean(cx)).collect()
63 impl<T: Clean<U>, U, V: Idx> Clean<IndexVec<V, U>> for IndexVec<V, T> {
64 fn clean(&self, cx: &DocContext<'_>) -> IndexVec<V, U> {
65 self.iter().map(|x| x.clean(cx)).collect()
69 impl<T: Clean<U>, U> Clean<U> for &T {
70 fn clean(&self, cx: &DocContext<'_>) -> U {
75 impl<T: Clean<U>, U> Clean<U> for Rc<T> {
76 fn clean(&self, cx: &DocContext<'_>) -> U {
81 impl<T: Clean<U>, U> Clean<Option<U>> for Option<T> {
82 fn clean(&self, cx: &DocContext<'_>) -> Option<U> {
83 self.as_ref().map(|v| v.clean(cx))
87 impl Clean<ExternalCrate> for CrateNum {
88 fn clean(&self, cx: &DocContext<'_>) -> ExternalCrate {
89 let root = DefId { krate: *self, index: CRATE_DEF_INDEX };
90 let krate_span = cx.tcx.def_span(root);
91 let krate_src = cx.sess().source_map().span_to_filename(krate_span);
93 // Collect all inner modules which are tagged as implementations of
96 // Note that this loop only searches the top-level items of the crate,
97 // and this is intentional. If we were to search the entire crate for an
98 // item tagged with `#[doc(primitive)]` then we would also have to
99 // search the entirety of external modules for items tagged
100 // `#[doc(primitive)]`, which is a pretty inefficient process (decoding
101 // all that metadata unconditionally).
103 // In order to keep the metadata load under control, the
104 // `#[doc(primitive)]` feature is explicitly designed to only allow the
105 // primitive tags to show up as the top level items in a crate.
107 // Also note that this does not attempt to deal with modules tagged
108 // duplicately for the same primitive. This is handled later on when
109 // rendering by delegating everything to a hash map.
110 let as_primitive = |res: Res| {
111 if let Res::Def(DefKind::Mod, def_id) = res {
112 let attrs = cx.tcx.get_attrs(def_id).clean(cx);
114 for attr in attrs.lists(sym::doc) {
115 if let Some(v) = attr.value_str() {
116 if attr.check_name(sym::primitive) {
117 prim = PrimitiveType::from_symbol(v);
121 // FIXME: should warn on unknown primitives?
125 return prim.map(|p| (def_id, p, attrs));
129 let primitives = if root.is_local() {
138 let item = cx.tcx.hir().expect_item(id.id);
140 hir::ItemKind::Mod(_) => as_primitive(Res::Def(
142 cx.tcx.hir().local_def_id(id.id).to_def_id(),
144 hir::ItemKind::Use(ref path, hir::UseKind::Single)
145 if item.vis.node.is_pub() =>
147 as_primitive(path.res).map(|(_, prim, attrs)| {
148 // Pretend the primitive is local.
149 (cx.tcx.hir().local_def_id(id.id).to_def_id(), prim, attrs)
160 .map(|item| item.res)
161 .filter_map(as_primitive)
165 let as_keyword = |res: Res| {
166 if let Res::Def(DefKind::Mod, def_id) = res {
167 let attrs = cx.tcx.get_attrs(def_id).clean(cx);
168 let mut keyword = None;
169 for attr in attrs.lists(sym::doc) {
170 if let Some(v) = attr.value_str() {
171 if attr.check_name(sym::keyword) {
172 if v.is_doc_keyword() {
173 keyword = Some(v.to_string());
176 // FIXME: should warn on unknown keywords?
180 return keyword.map(|p| (def_id, p, attrs));
184 let keywords = if root.is_local() {
193 let item = cx.tcx.hir().expect_item(id.id);
195 hir::ItemKind::Mod(_) => as_keyword(Res::Def(
197 cx.tcx.hir().local_def_id(id.id).to_def_id(),
199 hir::ItemKind::Use(ref path, hir::UseKind::Single)
200 if item.vis.node.is_pub() =>
202 as_keyword(path.res).map(|(_, prim, attrs)| {
203 (cx.tcx.hir().local_def_id(id.id).to_def_id(), prim, attrs)
211 cx.tcx.item_children(root).iter().map(|item| item.res).filter_map(as_keyword).collect()
215 name: cx.tcx.crate_name(*self).to_string(),
217 attrs: cx.tcx.get_attrs(root).clean(cx),
224 impl Clean<Item> for doctree::Module<'_> {
225 fn clean(&self, cx: &DocContext<'_>) -> Item {
226 let name = if self.name.is_some() {
227 self.name.expect("No name provided").clean(cx)
232 // maintain a stack of mod ids, for doc comment path resolution
233 // but we also need to resolve the module's own docs based on whether its docs were written
234 // inside or outside the module, so check for that
235 let attrs = self.attrs.clean(cx);
237 let mut items: Vec<Item> = vec![];
238 items.extend(self.extern_crates.iter().flat_map(|x| x.clean(cx)));
239 items.extend(self.imports.iter().flat_map(|x| x.clean(cx)));
240 items.extend(self.structs.iter().map(|x| x.clean(cx)));
241 items.extend(self.unions.iter().map(|x| x.clean(cx)));
242 items.extend(self.enums.iter().map(|x| x.clean(cx)));
243 items.extend(self.fns.iter().map(|x| x.clean(cx)));
244 items.extend(self.foreigns.iter().map(|x| x.clean(cx)));
245 items.extend(self.mods.iter().map(|x| x.clean(cx)));
246 items.extend(self.typedefs.iter().map(|x| x.clean(cx)));
247 items.extend(self.opaque_tys.iter().map(|x| x.clean(cx)));
248 items.extend(self.statics.iter().map(|x| x.clean(cx)));
249 items.extend(self.constants.iter().map(|x| x.clean(cx)));
250 items.extend(self.traits.iter().map(|x| x.clean(cx)));
251 items.extend(self.impls.iter().flat_map(|x| x.clean(cx)));
252 items.extend(self.macros.iter().map(|x| x.clean(cx)));
253 items.extend(self.proc_macros.iter().map(|x| x.clean(cx)));
254 items.extend(self.trait_aliases.iter().map(|x| x.clean(cx)));
256 // determine if we should display the inner contents or
257 // the outer `mod` item for the source code.
259 let sm = cx.sess().source_map();
260 let outer = sm.lookup_char_pos(self.where_outer.lo());
261 let inner = sm.lookup_char_pos(self.where_inner.lo());
262 if outer.file.start_pos == inner.file.start_pos {
266 // mod foo; (and a separate SourceFile for the contents)
274 source: whence.clean(cx),
275 visibility: self.vis.clean(cx),
276 stability: cx.stability(self.id).clean(cx),
277 deprecation: cx.deprecation(self.id).clean(cx),
278 def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
279 inner: ModuleItem(Module { is_crate: self.is_crate, items }),
284 impl Clean<Attributes> for [ast::Attribute] {
285 fn clean(&self, cx: &DocContext<'_>) -> Attributes {
286 Attributes::from_ast(cx.sess().diagnostic(), self)
290 impl Clean<GenericBound> for hir::GenericBound<'_> {
291 fn clean(&self, cx: &DocContext<'_>) -> GenericBound {
293 hir::GenericBound::Outlives(lt) => GenericBound::Outlives(lt.clean(cx)),
294 hir::GenericBound::Trait(ref t, modifier) => {
295 GenericBound::TraitBound(t.clean(cx), modifier)
301 impl Clean<Type> for (ty::TraitRef<'_>, &[TypeBinding]) {
302 fn clean(&self, cx: &DocContext<'_>) -> Type {
303 let (trait_ref, bounds) = *self;
304 inline::record_extern_fqn(cx, trait_ref.def_id, TypeKind::Trait);
305 let path = external_path(
307 cx.tcx.item_name(trait_ref.def_id),
308 Some(trait_ref.def_id),
314 debug!("ty::TraitRef\n subst: {:?}\n", trait_ref.substs);
316 ResolvedPath { path, param_names: None, did: trait_ref.def_id, is_generic: false }
320 impl<'tcx> Clean<GenericBound> for ty::TraitRef<'tcx> {
321 fn clean(&self, cx: &DocContext<'_>) -> GenericBound {
322 GenericBound::TraitBound(
323 PolyTrait { trait_: (*self, &[][..]).clean(cx), generic_params: vec![] },
324 hir::TraitBoundModifier::None,
329 impl Clean<GenericBound> for (ty::PolyTraitRef<'_>, &[TypeBinding]) {
330 fn clean(&self, cx: &DocContext<'_>) -> GenericBound {
331 let (poly_trait_ref, bounds) = *self;
332 let poly_trait_ref = poly_trait_ref.lift_to_tcx(cx.tcx).unwrap();
334 // collect any late bound regions
335 let late_bound_regions: Vec<_> = cx
337 .collect_referenced_late_bound_regions(&poly_trait_ref)
339 .filter_map(|br| match br {
340 ty::BrNamed(_, name) => Some(GenericParamDef {
341 name: name.to_string(),
342 kind: GenericParamDefKind::Lifetime,
348 GenericBound::TraitBound(
350 trait_: (poly_trait_ref.skip_binder(), bounds).clean(cx),
351 generic_params: late_bound_regions,
353 hir::TraitBoundModifier::None,
358 impl<'tcx> Clean<GenericBound> for ty::PolyTraitRef<'tcx> {
359 fn clean(&self, cx: &DocContext<'_>) -> GenericBound {
360 (*self, &[][..]).clean(cx)
364 impl<'tcx> Clean<Option<Vec<GenericBound>>> for InternalSubsts<'tcx> {
365 fn clean(&self, cx: &DocContext<'_>) -> Option<Vec<GenericBound>> {
366 let mut v = Vec::new();
367 v.extend(self.regions().filter_map(|r| r.clean(cx)).map(GenericBound::Outlives));
368 v.extend(self.types().map(|t| {
369 GenericBound::TraitBound(
370 PolyTrait { trait_: t.clean(cx), generic_params: Vec::new() },
371 hir::TraitBoundModifier::None,
374 if !v.is_empty() { Some(v) } else { None }
378 impl Clean<Lifetime> for hir::Lifetime {
379 fn clean(&self, cx: &DocContext<'_>) -> Lifetime {
380 let def = cx.tcx.named_region(self.hir_id);
383 rl::Region::EarlyBound(_, node_id, _)
384 | rl::Region::LateBound(_, node_id, _)
385 | rl::Region::Free(_, node_id),
387 if let Some(lt) = cx.lt_substs.borrow().get(&node_id).cloned() {
393 Lifetime(self.name.ident().to_string())
397 impl Clean<Lifetime> for hir::GenericParam<'_> {
398 fn clean(&self, _: &DocContext<'_>) -> Lifetime {
400 hir::GenericParamKind::Lifetime { .. } => {
401 if !self.bounds.is_empty() {
402 let mut bounds = self.bounds.iter().map(|bound| match bound {
403 hir::GenericBound::Outlives(lt) => lt,
406 let name = bounds.next().expect("no more bounds").name.ident();
407 let mut s = format!("{}: {}", self.name.ident(), name);
408 for bound in bounds {
409 s.push_str(&format!(" + {}", bound.name.ident()));
413 Lifetime(self.name.ident().to_string())
421 impl Clean<Constant> for hir::ConstArg {
422 fn clean(&self, cx: &DocContext<'_>) -> Constant {
426 .type_of(cx.tcx.hir().body_owner_def_id(self.value.body).to_def_id())
428 expr: print_const_expr(cx, self.value.body),
430 is_literal: is_literal_expr(cx, self.value.body.hir_id),
435 impl Clean<Lifetime> for ty::GenericParamDef {
436 fn clean(&self, _cx: &DocContext<'_>) -> Lifetime {
437 Lifetime(self.name.to_string())
441 impl Clean<Option<Lifetime>> for ty::RegionKind {
442 fn clean(&self, cx: &DocContext<'_>) -> Option<Lifetime> {
444 ty::ReStatic => Some(Lifetime::statik()),
445 ty::ReLateBound(_, ty::BrNamed(_, name)) => Some(Lifetime(name.to_string())),
446 ty::ReEarlyBound(ref data) => Some(Lifetime(data.name.clean(cx))),
451 | ty::RePlaceholder(..)
454 debug!("cannot clean region {:?}", self);
461 impl Clean<WherePredicate> for hir::WherePredicate<'_> {
462 fn clean(&self, cx: &DocContext<'_>) -> WherePredicate {
464 hir::WherePredicate::BoundPredicate(ref wbp) => WherePredicate::BoundPredicate {
465 ty: wbp.bounded_ty.clean(cx),
466 bounds: wbp.bounds.clean(cx),
469 hir::WherePredicate::RegionPredicate(ref wrp) => WherePredicate::RegionPredicate {
470 lifetime: wrp.lifetime.clean(cx),
471 bounds: wrp.bounds.clean(cx),
474 hir::WherePredicate::EqPredicate(ref wrp) => {
475 WherePredicate::EqPredicate { lhs: wrp.lhs_ty.clean(cx), rhs: wrp.rhs_ty.clean(cx) }
481 impl<'a> Clean<Option<WherePredicate>> for ty::Predicate<'a> {
482 fn clean(&self, cx: &DocContext<'_>) -> Option<WherePredicate> {
484 ty::PredicateKind::Trait(ref pred, _) => Some(pred.clean(cx)),
485 ty::PredicateKind::Subtype(ref pred) => Some(pred.clean(cx)),
486 ty::PredicateKind::RegionOutlives(ref pred) => pred.clean(cx),
487 ty::PredicateKind::TypeOutlives(ref pred) => pred.clean(cx),
488 ty::PredicateKind::Projection(ref pred) => Some(pred.clean(cx)),
490 ty::PredicateKind::WellFormed(..)
491 | ty::PredicateKind::ObjectSafe(..)
492 | ty::PredicateKind::ClosureKind(..)
493 | ty::PredicateKind::ConstEvaluatable(..)
494 | ty::PredicateKind::ConstEquate(..) => panic!("not user writable"),
499 impl<'a> Clean<WherePredicate> for ty::PolyTraitPredicate<'a> {
500 fn clean(&self, cx: &DocContext<'_>) -> WherePredicate {
501 let poly_trait_ref = self.map_bound(|pred| pred.trait_ref);
502 WherePredicate::BoundPredicate {
503 ty: poly_trait_ref.skip_binder().self_ty().clean(cx),
504 bounds: vec![poly_trait_ref.clean(cx)],
509 impl<'tcx> Clean<WherePredicate> for ty::PolySubtypePredicate<'tcx> {
510 fn clean(&self, _cx: &DocContext<'_>) -> WherePredicate {
512 "subtype predicates are an internal rustc artifact \
513 and should not be seen by rustdoc"
518 impl<'tcx> Clean<Option<WherePredicate>>
519 for ty::PolyOutlivesPredicate<ty::Region<'tcx>, ty::Region<'tcx>>
521 fn clean(&self, cx: &DocContext<'_>) -> Option<WherePredicate> {
522 let ty::OutlivesPredicate(a, b) = self.skip_binder();
524 if let (ty::ReEmpty(_), ty::ReEmpty(_)) = (a, b) {
528 Some(WherePredicate::RegionPredicate {
529 lifetime: a.clean(cx).expect("failed to clean lifetime"),
530 bounds: vec![GenericBound::Outlives(b.clean(cx).expect("failed to clean bounds"))],
535 impl<'tcx> Clean<Option<WherePredicate>> for ty::PolyOutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>> {
536 fn clean(&self, cx: &DocContext<'_>) -> Option<WherePredicate> {
537 let ty::OutlivesPredicate(ty, lt) = self.skip_binder();
539 if let ty::ReEmpty(_) = lt {
543 Some(WherePredicate::BoundPredicate {
545 bounds: vec![GenericBound::Outlives(lt.clean(cx).expect("failed to clean lifetimes"))],
550 impl<'tcx> Clean<WherePredicate> for ty::PolyProjectionPredicate<'tcx> {
551 fn clean(&self, cx: &DocContext<'_>) -> WherePredicate {
552 let ty::ProjectionPredicate { projection_ty, ty } = self.skip_binder();
553 WherePredicate::EqPredicate { lhs: projection_ty.clean(cx), rhs: ty.clean(cx) }
557 impl<'tcx> Clean<Type> for ty::ProjectionTy<'tcx> {
558 fn clean(&self, cx: &DocContext<'_>) -> Type {
559 let lifted = self.lift_to_tcx(cx.tcx).unwrap();
560 let trait_ = match lifted.trait_ref(cx.tcx).clean(cx) {
561 GenericBound::TraitBound(t, _) => t.trait_,
562 GenericBound::Outlives(_) => panic!("cleaning a trait got a lifetime"),
565 name: cx.tcx.associated_item(self.item_def_id).ident.name.clean(cx),
566 self_type: box self.self_ty().clean(cx),
572 impl Clean<GenericParamDef> for ty::GenericParamDef {
573 fn clean(&self, cx: &DocContext<'_>) -> GenericParamDef {
574 let (name, kind) = match self.kind {
575 ty::GenericParamDefKind::Lifetime => {
576 (self.name.to_string(), GenericParamDefKind::Lifetime)
578 ty::GenericParamDefKind::Type { has_default, synthetic, .. } => {
580 if has_default { Some(cx.tcx.type_of(self.def_id).clean(cx)) } else { None };
583 GenericParamDefKind::Type {
585 bounds: vec![], // These are filled in from the where-clauses.
591 ty::GenericParamDefKind::Const { .. } => (
593 GenericParamDefKind::Const {
595 ty: cx.tcx.type_of(self.def_id).clean(cx),
600 GenericParamDef { name, kind }
604 impl Clean<GenericParamDef> for hir::GenericParam<'_> {
605 fn clean(&self, cx: &DocContext<'_>) -> GenericParamDef {
606 let (name, kind) = match self.kind {
607 hir::GenericParamKind::Lifetime { .. } => {
608 let name = if !self.bounds.is_empty() {
609 let mut bounds = self.bounds.iter().map(|bound| match bound {
610 hir::GenericBound::Outlives(lt) => lt,
613 let name = bounds.next().expect("no more bounds").name.ident();
614 let mut s = format!("{}: {}", self.name.ident(), name);
615 for bound in bounds {
616 s.push_str(&format!(" + {}", bound.name.ident()));
620 self.name.ident().to_string()
622 (name, GenericParamDefKind::Lifetime)
624 hir::GenericParamKind::Type { ref default, synthetic } => (
625 self.name.ident().name.clean(cx),
626 GenericParamDefKind::Type {
627 did: cx.tcx.hir().local_def_id(self.hir_id).to_def_id(),
628 bounds: self.bounds.clean(cx),
629 default: default.clean(cx),
633 hir::GenericParamKind::Const { ref ty } => (
634 self.name.ident().name.clean(cx),
635 GenericParamDefKind::Const {
636 did: cx.tcx.hir().local_def_id(self.hir_id).to_def_id(),
642 GenericParamDef { name, kind }
646 impl Clean<Generics> for hir::Generics<'_> {
647 fn clean(&self, cx: &DocContext<'_>) -> Generics {
648 // Synthetic type-parameters are inserted after normal ones.
649 // In order for normal parameters to be able to refer to synthetic ones,
651 fn is_impl_trait(param: &hir::GenericParam<'_>) -> bool {
653 hir::GenericParamKind::Type { synthetic, .. } => {
654 synthetic == Some(hir::SyntheticTyParamKind::ImplTrait)
659 let impl_trait_params = self
662 .filter(|param| is_impl_trait(param))
664 let param: GenericParamDef = param.clean(cx);
666 GenericParamDefKind::Lifetime => unreachable!(),
667 GenericParamDefKind::Type { did, ref bounds, .. } => {
668 cx.impl_trait_bounds.borrow_mut().insert(did.into(), bounds.clone());
670 GenericParamDefKind::Const { .. } => unreachable!(),
674 .collect::<Vec<_>>();
676 let mut params = Vec::with_capacity(self.params.len());
677 for p in self.params.iter().filter(|p| !is_impl_trait(p)) {
681 params.extend(impl_trait_params);
684 Generics { params, where_predicates: self.where_clause.predicates.clean(cx) };
686 // Some duplicates are generated for ?Sized bounds between type params and where
687 // predicates. The point in here is to move the bounds definitions from type params
688 // to where predicates when such cases occur.
689 for where_pred in &mut generics.where_predicates {
691 WherePredicate::BoundPredicate { ty: Generic(ref name), ref mut bounds } => {
692 if bounds.is_empty() {
693 for param in &mut generics.params {
695 GenericParamDefKind::Lifetime => {}
696 GenericParamDefKind::Type { bounds: ref mut ty_bounds, .. } => {
697 if ¶m.name == name {
698 mem::swap(bounds, ty_bounds);
702 GenericParamDefKind::Const { .. } => {}
714 impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics, ty::GenericPredicates<'tcx>) {
715 fn clean(&self, cx: &DocContext<'_>) -> Generics {
716 use self::WherePredicate as WP;
717 use std::collections::BTreeMap;
719 let (gens, preds) = *self;
721 // Don't populate `cx.impl_trait_bounds` before `clean`ning `where` clauses,
722 // since `Clean for ty::Predicate` would consume them.
723 let mut impl_trait = BTreeMap::<ImplTraitParam, Vec<GenericBound>>::default();
725 // Bounds in the type_params and lifetimes fields are repeated in the
726 // predicates field (see rustc_typeck::collect::ty_generics), so remove
728 let stripped_typarams = gens
731 .filter_map(|param| match param.kind {
732 ty::GenericParamDefKind::Lifetime => None,
733 ty::GenericParamDefKind::Type { synthetic, .. } => {
734 if param.name == kw::SelfUpper {
735 assert_eq!(param.index, 0);
738 if synthetic == Some(hir::SyntheticTyParamKind::ImplTrait) {
739 impl_trait.insert(param.index.into(), vec![]);
742 Some(param.clean(cx))
744 ty::GenericParamDefKind::Const { .. } => None,
746 .collect::<Vec<GenericParamDef>>();
748 // param index -> [(DefId of trait, associated type name, type)]
749 let mut impl_trait_proj = FxHashMap::<u32, Vec<(DefId, String, Ty<'tcx>)>>::default();
751 let where_predicates = preds
755 let mut projection = None;
756 let param_idx = (|| {
757 if let Some(trait_ref) = p.to_opt_poly_trait_ref() {
758 if let ty::Param(param) = trait_ref.skip_binder().self_ty().kind {
759 return Some(param.index);
761 } else if let Some(outlives) = p.to_opt_type_outlives() {
762 if let ty::Param(param) = outlives.skip_binder().0.kind {
763 return Some(param.index);
765 } else if let ty::PredicateKind::Projection(p) = p.kind() {
766 if let ty::Param(param) = p.skip_binder().projection_ty.self_ty().kind {
767 projection = Some(p);
768 return Some(param.index);
775 if let Some(param_idx) = param_idx {
776 if let Some(b) = impl_trait.get_mut(¶m_idx.into()) {
777 let p = p.clean(cx)?;
784 .filter(|b| !b.is_sized_bound(cx)),
787 let proj = projection
788 .map(|p| (p.skip_binder().projection_ty.clean(cx), p.skip_binder().ty));
789 if let Some(((_, trait_did, name), rhs)) =
790 proj.as_ref().and_then(|(lhs, rhs)| Some((lhs.projection()?, rhs)))
792 impl_trait_proj.entry(param_idx).or_default().push((
805 .collect::<Vec<_>>();
807 for (param, mut bounds) in impl_trait {
808 // Move trait bounds to the front.
809 bounds.sort_by_key(|b| if let GenericBound::TraitBound(..) = b { false } else { true });
811 if let crate::core::ImplTraitParam::ParamIndex(idx) = param {
812 if let Some(proj) = impl_trait_proj.remove(&idx) {
813 for (trait_did, name, rhs) in proj {
814 simplify::merge_bounds(cx, &mut bounds, trait_did, &name, &rhs.clean(cx));
821 cx.impl_trait_bounds.borrow_mut().insert(param, bounds);
824 // Now that `cx.impl_trait_bounds` is populated, we can process
825 // remaining predicates which could contain `impl Trait`.
826 let mut where_predicates =
827 where_predicates.into_iter().flat_map(|p| p.clean(cx)).collect::<Vec<_>>();
829 // Type parameters and have a Sized bound by default unless removed with
830 // ?Sized. Scan through the predicates and mark any type parameter with
831 // a Sized bound, removing the bounds as we find them.
833 // Note that associated types also have a sized bound by default, but we
834 // don't actually know the set of associated types right here so that's
835 // handled in cleaning associated types
836 let mut sized_params = FxHashSet::default();
837 where_predicates.retain(|pred| match *pred {
838 WP::BoundPredicate { ty: Generic(ref g), ref bounds } => {
839 if bounds.iter().any(|b| b.is_sized_bound(cx)) {
840 sized_params.insert(g.clone());
849 // Run through the type parameters again and insert a ?Sized
850 // unbound for any we didn't find to be Sized.
851 for tp in &stripped_typarams {
852 if !sized_params.contains(&tp.name) {
853 where_predicates.push(WP::BoundPredicate {
854 ty: Type::Generic(tp.name.clone()),
855 bounds: vec![GenericBound::maybe_sized(cx)],
860 // It would be nice to collect all of the bounds on a type and recombine
861 // them if possible, to avoid e.g., `where T: Foo, T: Bar, T: Sized, T: 'a`
862 // and instead see `where T: Foo + Bar + Sized + 'a`
868 .flat_map(|param| match param.kind {
869 ty::GenericParamDefKind::Lifetime => Some(param.clean(cx)),
870 ty::GenericParamDefKind::Type { .. } => None,
871 ty::GenericParamDefKind::Const { .. } => Some(param.clean(cx)),
873 .chain(simplify::ty_params(stripped_typarams).into_iter())
875 where_predicates: simplify::where_clauses(cx, where_predicates),
880 impl<'a> Clean<Method>
881 for (&'a hir::FnSig<'a>, &'a hir::Generics<'a>, hir::BodyId, Option<hir::Defaultness>)
883 fn clean(&self, cx: &DocContext<'_>) -> Method {
884 let (generics, decl) =
885 enter_impl_trait(cx, || (self.1.clean(cx), (&*self.0.decl, self.2).clean(cx)));
886 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
887 Method { decl, generics, header: self.0.header, defaultness: self.3, all_types, ret_types }
891 impl Clean<Item> for doctree::Function<'_> {
892 fn clean(&self, cx: &DocContext<'_>) -> Item {
893 let (generics, decl) =
894 enter_impl_trait(cx, || (self.generics.clean(cx), (self.decl, self.body).clean(cx)));
896 let did = cx.tcx.hir().local_def_id(self.id);
897 let constness = if is_min_const_fn(cx.tcx, did.to_def_id()) {
898 hir::Constness::Const
900 hir::Constness::NotConst
902 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
904 name: Some(self.name.clean(cx)),
905 attrs: self.attrs.clean(cx),
906 source: self.whence.clean(cx),
907 visibility: self.vis.clean(cx),
908 stability: cx.stability(self.id).clean(cx),
909 deprecation: cx.deprecation(self.id).clean(cx),
910 def_id: did.to_def_id(),
911 inner: FunctionItem(Function {
914 header: hir::FnHeader { constness, ..self.header },
922 impl<'a> Clean<Arguments> for (&'a [hir::Ty<'a>], &'a [Ident]) {
923 fn clean(&self, cx: &DocContext<'_>) -> Arguments {
931 self.1.get(i).map(|ident| ident.to_string()).unwrap_or(String::new());
933 name = "_".to_string();
935 Argument { name, type_: ty.clean(cx) }
942 impl<'a> Clean<Arguments> for (&'a [hir::Ty<'a>], hir::BodyId) {
943 fn clean(&self, cx: &DocContext<'_>) -> Arguments {
944 let body = cx.tcx.hir().body(self.1);
951 .map(|(i, ty)| Argument {
952 name: name_from_pat(&body.params[i].pat),
960 impl<'a, A: Copy> Clean<FnDecl> for (&'a hir::FnDecl<'a>, A)
962 (&'a [hir::Ty<'a>], A): Clean<Arguments>,
964 fn clean(&self, cx: &DocContext<'_>) -> FnDecl {
966 inputs: (&self.0.inputs[..], self.1).clean(cx),
967 output: self.0.output.clean(cx),
968 c_variadic: self.0.c_variadic,
969 attrs: Attributes::default(),
974 impl<'tcx> Clean<FnDecl> for (DefId, ty::PolyFnSig<'tcx>) {
975 fn clean(&self, cx: &DocContext<'_>) -> FnDecl {
976 let (did, sig) = *self;
977 let mut names = if did.is_local() { &[] } else { cx.tcx.fn_arg_names(did) }.iter();
980 output: Return(sig.skip_binder().output().clean(cx)),
981 attrs: Attributes::default(),
982 c_variadic: sig.skip_binder().c_variadic,
990 name: names.next().map_or(String::new(), |name| name.to_string()),
998 impl Clean<FnRetTy> for hir::FnRetTy<'_> {
999 fn clean(&self, cx: &DocContext<'_>) -> FnRetTy {
1001 Self::Return(ref typ) => Return(typ.clean(cx)),
1002 Self::DefaultReturn(..) => DefaultReturn,
1007 impl Clean<Item> for doctree::Trait<'_> {
1008 fn clean(&self, cx: &DocContext<'_>) -> Item {
1009 let attrs = self.attrs.clean(cx);
1011 name: Some(self.name.clean(cx)),
1013 source: self.whence.clean(cx),
1014 def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
1015 visibility: self.vis.clean(cx),
1016 stability: cx.stability(self.id).clean(cx),
1017 deprecation: cx.deprecation(self.id).clean(cx),
1018 inner: TraitItem(Trait {
1019 auto: self.is_auto.clean(cx),
1020 unsafety: self.unsafety,
1021 items: self.items.iter().map(|ti| ti.clean(cx)).collect(),
1022 generics: self.generics.clean(cx),
1023 bounds: self.bounds.clean(cx),
1024 is_auto: self.is_auto.clean(cx),
1030 impl Clean<Item> for doctree::TraitAlias<'_> {
1031 fn clean(&self, cx: &DocContext<'_>) -> Item {
1032 let attrs = self.attrs.clean(cx);
1034 name: Some(self.name.clean(cx)),
1036 source: self.whence.clean(cx),
1037 def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
1038 visibility: self.vis.clean(cx),
1039 stability: cx.stability(self.id).clean(cx),
1040 deprecation: cx.deprecation(self.id).clean(cx),
1041 inner: TraitAliasItem(TraitAlias {
1042 generics: self.generics.clean(cx),
1043 bounds: self.bounds.clean(cx),
1049 impl Clean<bool> for hir::IsAuto {
1050 fn clean(&self, _: &DocContext<'_>) -> bool {
1052 hir::IsAuto::Yes => true,
1053 hir::IsAuto::No => false,
1058 impl Clean<Type> for hir::TraitRef<'_> {
1059 fn clean(&self, cx: &DocContext<'_>) -> Type {
1060 resolve_type(cx, self.path.clean(cx), self.hir_ref_id)
1064 impl Clean<PolyTrait> for hir::PolyTraitRef<'_> {
1065 fn clean(&self, cx: &DocContext<'_>) -> PolyTrait {
1067 trait_: self.trait_ref.clean(cx),
1068 generic_params: self.bound_generic_params.clean(cx),
1073 impl Clean<TypeKind> for hir::def::DefKind {
1074 fn clean(&self, _: &DocContext<'_>) -> TypeKind {
1076 hir::def::DefKind::Mod => TypeKind::Module,
1077 hir::def::DefKind::Struct => TypeKind::Struct,
1078 hir::def::DefKind::Union => TypeKind::Union,
1079 hir::def::DefKind::Enum => TypeKind::Enum,
1080 hir::def::DefKind::Trait => TypeKind::Trait,
1081 hir::def::DefKind::TyAlias => TypeKind::Typedef,
1082 hir::def::DefKind::ForeignTy => TypeKind::Foreign,
1083 hir::def::DefKind::TraitAlias => TypeKind::TraitAlias,
1084 hir::def::DefKind::Fn => TypeKind::Function,
1085 hir::def::DefKind::Const => TypeKind::Const,
1086 hir::def::DefKind::Static => TypeKind::Static,
1087 hir::def::DefKind::Macro(_) => TypeKind::Macro,
1088 _ => TypeKind::Foreign,
1093 impl Clean<Item> for hir::TraitItem<'_> {
1094 fn clean(&self, cx: &DocContext<'_>) -> Item {
1095 let inner = match self.kind {
1096 hir::TraitItemKind::Const(ref ty, default) => {
1097 AssocConstItem(ty.clean(cx), default.map(|e| print_const_expr(cx, e)))
1099 hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => {
1100 MethodItem((sig, &self.generics, body, None).clean(cx))
1102 hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Required(ref names)) => {
1103 let (generics, decl) = enter_impl_trait(cx, || {
1104 (self.generics.clean(cx), (&*sig.decl, &names[..]).clean(cx))
1106 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
1107 TyMethodItem(TyMethod { header: sig.header, decl, generics, all_types, ret_types })
1109 hir::TraitItemKind::Type(ref bounds, ref default) => {
1110 AssocTypeItem(bounds.clean(cx), default.clean(cx))
1113 let local_did = cx.tcx.hir().local_def_id(self.hir_id);
1115 name: Some(self.ident.name.clean(cx)),
1116 attrs: self.attrs.clean(cx),
1117 source: self.span.clean(cx),
1118 def_id: local_did.to_def_id(),
1119 visibility: Visibility::Inherited,
1120 stability: get_stability(cx, local_did.to_def_id()),
1121 deprecation: get_deprecation(cx, local_did.to_def_id()),
1127 impl Clean<Item> for hir::ImplItem<'_> {
1128 fn clean(&self, cx: &DocContext<'_>) -> Item {
1129 let inner = match self.kind {
1130 hir::ImplItemKind::Const(ref ty, expr) => {
1131 AssocConstItem(ty.clean(cx), Some(print_const_expr(cx, expr)))
1133 hir::ImplItemKind::Fn(ref sig, body) => {
1134 MethodItem((sig, &self.generics, body, Some(self.defaultness)).clean(cx))
1136 hir::ImplItemKind::TyAlias(ref ty) => {
1137 let type_ = ty.clean(cx);
1138 let item_type = type_.def_id().and_then(|did| inline::build_ty(cx, did));
1139 TypedefItem(Typedef { type_, generics: Generics::default(), item_type }, true)
1142 let local_did = cx.tcx.hir().local_def_id(self.hir_id);
1144 name: Some(self.ident.name.clean(cx)),
1145 source: self.span.clean(cx),
1146 attrs: self.attrs.clean(cx),
1147 def_id: local_did.to_def_id(),
1148 visibility: self.vis.clean(cx),
1149 stability: get_stability(cx, local_did.to_def_id()),
1150 deprecation: get_deprecation(cx, local_did.to_def_id()),
1156 impl Clean<Item> for ty::AssocItem {
1157 fn clean(&self, cx: &DocContext<'_>) -> Item {
1158 let inner = match self.kind {
1159 ty::AssocKind::Const => {
1160 let ty = cx.tcx.type_of(self.def_id);
1161 let default = if self.defaultness.has_value() {
1162 Some(inline::print_inlined_const(cx, self.def_id))
1166 AssocConstItem(ty.clean(cx), default)
1168 ty::AssocKind::Fn => {
1170 (cx.tcx.generics_of(self.def_id), cx.tcx.explicit_predicates_of(self.def_id))
1172 let sig = cx.tcx.fn_sig(self.def_id);
1173 let mut decl = (self.def_id, sig).clean(cx);
1175 if self.fn_has_self_parameter {
1176 let self_ty = match self.container {
1177 ty::ImplContainer(def_id) => cx.tcx.type_of(def_id),
1178 ty::TraitContainer(_) => cx.tcx.types.self_param,
1180 let self_arg_ty = sig.input(0).skip_binder();
1181 if self_arg_ty == self_ty {
1182 decl.inputs.values[0].type_ = Generic(String::from("Self"));
1183 } else if let ty::Ref(_, ty, _) = self_arg_ty.kind {
1185 match decl.inputs.values[0].type_ {
1186 BorrowedRef { ref mut type_, .. } => {
1187 **type_ = Generic(String::from("Self"))
1189 _ => unreachable!(),
1195 let provided = match self.container {
1196 ty::ImplContainer(_) => true,
1197 ty::TraitContainer(_) => self.defaultness.has_value(),
1199 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
1201 let constness = if is_min_const_fn(cx.tcx, self.def_id) {
1202 hir::Constness::Const
1204 hir::Constness::NotConst
1206 let asyncness = cx.tcx.asyncness(self.def_id);
1207 let defaultness = match self.container {
1208 ty::ImplContainer(_) => Some(self.defaultness),
1209 ty::TraitContainer(_) => None,
1214 header: hir::FnHeader {
1215 unsafety: sig.unsafety(),
1225 TyMethodItem(TyMethod {
1228 header: hir::FnHeader {
1229 unsafety: sig.unsafety(),
1231 constness: hir::Constness::NotConst,
1232 asyncness: hir::IsAsync::NotAsync,
1239 ty::AssocKind::Type => {
1240 let my_name = self.ident.name.clean(cx);
1242 if let ty::TraitContainer(did) = self.container {
1243 // When loading a cross-crate associated type, the bounds for this type
1244 // are actually located on the trait/impl itself, so we need to load
1245 // all of the generics from there and then look for bounds that are
1246 // applied to this associated type in question.
1247 let predicates = cx.tcx.explicit_predicates_of(did);
1248 let generics = (cx.tcx.generics_of(did), predicates).clean(cx);
1249 let mut bounds = generics
1252 .filter_map(|pred| {
1253 let (name, self_type, trait_, bounds) = match *pred {
1254 WherePredicate::BoundPredicate {
1255 ty: QPath { ref name, ref self_type, ref trait_ },
1257 } => (name, self_type, trait_, bounds),
1260 if *name != my_name {
1264 ResolvedPath { did, .. } if did == self.container.id() => {}
1268 Generic(ref s) if *s == "Self" => {}
1273 .flat_map(|i| i.iter().cloned())
1274 .collect::<Vec<_>>();
1275 // Our Sized/?Sized bound didn't get handled when creating the generics
1276 // because we didn't actually get our whole set of bounds until just now
1277 // (some of them may have come from the trait). If we do have a sized
1278 // bound, we remove it, and if we don't then we add the `?Sized` bound
1280 match bounds.iter().position(|b| b.is_sized_bound(cx)) {
1284 None => bounds.push(GenericBound::maybe_sized(cx)),
1287 let ty = if self.defaultness.has_value() {
1288 Some(cx.tcx.type_of(self.def_id))
1293 AssocTypeItem(bounds, ty.clean(cx))
1295 let type_ = cx.tcx.type_of(self.def_id).clean(cx);
1296 let item_type = type_.def_id().and_then(|did| inline::build_ty(cx, did));
1300 generics: Generics { params: Vec::new(), where_predicates: Vec::new() },
1309 let visibility = match self.container {
1310 ty::ImplContainer(_) => self.vis.clean(cx),
1311 ty::TraitContainer(_) => Inherited,
1315 name: Some(self.ident.name.clean(cx)),
1317 stability: get_stability(cx, self.def_id),
1318 deprecation: get_deprecation(cx, self.def_id),
1319 def_id: self.def_id,
1320 attrs: inline::load_attrs(cx, self.def_id).clean(cx),
1321 source: cx.tcx.def_span(self.def_id).clean(cx),
1327 impl Clean<Type> for hir::Ty<'_> {
1328 fn clean(&self, cx: &DocContext<'_>) -> Type {
1332 TyKind::Never => Never,
1333 TyKind::Ptr(ref m) => RawPointer(m.mutbl, box m.ty.clean(cx)),
1334 TyKind::Rptr(ref l, ref m) => {
1335 let lifetime = if l.is_elided() { None } else { Some(l.clean(cx)) };
1336 BorrowedRef { lifetime, mutability: m.mutbl, type_: box m.ty.clean(cx) }
1338 TyKind::Slice(ref ty) => Slice(box ty.clean(cx)),
1339 TyKind::Array(ref ty, ref length) => {
1340 let def_id = cx.tcx.hir().local_def_id(length.hir_id);
1341 let length = match cx.tcx.const_eval_poly(def_id.to_def_id()) {
1343 print_const(cx, ty::Const::from_value(cx.tcx, length, cx.tcx.types.usize))
1348 .span_to_snippet(cx.tcx.def_span(def_id))
1349 .unwrap_or_else(|_| "_".to_string()),
1351 Array(box ty.clean(cx), length)
1353 TyKind::Tup(ref tys) => Tuple(tys.clean(cx)),
1354 TyKind::OpaqueDef(item_id, _) => {
1355 let item = cx.tcx.hir().expect_item(item_id.id);
1356 if let hir::ItemKind::OpaqueTy(ref ty) = item.kind {
1357 ImplTrait(ty.bounds.clean(cx))
1362 TyKind::Path(hir::QPath::Resolved(None, ref path)) => {
1363 if let Res::Def(DefKind::TyParam, did) = path.res {
1364 if let Some(new_ty) = cx.ty_substs.borrow().get(&did).cloned() {
1367 if let Some(bounds) = cx.impl_trait_bounds.borrow_mut().remove(&did.into()) {
1368 return ImplTrait(bounds);
1372 let mut alias = None;
1373 if let Res::Def(DefKind::TyAlias, def_id) = path.res {
1374 // Substitute private type aliases
1375 if let Some(def_id) = def_id.as_local() {
1376 let hir_id = cx.tcx.hir().as_local_hir_id(def_id);
1377 if !cx.renderinfo.borrow().access_levels.is_exported(def_id.to_def_id()) {
1378 alias = Some(&cx.tcx.hir().expect_item(hir_id).kind);
1383 if let Some(&hir::ItemKind::TyAlias(ref ty, ref generics)) = alias {
1384 let provided_params = &path.segments.last().expect("segments were empty");
1385 let mut ty_substs = FxHashMap::default();
1386 let mut lt_substs = FxHashMap::default();
1387 let mut ct_substs = FxHashMap::default();
1388 let generic_args = provided_params.generic_args();
1390 let mut indices: GenericParamCount = Default::default();
1391 for param in generics.params.iter() {
1393 hir::GenericParamKind::Lifetime { .. } => {
1396 generic_args.args.iter().find_map(|arg| match arg {
1397 hir::GenericArg::Lifetime(lt) => {
1398 if indices.lifetimes == j {
1406 if let Some(lt) = lifetime.cloned() {
1407 if !lt.is_elided() {
1408 let lt_def_id = cx.tcx.hir().local_def_id(param.hir_id);
1409 lt_substs.insert(lt_def_id.to_def_id(), lt.clean(cx));
1412 indices.lifetimes += 1;
1414 hir::GenericParamKind::Type { ref default, .. } => {
1415 let ty_param_def_id = cx.tcx.hir().local_def_id(param.hir_id);
1418 generic_args.args.iter().find_map(|arg| match arg {
1419 hir::GenericArg::Type(ty) => {
1420 if indices.types == j {
1428 if let Some(ty) = type_ {
1429 ty_substs.insert(ty_param_def_id.to_def_id(), ty.clean(cx));
1430 } else if let Some(default) = *default {
1432 .insert(ty_param_def_id.to_def_id(), default.clean(cx));
1436 hir::GenericParamKind::Const { .. } => {
1437 let const_param_def_id =
1438 cx.tcx.hir().local_def_id(param.hir_id);
1441 generic_args.args.iter().find_map(|arg| match arg {
1442 hir::GenericArg::Const(ct) => {
1443 if indices.consts == j {
1451 if let Some(ct) = const_ {
1453 .insert(const_param_def_id.to_def_id(), ct.clean(cx));
1455 // FIXME(const_generics:defaults)
1456 indices.consts += 1;
1461 return cx.enter_alias(ty_substs, lt_substs, ct_substs, || ty.clean(cx));
1463 resolve_type(cx, path.clean(cx), self.hir_id)
1465 TyKind::Path(hir::QPath::Resolved(Some(ref qself), ref p)) => {
1466 let segments = if p.is_global() { &p.segments[1..] } else { &p.segments };
1467 let trait_segments = &segments[..segments.len() - 1];
1468 let trait_path = self::Path {
1469 global: p.is_global(),
1472 cx.tcx.associated_item(p.res.def_id()).container.id(),
1474 segments: trait_segments.clean(cx),
1477 name: p.segments.last().expect("segments were empty").ident.name.clean(cx),
1478 self_type: box qself.clean(cx),
1479 trait_: box resolve_type(cx, trait_path, self.hir_id),
1482 TyKind::Path(hir::QPath::TypeRelative(ref qself, ref segment)) => {
1483 let mut res = Res::Err;
1484 let ty = hir_ty_to_ty(cx.tcx, self);
1485 if let ty::Projection(proj) = ty.kind {
1486 res = Res::Def(DefKind::Trait, proj.trait_ref(cx.tcx).def_id);
1488 let trait_path = hir::Path { span: self.span, res, segments: &[] };
1490 name: segment.ident.name.clean(cx),
1491 self_type: box qself.clean(cx),
1492 trait_: box resolve_type(cx, trait_path.clean(cx), self.hir_id),
1495 TyKind::TraitObject(ref bounds, ref lifetime) => {
1496 match bounds[0].clean(cx).trait_ {
1497 ResolvedPath { path, param_names: None, did, is_generic } => {
1498 let mut bounds: Vec<self::GenericBound> = bounds[1..]
1501 self::GenericBound::TraitBound(
1503 hir::TraitBoundModifier::None,
1507 if !lifetime.is_elided() {
1508 bounds.push(self::GenericBound::Outlives(lifetime.clean(cx)));
1510 ResolvedPath { path, param_names: Some(bounds), did, is_generic }
1512 _ => Infer, // shouldn't happen
1515 TyKind::BareFn(ref barefn) => BareFunction(box barefn.clean(cx)),
1516 TyKind::Infer | TyKind::Err => Infer,
1517 TyKind::Typeof(..) => panic!("unimplemented type {:?}", self.kind),
1522 impl<'tcx> Clean<Type> for Ty<'tcx> {
1523 fn clean(&self, cx: &DocContext<'_>) -> Type {
1524 debug!("cleaning type: {:?}", self);
1527 ty::Bool => Primitive(PrimitiveType::Bool),
1528 ty::Char => Primitive(PrimitiveType::Char),
1529 ty::Int(int_ty) => Primitive(int_ty.into()),
1530 ty::Uint(uint_ty) => Primitive(uint_ty.into()),
1531 ty::Float(float_ty) => Primitive(float_ty.into()),
1532 ty::Str => Primitive(PrimitiveType::Str),
1533 ty::Slice(ty) => Slice(box ty.clean(cx)),
1534 ty::Array(ty, n) => {
1535 let mut n = cx.tcx.lift(&n).expect("array lift failed");
1536 n = n.eval(cx.tcx, ty::ParamEnv::reveal_all());
1537 let n = print_const(cx, n);
1538 Array(box ty.clean(cx), n)
1540 ty::RawPtr(mt) => RawPointer(mt.mutbl, box mt.ty.clean(cx)),
1541 ty::Ref(r, ty, mutbl) => {
1542 BorrowedRef { lifetime: r.clean(cx), mutability: mutbl, type_: box ty.clean(cx) }
1544 ty::FnDef(..) | ty::FnPtr(_) => {
1545 let ty = cx.tcx.lift(self).expect("FnPtr lift failed");
1546 let sig = ty.fn_sig(cx.tcx);
1547 let def_id = DefId::local(CRATE_DEF_INDEX);
1548 BareFunction(box BareFunctionDecl {
1549 unsafety: sig.unsafety(),
1550 generic_params: Vec::new(),
1551 decl: (def_id, sig).clean(cx),
1555 ty::Adt(def, substs) => {
1557 let kind = match def.adt_kind() {
1558 AdtKind::Struct => TypeKind::Struct,
1559 AdtKind::Union => TypeKind::Union,
1560 AdtKind::Enum => TypeKind::Enum,
1562 inline::record_extern_fqn(cx, did, kind);
1563 let path = external_path(cx, cx.tcx.item_name(did), None, false, vec![], substs);
1564 ResolvedPath { path, param_names: None, did, is_generic: false }
1566 ty::Foreign(did) => {
1567 inline::record_extern_fqn(cx, did, TypeKind::Foreign);
1568 let path = external_path(
1570 cx.tcx.item_name(did),
1574 InternalSubsts::empty(),
1576 ResolvedPath { path, param_names: None, did, is_generic: false }
1578 ty::Dynamic(ref obj, ref reg) => {
1579 // HACK: pick the first `did` as the `did` of the trait object. Someone
1580 // might want to implement "native" support for marker-trait-only
1582 let mut dids = obj.principal_def_id().into_iter().chain(obj.auto_traits());
1585 .unwrap_or_else(|| panic!("found trait object `{:?}` with no traits?", self));
1586 let substs = match obj.principal() {
1587 Some(principal) => principal.skip_binder().substs,
1588 // marker traits have no substs.
1589 _ => cx.tcx.intern_substs(&[]),
1592 inline::record_extern_fqn(cx, did, TypeKind::Trait);
1594 let mut param_names = vec![];
1595 if let Some(b) = reg.clean(cx) {
1596 param_names.push(GenericBound::Outlives(b));
1599 let empty = cx.tcx.intern_substs(&[]);
1601 external_path(cx, cx.tcx.item_name(did), Some(did), false, vec![], empty);
1602 inline::record_extern_fqn(cx, did, TypeKind::Trait);
1603 let bound = GenericBound::TraitBound(
1605 trait_: ResolvedPath {
1611 generic_params: Vec::new(),
1613 hir::TraitBoundModifier::None,
1615 param_names.push(bound);
1618 let mut bindings = vec![];
1619 for pb in obj.projection_bounds() {
1620 bindings.push(TypeBinding {
1621 name: cx.tcx.associated_item(pb.item_def_id()).ident.name.clean(cx),
1622 kind: TypeBindingKind::Equality { ty: pb.skip_binder().ty.clean(cx) },
1627 external_path(cx, cx.tcx.item_name(did), Some(did), false, bindings, substs);
1628 ResolvedPath { path, param_names: Some(param_names), did, is_generic: false }
1630 ty::Tuple(ref t) => {
1631 Tuple(t.iter().map(|t| t.expect_ty()).collect::<Vec<_>>().clean(cx))
1634 ty::Projection(ref data) => data.clean(cx),
1636 ty::Param(ref p) => {
1637 if let Some(bounds) = cx.impl_trait_bounds.borrow_mut().remove(&p.index.into()) {
1640 Generic(p.name.to_string())
1644 ty::Opaque(def_id, substs) => {
1645 // Grab the "TraitA + TraitB" from `impl TraitA + TraitB`,
1646 // by looking up the projections associated with the def_id.
1647 let predicates_of = cx.tcx.explicit_predicates_of(def_id);
1648 let substs = cx.tcx.lift(&substs).expect("Opaque lift failed");
1649 let bounds = predicates_of.instantiate(cx.tcx, substs);
1650 let mut regions = vec![];
1651 let mut has_sized = false;
1652 let mut bounds = bounds
1655 .filter_map(|predicate| {
1656 let trait_ref = if let Some(tr) = predicate.to_opt_poly_trait_ref() {
1658 } else if let ty::PredicateKind::TypeOutlives(pred) = predicate.kind() {
1659 // these should turn up at the end
1660 if let Some(r) = pred.skip_binder().1.clean(cx) {
1661 regions.push(GenericBound::Outlives(r));
1668 if let Some(sized) = cx.tcx.lang_items().sized_trait() {
1669 if trait_ref.def_id() == sized {
1675 let bounds: Vec<_> = bounds
1678 .filter_map(|pred| {
1679 if let ty::PredicateKind::Projection(proj) = pred.kind() {
1680 let proj = proj.skip_binder();
1681 if proj.projection_ty.trait_ref(cx.tcx)
1682 == trait_ref.skip_binder()
1687 .associated_item(proj.projection_ty.item_def_id)
1691 kind: TypeBindingKind::Equality {
1692 ty: proj.ty.clean(cx),
1704 Some((trait_ref, &bounds[..]).clean(cx))
1706 .collect::<Vec<_>>();
1707 bounds.extend(regions);
1708 if !has_sized && !bounds.is_empty() {
1709 bounds.insert(0, GenericBound::maybe_sized(cx));
1714 ty::Closure(..) | ty::Generator(..) => Tuple(vec![]), // FIXME(pcwalton)
1716 ty::Bound(..) => panic!("Bound"),
1717 ty::Placeholder(..) => panic!("Placeholder"),
1718 ty::GeneratorWitness(..) => panic!("GeneratorWitness"),
1719 ty::Infer(..) => panic!("Infer"),
1720 ty::Error(_) => panic!("Error"),
1725 impl<'tcx> Clean<Constant> for ty::Const<'tcx> {
1726 fn clean(&self, cx: &DocContext<'_>) -> Constant {
1728 type_: self.ty.clean(cx),
1729 expr: format!("{}", self),
1736 impl Clean<Item> for hir::StructField<'_> {
1737 fn clean(&self, cx: &DocContext<'_>) -> Item {
1738 let local_did = cx.tcx.hir().local_def_id(self.hir_id);
1741 name: Some(self.ident.name).clean(cx),
1742 attrs: self.attrs.clean(cx),
1743 source: self.span.clean(cx),
1744 visibility: self.vis.clean(cx),
1745 stability: get_stability(cx, local_did.to_def_id()),
1746 deprecation: get_deprecation(cx, local_did.to_def_id()),
1747 def_id: local_did.to_def_id(),
1748 inner: StructFieldItem(self.ty.clean(cx)),
1753 impl Clean<Item> for ty::FieldDef {
1754 fn clean(&self, cx: &DocContext<'_>) -> Item {
1756 name: Some(self.ident.name).clean(cx),
1757 attrs: cx.tcx.get_attrs(self.did).clean(cx),
1758 source: cx.tcx.def_span(self.did).clean(cx),
1759 visibility: self.vis.clean(cx),
1760 stability: get_stability(cx, self.did),
1761 deprecation: get_deprecation(cx, self.did),
1763 inner: StructFieldItem(cx.tcx.type_of(self.did).clean(cx)),
1768 impl Clean<Visibility> for hir::Visibility<'_> {
1769 fn clean(&self, cx: &DocContext<'_>) -> Visibility {
1771 hir::VisibilityKind::Public => Visibility::Public,
1772 hir::VisibilityKind::Inherited => Visibility::Inherited,
1773 hir::VisibilityKind::Crate(_) => Visibility::Crate,
1774 hir::VisibilityKind::Restricted { ref path, .. } => {
1775 let path = path.clean(cx);
1776 let did = register_res(cx, path.res);
1777 Visibility::Restricted(did, path)
1783 impl Clean<Visibility> for ty::Visibility {
1784 fn clean(&self, _: &DocContext<'_>) -> Visibility {
1785 if *self == ty::Visibility::Public { Public } else { Inherited }
1789 impl Clean<Item> for doctree::Struct<'_> {
1790 fn clean(&self, cx: &DocContext<'_>) -> Item {
1792 name: Some(self.name.clean(cx)),
1793 attrs: self.attrs.clean(cx),
1794 source: self.whence.clean(cx),
1795 def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
1796 visibility: self.vis.clean(cx),
1797 stability: cx.stability(self.id).clean(cx),
1798 deprecation: cx.deprecation(self.id).clean(cx),
1799 inner: StructItem(Struct {
1800 struct_type: self.struct_type,
1801 generics: self.generics.clean(cx),
1802 fields: self.fields.clean(cx),
1803 fields_stripped: false,
1809 impl Clean<Item> for doctree::Union<'_> {
1810 fn clean(&self, cx: &DocContext<'_>) -> Item {
1812 name: Some(self.name.clean(cx)),
1813 attrs: self.attrs.clean(cx),
1814 source: self.whence.clean(cx),
1815 def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
1816 visibility: self.vis.clean(cx),
1817 stability: cx.stability(self.id).clean(cx),
1818 deprecation: cx.deprecation(self.id).clean(cx),
1819 inner: UnionItem(Union {
1820 struct_type: self.struct_type,
1821 generics: self.generics.clean(cx),
1822 fields: self.fields.clean(cx),
1823 fields_stripped: false,
1829 impl Clean<VariantStruct> for rustc_hir::VariantData<'_> {
1830 fn clean(&self, cx: &DocContext<'_>) -> VariantStruct {
1832 struct_type: doctree::struct_type_from_def(self),
1833 fields: self.fields().iter().map(|x| x.clean(cx)).collect(),
1834 fields_stripped: false,
1839 impl Clean<Item> for doctree::Enum<'_> {
1840 fn clean(&self, cx: &DocContext<'_>) -> Item {
1842 name: Some(self.name.clean(cx)),
1843 attrs: self.attrs.clean(cx),
1844 source: self.whence.clean(cx),
1845 def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
1846 visibility: self.vis.clean(cx),
1847 stability: cx.stability(self.id).clean(cx),
1848 deprecation: cx.deprecation(self.id).clean(cx),
1849 inner: EnumItem(Enum {
1850 variants: self.variants.iter().map(|v| v.clean(cx)).collect(),
1851 generics: self.generics.clean(cx),
1852 variants_stripped: false,
1858 impl Clean<Item> for doctree::Variant<'_> {
1859 fn clean(&self, cx: &DocContext<'_>) -> Item {
1861 name: Some(self.name.clean(cx)),
1862 attrs: self.attrs.clean(cx),
1863 source: self.whence.clean(cx),
1864 visibility: Inherited,
1865 stability: cx.stability(self.id).clean(cx),
1866 deprecation: cx.deprecation(self.id).clean(cx),
1867 def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
1868 inner: VariantItem(Variant { kind: self.def.clean(cx) }),
1873 impl Clean<Item> for ty::VariantDef {
1874 fn clean(&self, cx: &DocContext<'_>) -> Item {
1875 let kind = match self.ctor_kind {
1876 CtorKind::Const => VariantKind::CLike,
1877 CtorKind::Fn => VariantKind::Tuple(
1878 self.fields.iter().map(|f| cx.tcx.type_of(f.did).clean(cx)).collect(),
1880 CtorKind::Fictive => VariantKind::Struct(VariantStruct {
1881 struct_type: doctree::Plain,
1882 fields_stripped: false,
1887 source: cx.tcx.def_span(field.did).clean(cx),
1888 name: Some(field.ident.name.clean(cx)),
1889 attrs: cx.tcx.get_attrs(field.did).clean(cx),
1890 visibility: field.vis.clean(cx),
1892 stability: get_stability(cx, field.did),
1893 deprecation: get_deprecation(cx, field.did),
1894 inner: StructFieldItem(cx.tcx.type_of(field.did).clean(cx)),
1900 name: Some(self.ident.clean(cx)),
1901 attrs: inline::load_attrs(cx, self.def_id).clean(cx),
1902 source: cx.tcx.def_span(self.def_id).clean(cx),
1903 visibility: Inherited,
1904 def_id: self.def_id,
1905 inner: VariantItem(Variant { kind }),
1906 stability: get_stability(cx, self.def_id),
1907 deprecation: get_deprecation(cx, self.def_id),
1912 impl Clean<VariantKind> for hir::VariantData<'_> {
1913 fn clean(&self, cx: &DocContext<'_>) -> VariantKind {
1915 hir::VariantData::Struct(..) => VariantKind::Struct(self.clean(cx)),
1916 hir::VariantData::Tuple(..) => {
1917 VariantKind::Tuple(self.fields().iter().map(|x| x.ty.clean(cx)).collect())
1919 hir::VariantData::Unit(..) => VariantKind::CLike,
1924 impl Clean<Span> for rustc_span::Span {
1925 fn clean(&self, cx: &DocContext<'_>) -> Span {
1926 if self.is_dummy() {
1927 return Span::empty();
1930 let sm = cx.sess().source_map();
1931 let filename = sm.span_to_filename(*self);
1932 let lo = sm.lookup_char_pos(self.lo());
1933 let hi = sm.lookup_char_pos(self.hi());
1938 locol: lo.col.to_usize(),
1940 hicol: hi.col.to_usize(),
1946 impl Clean<Path> for hir::Path<'_> {
1947 fn clean(&self, cx: &DocContext<'_>) -> Path {
1949 global: self.is_global(),
1951 segments: if self.is_global() { &self.segments[1..] } else { &self.segments }.clean(cx),
1956 impl Clean<GenericArgs> for hir::GenericArgs<'_> {
1957 fn clean(&self, cx: &DocContext<'_>) -> GenericArgs {
1958 if self.parenthesized {
1959 let output = self.bindings[0].ty().clean(cx);
1960 GenericArgs::Parenthesized {
1961 inputs: self.inputs().clean(cx),
1962 output: if output != Type::Tuple(Vec::new()) { Some(output) } else { None },
1965 let elide_lifetimes = self.args.iter().all(|arg| match arg {
1966 hir::GenericArg::Lifetime(lt) => lt.is_elided(),
1969 GenericArgs::AngleBracketed {
1973 .filter_map(|arg| match arg {
1974 hir::GenericArg::Lifetime(lt) if !elide_lifetimes => {
1975 Some(GenericArg::Lifetime(lt.clean(cx)))
1977 hir::GenericArg::Lifetime(_) => None,
1978 hir::GenericArg::Type(ty) => Some(GenericArg::Type(ty.clean(cx))),
1979 hir::GenericArg::Const(ct) => Some(GenericArg::Const(ct.clean(cx))),
1982 bindings: self.bindings.clean(cx),
1988 impl Clean<PathSegment> for hir::PathSegment<'_> {
1989 fn clean(&self, cx: &DocContext<'_>) -> PathSegment {
1990 PathSegment { name: self.ident.name.clean(cx), args: self.generic_args().clean(cx) }
1994 impl Clean<String> for Ident {
1996 fn clean(&self, cx: &DocContext<'_>) -> String {
2001 impl Clean<String> for Symbol {
2003 fn clean(&self, _: &DocContext<'_>) -> String {
2008 impl Clean<Item> for doctree::Typedef<'_> {
2009 fn clean(&self, cx: &DocContext<'_>) -> Item {
2010 let type_ = self.ty.clean(cx);
2011 let item_type = type_.def_id().and_then(|did| inline::build_ty(cx, did));
2013 name: Some(self.name.clean(cx)),
2014 attrs: self.attrs.clean(cx),
2015 source: self.whence.clean(cx),
2016 def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
2017 visibility: self.vis.clean(cx),
2018 stability: cx.stability(self.id).clean(cx),
2019 deprecation: cx.deprecation(self.id).clean(cx),
2020 inner: TypedefItem(Typedef { type_, generics: self.gen.clean(cx), item_type }, false),
2025 impl Clean<Item> for doctree::OpaqueTy<'_> {
2026 fn clean(&self, cx: &DocContext<'_>) -> Item {
2028 name: Some(self.name.clean(cx)),
2029 attrs: self.attrs.clean(cx),
2030 source: self.whence.clean(cx),
2031 def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
2032 visibility: self.vis.clean(cx),
2033 stability: cx.stability(self.id).clean(cx),
2034 deprecation: cx.deprecation(self.id).clean(cx),
2035 inner: OpaqueTyItem(
2037 bounds: self.opaque_ty.bounds.clean(cx),
2038 generics: self.opaque_ty.generics.clean(cx),
2046 impl Clean<BareFunctionDecl> for hir::BareFnTy<'_> {
2047 fn clean(&self, cx: &DocContext<'_>) -> BareFunctionDecl {
2048 let (generic_params, decl) = enter_impl_trait(cx, || {
2049 (self.generic_params.clean(cx), (&*self.decl, &self.param_names[..]).clean(cx))
2051 BareFunctionDecl { unsafety: self.unsafety, abi: self.abi, decl, generic_params }
2055 impl Clean<Item> for doctree::Static<'_> {
2056 fn clean(&self, cx: &DocContext<'_>) -> Item {
2057 debug!("cleaning static {}: {:?}", self.name.clean(cx), self);
2059 name: Some(self.name.clean(cx)),
2060 attrs: self.attrs.clean(cx),
2061 source: self.whence.clean(cx),
2062 def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
2063 visibility: self.vis.clean(cx),
2064 stability: cx.stability(self.id).clean(cx),
2065 deprecation: cx.deprecation(self.id).clean(cx),
2066 inner: StaticItem(Static {
2067 type_: self.type_.clean(cx),
2068 mutability: self.mutability,
2069 expr: print_const_expr(cx, self.expr),
2075 impl Clean<Item> for doctree::Constant<'_> {
2076 fn clean(&self, cx: &DocContext<'_>) -> Item {
2077 let def_id = cx.tcx.hir().local_def_id(self.id);
2080 name: Some(self.name.clean(cx)),
2081 attrs: self.attrs.clean(cx),
2082 source: self.whence.clean(cx),
2083 def_id: def_id.to_def_id(),
2084 visibility: self.vis.clean(cx),
2085 stability: cx.stability(self.id).clean(cx),
2086 deprecation: cx.deprecation(self.id).clean(cx),
2087 inner: ConstantItem(Constant {
2088 type_: self.type_.clean(cx),
2089 expr: print_const_expr(cx, self.expr),
2090 value: print_evaluated_const(cx, def_id.to_def_id()),
2091 is_literal: is_literal_expr(cx, self.expr.hir_id),
2097 impl Clean<ImplPolarity> for ty::ImplPolarity {
2098 fn clean(&self, _: &DocContext<'_>) -> ImplPolarity {
2100 &ty::ImplPolarity::Positive |
2101 // FIXME: do we want to do something else here?
2102 &ty::ImplPolarity::Reservation => ImplPolarity::Positive,
2103 &ty::ImplPolarity::Negative => ImplPolarity::Negative,
2108 impl Clean<Vec<Item>> for doctree::Impl<'_> {
2109 fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
2110 let mut ret = Vec::new();
2111 let trait_ = self.trait_.clean(cx);
2112 let items = self.items.iter().map(|ii| ii.clean(cx)).collect::<Vec<_>>();
2113 let def_id = cx.tcx.hir().local_def_id(self.id);
2115 // If this impl block is an implementation of the Deref trait, then we
2116 // need to try inlining the target's inherent impl blocks as well.
2117 if trait_.def_id() == cx.tcx.lang_items().deref_trait() {
2118 build_deref_target_impls(cx, &items, &mut ret);
2121 let provided: FxHashSet<String> = trait_
2124 cx.tcx.provided_trait_methods(did).map(|meth| meth.ident.to_string()).collect()
2126 .unwrap_or_default();
2128 let for_ = self.for_.clean(cx);
2129 let type_alias = for_.def_id().and_then(|did| match cx.tcx.def_kind(did) {
2130 DefKind::TyAlias => Some(cx.tcx.type_of(did).clean(cx)),
2133 let make_item = |trait_: Option<Type>, for_: Type, items: Vec<Item>| Item {
2135 attrs: self.attrs.clean(cx),
2136 source: self.whence.clean(cx),
2137 def_id: def_id.to_def_id(),
2138 visibility: self.vis.clean(cx),
2139 stability: cx.stability(self.id).clean(cx),
2140 deprecation: cx.deprecation(self.id).clean(cx),
2141 inner: ImplItem(Impl {
2142 unsafety: self.unsafety,
2143 generics: self.generics.clean(cx),
2144 provided_trait_methods: provided.clone(),
2148 polarity: Some(cx.tcx.impl_polarity(def_id).clean(cx)),
2153 if let Some(type_alias) = type_alias {
2154 ret.push(make_item(trait_.clone(), type_alias, items.clone()));
2156 ret.push(make_item(trait_, for_, items));
2161 impl Clean<Vec<Item>> for doctree::ExternCrate<'_> {
2162 fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
2163 let please_inline = self.vis.node.is_pub()
2164 && self.attrs.iter().any(|a| {
2165 a.check_name(sym::doc)
2166 && match a.meta_item_list() {
2167 Some(l) => attr::list_contains_name(&l, sym::inline),
2173 let mut visited = FxHashSet::default();
2175 let res = Res::Def(DefKind::Mod, DefId { krate: self.cnum, index: CRATE_DEF_INDEX });
2177 if let Some(items) =
2178 inline::try_inline(cx, res, self.name, Some(self.attrs), &mut visited)
2186 attrs: self.attrs.clean(cx),
2187 source: self.whence.clean(cx),
2188 def_id: DefId { krate: self.cnum, index: CRATE_DEF_INDEX },
2189 visibility: self.vis.clean(cx),
2192 inner: ExternCrateItem(self.name.clean(cx), self.path.clone()),
2197 impl Clean<Vec<Item>> for doctree::Import<'_> {
2198 fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
2199 // We consider inlining the documentation of `pub use` statements, but we
2200 // forcefully don't inline if this is not public or if the
2201 // #[doc(no_inline)] attribute is present.
2202 // Don't inline doc(hidden) imports so they can be stripped at a later stage.
2203 let mut denied = !self.vis.node.is_pub()
2204 || self.attrs.iter().any(|a| {
2205 a.check_name(sym::doc)
2206 && match a.meta_item_list() {
2208 attr::list_contains_name(&l, sym::no_inline)
2209 || attr::list_contains_name(&l, sym::hidden)
2214 // Also check whether imports were asked to be inlined, in case we're trying to re-export a
2215 // crate in Rust 2018+
2216 let please_inline = self.attrs.lists(sym::doc).has_word(sym::inline);
2217 let path = self.path.clean(cx);
2218 let inner = if self.glob {
2220 let mut visited = FxHashSet::default();
2221 if let Some(items) = inline::try_inline_glob(cx, path.res, &mut visited) {
2226 Import::Glob(resolve_use_source(cx, path))
2228 let name = self.name;
2230 if let Res::Def(DefKind::Mod, did) = path.res {
2231 if !did.is_local() && did.index == CRATE_DEF_INDEX {
2232 // if we're `pub use`ing an extern crate root, don't inline it unless we
2233 // were specifically asked for it
2239 let mut visited = FxHashSet::default();
2240 if let Some(items) =
2241 inline::try_inline(cx, path.res, name, Some(self.attrs), &mut visited)
2246 Import::Simple(name.clean(cx), resolve_use_source(cx, path))
2251 attrs: self.attrs.clean(cx),
2252 source: self.whence.clean(cx),
2253 def_id: DefId::local(CRATE_DEF_INDEX),
2254 visibility: self.vis.clean(cx),
2257 inner: ImportItem(inner),
2262 impl Clean<Item> for doctree::ForeignItem<'_> {
2263 fn clean(&self, cx: &DocContext<'_>) -> Item {
2264 let inner = match self.kind {
2265 hir::ForeignItemKind::Fn(ref decl, ref names, ref generics) => {
2266 let abi = cx.tcx.hir().get_foreign_abi(self.id);
2267 let (generics, decl) =
2268 enter_impl_trait(cx, || (generics.clean(cx), (&**decl, &names[..]).clean(cx)));
2269 let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
2270 ForeignFunctionItem(Function {
2273 header: hir::FnHeader {
2274 unsafety: hir::Unsafety::Unsafe,
2276 constness: hir::Constness::NotConst,
2277 asyncness: hir::IsAsync::NotAsync,
2283 hir::ForeignItemKind::Static(ref ty, mutbl) => ForeignStaticItem(Static {
2284 type_: ty.clean(cx),
2286 expr: String::new(),
2288 hir::ForeignItemKind::Type => ForeignTypeItem,
2292 name: Some(self.name.clean(cx)),
2293 attrs: self.attrs.clean(cx),
2294 source: self.whence.clean(cx),
2295 def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
2296 visibility: self.vis.clean(cx),
2297 stability: cx.stability(self.id).clean(cx),
2298 deprecation: cx.deprecation(self.id).clean(cx),
2304 impl Clean<Item> for doctree::Macro<'_> {
2305 fn clean(&self, cx: &DocContext<'_>) -> Item {
2306 let name = self.name.clean(cx);
2308 name: Some(name.clone()),
2309 attrs: self.attrs.clean(cx),
2310 source: self.whence.clean(cx),
2312 stability: cx.stability(self.hid).clean(cx),
2313 deprecation: cx.deprecation(self.hid).clean(cx),
2314 def_id: self.def_id,
2315 inner: MacroItem(Macro {
2317 "macro_rules! {} {{\n{}}}",
2321 .map(|span| { format!(" {} => {{ ... }};\n", span.to_src(cx)) })
2322 .collect::<String>()
2324 imported_from: self.imported_from.clean(cx),
2330 impl Clean<Item> for doctree::ProcMacro<'_> {
2331 fn clean(&self, cx: &DocContext<'_>) -> Item {
2333 name: Some(self.name.clean(cx)),
2334 attrs: self.attrs.clean(cx),
2335 source: self.whence.clean(cx),
2337 stability: cx.stability(self.id).clean(cx),
2338 deprecation: cx.deprecation(self.id).clean(cx),
2339 def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
2340 inner: ProcMacroItem(ProcMacro { kind: self.kind, helpers: self.helpers.clean(cx) }),
2345 impl Clean<Stability> for attr::Stability {
2346 fn clean(&self, _: &DocContext<'_>) -> Stability {
2348 level: stability::StabilityLevel::from_attr_level(&self.level),
2349 feature: Some(self.feature.to_string()).filter(|f| !f.is_empty()),
2350 since: match self.level {
2351 attr::Stable { ref since } => since.to_string(),
2354 deprecation: self.rustc_depr.as_ref().map(|d| Deprecation {
2355 note: Some(d.reason.to_string()).filter(|r| !r.is_empty()),
2356 since: Some(d.since.to_string()).filter(|d| !d.is_empty()),
2358 unstable_reason: match self.level {
2359 attr::Unstable { reason: Some(ref reason), .. } => Some(reason.to_string()),
2362 issue: match self.level {
2363 attr::Unstable { issue, .. } => issue,
2370 impl Clean<Deprecation> for attr::Deprecation {
2371 fn clean(&self, _: &DocContext<'_>) -> Deprecation {
2373 since: self.since.map(|s| s.to_string()).filter(|s| !s.is_empty()),
2374 note: self.note.map(|n| n.to_string()).filter(|n| !n.is_empty()),
2379 impl Clean<TypeBinding> for hir::TypeBinding<'_> {
2380 fn clean(&self, cx: &DocContext<'_>) -> TypeBinding {
2381 TypeBinding { name: self.ident.name.clean(cx), kind: self.kind.clean(cx) }
2385 impl Clean<TypeBindingKind> for hir::TypeBindingKind<'_> {
2386 fn clean(&self, cx: &DocContext<'_>) -> TypeBindingKind {
2388 hir::TypeBindingKind::Equality { ref ty } => {
2389 TypeBindingKind::Equality { ty: ty.clean(cx) }
2391 hir::TypeBindingKind::Constraint { ref bounds } => {
2392 TypeBindingKind::Constraint { bounds: bounds.iter().map(|b| b.clean(cx)).collect() }
2399 TraitBound(Vec<PathSegment>, Vec<SimpleBound>, Vec<GenericParamDef>, hir::TraitBoundModifier),
2403 impl From<GenericBound> for SimpleBound {
2404 fn from(bound: GenericBound) -> Self {
2405 match bound.clone() {
2406 GenericBound::Outlives(l) => SimpleBound::Outlives(l),
2407 GenericBound::TraitBound(t, mod_) => match t.trait_ {
2408 Type::ResolvedPath { path, param_names, .. } => SimpleBound::TraitBound(
2410 param_names.map_or_else(Vec::new, |v| {
2411 v.iter().map(|p| SimpleBound::from(p.clone())).collect()
2416 _ => panic!("Unexpected bound {:?}", bound),