Skip to main content

rustdoc/clean/
inline.rs

1//! Support for inlining external documentation into the current AST.
2
3use std::iter::once;
4use std::sync::Arc;
5
6use rustc_data_structures::fx::FxHashSet;
7use rustc_data_structures::thin_vec::{ThinVec, thin_vec};
8use rustc_hir::def::{DefKind, MacroKinds, Res};
9use rustc_hir::def_id::{DefId, DefIdSet, LocalDefId, LocalModDefId};
10use rustc_hir::{self as hir, Mutability, find_attr};
11use rustc_metadata::creader::{CStore, LoadedMacro};
12use rustc_middle::ty::fast_reject::SimplifiedType;
13use rustc_middle::ty::{self, TyCtxt};
14use rustc_span::def_id::LOCAL_CRATE;
15use rustc_span::hygiene::MacroKind;
16use rustc_span::symbol::{Symbol, sym};
17use tracing::{debug, trace};
18
19use super::{Item, extract_cfg_from_attrs};
20use crate::clean::{
21    self, Attributes, CfgInfo, ImplKind, ItemId, Type, clean_bound_vars, clean_generics,
22    clean_impl_item, clean_middle_assoc_item, clean_middle_field, clean_middle_ty,
23    clean_poly_fn_sig, clean_trait_ref_with_constraints, clean_ty, clean_ty_alias_inner_type,
24    clean_ty_generics, clean_variant_def, utils,
25};
26use crate::core::DocContext;
27use crate::formats::item_type::ItemType;
28
29/// Attempt to inline a definition into this AST.
30///
31/// This function will fetch the definition specified, and if it is
32/// from another crate it will attempt to inline the documentation
33/// from the other crate into this crate.
34///
35/// This is primarily used for `pub use` statements which are, in general,
36/// implementation details. Inlining the documentation should help provide a
37/// better experience when reading the documentation in this use case.
38///
39/// The returned value is `None` if the definition could not be inlined,
40/// and `Some` of a vector of items if it was successfully expanded.
41pub(crate) fn try_inline(
42    cx: &mut DocContext<'_>,
43    res: Res,
44    name: Symbol,
45    attrs: Option<(&[hir::Attribute], Option<LocalDefId>)>,
46    visited: &mut DefIdSet,
47) -> Option<Vec<clean::Item>> {
48    let did = res.opt_def_id()?;
49    if did.is_local() {
50        return None;
51    }
52    let mut ret = Vec::new();
53
54    debug!("attrs={attrs:?}");
55
56    let attrs_without_docs = attrs.map(|(attrs, def_id)| {
57        (attrs.iter().filter(|a| a.doc_str().is_none()).cloned().collect::<Vec<_>>(), def_id)
58    });
59    let attrs_without_docs =
60        attrs_without_docs.as_ref().map(|(attrs, def_id)| (&attrs[..], *def_id));
61
62    let import_def_id = attrs.and_then(|(_, def_id)| def_id);
63
64    let kind = match res {
65        Res::Def(DefKind::Trait, did) => {
66            record_extern_fqn(cx, did, ItemType::Trait);
67            cx.with_param_env(did, |cx| {
68                build_impls(cx, did, attrs_without_docs, &mut ret);
69                clean::TraitItem(Box::new(build_trait(cx, did)))
70            })
71        }
72        Res::Def(DefKind::TraitAlias, did) => {
73            record_extern_fqn(cx, did, ItemType::TraitAlias);
74            cx.with_param_env(did, |cx| clean::TraitAliasItem(build_trait_alias(cx, did)))
75        }
76        Res::Def(DefKind::Fn, did) => {
77            record_extern_fqn(cx, did, ItemType::Function);
78            cx.with_param_env(did, |cx| {
79                clean::enter_impl_trait(cx, |cx| clean::FunctionItem(build_function(cx, did)))
80            })
81        }
82        Res::Def(DefKind::Struct, did) => {
83            record_extern_fqn(cx, did, ItemType::Struct);
84            cx.with_param_env(did, |cx| {
85                build_impls(cx, did, attrs_without_docs, &mut ret);
86                clean::StructItem(build_struct(cx, did))
87            })
88        }
89        Res::Def(DefKind::Union, did) => {
90            record_extern_fqn(cx, did, ItemType::Union);
91            cx.with_param_env(did, |cx| {
92                build_impls(cx, did, attrs_without_docs, &mut ret);
93                clean::UnionItem(build_union(cx, did))
94            })
95        }
96        Res::Def(DefKind::TyAlias, did) => {
97            record_extern_fqn(cx, did, ItemType::TypeAlias);
98            cx.with_param_env(did, |cx| {
99                build_impls(cx, did, attrs_without_docs, &mut ret);
100                clean::TypeAliasItem(build_type_alias(cx, did, &mut ret))
101            })
102        }
103        Res::Def(DefKind::Enum, did) => {
104            record_extern_fqn(cx, did, ItemType::Enum);
105            cx.with_param_env(did, |cx| {
106                build_impls(cx, did, attrs_without_docs, &mut ret);
107                clean::EnumItem(build_enum(cx, did))
108            })
109        }
110        Res::Def(DefKind::ForeignTy, did) => {
111            record_extern_fqn(cx, did, ItemType::ForeignType);
112            cx.with_param_env(did, |cx| {
113                build_impls(cx, did, attrs_without_docs, &mut ret);
114                clean::ForeignTypeItem
115            })
116        }
117        // Never inline enum variants but leave them shown as re-exports.
118        Res::Def(DefKind::Variant, _) => return None,
119        // Assume that enum variants and struct types are re-exported next to
120        // their constructors.
121        Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) => return Some(Vec::new()),
122        Res::Def(DefKind::Mod, did) => {
123            record_extern_fqn(cx, did, ItemType::Module);
124            clean::ModuleItem(build_module(cx, did, visited))
125        }
126        Res::Def(DefKind::Static { .. }, did) => {
127            record_extern_fqn(cx, did, ItemType::Static);
128            cx.with_param_env(did, |cx| {
129                clean::StaticItem(build_static(cx, did, cx.tcx.is_mutable_static(did)))
130            })
131        }
132        Res::Def(DefKind::Const, did) => {
133            record_extern_fqn(cx, did, ItemType::Constant);
134            cx.with_param_env(did, |cx| {
135                let ct = build_const_item(cx, did);
136                clean::ConstantItem(Box::new(ct))
137            })
138        }
139        Res::Def(DefKind::Macro(kinds), did) => {
140            let mac = build_macro(cx, did, name, kinds);
141
142            // FIXME: handle attributes and derives that aren't proc macros, and macros with
143            // multiple kinds
144            let type_kind = match kinds {
145                MacroKinds::BANG => ItemType::Macro,
146                MacroKinds::ATTR => ItemType::ProcAttribute,
147                MacroKinds::DERIVE => ItemType::ProcDerive,
148                _ => todo!("Handle macros with multiple kinds"),
149            };
150            record_extern_fqn(cx, did, type_kind);
151            mac
152        }
153        _ => return None,
154    };
155
156    cx.inlined.insert(did.into());
157    let mut item = crate::clean::generate_item_with_correct_attrs(
158        cx,
159        kind,
160        did,
161        name,
162        import_def_id.as_slice(),
163        None,
164    );
165    // The visibility needs to reflect the one from the reexport and not from the "source" DefId.
166    item.inner.inline_stmt_id = import_def_id;
167    ret.push(item);
168    Some(ret)
169}
170
171pub(crate) fn try_inline_glob(
172    cx: &mut DocContext<'_>,
173    res: Res,
174    current_mod: LocalModDefId,
175    visited: &mut DefIdSet,
176    inlined_names: &mut FxHashSet<(ItemType, Symbol)>,
177    import: &hir::Item<'_>,
178) -> Option<Vec<clean::Item>> {
179    let did = res.opt_def_id()?;
180    if did.is_local() {
181        return None;
182    }
183
184    match res {
185        Res::Def(DefKind::Mod, did) => {
186            // Use the set of module reexports to filter away names that are not actually
187            // reexported by the glob, e.g. because they are shadowed by something else.
188            let reexports = cx
189                .tcx
190                .module_children_local(current_mod.to_local_def_id())
191                .iter()
192                .filter(|child| !child.reexport_chain.is_empty())
193                .filter_map(|child| child.res.opt_def_id())
194                .filter(|&def_id| !cx.tcx.is_doc_hidden(def_id))
195                .collect();
196            let attrs = cx.tcx.hir_attrs(import.hir_id());
197            let mut items = build_module_items(
198                cx,
199                did,
200                visited,
201                inlined_names,
202                Some(&reexports),
203                Some((attrs, Some(import.owner_id.def_id))),
204            );
205            items.retain(|item| {
206                if let Some(name) = item.name {
207                    // If an item with the same type and name already exists,
208                    // it takes priority over the inlined stuff.
209                    inlined_names.insert((item.type_(), name))
210                } else {
211                    true
212                }
213            });
214            Some(items)
215        }
216        // glob imports on things like enums aren't inlined even for local exports, so just bail
217        _ => None,
218    }
219}
220
221pub(crate) fn load_attrs<'hir>(cx: &DocContext<'hir>, did: DefId) -> &'hir [hir::Attribute] {
222    // FIXME: all uses should use `find_attr`!
223    #[allow(deprecated)]
224    cx.tcx.get_all_attrs(did)
225}
226
227pub(crate) fn item_relative_path(tcx: TyCtxt<'_>, def_id: DefId) -> Vec<Symbol> {
228    tcx.def_path(def_id).data.into_iter().filter_map(|elem| elem.data.get_opt_name()).collect()
229}
230
231/// Get the public Rust path to an item. This is used to generate the URL to the item's page.
232///
233/// In particular: we handle macro differently: if it's not a macro 2.0 oe a built-in macro, then
234/// it is generated at the top-level of the crate and its path will be `[crate_name, macro_name]`.
235pub(crate) fn get_item_path(tcx: TyCtxt<'_>, def_id: DefId, kind: ItemType) -> Vec<Symbol> {
236    let crate_name = tcx.crate_name(def_id.krate);
237    let relative = item_relative_path(tcx, def_id);
238
239    if let ItemType::Macro = kind {
240        // Check to see if it is a macro 2.0 or built-in macro
241        // More information in <https://rust-lang.github.io/rfcs/1584-macros.html>.
242        if matches!(
243            CStore::from_tcx(tcx).load_macro_untracked(tcx, def_id),
244            LoadedMacro::MacroDef { def, .. } if !def.macro_rules
245        ) {
246            once(crate_name).chain(relative).collect()
247        } else {
248            vec![crate_name, *relative.last().expect("relative was empty")]
249        }
250    } else {
251        once(crate_name).chain(relative).collect()
252    }
253}
254
255/// Record an external fully qualified name in the external_paths cache.
256///
257/// These names are used later on by HTML rendering to generate things like
258/// source links back to the original item.
259pub(crate) fn record_extern_fqn(cx: &mut DocContext<'_>, did: DefId, kind: ItemType) {
260    if did.is_local() {
261        if cx.cache.exact_paths.contains_key(&did) {
262            return;
263        }
264    } else if cx.cache.external_paths.contains_key(&did) {
265        return;
266    }
267
268    let item_path = get_item_path(cx.tcx, did, kind);
269
270    if did.is_local() {
271        cx.cache.exact_paths.insert(did, item_path);
272    } else {
273        cx.cache.external_paths.insert(did, (item_path, kind));
274    }
275}
276
277pub(crate) fn build_trait(cx: &mut DocContext<'_>, did: DefId) -> clean::Trait {
278    let trait_items = cx
279        .tcx
280        .associated_items(did)
281        .in_definition_order()
282        .filter(|item| !item.is_impl_trait_in_trait())
283        .map(|item| clean_middle_assoc_item(item, cx))
284        .collect();
285
286    let generics = clean_ty_generics(cx, did);
287    let (generics, mut supertrait_bounds) = separate_self_bounds(generics);
288
289    supertrait_bounds.retain(|b| {
290        // FIXME(sized-hierarchy): Always skip `MetaSized` bounds so that only `?Sized`
291        // is shown and none of the new sizedness traits leak into documentation.
292        !b.is_meta_sized_bound(cx)
293    });
294
295    clean::Trait { def_id: did, generics, items: trait_items, bounds: supertrait_bounds }
296}
297
298fn build_trait_alias(cx: &mut DocContext<'_>, did: DefId) -> clean::TraitAlias {
299    let generics = clean_ty_generics(cx, did);
300    let (generics, mut bounds) = separate_self_bounds(generics);
301
302    bounds.retain(|b| {
303        // FIXME(sized-hierarchy): Always skip `MetaSized` bounds so that only `?Sized`
304        // is shown and none of the new sizedness traits leak into documentation.
305        !b.is_meta_sized_bound(cx)
306    });
307
308    clean::TraitAlias { generics, bounds }
309}
310
311pub(super) fn build_function(cx: &mut DocContext<'_>, def_id: DefId) -> Box<clean::Function> {
312    let sig = cx.tcx.fn_sig(def_id).instantiate_identity();
313    // The generics need to be cleaned before the signature.
314    let mut generics = clean_ty_generics(cx, def_id);
315    let bound_vars = clean_bound_vars(sig.bound_vars(), cx);
316
317    // At the time of writing early & late-bound params are stored separately in rustc,
318    // namely in `generics.params` and `bound_vars` respectively.
319    //
320    // To reestablish the original source code order of the generic parameters, we
321    // need to manually sort them by their definition span after concatenation.
322    //
323    // See also:
324    // * https://rustc-dev-guide.rust-lang.org/bound-vars-and-params.html
325    // * https://rustc-dev-guide.rust-lang.org/what-does-early-late-bound-mean.html
326    let has_early_bound_params = !generics.params.is_empty();
327    let has_late_bound_params = !bound_vars.is_empty();
328    generics.params.extend(bound_vars);
329    if has_early_bound_params && has_late_bound_params {
330        // If this ever becomes a performances bottleneck either due to the sorting
331        // or due to the query calls, consider inserting the late-bound lifetime params
332        // right after the last early-bound lifetime param followed by only sorting
333        // the slice of lifetime params.
334        generics.params.sort_by_key(|param| cx.tcx.def_ident_span(param.def_id).unwrap());
335    }
336
337    let decl = clean_poly_fn_sig(cx, Some(def_id), sig);
338
339    Box::new(clean::Function { decl, generics })
340}
341
342fn build_enum(cx: &mut DocContext<'_>, did: DefId) -> clean::Enum {
343    clean::Enum {
344        generics: clean_ty_generics(cx, did),
345        variants: cx.tcx.adt_def(did).variants().iter().map(|v| clean_variant_def(v, cx)).collect(),
346    }
347}
348
349fn build_struct(cx: &mut DocContext<'_>, did: DefId) -> clean::Struct {
350    let variant = cx.tcx.adt_def(did).non_enum_variant();
351
352    clean::Struct {
353        ctor_kind: variant.ctor_kind(),
354        generics: clean_ty_generics(cx, did),
355        fields: variant.fields.iter().map(|x| clean_middle_field(x, cx)).collect(),
356    }
357}
358
359fn build_union(cx: &mut DocContext<'_>, did: DefId) -> clean::Union {
360    let variant = cx.tcx.adt_def(did).non_enum_variant();
361
362    let generics = clean_ty_generics(cx, did);
363    let fields = variant.fields.iter().map(|x| clean_middle_field(x, cx)).collect();
364    clean::Union { generics, fields }
365}
366
367fn build_type_alias(
368    cx: &mut DocContext<'_>,
369    did: DefId,
370    ret: &mut Vec<Item>,
371) -> Box<clean::TypeAlias> {
372    let ty = cx.tcx.type_of(did).instantiate_identity();
373    let type_ = clean_middle_ty(ty::Binder::dummy(ty), cx, Some(did), None);
374    let inner_type = clean_ty_alias_inner_type(ty, cx, ret);
375
376    Box::new(clean::TypeAlias {
377        type_,
378        generics: clean_ty_generics(cx, did),
379        inner_type,
380        item_type: None,
381    })
382}
383
384/// Builds all inherent implementations of an ADT (struct/union/enum) or Trait item/path/reexport.
385pub(crate) fn build_impls(
386    cx: &mut DocContext<'_>,
387    did: DefId,
388    attrs: Option<(&[hir::Attribute], Option<LocalDefId>)>,
389    ret: &mut Vec<clean::Item>,
390) {
391    let _prof_timer = cx.tcx.sess.prof.generic_activity("build_inherent_impls");
392    let tcx = cx.tcx;
393
394    // for each implementation of an item represented by `did`, build the clean::Item for that impl
395    for &did in tcx.inherent_impls(did).iter() {
396        cx.with_param_env(did, |cx| {
397            build_impl(cx, did, attrs, ret);
398        });
399    }
400
401    // This pretty much exists expressly for `dyn Error` traits that exist in the `alloc` crate.
402    // See also:
403    //
404    // * https://github.com/rust-lang/rust/issues/103170 — where it didn't used to get documented
405    // * https://github.com/rust-lang/rust/pull/99917 — where the feature got used
406    // * https://github.com/rust-lang/rust/issues/53487 — overall tracking issue for Error
407    if find_attr!(tcx, did, RustcHasIncoherentInherentImpls) {
408        let type_ =
409            if tcx.is_trait(did) { SimplifiedType::Trait(did) } else { SimplifiedType::Adt(did) };
410        for &did in tcx.incoherent_impls(type_).iter() {
411            cx.with_param_env(did, |cx| {
412                build_impl(cx, did, attrs, ret);
413            });
414        }
415    }
416}
417
418pub(crate) fn merge_attrs(
419    cx: &mut DocContext<'_>,
420    old_attrs: &[hir::Attribute],
421    new_attrs: Option<(&[hir::Attribute], Option<LocalDefId>)>,
422    cfg_info: &mut CfgInfo,
423) -> (clean::Attributes, Option<Arc<clean::cfg::Cfg>>) {
424    // NOTE: If we have additional attributes (from a re-export),
425    // always insert them first. This ensure that re-export
426    // doc comments show up before the original doc comments
427    // when we render them.
428    if let Some((inner, item_id)) = new_attrs {
429        let mut both = inner.to_vec();
430        both.extend_from_slice(old_attrs);
431        (
432            if let Some(item_id) = item_id {
433                Attributes::from_hir_with_additional(old_attrs, (inner, item_id.to_def_id()))
434            } else {
435                Attributes::from_hir(&both)
436            },
437            extract_cfg_from_attrs(both.iter(), cx.tcx, cfg_info),
438        )
439    } else {
440        (
441            Attributes::from_hir(old_attrs),
442            extract_cfg_from_attrs(old_attrs.iter(), cx.tcx, cfg_info),
443        )
444    }
445}
446
447/// Inline an `impl`, inherent or of a trait. The `did` must be for an `impl`.
448pub(crate) fn build_impl(
449    cx: &mut DocContext<'_>,
450    did: DefId,
451    attrs: Option<(&[hir::Attribute], Option<LocalDefId>)>,
452    ret: &mut Vec<clean::Item>,
453) {
454    if !cx.inlined.insert(did.into()) {
455        return;
456    }
457
458    let tcx = cx.tcx;
459    let _prof_timer = tcx.sess.prof.generic_activity("build_impl");
460
461    let associated_trait = tcx.impl_opt_trait_ref(did).map(ty::EarlyBinder::skip_binder);
462
463    // Do not inline compiler-internal items unless we're a compiler-internal crate.
464    let is_compiler_internal = |did| {
465        tcx.lookup_stability(did)
466            .is_some_and(|stab| stab.is_unstable() && stab.feature == sym::rustc_private)
467    };
468    let document_compiler_internal = is_compiler_internal(LOCAL_CRATE.as_def_id());
469    let is_directly_public = |cx: &mut DocContext<'_>, did| {
470        cx.cache.effective_visibilities.is_directly_public(tcx, did)
471            && (document_compiler_internal || !is_compiler_internal(did))
472    };
473
474    // Only inline impl if the implemented trait is
475    // reachable in rustdoc generated documentation
476    if !did.is_local()
477        && let Some(traitref) = associated_trait
478        && !is_directly_public(cx, traitref.def_id)
479    {
480        return;
481    }
482
483    let impl_item = match did.as_local() {
484        Some(did) => match &tcx.hir_expect_item(did).kind {
485            hir::ItemKind::Impl(impl_) => Some(impl_),
486            _ => panic!("`DefID` passed to `build_impl` is not an `impl"),
487        },
488        None => None,
489    };
490
491    let for_ = match &impl_item {
492        Some(impl_) => clean_ty(impl_.self_ty, cx),
493        None => clean_middle_ty(
494            ty::Binder::dummy(tcx.type_of(did).instantiate_identity()),
495            cx,
496            Some(did),
497            None,
498        ),
499    };
500
501    // Only inline impl if the implementing type is
502    // reachable in rustdoc generated documentation
503    if !did.is_local()
504        && let Some(did) = for_.def_id(&cx.cache)
505        && !is_directly_public(cx, did)
506    {
507        return;
508    }
509
510    let document_hidden = cx.document_hidden();
511    let (trait_items, generics) = match impl_item {
512        Some(impl_) => (
513            impl_
514                .items
515                .iter()
516                .map(|&item| tcx.hir_impl_item(item))
517                .filter(|item| {
518                    // Filter out impl items whose corresponding trait item has `doc(hidden)`
519                    // not to document such impl items.
520                    // For inherent impls, we don't do any filtering, because that's already done in strip_hidden.rs.
521
522                    // When `--document-hidden-items` is passed, we don't
523                    // do any filtering, too.
524                    if document_hidden {
525                        return true;
526                    }
527                    if let Some(associated_trait) = associated_trait {
528                        let assoc_tag = match item.kind {
529                            hir::ImplItemKind::Const(..) => ty::AssocTag::Const,
530                            hir::ImplItemKind::Fn(..) => ty::AssocTag::Fn,
531                            hir::ImplItemKind::Type(..) => ty::AssocTag::Type,
532                        };
533                        let trait_item = tcx
534                            .associated_items(associated_trait.def_id)
535                            .find_by_ident_and_kind(
536                                tcx,
537                                item.ident,
538                                assoc_tag,
539                                associated_trait.def_id,
540                            )
541                            .unwrap(); // SAFETY: For all impl items there exists trait item that has the same name.
542                        !tcx.is_doc_hidden(trait_item.def_id)
543                    } else {
544                        true
545                    }
546                })
547                .map(|item| clean_impl_item(item, cx))
548                .collect::<Vec<_>>(),
549            clean_generics(impl_.generics, cx),
550        ),
551        None => (
552            tcx.associated_items(did)
553                .in_definition_order()
554                .filter(|item| !item.is_impl_trait_in_trait())
555                .filter(|item| {
556                    // If this is a trait impl, filter out associated items whose corresponding item
557                    // in the associated trait is marked `doc(hidden)`.
558                    // If this is an inherent impl, filter out private associated items.
559                    if let Some(associated_trait) = associated_trait {
560                        let trait_item = tcx
561                            .associated_items(associated_trait.def_id)
562                            .find_by_ident_and_kind(
563                                tcx,
564                                item.ident(tcx),
565                                item.tag(),
566                                associated_trait.def_id,
567                            )
568                            .unwrap(); // corresponding associated item has to exist
569                        document_hidden || !tcx.is_doc_hidden(trait_item.def_id)
570                    } else {
571                        item.visibility(tcx).is_public()
572                    }
573                })
574                .map(|item| clean_middle_assoc_item(item, cx))
575                .collect::<Vec<_>>(),
576            clean::enter_impl_trait(cx, |cx| clean_ty_generics(cx, did)),
577        ),
578    };
579    let polarity = if associated_trait.is_some() {
580        tcx.impl_polarity(did)
581    } else {
582        ty::ImplPolarity::Positive
583    };
584    let trait_ = associated_trait
585        .map(|t| clean_trait_ref_with_constraints(cx, ty::Binder::dummy(t), ThinVec::new()));
586    if trait_.as_ref().map(|t| t.def_id()) == tcx.lang_items().deref_trait() {
587        super::build_deref_target_impls(cx, &trait_items, ret);
588    }
589
590    if !document_hidden {
591        // Return if the trait itself or any types of the generic parameters are doc(hidden).
592        let mut stack: Vec<&Type> = vec![&for_];
593
594        if let Some(did) = trait_.as_ref().map(|t| t.def_id())
595            && tcx.is_doc_hidden(did)
596        {
597            return;
598        }
599
600        if let Some(generics) = trait_.as_ref().and_then(|t| t.generics()) {
601            stack.extend(generics);
602        }
603
604        while let Some(ty) = stack.pop() {
605            if let Some(did) = ty.def_id(&cx.cache)
606                && tcx.is_doc_hidden(did)
607            {
608                return;
609            }
610            if let Some(generics) = ty.generics() {
611                stack.extend(generics);
612            }
613        }
614    }
615
616    if let Some(did) = trait_.as_ref().map(|t| t.def_id()) {
617        cx.with_param_env(did, |cx| {
618            record_extern_trait(cx, did);
619        });
620    }
621
622    // In here, we pass an empty `CfgInfo` because the computation of `cfg` happens later, so it
623    // doesn't matter at this point.
624    //
625    // We need to pass this empty `CfgInfo` because `merge_attrs` is used when computing the `cfg`.
626    let (merged_attrs, cfg) = merge_attrs(cx, load_attrs(cx, did), attrs, &mut CfgInfo::default());
627    trace!("merged_attrs={merged_attrs:?}");
628
629    trace!(
630        "build_impl: impl {:?} for {:?}",
631        trait_.as_ref().map(|t| t.def_id()),
632        for_.def_id(&cx.cache)
633    );
634    ret.push(clean::Item::from_def_id_and_attrs_and_parts(
635        did,
636        None,
637        clean::ImplItem(Box::new(clean::Impl {
638            safety: hir::Safety::Safe,
639            generics,
640            trait_,
641            for_,
642            items: trait_items,
643            polarity,
644            kind: if utils::has_doc_flag(tcx, did, |d| d.fake_variadic.is_some()) {
645                ImplKind::FakeVariadic
646            } else {
647                ImplKind::Normal
648            },
649            is_deprecated: tcx
650                .lookup_deprecation(did)
651                .is_some_and(|deprecation| deprecation.is_in_effect()),
652        })),
653        merged_attrs,
654        cfg,
655    ));
656}
657
658fn build_module(cx: &mut DocContext<'_>, did: DefId, visited: &mut DefIdSet) -> clean::Module {
659    let items = build_module_items(cx, did, visited, &mut FxHashSet::default(), None, None);
660
661    let span = clean::Span::new(cx.tcx.def_span(did));
662    clean::Module { items, span }
663}
664
665fn build_module_items(
666    cx: &mut DocContext<'_>,
667    did: DefId,
668    visited: &mut DefIdSet,
669    inlined_names: &mut FxHashSet<(ItemType, Symbol)>,
670    allowed_def_ids: Option<&DefIdSet>,
671    attrs: Option<(&[hir::Attribute], Option<LocalDefId>)>,
672) -> Vec<clean::Item> {
673    let mut items = Vec::new();
674
675    // If we're re-exporting a re-export it may actually re-export something in
676    // two namespaces, so the target may be listed twice. Make sure we only
677    // visit each node at most once.
678    for item in cx.tcx.module_children(did).iter() {
679        if item.vis.is_public() {
680            let res = item.res.expect_non_local();
681            if let Some(def_id) = res.opt_def_id()
682                && let Some(allowed_def_ids) = allowed_def_ids
683                && !allowed_def_ids.contains(&def_id)
684            {
685                continue;
686            }
687            if let Some(def_id) = res.mod_def_id() {
688                // If we're inlining a glob import, it's possible to have
689                // two distinct modules with the same name. We don't want to
690                // inline it, or mark any of its contents as visited.
691                if did == def_id
692                    || inlined_names.contains(&(ItemType::Module, item.ident.name))
693                    || !visited.insert(def_id)
694                {
695                    continue;
696                }
697            }
698            if let Res::PrimTy(p) = res {
699                // Primitive types can't be inlined so generate an import instead.
700                let prim_ty = clean::PrimitiveType::from(p);
701                items.push(clean::Item {
702                    inner: Box::new(clean::ItemInner {
703                        name: None,
704                        // We can use the item's `DefId` directly since the only information ever
705                        // used from it is `DefId.krate`.
706                        item_id: ItemId::DefId(did),
707                        attrs: Default::default(),
708                        stability: None,
709                        kind: clean::ImportItem(clean::Import::new_simple(
710                            item.ident.name,
711                            clean::ImportSource {
712                                path: clean::Path {
713                                    res,
714                                    segments: thin_vec![clean::PathSegment {
715                                        name: prim_ty.as_sym(),
716                                        args: clean::GenericArgs::AngleBracketed {
717                                            args: Default::default(),
718                                            constraints: ThinVec::new(),
719                                        },
720                                    }],
721                                },
722                                did: None,
723                            },
724                            true,
725                        )),
726                        cfg: None,
727                        inline_stmt_id: None,
728                    }),
729                });
730            } else if let Some(i) = try_inline(cx, res, item.ident.name, attrs, visited) {
731                items.extend(i)
732            }
733        }
734    }
735
736    items
737}
738
739pub(crate) fn print_inlined_const(tcx: TyCtxt<'_>, did: DefId) -> String {
740    if let Some(did) = did.as_local() {
741        let hir_id = tcx.local_def_id_to_hir_id(did);
742        rustc_hir_pretty::id_to_string(&tcx, hir_id)
743    } else {
744        tcx.rendered_const(did).clone()
745    }
746}
747
748fn build_const_item(cx: &mut DocContext<'_>, def_id: DefId) -> clean::Constant {
749    let mut generics = clean_ty_generics(cx, def_id);
750    clean::simplify::move_bounds_to_generic_parameters(&mut generics);
751    let ty = clean_middle_ty(
752        ty::Binder::dummy(cx.tcx.type_of(def_id).instantiate_identity()),
753        cx,
754        None,
755        None,
756    );
757    clean::Constant { generics, type_: ty, kind: clean::ConstantKind::Extern { def_id } }
758}
759
760fn build_static(cx: &mut DocContext<'_>, did: DefId, mutable: bool) -> clean::Static {
761    clean::Static {
762        type_: Box::new(clean_middle_ty(
763            ty::Binder::dummy(cx.tcx.type_of(did).instantiate_identity()),
764            cx,
765            Some(did),
766            None,
767        )),
768        mutability: if mutable { Mutability::Mut } else { Mutability::Not },
769        expr: None,
770    }
771}
772
773fn build_macro(
774    cx: &mut DocContext<'_>,
775    def_id: DefId,
776    name: Symbol,
777    macro_kinds: MacroKinds,
778) -> clean::ItemKind {
779    match CStore::from_tcx(cx.tcx).load_macro_untracked(cx.tcx, def_id) {
780        // FIXME: handle attributes and derives that aren't proc macros, and macros with multiple
781        // kinds
782        LoadedMacro::MacroDef { def, .. } => match macro_kinds {
783            MacroKinds::BANG => clean::MacroItem(clean::Macro {
784                source: utils::display_macro_source(cx, name, &def),
785                macro_rules: def.macro_rules,
786            }),
787            MacroKinds::DERIVE => clean::ProcMacroItem(clean::ProcMacro {
788                kind: MacroKind::Derive,
789                helpers: Vec::new(),
790            }),
791            MacroKinds::ATTR => clean::ProcMacroItem(clean::ProcMacro {
792                kind: MacroKind::Attr,
793                helpers: Vec::new(),
794            }),
795            _ => todo!("Handle macros with multiple kinds"),
796        },
797        LoadedMacro::ProcMacro(ext) => {
798            // Proc macros can only have a single kind
799            let kind = match ext.macro_kinds() {
800                MacroKinds::BANG => MacroKind::Bang,
801                MacroKinds::ATTR => MacroKind::Attr,
802                MacroKinds::DERIVE => MacroKind::Derive,
803                _ => unreachable!(),
804            };
805            clean::ProcMacroItem(clean::ProcMacro { kind, helpers: ext.helper_attrs })
806        }
807    }
808}
809
810fn separate_self_bounds(mut g: clean::Generics) -> (clean::Generics, Vec<clean::GenericBound>) {
811    let mut ty_bounds = Vec::new();
812    g.where_predicates.retain(|pred| match *pred {
813        clean::WherePredicate::BoundPredicate { ty: clean::SelfTy, ref bounds, .. } => {
814            ty_bounds.extend(bounds.iter().cloned());
815            false
816        }
817        _ => true,
818    });
819    (g, ty_bounds)
820}
821
822pub(crate) fn record_extern_trait(cx: &mut DocContext<'_>, did: DefId) {
823    if did.is_local()
824        || cx.external_traits.contains_key(&did)
825        || cx.active_extern_traits.contains(&did)
826    {
827        return;
828    }
829
830    cx.active_extern_traits.insert(did);
831
832    debug!("record_extern_trait: {did:?}");
833    let trait_ = build_trait(cx, did);
834
835    cx.external_traits.insert(did, trait_);
836    cx.active_extern_traits.remove(&did);
837}