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, 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
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 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 Res::Def(DefKind::Variant, _) => return None,
119 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 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 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 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 inlined_names.insert((item.type_(), name))
210 } else {
211 true
212 }
213 });
214 Some(items)
215 }
216 _ => None,
218 }
219}
220
221pub(crate) fn load_attrs<'hir>(cx: &DocContext<'hir>, did: DefId) -> &'hir [hir::Attribute] {
222 #[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
231pub(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 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
255pub(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 !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 !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 let mut generics = clean_ty_generics(cx, def_id);
315 let bound_vars = clean_bound_vars(sig.bound_vars(), cx);
316
317 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 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
384pub(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 &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 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 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
447pub(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 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 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 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 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(); !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 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(); 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 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 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 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 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 let prim_ty = clean::PrimitiveType::from(p);
701 items.push(clean::Item {
702 inner: Box::new(clean::ItemInner {
703 name: None,
704 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 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 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}