1 //! Support for inlining external documentation into the current AST.
9 use rustc_data_structures::fx::FxHashSet;
11 use rustc_hir::def::{DefKind, Res};
12 use rustc_hir::def_id::DefId;
13 use rustc_hir::Mutability;
14 use rustc_metadata::creader::{CStore, LoadedMacro};
15 use rustc_middle::ty::{self, TyCtxt};
16 use rustc_span::hygiene::MacroKind;
17 use rustc_span::symbol::{kw, sym, Symbol};
20 self, clean_fn_decl_from_did_and_sig, clean_generics, clean_impl_item, clean_middle_assoc_item,
21 clean_middle_field, clean_middle_ty, clean_trait_ref_with_bindings, clean_ty,
22 clean_ty_generics, clean_variant_def, utils, Attributes, AttributesExt, ImplKind, ItemId, Type,
24 use crate::core::DocContext;
25 use crate::formats::item_type::ItemType;
27 /// Attempt to inline a definition into this AST.
29 /// This function will fetch the definition specified, and if it is
30 /// from another crate it will attempt to inline the documentation
31 /// from the other crate into this crate.
33 /// This is primarily used for `pub use` statements which are, in general,
34 /// implementation details. Inlining the documentation should help provide a
35 /// better experience when reading the documentation in this use case.
37 /// The returned value is `None` if the definition could not be inlined,
38 /// and `Some` of a vector of items if it was successfully expanded.
40 /// `parent_module` refers to the parent of the *re-export*, not the original item.
41 pub(crate) fn try_inline(
42 cx: &mut DocContext<'_>,
44 import_def_id: Option<DefId>,
47 attrs: Option<&[ast::Attribute]>,
48 visited: &mut FxHashSet<DefId>,
49 ) -> Option<Vec<clean::Item>> {
50 let did = res.opt_def_id()?;
54 let mut ret = Vec::new();
56 debug!("attrs={:?}", attrs);
58 let attrs_without_docs = attrs.map(|attrs| {
59 attrs.into_iter().filter(|a| a.doc_str().is_none()).cloned().collect::<Vec<_>>()
61 // We need this ugly code because:
64 // attrs_without_docs.map(|a| a.as_slice())
67 // will fail because it returns a temporary slice and:
70 // attrs_without_docs.map(|s| {
71 // vec = s.as_slice();
76 // will fail because we're moving an uninitialized variable into a closure.
78 let attrs_without_docs = match attrs_without_docs {
86 let kind = match res {
87 Res::Def(DefKind::Trait, did) => {
88 record_extern_fqn(cx, did, ItemType::Trait);
89 build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret);
90 clean::TraitItem(Box::new(build_external_trait(cx, did)))
92 Res::Def(DefKind::Fn, did) => {
93 record_extern_fqn(cx, did, ItemType::Function);
94 clean::FunctionItem(build_external_function(cx, did))
96 Res::Def(DefKind::Struct, did) => {
97 record_extern_fqn(cx, did, ItemType::Struct);
98 build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret);
99 clean::StructItem(build_struct(cx, did))
101 Res::Def(DefKind::Union, did) => {
102 record_extern_fqn(cx, did, ItemType::Union);
103 build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret);
104 clean::UnionItem(build_union(cx, did))
106 Res::Def(DefKind::TyAlias, did) => {
107 record_extern_fqn(cx, did, ItemType::Typedef);
108 build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret);
109 clean::TypedefItem(build_type_alias(cx, did))
111 Res::Def(DefKind::Enum, did) => {
112 record_extern_fqn(cx, did, ItemType::Enum);
113 build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret);
114 clean::EnumItem(build_enum(cx, did))
116 Res::Def(DefKind::ForeignTy, did) => {
117 record_extern_fqn(cx, did, ItemType::ForeignType);
118 build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret);
119 clean::ForeignTypeItem
121 // Never inline enum variants but leave them shown as re-exports.
122 Res::Def(DefKind::Variant, _) => return None,
123 // Assume that enum variants and struct types are re-exported next to
124 // their constructors.
125 Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) => return Some(Vec::new()),
126 Res::Def(DefKind::Mod, did) => {
127 record_extern_fqn(cx, did, ItemType::Module);
128 clean::ModuleItem(build_module(cx, did, visited))
130 Res::Def(DefKind::Static(_), did) => {
131 record_extern_fqn(cx, did, ItemType::Static);
132 clean::StaticItem(build_static(cx, did, cx.tcx.is_mutable_static(did)))
134 Res::Def(DefKind::Const, did) => {
135 record_extern_fqn(cx, did, ItemType::Constant);
136 clean::ConstantItem(build_const(cx, did))
138 Res::Def(DefKind::Macro(kind), did) => {
139 let mac = build_macro(cx, did, name, import_def_id);
141 let type_kind = match kind {
142 MacroKind::Bang => ItemType::Macro,
143 MacroKind::Attr => ItemType::ProcAttribute,
144 MacroKind::Derive => ItemType::ProcDerive,
146 record_extern_fqn(cx, did, type_kind);
152 let (attrs, cfg) = merge_attrs(cx, Some(parent_module), load_attrs(cx, did), attrs);
153 cx.inlined.insert(did.into());
155 clean::Item::from_def_id_and_attrs_and_parts(did, Some(name), kind, Box::new(attrs), cfg);
156 // The visibility needs to reflect the one from the reexport and not from the "source" DefId.
157 item.inline_stmt_id = import_def_id;
162 pub(crate) fn try_inline_glob(
163 cx: &mut DocContext<'_>,
165 visited: &mut FxHashSet<DefId>,
166 inlined_names: &mut FxHashSet<(ItemType, Symbol)>,
167 ) -> Option<Vec<clean::Item>> {
168 let did = res.opt_def_id()?;
174 Res::Def(DefKind::Mod, did) => {
175 let mut items = build_module_items(cx, did, visited, inlined_names);
176 items.drain_filter(|item| {
177 if let Some(name) = item.name {
178 // If an item with the same type and name already exists,
179 // it takes priority over the inlined stuff.
180 !inlined_names.insert((item.type_(), name))
187 // glob imports on things like enums aren't inlined even for local exports, so just bail
192 pub(crate) fn load_attrs<'hir>(cx: &DocContext<'hir>, did: DefId) -> &'hir [ast::Attribute] {
193 cx.tcx.get_attrs_unchecked(did)
196 /// Record an external fully qualified name in the external_paths cache.
198 /// These names are used later on by HTML rendering to generate things like
199 /// source links back to the original item.
200 pub(crate) fn record_extern_fqn(cx: &mut DocContext<'_>, did: DefId, kind: ItemType) {
201 let crate_name = cx.tcx.crate_name(did.krate);
204 cx.tcx.def_path(did).data.into_iter().filter_map(|elem| elem.data.get_opt_name());
205 let fqn = if let ItemType::Macro = kind {
206 // Check to see if it is a macro 2.0 or built-in macro
208 CStore::from_tcx(cx.tcx).load_macro_untracked(did, cx.sess()),
209 LoadedMacro::MacroDef(def, _)
210 if matches!(&def.kind, ast::ItemKind::MacroDef(ast_def)
211 if !ast_def.macro_rules)
213 once(crate_name).chain(relative).collect()
215 vec![crate_name, relative.last().expect("relative was empty")]
218 once(crate_name).chain(relative).collect()
222 cx.cache.exact_paths.insert(did, fqn);
224 cx.cache.external_paths.insert(did, (fqn, kind));
228 pub(crate) fn build_external_trait(cx: &mut DocContext<'_>, did: DefId) -> clean::Trait {
231 .associated_items(did)
232 .in_definition_order()
233 .map(|item| clean_middle_assoc_item(item, cx))
236 let predicates = cx.tcx.predicates_of(did);
237 let generics = clean_ty_generics(cx, cx.tcx.generics_of(did), predicates);
238 let generics = filter_non_trait_generics(did, generics);
239 let (generics, supertrait_bounds) = separate_supertrait_bounds(generics);
240 clean::Trait { def_id: did, generics, items: trait_items, bounds: supertrait_bounds }
243 fn build_external_function<'tcx>(cx: &mut DocContext<'tcx>, did: DefId) -> Box<clean::Function> {
244 let sig = cx.tcx.fn_sig(did);
246 let predicates = cx.tcx.predicates_of(did);
247 let (generics, decl) = clean::enter_impl_trait(cx, |cx| {
248 // NOTE: generics need to be cleaned before the decl!
249 let generics = clean_ty_generics(cx, cx.tcx.generics_of(did), predicates);
250 let decl = clean_fn_decl_from_did_and_sig(cx, Some(did), sig);
253 Box::new(clean::Function { decl, generics })
256 fn build_enum(cx: &mut DocContext<'_>, did: DefId) -> clean::Enum {
257 let predicates = cx.tcx.explicit_predicates_of(did);
260 generics: clean_ty_generics(cx, cx.tcx.generics_of(did), predicates),
261 variants: cx.tcx.adt_def(did).variants().iter().map(|v| clean_variant_def(v, cx)).collect(),
265 fn build_struct(cx: &mut DocContext<'_>, did: DefId) -> clean::Struct {
266 let predicates = cx.tcx.explicit_predicates_of(did);
267 let variant = cx.tcx.adt_def(did).non_enum_variant();
270 struct_type: variant.ctor_kind,
271 generics: clean_ty_generics(cx, cx.tcx.generics_of(did), predicates),
272 fields: variant.fields.iter().map(|x| clean_middle_field(x, cx)).collect(),
276 fn build_union(cx: &mut DocContext<'_>, did: DefId) -> clean::Union {
277 let predicates = cx.tcx.explicit_predicates_of(did);
278 let variant = cx.tcx.adt_def(did).non_enum_variant();
280 let generics = clean_ty_generics(cx, cx.tcx.generics_of(did), predicates);
281 let fields = variant.fields.iter().map(|x| clean_middle_field(x, cx)).collect();
282 clean::Union { generics, fields }
285 fn build_type_alias(cx: &mut DocContext<'_>, did: DefId) -> Box<clean::Typedef> {
286 let predicates = cx.tcx.explicit_predicates_of(did);
287 let type_ = clean_middle_ty(cx.tcx.type_of(did), cx, Some(did));
289 Box::new(clean::Typedef {
291 generics: clean_ty_generics(cx, cx.tcx.generics_of(did), predicates),
296 /// Builds all inherent implementations of an ADT (struct/union/enum) or Trait item/path/reexport.
297 pub(crate) fn build_impls(
298 cx: &mut DocContext<'_>,
299 parent_module: Option<DefId>,
301 attrs: Option<&[ast::Attribute]>,
302 ret: &mut Vec<clean::Item>,
304 let _prof_timer = cx.tcx.sess.prof.generic_activity("build_inherent_impls");
307 // for each implementation of an item represented by `did`, build the clean::Item for that impl
308 for &did in tcx.inherent_impls(did).iter() {
309 build_impl(cx, parent_module, did, attrs, ret);
312 // This pretty much exists expressly for `dyn Error` traits that exist in the `alloc` crate.
315 // * https://github.com/rust-lang/rust/issues/103170 — where it didn't used to get documented
316 // * https://github.com/rust-lang/rust/pull/99917 — where the feature got used
317 // * https://github.com/rust-lang/rust/issues/53487 — overall tracking issue for Error
318 if tcx.has_attr(did, sym::rustc_has_incoherent_inherent_impls) {
319 use rustc_middle::ty::fast_reject::SimplifiedTypeGen::*;
321 if tcx.is_trait(did) { TraitSimplifiedType(did) } else { AdtSimplifiedType(did) };
322 for &did in tcx.incoherent_impls(type_) {
323 build_impl(cx, parent_module, did, attrs, ret);
328 /// `parent_module` refers to the parent of the re-export, not the original item
329 pub(crate) fn merge_attrs(
330 cx: &mut DocContext<'_>,
331 parent_module: Option<DefId>,
332 old_attrs: &[ast::Attribute],
333 new_attrs: Option<&[ast::Attribute]>,
334 ) -> (clean::Attributes, Option<Arc<clean::cfg::Cfg>>) {
335 // NOTE: If we have additional attributes (from a re-export),
336 // always insert them first. This ensure that re-export
337 // doc comments show up before the original doc comments
338 // when we render them.
339 if let Some(inner) = new_attrs {
340 let mut both = inner.to_vec();
341 both.extend_from_slice(old_attrs);
343 if let Some(new_id) = parent_module {
344 Attributes::from_ast_with_additional(old_attrs, (inner, new_id))
346 Attributes::from_ast(&both)
348 both.cfg(cx.tcx, &cx.cache.hidden_cfg),
351 (Attributes::from_ast(&old_attrs), old_attrs.cfg(cx.tcx, &cx.cache.hidden_cfg))
355 /// Inline an `impl`, inherent or of a trait. The `did` must be for an `impl`.
356 pub(crate) fn build_impl(
357 cx: &mut DocContext<'_>,
358 parent_module: Option<DefId>,
360 attrs: Option<&[ast::Attribute]>,
361 ret: &mut Vec<clean::Item>,
363 if !cx.inlined.insert(did.into()) {
367 let _prof_timer = cx.tcx.sess.prof.generic_activity("build_impl");
370 let associated_trait = tcx.impl_trait_ref(did);
372 // Only inline impl if the implemented trait is
373 // reachable in rustdoc generated documentation
375 if let Some(traitref) = associated_trait {
376 let did = traitref.def_id;
377 if !cx.cache.effective_visibilities.is_directly_public(tcx, did) {
381 if let Some(stab) = tcx.lookup_stability(did) {
382 if stab.is_unstable() && stab.feature == sym::rustc_private {
389 let impl_item = match did.as_local() {
390 Some(did) => match &tcx.hir().expect_item(did).kind {
391 hir::ItemKind::Impl(impl_) => Some(impl_),
392 _ => panic!("`DefID` passed to `build_impl` is not an `impl"),
397 let for_ = match &impl_item {
398 Some(impl_) => clean_ty(impl_.self_ty, cx),
399 None => clean_middle_ty(tcx.type_of(did), cx, Some(did)),
402 // Only inline impl if the implementing type is
403 // reachable in rustdoc generated documentation
405 if let Some(did) = for_.def_id(&cx.cache) {
406 if !cx.cache.effective_visibilities.is_directly_public(tcx, did) {
410 if let Some(stab) = tcx.lookup_stability(did) {
411 if stab.is_unstable() && stab.feature == sym::rustc_private {
418 let document_hidden = cx.render_options.document_hidden;
419 let predicates = tcx.explicit_predicates_of(did);
420 let (trait_items, generics) = match impl_item {
425 .map(|item| tcx.hir().impl_item(item.id))
427 // Filter out impl items whose corresponding trait item has `doc(hidden)`
428 // not to document such impl items.
429 // For inherent impls, we don't do any filtering, because that's already done in strip_hidden.rs.
431 // When `--document-hidden-items` is passed, we don't
432 // do any filtering, too.
436 if let Some(associated_trait) = associated_trait {
437 let assoc_kind = match item.kind {
438 hir::ImplItemKind::Const(..) => ty::AssocKind::Const,
439 hir::ImplItemKind::Fn(..) => ty::AssocKind::Fn,
440 hir::ImplItemKind::Type(..) => ty::AssocKind::Type,
443 .associated_items(associated_trait.def_id)
444 .find_by_name_and_kind(
448 associated_trait.def_id,
450 .unwrap(); // SAFETY: For all impl items there exists trait item that has the same name.
451 !tcx.is_doc_hidden(trait_item.def_id)
456 .map(|item| clean_impl_item(item, cx))
457 .collect::<Vec<_>>(),
458 clean_generics(impl_.generics, cx),
461 tcx.associated_items(did)
462 .in_definition_order()
464 // If this is a trait impl, filter out associated items whose corresponding item
465 // in the associated trait is marked `doc(hidden)`.
466 // If this is an inherent impl, filter out private associated items.
467 if let Some(associated_trait) = associated_trait {
469 .associated_items(associated_trait.def_id)
470 .find_by_name_and_kind(
474 associated_trait.def_id,
476 .unwrap(); // corresponding associated item has to exist
477 !tcx.is_doc_hidden(trait_item.def_id)
479 item.visibility(tcx).is_public()
482 .map(|item| clean_middle_assoc_item(item, cx))
483 .collect::<Vec<_>>(),
484 clean::enter_impl_trait(cx, |cx| {
485 clean_ty_generics(cx, tcx.generics_of(did), predicates)
489 let polarity = tcx.impl_polarity(did);
490 let trait_ = associated_trait.map(|t| clean_trait_ref_with_bindings(cx, t, ThinVec::new()));
491 if trait_.as_ref().map(|t| t.def_id()) == tcx.lang_items().deref_trait() {
492 super::build_deref_target_impls(cx, &trait_items, ret);
495 // Return if the trait itself or any types of the generic parameters are doc(hidden).
496 let mut stack: Vec<&Type> = vec![&for_];
498 if let Some(did) = trait_.as_ref().map(|t| t.def_id()) {
499 if tcx.is_doc_hidden(did) {
503 if let Some(generics) = trait_.as_ref().and_then(|t| t.generics()) {
504 stack.extend(generics);
507 while let Some(ty) = stack.pop() {
508 if let Some(did) = ty.def_id(&cx.cache) {
509 if tcx.is_doc_hidden(did) {
513 if let Some(generics) = ty.generics() {
514 stack.extend(generics);
518 if let Some(did) = trait_.as_ref().map(|t| t.def_id()) {
519 record_extern_trait(cx, did);
522 let (merged_attrs, cfg) = merge_attrs(cx, parent_module, load_attrs(cx, did), attrs);
523 trace!("merged_attrs={:?}", merged_attrs);
526 "build_impl: impl {:?} for {:?}",
527 trait_.as_ref().map(|t| t.def_id()),
528 for_.def_id(&cx.cache)
530 ret.push(clean::Item::from_def_id_and_attrs_and_parts(
533 clean::ImplItem(Box::new(clean::Impl {
534 unsafety: hir::Unsafety::Normal,
540 kind: if utils::has_doc_flag(tcx, did, sym::fake_variadic) {
541 ImplKind::FakeVaradic
546 Box::new(merged_attrs),
552 cx: &mut DocContext<'_>,
554 visited: &mut FxHashSet<DefId>,
556 let items = build_module_items(cx, did, visited, &mut FxHashSet::default());
558 let span = clean::Span::new(cx.tcx.def_span(did));
559 clean::Module { items, span }
562 fn build_module_items(
563 cx: &mut DocContext<'_>,
565 visited: &mut FxHashSet<DefId>,
566 inlined_names: &mut FxHashSet<(ItemType, Symbol)>,
567 ) -> Vec<clean::Item> {
568 let mut items = Vec::new();
570 // If we're re-exporting a re-export it may actually re-export something in
571 // two namespaces, so the target may be listed twice. Make sure we only
572 // visit each node at most once.
573 for &item in cx.tcx.module_children(did).iter() {
574 if item.vis.is_public() {
575 let res = item.res.expect_non_local();
576 if let Some(def_id) = res.mod_def_id() {
577 // If we're inlining a glob import, it's possible to have
578 // two distinct modules with the same name. We don't want to
579 // inline it, or mark any of its contents as visited.
581 || inlined_names.contains(&(ItemType::Module, item.ident.name))
582 || !visited.insert(def_id)
587 if let Res::PrimTy(p) = res {
588 // Primitive types can't be inlined so generate an import instead.
589 let prim_ty = clean::PrimitiveType::from(p);
590 items.push(clean::Item {
592 attrs: Box::new(clean::Attributes::default()),
593 item_id: ItemId::Primitive(prim_ty, did.krate),
594 kind: Box::new(clean::ImportItem(clean::Import::new_simple(
596 clean::ImportSource {
599 segments: vec![clean::PathSegment {
600 name: prim_ty.as_sym(),
601 args: clean::GenericArgs::AngleBracketed {
602 args: Default::default(),
603 bindings: ThinVec::new(),
612 inline_stmt_id: None,
614 } else if let Some(i) = try_inline(cx, did, None, res, item.ident.name, None, visited) {
623 pub(crate) fn print_inlined_const(tcx: TyCtxt<'_>, did: DefId) -> String {
624 if let Some(did) = did.as_local() {
625 let hir_id = tcx.hir().local_def_id_to_hir_id(did);
626 rustc_hir_pretty::id_to_string(&tcx.hir(), hir_id)
628 tcx.rendered_const(did).clone()
632 fn build_const(cx: &mut DocContext<'_>, def_id: DefId) -> clean::Constant {
634 type_: clean_middle_ty(cx.tcx.type_of(def_id), cx, Some(def_id)),
635 kind: clean::ConstantKind::Extern { def_id },
639 fn build_static(cx: &mut DocContext<'_>, did: DefId, mutable: bool) -> clean::Static {
641 type_: clean_middle_ty(cx.tcx.type_of(did), cx, Some(did)),
642 mutability: if mutable { Mutability::Mut } else { Mutability::Not },
648 cx: &mut DocContext<'_>,
651 import_def_id: Option<DefId>,
652 ) -> clean::ItemKind {
653 match CStore::from_tcx(cx.tcx).load_macro_untracked(def_id, cx.sess()) {
654 LoadedMacro::MacroDef(item_def, _) => {
655 if let ast::ItemKind::MacroDef(ref def) = item_def.kind {
656 let vis = cx.tcx.visibility(import_def_id.unwrap_or(def_id));
657 clean::MacroItem(clean::Macro {
658 source: utils::display_macro_source(cx, name, def, def_id, vis),
664 LoadedMacro::ProcMacro(ext) => clean::ProcMacroItem(clean::ProcMacro {
665 kind: ext.macro_kind(),
666 helpers: ext.helper_attrs,
671 /// A trait's generics clause actually contains all of the predicates for all of
672 /// its associated types as well. We specifically move these clauses to the
673 /// associated types instead when displaying, so when we're generating the
674 /// generics for the trait itself we need to be sure to remove them.
675 /// We also need to remove the implied "recursive" Self: Trait bound.
677 /// The inverse of this filtering logic can be found in the `Clean`
678 /// implementation for `AssociatedType`
679 fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics) -> clean::Generics {
680 for pred in &mut g.where_predicates {
682 clean::WherePredicate::BoundPredicate {
683 ty: clean::Generic(ref s),
686 } if *s == kw::SelfUpper => {
687 bounds.retain(|bound| match bound {
688 clean::GenericBound::TraitBound(clean::PolyTrait { trait_, .. }, _) => {
689 trait_.def_id() != trait_did
698 g.where_predicates.retain(|pred| match pred {
699 clean::WherePredicate::BoundPredicate {
700 ty: clean::QPath(box clean::QPathData { self_type: clean::Generic(ref s), trait_, .. }),
703 } => !(bounds.is_empty() || *s == kw::SelfUpper && trait_.def_id() == trait_did),
709 /// Supertrait bounds for a trait are also listed in the generics coming from
710 /// the metadata for a crate, so we want to separate those out and create a new
711 /// list of explicit supertrait bounds to render nicely.
712 fn separate_supertrait_bounds(
713 mut g: clean::Generics,
714 ) -> (clean::Generics, Vec<clean::GenericBound>) {
715 let mut ty_bounds = Vec::new();
716 g.where_predicates.retain(|pred| match *pred {
717 clean::WherePredicate::BoundPredicate { ty: clean::Generic(ref s), ref bounds, .. }
718 if *s == kw::SelfUpper =>
720 ty_bounds.extend(bounds.iter().cloned());
728 pub(crate) fn record_extern_trait(cx: &mut DocContext<'_>, did: DefId) {
734 if cx.external_traits.borrow().contains_key(&did) || cx.active_extern_traits.contains(&did)
741 cx.active_extern_traits.insert(did);
744 debug!("record_extern_trait: {:?}", did);
745 let trait_ = build_external_trait(cx, did);
747 cx.external_traits.borrow_mut().insert(did, trait_);
748 cx.active_extern_traits.remove(&did);