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 as hir;
9use rustc_hir::Mutability;
10use rustc_hir::def::{DefKind, MacroKinds, Res};
11use rustc_hir::def_id::{DefId, DefIdSet, LocalDefId, LocalModDefId};
12use rustc_metadata::creader::{CStore, LoadedMacro};
13use rustc_middle::ty::fast_reject::SimplifiedType;
14use rustc_middle::ty::{self, TyCtxt};
15use rustc_span::def_id::LOCAL_CRATE;
16use rustc_span::hygiene::MacroKind;
17use rustc_span::symbol::{Symbol, sym};
18use tracing::{debug, trace};
19
20use super::{Item, extract_cfg_from_attrs};
21use crate::clean::{
22 self, Attributes, CfgInfo, ImplKind, ItemId, Type, clean_bound_vars, clean_generics,
23 clean_impl_item, clean_middle_assoc_item, clean_middle_field, clean_middle_ty,
24 clean_poly_fn_sig, clean_trait_ref_with_constraints, clean_ty, clean_ty_alias_inner_type,
25 clean_ty_generics, clean_variant_def, utils,
26};
27use crate::core::DocContext;
28use crate::formats::item_type::ItemType;
29
30pub(crate) fn try_inline(
43 cx: &mut DocContext<'_>,
44 res: Res,
45 name: Symbol,
46 attrs: Option<(&[hir::Attribute], Option<LocalDefId>)>,
47 visited: &mut DefIdSet,
48) -> Option<Vec<clean::Item>> {
49 let did = res.opt_def_id()?;
50 if did.is_local() {
51 return None;
52 }
53 let mut ret = Vec::new();
54
55 debug!("attrs={attrs:?}");
56
57 let attrs_without_docs = attrs.map(|(attrs, def_id)| {
58 (attrs.iter().filter(|a| a.doc_str().is_none()).cloned().collect::<Vec<_>>(), def_id)
59 });
60 let attrs_without_docs =
61 attrs_without_docs.as_ref().map(|(attrs, def_id)| (&attrs[..], *def_id));
62
63 let import_def_id = attrs.and_then(|(_, def_id)| def_id);
64
65 let kind = match res {
66 Res::Def(DefKind::Trait, did) => {
67 record_extern_fqn(cx, did, ItemType::Trait);
68 cx.with_param_env(did, |cx| {
69 build_impls(cx, did, attrs_without_docs, &mut ret);
70 clean::TraitItem(Box::new(build_trait(cx, did)))
71 })
72 }
73 Res::Def(DefKind::TraitAlias, did) => {
74 record_extern_fqn(cx, did, ItemType::TraitAlias);
75 cx.with_param_env(did, |cx| clean::TraitAliasItem(build_trait_alias(cx, did)))
76 }
77 Res::Def(DefKind::Fn, did) => {
78 record_extern_fqn(cx, did, ItemType::Function);
79 cx.with_param_env(did, |cx| {
80 clean::enter_impl_trait(cx, |cx| clean::FunctionItem(build_function(cx, did)))
81 })
82 }
83 Res::Def(DefKind::Struct, did) => {
84 record_extern_fqn(cx, did, ItemType::Struct);
85 cx.with_param_env(did, |cx| {
86 build_impls(cx, did, attrs_without_docs, &mut ret);
87 clean::StructItem(build_struct(cx, did))
88 })
89 }
90 Res::Def(DefKind::Union, did) => {
91 record_extern_fqn(cx, did, ItemType::Union);
92 cx.with_param_env(did, |cx| {
93 build_impls(cx, did, attrs_without_docs, &mut ret);
94 clean::UnionItem(build_union(cx, did))
95 })
96 }
97 Res::Def(DefKind::TyAlias, did) => {
98 record_extern_fqn(cx, did, ItemType::TypeAlias);
99 cx.with_param_env(did, |cx| {
100 build_impls(cx, did, attrs_without_docs, &mut ret);
101 clean::TypeAliasItem(build_type_alias(cx, did, &mut ret))
102 })
103 }
104 Res::Def(DefKind::Enum, did) => {
105 record_extern_fqn(cx, did, ItemType::Enum);
106 cx.with_param_env(did, |cx| {
107 build_impls(cx, did, attrs_without_docs, &mut ret);
108 clean::EnumItem(build_enum(cx, did))
109 })
110 }
111 Res::Def(DefKind::ForeignTy, did) => {
112 record_extern_fqn(cx, did, ItemType::ForeignType);
113 cx.with_param_env(did, |cx| {
114 build_impls(cx, did, attrs_without_docs, &mut ret);
115 clean::ForeignTypeItem
116 })
117 }
118 Res::Def(DefKind::Variant, _) => return None,
120 Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) => return Some(Vec::new()),
123 Res::Def(DefKind::Mod, did) => {
124 record_extern_fqn(cx, did, ItemType::Module);
125 clean::ModuleItem(build_module(cx, did, visited))
126 }
127 Res::Def(DefKind::Static { .. }, did) => {
128 record_extern_fqn(cx, did, ItemType::Static);
129 cx.with_param_env(did, |cx| {
130 clean::StaticItem(build_static(cx, did, cx.tcx.is_mutable_static(did)))
131 })
132 }
133 Res::Def(DefKind::Const, did) => {
134 record_extern_fqn(cx, did, ItemType::Constant);
135 cx.with_param_env(did, |cx| {
136 let ct = build_const_item(cx, did);
137 clean::ConstantItem(Box::new(ct))
138 })
139 }
140 Res::Def(DefKind::Macro(kinds), did) => {
141 let mac = build_macro(cx, did, name, kinds);
142
143 let type_kind = match kinds {
146 MacroKinds::BANG => ItemType::Macro,
147 MacroKinds::ATTR => ItemType::ProcAttribute,
148 MacroKinds::DERIVE => ItemType::ProcDerive,
149 _ => todo!("Handle macros with multiple kinds"),
150 };
151 record_extern_fqn(cx, did, type_kind);
152 mac
153 }
154 _ => return None,
155 };
156
157 cx.inlined.insert(did.into());
158 let mut item = crate::clean::generate_item_with_correct_attrs(
159 cx,
160 kind,
161 did,
162 name,
163 import_def_id.as_slice(),
164 None,
165 );
166 item.inner.inline_stmt_id = import_def_id;
168 ret.push(item);
169 Some(ret)
170}
171
172pub(crate) fn try_inline_glob(
173 cx: &mut DocContext<'_>,
174 res: Res,
175 current_mod: LocalModDefId,
176 visited: &mut DefIdSet,
177 inlined_names: &mut FxHashSet<(ItemType, Symbol)>,
178 import: &hir::Item<'_>,
179) -> Option<Vec<clean::Item>> {
180 let did = res.opt_def_id()?;
181 if did.is_local() {
182 return None;
183 }
184
185 match res {
186 Res::Def(DefKind::Mod, did) => {
187 let reexports = cx
190 .tcx
191 .module_children_local(current_mod.to_local_def_id())
192 .iter()
193 .filter(|child| !child.reexport_chain.is_empty())
194 .filter_map(|child| child.res.opt_def_id())
195 .filter(|def_id| !cx.tcx.is_doc_hidden(def_id))
196 .collect();
197 let attrs = cx.tcx.hir_attrs(import.hir_id());
198 let mut items = build_module_items(
199 cx,
200 did,
201 visited,
202 inlined_names,
203 Some(&reexports),
204 Some((attrs, Some(import.owner_id.def_id))),
205 );
206 items.retain(|item| {
207 if let Some(name) = item.name {
208 inlined_names.insert((item.type_(), name))
211 } else {
212 true
213 }
214 });
215 Some(items)
216 }
217 _ => None,
219 }
220}
221
222pub(crate) fn load_attrs<'hir>(cx: &DocContext<'hir>, did: DefId) -> &'hir [hir::Attribute] {
223 cx.tcx.get_all_attrs(did)
224}
225
226pub(crate) fn item_relative_path(tcx: TyCtxt<'_>, def_id: DefId) -> Vec<Symbol> {
227 tcx.def_path(def_id).data.into_iter().filter_map(|elem| elem.data.get_opt_name()).collect()
228}
229
230pub(crate) fn get_item_path(tcx: TyCtxt<'_>, def_id: DefId, kind: ItemType) -> Vec<Symbol> {
235 let crate_name = tcx.crate_name(def_id.krate);
236 let relative = item_relative_path(tcx, def_id);
237
238 if let ItemType::Macro = kind {
239 if matches!(
242 CStore::from_tcx(tcx).load_macro_untracked(def_id, tcx),
243 LoadedMacro::MacroDef { def, .. } if !def.macro_rules
244 ) {
245 once(crate_name).chain(relative).collect()
246 } else {
247 vec![crate_name, *relative.last().expect("relative was empty")]
248 }
249 } else {
250 once(crate_name).chain(relative).collect()
251 }
252}
253
254pub(crate) fn record_extern_fqn(cx: &mut DocContext<'_>, did: DefId, kind: ItemType) {
259 if did.is_local() {
260 if cx.cache.exact_paths.contains_key(&did) {
261 return;
262 }
263 } else if cx.cache.external_paths.contains_key(&did) {
264 return;
265 }
266
267 let item_path = get_item_path(cx.tcx, did, kind);
268
269 if did.is_local() {
270 cx.cache.exact_paths.insert(did, item_path);
271 } else {
272 cx.cache.external_paths.insert(did, (item_path, kind));
273 }
274}
275
276pub(crate) fn build_trait(cx: &mut DocContext<'_>, did: DefId) -> clean::Trait {
277 let trait_items = cx
278 .tcx
279 .associated_items(did)
280 .in_definition_order()
281 .filter(|item| !item.is_impl_trait_in_trait())
282 .map(|item| clean_middle_assoc_item(item, cx))
283 .collect();
284
285 let generics = clean_ty_generics(cx, did);
286 let (generics, mut supertrait_bounds) = separate_self_bounds(generics);
287
288 supertrait_bounds.retain(|b| {
289 !b.is_meta_sized_bound(cx)
292 });
293
294 clean::Trait { def_id: did, generics, items: trait_items, bounds: supertrait_bounds }
295}
296
297fn build_trait_alias(cx: &mut DocContext<'_>, did: DefId) -> clean::TraitAlias {
298 let generics = clean_ty_generics(cx, did);
299 let (generics, mut bounds) = separate_self_bounds(generics);
300
301 bounds.retain(|b| {
302 !b.is_meta_sized_bound(cx)
305 });
306
307 clean::TraitAlias { generics, bounds }
308}
309
310pub(super) fn build_function(cx: &mut DocContext<'_>, def_id: DefId) -> Box<clean::Function> {
311 let sig = cx.tcx.fn_sig(def_id).instantiate_identity();
312 let mut generics = clean_ty_generics(cx, def_id);
314 let bound_vars = clean_bound_vars(sig.bound_vars(), cx);
315
316 let has_early_bound_params = !generics.params.is_empty();
326 let has_late_bound_params = !bound_vars.is_empty();
327 generics.params.extend(bound_vars);
328 if has_early_bound_params && has_late_bound_params {
329 generics.params.sort_by_key(|param| cx.tcx.def_ident_span(param.def_id).unwrap());
334 }
335
336 let decl = clean_poly_fn_sig(cx, Some(def_id), sig);
337
338 Box::new(clean::Function { decl, generics })
339}
340
341fn build_enum(cx: &mut DocContext<'_>, did: DefId) -> clean::Enum {
342 clean::Enum {
343 generics: clean_ty_generics(cx, did),
344 variants: cx.tcx.adt_def(did).variants().iter().map(|v| clean_variant_def(v, cx)).collect(),
345 }
346}
347
348fn build_struct(cx: &mut DocContext<'_>, did: DefId) -> clean::Struct {
349 let variant = cx.tcx.adt_def(did).non_enum_variant();
350
351 clean::Struct {
352 ctor_kind: variant.ctor_kind(),
353 generics: clean_ty_generics(cx, did),
354 fields: variant.fields.iter().map(|x| clean_middle_field(x, cx)).collect(),
355 }
356}
357
358fn build_union(cx: &mut DocContext<'_>, did: DefId) -> clean::Union {
359 let variant = cx.tcx.adt_def(did).non_enum_variant();
360
361 let generics = clean_ty_generics(cx, did);
362 let fields = variant.fields.iter().map(|x| clean_middle_field(x, cx)).collect();
363 clean::Union { generics, fields }
364}
365
366fn build_type_alias(
367 cx: &mut DocContext<'_>,
368 did: DefId,
369 ret: &mut Vec<Item>,
370) -> Box<clean::TypeAlias> {
371 let ty = cx.tcx.type_of(did).instantiate_identity();
372 let type_ = clean_middle_ty(ty::Binder::dummy(ty), cx, Some(did), None);
373 let inner_type = clean_ty_alias_inner_type(ty, cx, ret);
374
375 Box::new(clean::TypeAlias {
376 type_,
377 generics: clean_ty_generics(cx, did),
378 inner_type,
379 item_type: None,
380 })
381}
382
383pub(crate) fn build_impls(
385 cx: &mut DocContext<'_>,
386 did: DefId,
387 attrs: Option<(&[hir::Attribute], Option<LocalDefId>)>,
388 ret: &mut Vec<clean::Item>,
389) {
390 let _prof_timer = cx.tcx.sess.prof.generic_activity("build_inherent_impls");
391 let tcx = cx.tcx;
392
393 for &did in tcx.inherent_impls(did).iter() {
395 cx.with_param_env(did, |cx| {
396 build_impl(cx, did, attrs, ret);
397 });
398 }
399
400 if tcx.has_attr(did, sym::rustc_has_incoherent_inherent_impls) {
407 let type_ =
408 if tcx.is_trait(did) { SimplifiedType::Trait(did) } else { SimplifiedType::Adt(did) };
409 for &did in tcx.incoherent_impls(type_).iter() {
410 cx.with_param_env(did, |cx| {
411 build_impl(cx, did, attrs, ret);
412 });
413 }
414 }
415}
416
417pub(crate) fn merge_attrs(
418 cx: &mut DocContext<'_>,
419 old_attrs: &[hir::Attribute],
420 new_attrs: Option<(&[hir::Attribute], Option<LocalDefId>)>,
421 cfg_info: &mut CfgInfo,
422) -> (clean::Attributes, Option<Arc<clean::cfg::Cfg>>) {
423 if let Some((inner, item_id)) = new_attrs {
428 let mut both = inner.to_vec();
429 both.extend_from_slice(old_attrs);
430 (
431 if let Some(item_id) = item_id {
432 Attributes::from_hir_with_additional(old_attrs, (inner, item_id.to_def_id()))
433 } else {
434 Attributes::from_hir(&both)
435 },
436 extract_cfg_from_attrs(both.iter(), cx.tcx, cfg_info),
437 )
438 } else {
439 (
440 Attributes::from_hir(old_attrs),
441 extract_cfg_from_attrs(old_attrs.iter(), cx.tcx, cfg_info),
442 )
443 }
444}
445
446pub(crate) fn build_impl(
448 cx: &mut DocContext<'_>,
449 did: DefId,
450 attrs: Option<(&[hir::Attribute], Option<LocalDefId>)>,
451 ret: &mut Vec<clean::Item>,
452) {
453 if !cx.inlined.insert(did.into()) {
454 return;
455 }
456
457 let tcx = cx.tcx;
458 let _prof_timer = tcx.sess.prof.generic_activity("build_impl");
459
460 let associated_trait = tcx.impl_opt_trait_ref(did).map(ty::EarlyBinder::skip_binder);
461
462 let is_compiler_internal = |did| {
464 tcx.lookup_stability(did)
465 .is_some_and(|stab| stab.is_unstable() && stab.feature == sym::rustc_private)
466 };
467 let document_compiler_internal = is_compiler_internal(LOCAL_CRATE.as_def_id());
468 let is_directly_public = |cx: &mut DocContext<'_>, did| {
469 cx.cache.effective_visibilities.is_directly_public(tcx, did)
470 && (document_compiler_internal || !is_compiler_internal(did))
471 };
472
473 if !did.is_local()
476 && let Some(traitref) = associated_trait
477 && !is_directly_public(cx, traitref.def_id)
478 {
479 return;
480 }
481
482 let impl_item = match did.as_local() {
483 Some(did) => match &tcx.hir_expect_item(did).kind {
484 hir::ItemKind::Impl(impl_) => Some(impl_),
485 _ => panic!("`DefID` passed to `build_impl` is not an `impl"),
486 },
487 None => None,
488 };
489
490 let for_ = match &impl_item {
491 Some(impl_) => clean_ty(impl_.self_ty, cx),
492 None => clean_middle_ty(
493 ty::Binder::dummy(tcx.type_of(did).instantiate_identity()),
494 cx,
495 Some(did),
496 None,
497 ),
498 };
499
500 if !did.is_local()
503 && let Some(did) = for_.def_id(&cx.cache)
504 && !is_directly_public(cx, did)
505 {
506 return;
507 }
508
509 let document_hidden = cx.document_hidden();
510 let (trait_items, generics) = match impl_item {
511 Some(impl_) => (
512 impl_
513 .items
514 .iter()
515 .map(|&item| tcx.hir_impl_item(item))
516 .filter(|item| {
517 if document_hidden {
524 return true;
525 }
526 if let Some(associated_trait) = associated_trait {
527 let assoc_tag = match item.kind {
528 hir::ImplItemKind::Const(..) => ty::AssocTag::Const,
529 hir::ImplItemKind::Fn(..) => ty::AssocTag::Fn,
530 hir::ImplItemKind::Type(..) => ty::AssocTag::Type,
531 };
532 let trait_item = tcx
533 .associated_items(associated_trait.def_id)
534 .find_by_ident_and_kind(
535 tcx,
536 item.ident,
537 assoc_tag,
538 associated_trait.def_id,
539 )
540 .unwrap(); !tcx.is_doc_hidden(trait_item.def_id)
542 } else {
543 true
544 }
545 })
546 .map(|item| clean_impl_item(item, cx))
547 .collect::<Vec<_>>(),
548 clean_generics(impl_.generics, cx),
549 ),
550 None => (
551 tcx.associated_items(did)
552 .in_definition_order()
553 .filter(|item| !item.is_impl_trait_in_trait())
554 .filter(|item| {
555 if let Some(associated_trait) = associated_trait {
559 let trait_item = tcx
560 .associated_items(associated_trait.def_id)
561 .find_by_ident_and_kind(
562 tcx,
563 item.ident(tcx),
564 item.as_tag(),
565 associated_trait.def_id,
566 )
567 .unwrap(); document_hidden || !tcx.is_doc_hidden(trait_item.def_id)
569 } else {
570 item.visibility(tcx).is_public()
571 }
572 })
573 .map(|item| clean_middle_assoc_item(item, cx))
574 .collect::<Vec<_>>(),
575 clean::enter_impl_trait(cx, |cx| clean_ty_generics(cx, did)),
576 ),
577 };
578 let polarity = if associated_trait.is_some() {
579 tcx.impl_polarity(did)
580 } else {
581 ty::ImplPolarity::Positive
582 };
583 let trait_ = associated_trait
584 .map(|t| clean_trait_ref_with_constraints(cx, ty::Binder::dummy(t), ThinVec::new()));
585 if trait_.as_ref().map(|t| t.def_id()) == tcx.lang_items().deref_trait() {
586 super::build_deref_target_impls(cx, &trait_items, ret);
587 }
588
589 if !document_hidden {
590 let mut stack: Vec<&Type> = vec![&for_];
592
593 if let Some(did) = trait_.as_ref().map(|t| t.def_id())
594 && tcx.is_doc_hidden(did)
595 {
596 return;
597 }
598
599 if let Some(generics) = trait_.as_ref().and_then(|t| t.generics()) {
600 stack.extend(generics);
601 }
602
603 while let Some(ty) = stack.pop() {
604 if let Some(did) = ty.def_id(&cx.cache)
605 && tcx.is_doc_hidden(did)
606 {
607 return;
608 }
609 if let Some(generics) = ty.generics() {
610 stack.extend(generics);
611 }
612 }
613 }
614
615 if let Some(did) = trait_.as_ref().map(|t| t.def_id()) {
616 cx.with_param_env(did, |cx| {
617 record_extern_trait(cx, did);
618 });
619 }
620
621 let (merged_attrs, cfg) = merge_attrs(cx, load_attrs(cx, did), attrs, &mut CfgInfo::default());
626 trace!("merged_attrs={merged_attrs:?}");
627
628 trace!(
629 "build_impl: impl {:?} for {:?}",
630 trait_.as_ref().map(|t| t.def_id()),
631 for_.def_id(&cx.cache)
632 );
633 ret.push(clean::Item::from_def_id_and_attrs_and_parts(
634 did,
635 None,
636 clean::ImplItem(Box::new(clean::Impl {
637 safety: hir::Safety::Safe,
638 generics,
639 trait_,
640 for_,
641 items: trait_items,
642 polarity,
643 kind: if utils::has_doc_flag(tcx, did, sym::fake_variadic) {
644 ImplKind::FakeVariadic
645 } else {
646 ImplKind::Normal
647 },
648 })),
649 merged_attrs,
650 cfg,
651 ));
652}
653
654fn build_module(cx: &mut DocContext<'_>, did: DefId, visited: &mut DefIdSet) -> clean::Module {
655 let items = build_module_items(cx, did, visited, &mut FxHashSet::default(), None, None);
656
657 let span = clean::Span::new(cx.tcx.def_span(did));
658 clean::Module { items, span }
659}
660
661fn build_module_items(
662 cx: &mut DocContext<'_>,
663 did: DefId,
664 visited: &mut DefIdSet,
665 inlined_names: &mut FxHashSet<(ItemType, Symbol)>,
666 allowed_def_ids: Option<&DefIdSet>,
667 attrs: Option<(&[hir::Attribute], Option<LocalDefId>)>,
668) -> Vec<clean::Item> {
669 let mut items = Vec::new();
670
671 for item in cx.tcx.module_children(did).iter() {
675 if item.vis.is_public() {
676 let res = item.res.expect_non_local();
677 if let Some(def_id) = res.opt_def_id()
678 && let Some(allowed_def_ids) = allowed_def_ids
679 && !allowed_def_ids.contains(&def_id)
680 {
681 continue;
682 }
683 if let Some(def_id) = res.mod_def_id() {
684 if did == def_id
688 || inlined_names.contains(&(ItemType::Module, item.ident.name))
689 || !visited.insert(def_id)
690 {
691 continue;
692 }
693 }
694 if let Res::PrimTy(p) = res {
695 let prim_ty = clean::PrimitiveType::from(p);
697 items.push(clean::Item {
698 inner: Box::new(clean::ItemInner {
699 name: None,
700 item_id: ItemId::DefId(did),
703 attrs: Default::default(),
704 stability: None,
705 kind: clean::ImportItem(clean::Import::new_simple(
706 item.ident.name,
707 clean::ImportSource {
708 path: clean::Path {
709 res,
710 segments: thin_vec![clean::PathSegment {
711 name: prim_ty.as_sym(),
712 args: clean::GenericArgs::AngleBracketed {
713 args: Default::default(),
714 constraints: ThinVec::new(),
715 },
716 }],
717 },
718 did: None,
719 },
720 true,
721 )),
722 cfg: None,
723 inline_stmt_id: None,
724 }),
725 });
726 } else if let Some(i) = try_inline(cx, res, item.ident.name, attrs, visited) {
727 items.extend(i)
728 }
729 }
730 }
731
732 items
733}
734
735pub(crate) fn print_inlined_const(tcx: TyCtxt<'_>, did: DefId) -> String {
736 if let Some(did) = did.as_local() {
737 let hir_id = tcx.local_def_id_to_hir_id(did);
738 rustc_hir_pretty::id_to_string(&tcx, hir_id)
739 } else {
740 tcx.rendered_const(did).clone()
741 }
742}
743
744fn build_const_item(cx: &mut DocContext<'_>, def_id: DefId) -> clean::Constant {
745 let mut generics = clean_ty_generics(cx, def_id);
746 clean::simplify::move_bounds_to_generic_parameters(&mut generics);
747 let ty = clean_middle_ty(
748 ty::Binder::dummy(cx.tcx.type_of(def_id).instantiate_identity()),
749 cx,
750 None,
751 None,
752 );
753 clean::Constant { generics, type_: ty, kind: clean::ConstantKind::Extern { def_id } }
754}
755
756fn build_static(cx: &mut DocContext<'_>, did: DefId, mutable: bool) -> clean::Static {
757 clean::Static {
758 type_: Box::new(clean_middle_ty(
759 ty::Binder::dummy(cx.tcx.type_of(did).instantiate_identity()),
760 cx,
761 Some(did),
762 None,
763 )),
764 mutability: if mutable { Mutability::Mut } else { Mutability::Not },
765 expr: None,
766 }
767}
768
769fn build_macro(
770 cx: &mut DocContext<'_>,
771 def_id: DefId,
772 name: Symbol,
773 macro_kinds: MacroKinds,
774) -> clean::ItemKind {
775 match CStore::from_tcx(cx.tcx).load_macro_untracked(def_id, cx.tcx) {
776 LoadedMacro::MacroDef { def, .. } => match macro_kinds {
779 MacroKinds::BANG => clean::MacroItem(clean::Macro {
780 source: utils::display_macro_source(cx, name, &def),
781 macro_rules: def.macro_rules,
782 }),
783 MacroKinds::DERIVE => clean::ProcMacroItem(clean::ProcMacro {
784 kind: MacroKind::Derive,
785 helpers: Vec::new(),
786 }),
787 MacroKinds::ATTR => clean::ProcMacroItem(clean::ProcMacro {
788 kind: MacroKind::Attr,
789 helpers: Vec::new(),
790 }),
791 _ => todo!("Handle macros with multiple kinds"),
792 },
793 LoadedMacro::ProcMacro(ext) => {
794 let kind = match ext.macro_kinds() {
796 MacroKinds::BANG => MacroKind::Bang,
797 MacroKinds::ATTR => MacroKind::Attr,
798 MacroKinds::DERIVE => MacroKind::Derive,
799 _ => unreachable!(),
800 };
801 clean::ProcMacroItem(clean::ProcMacro { kind, helpers: ext.helper_attrs })
802 }
803 }
804}
805
806fn separate_self_bounds(mut g: clean::Generics) -> (clean::Generics, Vec<clean::GenericBound>) {
807 let mut ty_bounds = Vec::new();
808 g.where_predicates.retain(|pred| match *pred {
809 clean::WherePredicate::BoundPredicate { ty: clean::SelfTy, ref bounds, .. } => {
810 ty_bounds.extend(bounds.iter().cloned());
811 false
812 }
813 _ => true,
814 });
815 (g, ty_bounds)
816}
817
818pub(crate) fn record_extern_trait(cx: &mut DocContext<'_>, did: DefId) {
819 if did.is_local()
820 || cx.external_traits.contains_key(&did)
821 || cx.active_extern_traits.contains(&did)
822 {
823 return;
824 }
825
826 cx.active_extern_traits.insert(did);
827
828 debug!("record_extern_trait: {did:?}");
829 let trait_ = build_trait(cx, did);
830
831 cx.external_traits.insert(did, trait_);
832 cx.active_extern_traits.remove(&did);
833}