1use 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, LocalModId};
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, instrument, 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
29pub(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 fn try_inline_inner(
49 cx: &mut DocContext<'_>,
50 kind: clean::ItemKind,
51 did: DefId,
52 name: Symbol,
53 import_def_id: Option<LocalDefId>,
54 ) -> clean::Item {
55 cx.inlined.insert(did.into());
56 let mut item = crate::clean::generate_item_with_correct_attrs(
57 cx,
58 kind,
59 did,
60 name,
61 import_def_id.as_slice(),
62 None,
63 );
64 item.inner.inline_stmt_id = import_def_id;
66 item
67 }
68
69 let did = res.opt_def_id()?;
70 if did.is_local() {
71 return None;
72 }
73 let mut ret = Vec::new();
74
75 debug!("attrs={attrs:?}");
76
77 let attrs_without_docs = attrs.map(|(attrs, def_id)| {
78 (attrs.iter().filter(|a| a.doc_str().is_none()).cloned().collect::<Vec<_>>(), def_id)
79 });
80 let attrs_without_docs =
81 attrs_without_docs.as_ref().map(|(attrs, def_id)| (&attrs[..], *def_id));
82
83 let import_def_id = attrs.and_then(|(_, def_id)| def_id);
84
85 let kind = match res {
86 Res::Def(DefKind::Trait, did) => {
87 record_extern_fqn(cx, did, ItemType::Trait);
88 cx.with_param_env(did, |cx| {
89 build_impls(cx, did, attrs_without_docs, &mut ret);
90 clean::TraitItem(Box::new(build_trait(cx, did)))
91 })
92 }
93 Res::Def(DefKind::TraitAlias, did) => {
94 record_extern_fqn(cx, did, ItemType::TraitAlias);
95 cx.with_param_env(did, |cx| clean::TraitAliasItem(build_trait_alias(cx, did)))
96 }
97 Res::Def(DefKind::Fn, did) => {
98 record_extern_fqn(cx, did, ItemType::Function);
99 cx.with_param_env(did, |cx| {
100 clean::enter_impl_trait(cx, |cx| clean::FunctionItem(build_function(cx, did)))
101 })
102 }
103 Res::Def(DefKind::Struct, did) => {
104 record_extern_fqn(cx, did, ItemType::Struct);
105 cx.with_param_env(did, |cx| {
106 build_impls(cx, did, attrs_without_docs, &mut ret);
107 clean::StructItem(build_struct(cx, did))
108 })
109 }
110 Res::Def(DefKind::Union, did) => {
111 record_extern_fqn(cx, did, ItemType::Union);
112 cx.with_param_env(did, |cx| {
113 build_impls(cx, did, attrs_without_docs, &mut ret);
114 clean::UnionItem(build_union(cx, did))
115 })
116 }
117 Res::Def(DefKind::TyAlias, did) => {
118 record_extern_fqn(cx, did, ItemType::TypeAlias);
119 cx.with_param_env(did, |cx| {
120 build_impls(cx, did, attrs_without_docs, &mut ret);
121 clean::TypeAliasItem(build_type_alias(cx, did, &mut ret))
122 })
123 }
124 Res::Def(DefKind::Enum, did) => {
125 record_extern_fqn(cx, did, ItemType::Enum);
126 cx.with_param_env(did, |cx| {
127 build_impls(cx, did, attrs_without_docs, &mut ret);
128 clean::EnumItem(build_enum(cx, did))
129 })
130 }
131 Res::Def(DefKind::ForeignTy, did) => {
132 record_extern_fqn(cx, did, ItemType::ForeignType);
133 cx.with_param_env(did, |cx| {
134 build_impls(cx, did, attrs_without_docs, &mut ret);
135 clean::ForeignTypeItem
136 })
137 }
138 Res::Def(DefKind::Variant, _) => return None,
140 Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) => return Some(Vec::new()),
143 Res::Def(DefKind::Mod, did) => {
144 record_extern_fqn(cx, did, ItemType::Module);
145 clean::ModuleItem(build_module(cx, did, name, visited))
146 }
147 Res::Def(DefKind::Static { .. }, did) => {
148 record_extern_fqn(cx, did, ItemType::Static);
149 cx.with_param_env(did, |cx| {
150 clean::StaticItem(build_static(cx, did, cx.tcx.is_mutable_static(did)))
151 })
152 }
153 Res::Def(DefKind::Const { .. }, did) => {
154 record_extern_fqn(cx, did, ItemType::Constant);
155 cx.with_param_env(did, |cx| {
156 let ct = build_const_item(cx, did);
157 clean::ConstantItem(Box::new(ct))
158 })
159 }
160 Res::Def(DefKind::Macro(kinds), did) => {
161 let mac = build_macro(cx.tcx, did, name, kinds);
162
163 let type_kind = match kinds {
164 MacroKinds::BANG => ItemType::Macro,
165 MacroKinds::ATTR => ItemType::ProcAttribute,
166 MacroKinds::DERIVE => ItemType::ProcDerive,
167 _ => ItemType::Macro,
169 };
170 record_extern_fqn(cx, did, type_kind);
171 ret.push(try_inline_inner(cx, mac, did, name, import_def_id));
172 return Some(ret);
173 }
174 _ => return None,
175 };
176
177 ret.push(try_inline_inner(cx, kind, did, name, import_def_id));
178 Some(ret)
179}
180
181pub(crate) fn try_inline_glob(
182 cx: &mut DocContext<'_>,
183 res: Res,
184 current_mod: LocalModId,
185 visited: &mut DefIdSet,
186 inlined_names: &mut FxHashSet<(ItemType, Symbol)>,
187 import: &hir::Item<'_>,
188) -> Option<Vec<clean::Item>> {
189 let did = res.opt_def_id()?;
190 if did.is_local() {
191 return None;
192 }
193
194 match res {
195 Res::Def(DefKind::Mod, did) => {
196 let reexports = cx
199 .tcx
200 .module_children_local(current_mod.to_local_def_id())
201 .iter()
202 .filter(|child| !child.reexport_chain.is_empty())
203 .filter_map(|child| child.res.opt_def_id())
204 .filter(|&def_id| !cx.tcx.is_doc_hidden(def_id))
205 .collect();
206 let attrs = cx.tcx.hir_attrs(import.hir_id());
207 let mut items = build_module_items(
208 cx,
209 did,
210 cx.tcx.item_name(did),
211 visited,
212 inlined_names,
213 Some(&reexports),
214 Some((attrs, Some(import.owner_id.def_id))),
215 );
216 items.retain(|item| {
217 if let Some(name) = item.name {
218 inlined_names.insert((item.type_(), name))
221 } else {
222 true
223 }
224 });
225 Some(items)
226 }
227 _ => None,
229 }
230}
231
232pub(crate) fn load_attrs<'hir>(tcx: TyCtxt<'hir>, did: DefId) -> &'hir [hir::Attribute] {
233 #[allow(deprecated)]
235 tcx.get_all_attrs(did)
236}
237
238pub(crate) fn item_relative_path(tcx: TyCtxt<'_>, def_id: DefId) -> Vec<Symbol> {
239 tcx.def_path(def_id).data.into_iter().filter_map(|elem| elem.data.get_opt_name()).collect()
240}
241
242pub(crate) fn get_item_path(tcx: TyCtxt<'_>, def_id: DefId, kind: ItemType) -> Vec<Symbol> {
247 let crate_name = tcx.crate_name(def_id.krate);
248 let relative = item_relative_path(tcx, def_id);
249
250 if let ItemType::Macro = kind {
251 let is_macro_2_0_or_builtin = if let Some(local_def_id) = def_id.as_local() {
254 let (_, macro_def, _) = tcx.hir_expect_item(local_def_id).expect_macro();
255 !macro_def.macro_rules
256 } else {
257 matches!(
258 CStore::from_tcx(tcx).load_macro_untracked(tcx, def_id),
259 LoadedMacro::MacroDef { def, .. } if !def.macro_rules
260 )
261 };
262 if !is_macro_2_0_or_builtin {
263 return vec![crate_name, *relative.last().expect("relative was empty")];
264 }
265 }
266
267 once(crate_name).chain(relative).collect()
268}
269
270pub(crate) fn record_extern_fqn(cx: &mut DocContext<'_>, did: DefId, kind: ItemType) {
275 if did.is_local() {
276 if cx.cache.exact_paths.contains_key(&did) {
277 return;
278 }
279 } else if cx.cache.external_paths.contains_key(&did) {
280 return;
281 }
282
283 let item_path = get_item_path(cx.tcx, did, kind);
284
285 if did.is_local() {
286 cx.cache.exact_paths.insert(did, item_path);
287 } else {
288 cx.cache.external_paths.insert(did, (item_path, kind));
289 }
290}
291
292pub(crate) fn build_trait(cx: &mut DocContext<'_>, did: DefId) -> clean::Trait {
293 let trait_items = cx
294 .tcx
295 .associated_items(did)
296 .in_definition_order()
297 .filter(|item| !item.is_impl_trait_in_trait())
298 .map(|item| clean_middle_assoc_item(item, cx))
299 .collect();
300
301 let generics = clean_ty_generics(cx, did);
302 let (generics, mut supertrait_bounds) = separate_self_bounds(generics);
303
304 supertrait_bounds.retain(|b| {
305 !b.is_meta_sized_bound(cx.tcx)
308 });
309
310 clean::Trait { def_id: did, generics, items: trait_items, bounds: supertrait_bounds }
311}
312
313fn build_trait_alias(cx: &mut DocContext<'_>, did: DefId) -> clean::TraitAlias {
314 let generics = clean_ty_generics(cx, did);
315 let (generics, mut bounds) = separate_self_bounds(generics);
316
317 bounds.retain(|b| {
318 !b.is_meta_sized_bound(cx.tcx)
321 });
322
323 clean::TraitAlias { generics, bounds }
324}
325
326pub(super) fn build_function(cx: &mut DocContext<'_>, def_id: DefId) -> Box<clean::Function> {
327 let sig = cx.tcx.fn_sig(def_id).instantiate_identity().skip_norm_wip();
328 let mut generics = clean_ty_generics(cx, def_id);
330 let bound_vars = clean_bound_vars(sig.bound_vars(), cx.tcx);
331
332 let has_early_bound_params = !generics.params.is_empty();
342 let has_late_bound_params = !bound_vars.is_empty();
343 generics.params.extend(bound_vars);
344 if has_early_bound_params && has_late_bound_params {
345 generics.params.sort_by_key(|param| cx.tcx.def_ident_span(param.def_id).unwrap());
350 }
351
352 let decl = clean_poly_fn_sig(cx, Some(def_id), sig);
353
354 Box::new(clean::Function { decl, generics })
355}
356
357fn build_enum(cx: &mut DocContext<'_>, did: DefId) -> clean::Enum {
358 clean::Enum {
359 generics: clean_ty_generics(cx, did),
360 variants: cx.tcx.adt_def(did).variants().iter().map(|v| clean_variant_def(v, cx)).collect(),
361 }
362}
363
364fn build_struct(cx: &mut DocContext<'_>, did: DefId) -> clean::Struct {
365 let variant = cx.tcx.adt_def(did).non_enum_variant();
366
367 clean::Struct {
368 ctor_kind: variant.ctor_kind(),
369 generics: clean_ty_generics(cx, did),
370 fields: variant.fields.iter().map(|x| clean_middle_field(x, cx)).collect(),
371 }
372}
373
374fn build_union(cx: &mut DocContext<'_>, did: DefId) -> clean::Union {
375 let variant = cx.tcx.adt_def(did).non_enum_variant();
376
377 let generics = clean_ty_generics(cx, did);
378 let fields = variant.fields.iter().map(|x| clean_middle_field(x, cx)).collect();
379 clean::Union { generics, fields }
380}
381
382fn build_type_alias(
383 cx: &mut DocContext<'_>,
384 did: DefId,
385 ret: &mut Vec<Item>,
386) -> Box<clean::TypeAlias> {
387 let ty = cx.tcx.type_of(did).instantiate_identity().skip_norm_wip();
388 let type_ = clean_middle_ty(ty::Binder::dummy(ty), cx, Some(did), None);
389 let inner_type = clean_ty_alias_inner_type(ty, cx, ret);
390
391 Box::new(clean::TypeAlias {
392 type_,
393 generics: clean_ty_generics(cx, did),
394 inner_type,
395 item_type: None,
396 })
397}
398
399pub(crate) fn build_impls(
401 cx: &mut DocContext<'_>,
402 did: DefId,
403 attrs: Option<(&[hir::Attribute], Option<LocalDefId>)>,
404 ret: &mut Vec<clean::Item>,
405) {
406 let tcx = cx.tcx;
407 let _prof_timer = tcx.sess.prof.generic_activity("build_inherent_impls");
408
409 for &did in tcx.inherent_impls(did).iter() {
411 cx.with_param_env(did, |cx| {
412 build_impl(cx, did, attrs, ret);
413 });
414 }
415
416 if find_attr!(tcx, did, RustcHasIncoherentInherentImpls) {
423 let type_ =
424 if tcx.is_trait(did) { SimplifiedType::Trait(did) } else { SimplifiedType::Adt(did) };
425 for &did in tcx.incoherent_impls(type_).iter() {
426 cx.with_param_env(did, |cx| {
427 build_impl(cx, did, attrs, ret);
428 });
429 }
430 }
431}
432
433pub(crate) fn merge_attrs(
434 tcx: TyCtxt<'_>,
435 old_attrs: &[hir::Attribute],
436 new_attrs: Option<(&[hir::Attribute], Option<LocalDefId>)>,
437 cfg_info: &mut CfgInfo,
438) -> (clean::Attributes, Option<Arc<clean::cfg::Cfg>>) {
439 if let Some((inner, item_id)) = new_attrs {
444 let mut both = inner.to_vec();
445 both.extend_from_slice(old_attrs);
446 (
447 if let Some(item_id) = item_id {
448 Attributes::from_hir_with_additional(old_attrs, (inner, item_id.to_def_id()))
449 } else {
450 Attributes::from_hir(&both)
451 },
452 extract_cfg_from_attrs(both.iter(), tcx, cfg_info),
453 )
454 } else {
455 (Attributes::from_hir(old_attrs), extract_cfg_from_attrs(old_attrs.iter(), tcx, cfg_info))
456 }
457}
458
459#[instrument(level = "debug", skip(cx, ret))]
461pub(crate) fn build_impl(
462 cx: &mut DocContext<'_>,
463 did: DefId,
464 attrs: Option<(&[hir::Attribute], Option<LocalDefId>)>,
465 ret: &mut Vec<clean::Item>,
466) {
467 if !cx.inlined.insert(did.into()) {
468 return;
469 }
470
471 let tcx = cx.tcx;
472 let _prof_timer = tcx.sess.prof.generic_activity("build_impl");
473
474 let associated_trait = tcx.impl_opt_trait_ref(did).map(ty::EarlyBinder::skip_binder);
475
476 let is_compiler_internal = |did| {
478 tcx.lookup_stability(did)
479 .is_some_and(|stab| stab.is_unstable() && stab.feature == sym::rustc_private)
480 };
481 let document_compiler_internal = is_compiler_internal(LOCAL_CRATE.as_def_id());
482 let is_directly_public = |cx: &mut DocContext<'_>, did| {
483 cx.cache.effective_visibilities.is_directly_public(tcx, did)
484 && (document_compiler_internal || !is_compiler_internal(did))
485 };
486
487 if !did.is_local()
490 && let Some(traitref) = associated_trait
491 && !is_directly_public(cx, traitref.def_id)
492 {
493 return;
494 }
495
496 let impl_item = match did.as_local() {
497 Some(did) => match &tcx.hir_expect_item(did).kind {
498 hir::ItemKind::Impl(impl_) => Some(impl_),
499 _ => panic!("`DefID` passed to `build_impl` is not an `impl"),
500 },
501 None => None,
502 };
503
504 let for_ = match &impl_item {
505 Some(impl_) => clean_ty(impl_.self_ty, cx),
506 None => clean_middle_ty(
507 ty::Binder::dummy(tcx.type_of(did).instantiate_identity().skip_norm_wip()),
508 cx,
509 Some(did),
510 None,
511 ),
512 };
513
514 if !did.is_local()
517 && let Some(did) = for_.def_id(&cx.cache)
518 && !is_directly_public(cx, did)
519 {
520 return;
521 }
522
523 let document_hidden = cx.document_hidden();
524 let (trait_items, generics) = match impl_item {
525 Some(impl_) => (
526 impl_
527 .items
528 .iter()
529 .map(|&item| tcx.hir_impl_item(item))
530 .filter(|item| {
531 if document_hidden {
538 return true;
539 }
540 if let Some(associated_trait) = associated_trait {
541 let assoc_tag = match item.kind {
542 hir::ImplItemKind::Const(..) => ty::AssocTag::Const,
543 hir::ImplItemKind::Fn(..) => ty::AssocTag::Fn,
544 hir::ImplItemKind::Type(..) => ty::AssocTag::Type,
545 };
546 let trait_item = tcx
547 .associated_items(associated_trait.def_id)
548 .find_by_ident_and_kind(
549 tcx,
550 item.ident,
551 assoc_tag,
552 associated_trait.def_id,
553 )
554 .unwrap(); !tcx.is_doc_hidden(trait_item.def_id)
556 } else {
557 true
558 }
559 })
560 .map(|item| clean_impl_item(item, cx))
561 .collect::<Vec<_>>(),
562 clean_generics(impl_.generics, cx),
563 ),
564 None => (
565 tcx.associated_items(did)
566 .in_definition_order()
567 .filter(|item| !item.is_impl_trait_in_trait())
568 .filter(|item| {
569 if let Some(associated_trait) = associated_trait {
573 let trait_item = tcx
574 .associated_items(associated_trait.def_id)
575 .find_by_ident_and_kind(
576 tcx,
577 item.ident(tcx),
578 item.tag(),
579 associated_trait.def_id,
580 )
581 .unwrap(); document_hidden || !tcx.is_doc_hidden(trait_item.def_id)
583 } else {
584 item.visibility(tcx).is_public()
585 }
586 })
587 .map(|item| clean_middle_assoc_item(item, cx))
588 .collect::<Vec<_>>(),
589 clean::enter_impl_trait(cx, |cx| clean_ty_generics(cx, did)),
590 ),
591 };
592 let polarity = if associated_trait.is_some() {
593 tcx.impl_polarity(did)
594 } else {
595 ty::ImplPolarity::Positive
596 };
597 let trait_ = associated_trait
598 .map(|t| clean_trait_ref_with_constraints(cx, ty::Binder::dummy(t), ThinVec::new()));
599 if trait_.as_ref().map(|t| t.def_id()) == tcx.lang_items().deref_trait()
600 && polarity != ty::ImplPolarity::Negative
601 {
602 super::build_deref_target_impls(cx, &trait_items, ret);
603 }
604
605 if !document_hidden {
606 let mut stack: Vec<&Type> = vec![&for_];
608
609 if let Some(did) = trait_.as_ref().map(|t| t.def_id())
610 && tcx.is_doc_hidden(did)
611 {
612 return;
613 }
614
615 if let Some(generics) = trait_.as_ref().and_then(|t| t.generics()) {
616 stack.extend(generics);
617 }
618
619 while let Some(ty) = stack.pop() {
620 if let Some(did) = ty.def_id(&cx.cache)
621 && tcx.is_doc_hidden(did)
622 {
623 return;
624 }
625 if let Some(generics) = ty.generics() {
626 stack.extend(generics);
627 }
628 }
629 }
630
631 if let Some(did) = trait_.as_ref().map(|t| t.def_id()) {
632 cx.with_param_env(did, |cx| {
633 record_extern_trait(cx, did);
634 });
635 }
636
637 let (merged_attrs, cfg) =
642 merge_attrs(cx.tcx, load_attrs(cx.tcx, did), attrs, &mut CfgInfo::default());
643 trace!("merged_attrs={merged_attrs:?}");
644
645 trace!(
646 "build_impl: impl {:?} for {:?}",
647 trait_.as_ref().map(|t| t.def_id()),
648 for_.def_id(&cx.cache)
649 );
650 ret.push(clean::Item::from_def_id_and_attrs_and_parts(
651 did,
652 None,
653 clean::ImplItem(Box::new(clean::Impl {
654 safety: hir::Safety::Safe,
655 generics,
656 trait_,
657 for_,
658 items: trait_items,
659 polarity,
660 kind: if utils::has_doc_flag(tcx, did, |d| d.fake_variadic.is_some()) {
661 ImplKind::FakeVariadic
662 } else {
663 ImplKind::Normal
664 },
665 is_deprecated: tcx
666 .lookup_deprecation(did)
667 .is_some_and(|deprecation| deprecation.is_in_effect()),
668 })),
669 merged_attrs,
670 cfg,
671 ));
672}
673
674fn build_module(
675 cx: &mut DocContext<'_>,
676 did: DefId,
677 name: Symbol,
678 visited: &mut DefIdSet,
679) -> clean::Module {
680 let items = build_module_items(cx, did, name, visited, &mut FxHashSet::default(), None, None);
681
682 let span = clean::Span::new(cx.tcx.def_span(did));
683 clean::Module { items, span }
684}
685
686fn should_ignore_res(res: Res) -> bool {
689 !matches!(res, Res::Def(def_kind, _) if !should_ignore_def_kind(def_kind))
690}
691
692fn should_ignore_def_kind(kind: DefKind) -> bool {
693 !matches!(
694 kind,
695 DefKind::Trait
696 | DefKind::TraitAlias
697 | DefKind::Fn
698 | DefKind::Struct
699 | DefKind::Union
700 | DefKind::TyAlias
701 | DefKind::Enum
702 | DefKind::ForeignTy
703 | DefKind::Variant
704 | DefKind::Mod
705 | DefKind::Static { .. }
706 | DefKind::Const { .. }
707 | DefKind::Macro(_)
708 | DefKind::Use
709 )
710}
711
712fn build_module_items(
713 cx: &mut DocContext<'_>,
714 module_def_id: DefId,
715 module_name: Symbol,
716 visited: &mut DefIdSet,
717 inlined_names: &mut FxHashSet<(ItemType, Symbol)>,
718 allowed_def_ids: Option<&DefIdSet>,
719 attrs: Option<(&[hir::Attribute], Option<LocalDefId>)>,
720) -> Vec<clean::Item> {
721 let mut items = Vec::new();
722
723 for item in cx.tcx.module_children(module_def_id).iter() {
727 if !item.vis.is_public() {
728 continue;
729 }
730 let res = item.res.expect_non_local();
731 if let Some(def_id) = res.opt_def_id()
732 && let Some(allowed_def_ids) = allowed_def_ids
733 && !allowed_def_ids.contains(&def_id)
734 {
735 continue;
736 }
737 if let Some(def_id) = res.mod_def_id() {
738 if module_def_id == def_id
742 || inlined_names.contains(&(ItemType::Module, item.ident.name))
743 || !visited.insert(def_id)
744 {
745 continue;
746 }
747 }
748 if let Res::PrimTy(p) = res {
749 let prim_ty = clean::PrimitiveType::from(p);
751 items.push(clean::Item {
752 inner: Box::new(clean::ItemInner {
753 name: None,
754 item_id: ItemId::DefId(module_def_id),
757 attrs: Default::default(),
758 stability: None,
759 kind: clean::ImportItem(clean::Import::new_simple(
760 item.ident.name,
761 clean::ImportSource {
762 path: clean::Path {
763 res,
764 segments: thin_vec![clean::PathSegment {
765 name: prim_ty.as_sym(),
766 args: clean::GenericArgs::AngleBracketed {
767 args: Default::default(),
768 constraints: ThinVec::new(),
769 },
770 }],
771 },
772 did: None,
773 },
774 true,
775 )),
776 cfg: None,
777 inline_stmt_id: None,
778 }),
779 });
780 } else if let Some(def_id) = res.opt_def_id()
781 && let Some(reexport) = item.reexport_chain.first()
782 && let Some(reexport_def_id) = reexport.id()
783 && !should_ignore_def_kind(cx.tcx.def_kind(reexport_def_id))
784 && find_attr!(
785 load_attrs(cx.tcx, reexport_def_id),
786 Doc(d)
787 if d.inline.first().is_some_and(|(inline, _)| *inline == hir::attrs::DocInline::NoInline)
788 )
789 {
790 if should_ignore_res(res) || matches!(res, Res::Def(DefKind::Use, _)) {
792 continue;
793 }
794 let item = Item::from_def_id_and_parts(
796 module_def_id,
797 None,
798 clean::ImportItem(clean::Import::new_simple(
799 item.ident.name,
800 clean::ImportSource {
801 path: clean::Path {
802 res,
803 segments: thin_vec![
804 clean::PathSegment {
805 name: module_name,
806 args: clean::GenericArgs::AngleBracketed {
807 args: Default::default(),
808 constraints: ThinVec::new(),
809 },
810 },
811 clean::PathSegment {
812 name: cx.tcx.item_name(def_id),
813 args: clean::GenericArgs::AngleBracketed {
814 args: Default::default(),
815 constraints: ThinVec::new(),
816 },
817 },
818 ],
819 },
820 did: None,
821 },
822 true,
823 )),
824 cx.tcx,
825 );
826 items.push(item);
827 } else if let Some(i) = try_inline(cx, res, item.ident.name, attrs, visited) {
828 items.extend(i)
829 }
830 }
831
832 items
833}
834
835pub(crate) fn print_inlined_const(tcx: TyCtxt<'_>, did: DefId) -> String {
836 if let Some(did) = did.as_local() {
837 let hir_id = tcx.local_def_id_to_hir_id(did);
838 rustc_hir_pretty::id_to_string(&tcx, hir_id)
839 } else {
840 tcx.rendered_const(did).clone()
841 }
842}
843
844fn build_const_item(cx: &mut DocContext<'_>, def_id: DefId) -> clean::Constant {
845 let mut generics = clean_ty_generics(cx, def_id);
846 clean::simplify::move_bounds_to_generic_parameters(&mut generics);
847 let ty = clean_middle_ty(
848 ty::Binder::dummy(cx.tcx.type_of(def_id).instantiate_identity().skip_norm_wip()),
849 cx,
850 None,
851 None,
852 );
853 clean::Constant { generics, type_: ty, kind: clean::ConstantKind::Extern { def_id } }
854}
855
856fn build_static(cx: &mut DocContext<'_>, did: DefId, mutable: bool) -> clean::Static {
857 clean::Static {
858 type_: Box::new(clean_middle_ty(
859 ty::Binder::dummy(cx.tcx.type_of(did).instantiate_identity().skip_norm_wip()),
860 cx,
861 Some(did),
862 None,
863 )),
864 mutability: if mutable { Mutability::Mut } else { Mutability::Not },
865 expr: None,
866 }
867}
868
869fn build_macro(
870 tcx: TyCtxt<'_>,
871 def_id: DefId,
872 name: Symbol,
873 macro_kinds: MacroKinds,
874) -> clean::ItemKind {
875 match CStore::from_tcx(tcx).load_macro_untracked(tcx, def_id) {
876 LoadedMacro::MacroDef { def, .. } => match macro_kinds {
877 MacroKinds::DERIVE => clean::ProcMacroItem(clean::ProcMacro {
878 kind: MacroKind::Derive,
879 helpers: Vec::new(),
880 }),
881 MacroKinds::ATTR => clean::ProcMacroItem(clean::ProcMacro {
882 kind: MacroKind::Attr,
883 helpers: Vec::new(),
884 }),
885 _ => clean::MacroItem(
886 clean::Macro {
887 source: utils::display_macro_source(tcx, name, &def),
888 macro_rules: def.macro_rules,
889 },
890 macro_kinds,
891 ),
892 },
893 LoadedMacro::ProcMacro(ext) => {
894 let kind = match ext.macro_kinds() {
896 MacroKinds::BANG => MacroKind::Bang,
897 MacroKinds::ATTR => MacroKind::Attr,
898 MacroKinds::DERIVE => MacroKind::Derive,
899 _ => unreachable!(),
900 };
901 clean::ProcMacroItem(clean::ProcMacro { kind, helpers: ext.helper_attrs })
902 }
903 }
904}
905
906fn separate_self_bounds(mut g: clean::Generics) -> (clean::Generics, Vec<clean::GenericBound>) {
907 let mut ty_bounds = Vec::new();
908 g.where_predicates.retain(|pred| match *pred {
909 clean::WherePredicate::BoundPredicate { ty: clean::SelfTy, ref bounds, .. } => {
910 ty_bounds.extend(bounds.iter().cloned());
911 false
912 }
913 _ => true,
914 });
915 (g, ty_bounds)
916}
917
918pub(crate) fn record_extern_trait(cx: &mut DocContext<'_>, did: DefId) {
919 if did.is_local()
920 || cx.external_traits.contains_key(&did)
921 || cx.active_extern_traits.contains(&did)
922 {
923 return;
924 }
925
926 cx.active_extern_traits.insert(did);
927
928 debug!("record_extern_trait: {did:?}");
929 let trait_ = build_trait(cx, did);
930
931 cx.external_traits.insert(did, trait_);
932 cx.active_extern_traits.remove(&did);
933}