1mod auto_trait;
25mod blanket_impl;
26pub(crate) mod cfg;
27pub(crate) mod inline;
28mod render_macro_matchers;
29mod simplify;
30pub(crate) mod types;
31pub(crate) mod utils;
32
33use std::borrow::Cow;
34use std::collections::BTreeMap;
35use std::mem;
36
37use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet, IndexEntry};
38use rustc_data_structures::thin_vec::ThinVec;
39use rustc_errors::codes::*;
40use rustc_errors::{FatalError, struct_span_code_err};
41use rustc_hir as hir;
42use rustc_hir::attrs::{AttributeKind, DocAttribute, DocInline};
43use rustc_hir::def::{CtorKind, DefKind, MacroKinds, Res};
44use rustc_hir::def_id::{DefId, DefIdMap, DefIdSet, LOCAL_CRATE, LocalDefId};
45use rustc_hir::{LangItem, PredicateOrigin, find_attr};
46use rustc_hir_analysis::{lower_const_arg_for_rustdoc, lower_ty};
47use rustc_middle::metadata::Reexport;
48use rustc_middle::middle::resolve_bound_vars as rbv;
49use rustc_middle::ty::{
50 self, AdtKind, GenericArgsRef, Ty, TyCtxt, TypeVisitableExt, TypingMode, Unnormalized,
51};
52use rustc_middle::{bug, span_bug};
53use rustc_span::ExpnKind;
54use rustc_span::hygiene::{AstPass, MacroKind};
55use rustc_span::symbol::{Ident, Symbol, kw};
56use rustc_trait_selection::traits::wf::object_region_bounds;
57use tracing::{debug, instrument};
58use utils::*;
59
60pub(crate) use self::cfg::{CfgInfo, extract_cfg_from_attrs};
61pub(crate) use self::types::*;
62pub(crate) use self::utils::{krate, register_res, synthesize_auto_trait_and_blanket_impls};
63use crate::core::DocContext;
64use crate::formats::item_type::ItemType;
65use crate::visit_ast;
66
67pub(crate) fn clean_doc_module<'tcx>(
68 doc: &visit_ast::Module<'tcx>,
69 cx: &mut DocContext<'tcx>,
70) -> Item {
71 let mut items: Vec<Item> = vec![];
72 let mut inserted = FxHashSet::default();
73 items.extend(doc.foreigns.iter().map(|visit_ast::Foreign { item, renamed, import_id }| {
74 let item = clean_maybe_renamed_foreign_item(cx, item, *renamed, *import_id);
75 if let Some(name) = item.name
76 && (cx.document_hidden() || !item.is_doc_hidden())
77 {
78 inserted.insert((item.type_(), name));
79 }
80 item
81 }));
82 items.extend(doc.mods.iter().filter_map(|x| {
83 if !inserted.insert((ItemType::Module, x.name)) {
84 return None;
85 }
86 let item = clean_doc_module(x, cx);
87 if !cx.document_hidden() && item.is_doc_hidden() {
88 inserted.remove(&(ItemType::Module, x.name));
92 }
93 Some(item)
94 }));
95
96 items.extend(doc.items.values().flat_map(
102 |visit_ast::ItemEntry { item, renamed, import_ids }| {
103 if matches!(item.kind, hir::ItemKind::Use(_, hir::UseKind::Glob)) {
105 return Vec::new();
106 }
107 let v = clean_maybe_renamed_item(cx, item, *renamed, import_ids);
108 for item in &v {
109 if let Some(name) = item.name
110 && (cx.document_hidden() || !item.is_doc_hidden())
111 {
112 inserted.insert((item.type_(), name));
113 }
114 }
115 v
116 },
117 ));
118 items.extend(doc.inlined_foreigns.iter().flat_map(
119 |((_, renamed), visit_ast::InlinedForeign { res, import_id })| {
120 let Some(def_id) = res.opt_def_id() else { return Vec::new() };
121 let name = renamed.unwrap_or_else(|| cx.tcx.item_name(def_id));
122 let import = cx.tcx.hir_expect_item(*import_id);
123 match import.kind {
124 hir::ItemKind::Use(path, kind) => {
125 let hir::UsePath { segments, span, .. } = *path;
126 let path = hir::Path { segments, res: *res, span };
127 clean_use_statement_inner(
128 import,
129 Some(name),
130 &path,
131 kind,
132 cx,
133 &mut Default::default(),
134 )
135 }
136 _ => unreachable!(),
137 }
138 },
139 ));
140 items.extend(doc.items.values().flat_map(
141 |visit_ast::ItemEntry { item, renamed, import_ids: _ }| {
142 if let hir::ItemKind::Use(path, hir::UseKind::Glob) = item.kind {
144 clean_use_statement(item, *renamed, path, hir::UseKind::Glob, cx, &mut inserted)
145 } else {
146 Vec::new()
148 }
149 },
150 ));
151
152 let span = Span::new({
156 let where_outer = doc.where_outer(cx.tcx);
157 let sm = cx.sess().source_map();
158 let outer = sm.lookup_char_pos(where_outer.lo());
159 let inner = sm.lookup_char_pos(doc.where_inner.lo());
160 if outer.file.start_pos == inner.file.start_pos {
161 where_outer
163 } else {
164 doc.where_inner
166 }
167 });
168
169 let kind = ModuleItem(Module { items, span });
170 generate_item_with_correct_attrs(
171 cx,
172 kind,
173 doc.def_id.to_def_id(),
174 doc.name,
175 doc.import_id.as_slice(),
176 doc.renamed,
177 )
178}
179
180fn is_glob_import(tcx: TyCtxt<'_>, import_id: LocalDefId) -> bool {
181 if let hir::Node::Item(item) = tcx.hir_node_by_def_id(import_id)
182 && let hir::ItemKind::Use(_, use_kind) = item.kind
183 {
184 use_kind == hir::UseKind::Glob
185 } else {
186 false
187 }
188}
189
190pub(crate) fn macro_reexport_is_inline(
192 tcx: TyCtxt<'_>,
193 import_id: LocalDefId,
194 def_id: DefId,
195) -> bool {
196 if !matches!(tcx.def_kind(def_id), DefKind::Macro(MacroKinds::BANG)) {
197 return false;
198 }
199
200 for reexport_def_id in reexport_chain(tcx, import_id, def_id).iter().flat_map(|r| r.id()) {
201 let is_hidden = tcx.is_doc_hidden(reexport_def_id);
202 let is_inline = find_attr!(
203 inline::load_attrs(tcx, reexport_def_id),
204 Doc(d)
205 if d.inline.first().is_some_and(|(inline, _)| *inline == DocInline::Inline)
206 );
207
208 if is_hidden {
210 return false;
211 }
212 if is_inline {
213 return true;
214 }
215 }
216 false
217}
218
219fn generate_item_with_correct_attrs(
220 cx: &mut DocContext<'_>,
221 kind: ItemKind,
222 def_id: DefId,
223 name: Symbol,
224 import_ids: &[LocalDefId],
225 renamed: Option<Symbol>,
226) -> Item {
227 let tcx = cx.tcx;
228 let target_attrs = inline::load_attrs(tcx, def_id);
229 let attrs = if !import_ids.is_empty() {
230 let mut attrs = Vec::with_capacity(import_ids.len());
231 let mut is_inline = false;
232
233 for import_id in import_ids.iter().copied() {
234 let import_is_inline = find_attr!(
240 inline::load_attrs(tcx, import_id.to_def_id()),
241 Doc(d)
242 if d.inline.first().is_some_and(|(inline, _)| *inline == DocInline::Inline)
243 ) || (is_glob_import(tcx, import_id)
244 && (cx.document_hidden() || !tcx.is_doc_hidden(def_id)))
245 || macro_reexport_is_inline(tcx, import_id, def_id);
246 is_inline = is_inline || import_is_inline;
247 attrs.extend(get_all_import_attributes(cx, import_id, def_id, is_inline));
248 }
249 let keep_target_cfg = is_inline || matches!(kind, ItemKind::TypeAliasItem(..));
250 add_without_unwanted_attributes(&mut attrs, target_attrs, keep_target_cfg, None);
251 attrs
252 } else {
253 target_attrs.iter().map(|attr| (Cow::Borrowed(attr), None)).collect()
255 };
256 let attrs = Attributes::from_hir_iter(attrs.iter().map(|(attr, did)| (&**attr, *did)), false);
257
258 let name = renamed.or(Some(name));
259 let mut item = Item::from_def_id_and_attrs_and_parts(def_id, name, kind, attrs, None);
260 item.inner.inline_stmt_id = import_ids.first().copied();
262 item
263}
264
265fn clean_generic_bound<'tcx>(
266 bound: &hir::GenericBound<'tcx>,
267 cx: &mut DocContext<'tcx>,
268) -> Option<GenericBound> {
269 Some(match bound {
270 hir::GenericBound::Outlives(lt) => GenericBound::Outlives(clean_lifetime(lt, cx)),
271 hir::GenericBound::Trait(t) => {
272 if let hir::BoundConstness::Maybe(_) = t.modifiers.constness
274 && cx.tcx.lang_items().destruct_trait() == Some(t.trait_ref.trait_def_id().unwrap())
275 {
276 return None;
277 }
278
279 GenericBound::TraitBound(clean_poly_trait_ref(t, cx), t.modifiers)
280 }
281 hir::GenericBound::Use(args, ..) => {
282 GenericBound::Use(args.iter().map(|arg| clean_precise_capturing_arg(arg, cx)).collect())
283 }
284 })
285}
286
287pub(crate) fn clean_trait_ref_with_constraints<'tcx>(
288 cx: &mut DocContext<'tcx>,
289 trait_ref: ty::PolyTraitRef<'tcx>,
290 constraints: ThinVec<AssocItemConstraint>,
291) -> Path {
292 let kind = ItemType::from_def_id(trait_ref.def_id(), cx.tcx);
293 if !matches!(kind, ItemType::Trait | ItemType::TraitAlias) {
294 span_bug!(cx.tcx.def_span(trait_ref.def_id()), "`TraitRef` had unexpected kind {kind:?}");
295 }
296 inline::record_extern_fqn(cx, trait_ref.def_id(), kind);
297 let path = clean_middle_path(
298 cx,
299 trait_ref.def_id(),
300 true,
301 constraints,
302 trait_ref.map_bound(|tr| tr.args),
303 );
304
305 debug!(?trait_ref);
306
307 path
308}
309
310fn clean_poly_trait_ref_with_constraints<'tcx>(
311 cx: &mut DocContext<'tcx>,
312 poly_trait_ref: ty::PolyTraitRef<'tcx>,
313 constraints: ThinVec<AssocItemConstraint>,
314) -> GenericBound {
315 GenericBound::TraitBound(
316 PolyTrait {
317 trait_: clean_trait_ref_with_constraints(cx, poly_trait_ref, constraints),
318 generic_params: clean_bound_vars(poly_trait_ref.bound_vars(), cx.tcx),
319 },
320 hir::TraitBoundModifiers::NONE,
321 )
322}
323
324fn clean_lifetime(lifetime: &hir::Lifetime, cx: &DocContext<'_>) -> Lifetime {
325 if let Some(
326 rbv::ResolvedArg::EarlyBound(did)
327 | rbv::ResolvedArg::LateBound(_, _, did)
328 | rbv::ResolvedArg::Free(_, did),
329 ) = cx.tcx.named_bound_var(lifetime.hir_id)
330 && let Some(lt) = cx.args.get(&did.to_def_id()).and_then(|arg| arg.as_lt())
331 {
332 return *lt;
333 }
334 Lifetime(lifetime.ident.name)
335}
336
337pub(crate) fn clean_precise_capturing_arg(
338 arg: &hir::PreciseCapturingArg<'_>,
339 cx: &DocContext<'_>,
340) -> PreciseCapturingArg {
341 match arg {
342 hir::PreciseCapturingArg::Lifetime(lt) => {
343 PreciseCapturingArg::Lifetime(clean_lifetime(lt, cx))
344 }
345 hir::PreciseCapturingArg::Param(param) => PreciseCapturingArg::Param(param.ident.name),
346 }
347}
348
349pub(crate) fn clean_const_item_rhs<'tcx>(
350 ct_rhs: hir::ConstItemRhs<'tcx>,
351 parent: DefId,
352) -> ConstantKind {
353 match ct_rhs {
354 hir::ConstItemRhs::Body(body) => ConstantKind::Local { def_id: parent, body },
355 hir::ConstItemRhs::TypeConst(ct) => clean_const(ct),
356 }
357}
358
359pub(crate) fn clean_const<'tcx>(constant: &hir::ConstArg<'tcx>) -> ConstantKind {
360 match &constant.kind {
361 hir::ConstArgKind::Path(qpath) => {
362 ConstantKind::Path { path: qpath_to_string(qpath).into() }
363 }
364 hir::ConstArgKind::Struct(..) => {
365 ConstantKind::Path { path: "/* STRUCT EXPR */".to_string().into() }
367 }
368 hir::ConstArgKind::TupleCall(..) => {
369 ConstantKind::Path { path: "/* TUPLE CALL */".to_string().into() }
370 }
371 hir::ConstArgKind::Tup(..) => {
372 ConstantKind::Path { path: "/* TUPLE EXPR */".to_string().into() }
374 }
375 hir::ConstArgKind::Array(..) => {
376 ConstantKind::Path { path: "/* ARRAY EXPR */".to_string().into() }
377 }
378 hir::ConstArgKind::Anon(anon) => ConstantKind::Anonymous { body: anon.body },
379 hir::ConstArgKind::Infer(..) | hir::ConstArgKind::Error(..) => ConstantKind::Infer,
380 hir::ConstArgKind::Literal { .. } => {
381 ConstantKind::Path { path: "/* LITERAL */".to_string().into() }
382 }
383 }
384}
385
386pub(crate) fn clean_middle_const<'tcx>(
387 constant: ty::Binder<'tcx, ty::Const<'tcx>>,
388) -> ConstantKind {
389 ConstantKind::TyConst { expr: constant.skip_binder().to_string().into() }
391}
392
393pub(crate) fn clean_middle_region<'tcx>(
394 region: ty::Region<'tcx>,
395 tcx: TyCtxt<'tcx>,
396) -> Option<Lifetime> {
397 region.get_name(tcx).map(Lifetime)
398}
399
400fn clean_where_predicate<'tcx>(
401 predicate: &hir::WherePredicate<'tcx>,
402 cx: &mut DocContext<'tcx>,
403) -> Option<WherePredicate> {
404 if !predicate.kind.in_where_clause() {
405 return None;
406 }
407 Some(match predicate.kind {
408 hir::WherePredicateKind::BoundPredicate(wbp) => {
409 let bound_params = wbp
410 .bound_generic_params
411 .iter()
412 .map(|param| clean_generic_param(cx, None, param))
413 .collect();
414 WherePredicate::BoundPredicate {
415 ty: clean_ty(wbp.bounded_ty, cx),
416 bounds: wbp.bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(),
417 bound_params,
418 }
419 }
420 hir::WherePredicateKind::RegionPredicate(wrp) => WherePredicate::RegionPredicate {
421 lifetime: clean_lifetime(wrp.lifetime, cx),
422 bounds: wrp.bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(),
423 },
424 })
425}
426
427pub(crate) fn clean_predicate<'tcx>(
428 predicate: ty::Clause<'tcx>,
429 cx: &mut DocContext<'tcx>,
430) -> Option<WherePredicate> {
431 let bound_predicate = predicate.kind();
432 match bound_predicate.skip_binder() {
433 ty::ClauseKind::Trait(pred) => clean_poly_trait_predicate(bound_predicate.rebind(pred), cx),
434 ty::ClauseKind::RegionOutlives(pred) => Some(clean_region_outlives_predicate(pred, cx.tcx)),
435 ty::ClauseKind::TypeOutlives(pred) => {
436 Some(clean_type_outlives_predicate(bound_predicate.rebind(pred), cx))
437 }
438 ty::ClauseKind::Projection(pred) => {
439 Some(clean_projection_predicate(bound_predicate.rebind(pred), cx))
440 }
441 ty::ClauseKind::ConstEvaluatable(..)
443 | ty::ClauseKind::WellFormed(..)
444 | ty::ClauseKind::ConstArgHasType(..)
445 | ty::ClauseKind::UnstableFeature(..)
446 | ty::ClauseKind::HostEffect(_) => None,
448 }
449}
450
451fn clean_poly_trait_predicate<'tcx>(
452 pred: ty::PolyTraitPredicate<'tcx>,
453 cx: &mut DocContext<'tcx>,
454) -> Option<WherePredicate> {
455 if Some(pred.skip_binder().def_id()) == cx.tcx.lang_items().destruct_trait() {
458 return None;
459 }
460
461 let poly_trait_ref = pred.map_bound(|pred| pred.trait_ref);
462 Some(WherePredicate::BoundPredicate {
463 ty: clean_middle_ty(poly_trait_ref.self_ty(), cx, None, None),
464 bounds: vec![clean_poly_trait_ref_with_constraints(cx, poly_trait_ref, ThinVec::new())],
465 bound_params: Vec::new(),
466 })
467}
468
469fn clean_region_outlives_predicate<'tcx>(
470 pred: ty::RegionOutlivesPredicate<'tcx>,
471 tcx: TyCtxt<'tcx>,
472) -> WherePredicate {
473 let ty::OutlivesPredicate(a, b) = pred;
474
475 WherePredicate::RegionPredicate {
476 lifetime: clean_middle_region(a, tcx).expect("failed to clean lifetime"),
477 bounds: vec![GenericBound::Outlives(
478 clean_middle_region(b, tcx).expect("failed to clean bounds"),
479 )],
480 }
481}
482
483fn clean_type_outlives_predicate<'tcx>(
484 pred: ty::Binder<'tcx, ty::TypeOutlivesPredicate<'tcx>>,
485 cx: &mut DocContext<'tcx>,
486) -> WherePredicate {
487 let ty::OutlivesPredicate(ty, lt) = pred.skip_binder();
488
489 WherePredicate::BoundPredicate {
490 ty: clean_middle_ty(pred.rebind(ty), cx, None, None),
491 bounds: vec![GenericBound::Outlives(
492 clean_middle_region(lt, cx.tcx).expect("failed to clean lifetimes"),
493 )],
494 bound_params: Vec::new(),
495 }
496}
497
498fn clean_middle_term<'tcx>(
499 term: ty::Binder<'tcx, ty::Term<'tcx>>,
500 cx: &mut DocContext<'tcx>,
501) -> Term {
502 match term.skip_binder().kind() {
503 ty::TermKind::Ty(ty) => Term::Type(clean_middle_ty(term.rebind(ty), cx, None, None)),
504 ty::TermKind::Const(c) => Term::Constant(clean_middle_const(term.rebind(c))),
505 }
506}
507
508fn clean_hir_term<'tcx>(
509 assoc_item: Option<DefId>,
510 term: &hir::Term<'tcx>,
511 cx: &mut DocContext<'tcx>,
512) -> Term {
513 match term {
514 hir::Term::Ty(ty) => Term::Type(clean_ty(ty, cx)),
515 hir::Term::Const(c) => {
516 let ty = cx.tcx.type_of(assoc_item.unwrap()).instantiate_identity().skip_norm_wip();
518 let ct = lower_const_arg_for_rustdoc(cx.tcx, c, ty);
519 Term::Constant(clean_middle_const(ty::Binder::dummy(ct)))
520 }
521 }
522}
523
524fn clean_projection_predicate<'tcx>(
525 pred: ty::Binder<'tcx, ty::ProjectionPredicate<'tcx>>,
526 cx: &mut DocContext<'tcx>,
527) -> WherePredicate {
528 WherePredicate::ProjectionPredicate {
529 lhs: clean_projection(pred.map_bound(|p| p.projection_term), cx, None),
530 rhs: clean_middle_term(pred.map_bound(|p| p.term), cx),
531 }
532}
533
534fn clean_projection<'tcx>(
535 proj: ty::Binder<'tcx, ty::AliasTerm<'tcx>>,
536 cx: &mut DocContext<'tcx>,
537 parent_def_id: Option<DefId>,
538) -> QPathData {
539 let trait_ = clean_trait_ref_with_constraints(
540 cx,
541 proj.map_bound(|proj| proj.trait_ref(cx.tcx)),
542 ThinVec::new(),
543 );
544 let self_type = clean_middle_ty(proj.map_bound(|proj| proj.self_ty()), cx, None, None);
545 let self_def_id = match parent_def_id {
546 Some(parent_def_id) => cx.tcx.opt_parent(parent_def_id).or(Some(parent_def_id)),
547 None => self_type.def_id(&cx.cache),
548 };
549 let should_fully_qualify = should_fully_qualify_path(self_def_id, &trait_, &self_type);
550
551 QPathData {
552 assoc: projection_to_path_segment(proj, cx),
553 self_type,
554 should_fully_qualify,
555 trait_: Some(trait_),
556 }
557}
558
559fn should_fully_qualify_path(self_def_id: Option<DefId>, trait_: &Path, self_type: &Type) -> bool {
560 !trait_.segments.is_empty()
561 && self_def_id
562 .zip(Some(trait_.def_id()))
563 .map_or(!self_type.is_self_type(), |(id, trait_)| id != trait_)
564}
565
566fn projection_to_path_segment<'tcx>(
567 proj: ty::Binder<'tcx, ty::AliasTerm<'tcx>>,
568 cx: &mut DocContext<'tcx>,
569) -> PathSegment {
570 let def_id = proj.skip_binder().expect_projection_def_id();
571 let generics = cx.tcx.generics_of(def_id);
572 PathSegment {
573 name: cx.tcx.item_name(def_id),
574 args: GenericArgs::AngleBracketed {
575 args: clean_middle_generic_args(
576 cx,
577 proj.map_bound(|ty| &ty.args[generics.parent_count..]),
578 false,
579 def_id,
580 ),
581 constraints: Default::default(),
582 },
583 }
584}
585
586fn clean_generic_param_def(
587 def: &ty::GenericParamDef,
588 defaults: ParamDefaults,
589 cx: &mut DocContext<'_>,
590) -> GenericParamDef {
591 let (name, kind) = match def.kind {
592 ty::GenericParamDefKind::Lifetime => {
593 (def.name, GenericParamDefKind::Lifetime { outlives: ThinVec::new() })
594 }
595 ty::GenericParamDefKind::Type { has_default, synthetic, .. } => {
596 let default = if let ParamDefaults::Yes = defaults
597 && has_default
598 {
599 Some(clean_middle_ty(
600 ty::Binder::dummy(
601 cx.tcx.type_of(def.def_id).instantiate_identity().skip_norm_wip(),
602 ),
603 cx,
604 Some(def.def_id),
605 None,
606 ))
607 } else {
608 None
609 };
610 (
611 def.name,
612 GenericParamDefKind::Type {
613 bounds: ThinVec::new(), default: default.map(Box::new),
615 synthetic,
616 },
617 )
618 }
619 ty::GenericParamDefKind::Const { has_default } => (
620 def.name,
621 GenericParamDefKind::Const {
622 ty: Box::new(clean_middle_ty(
623 ty::Binder::dummy(
624 cx.tcx.type_of(def.def_id).instantiate_identity().skip_norm_wip(),
625 ),
626 cx,
627 Some(def.def_id),
628 None,
629 )),
630 default: if let ParamDefaults::Yes = defaults
631 && has_default
632 {
633 Some(Box::new(
634 cx.tcx
635 .const_param_default(def.def_id)
636 .instantiate_identity()
637 .skip_norm_wip()
638 .to_string(),
639 ))
640 } else {
641 None
642 },
643 },
644 ),
645 };
646
647 GenericParamDef { name, def_id: def.def_id, kind }
648}
649
650enum ParamDefaults {
652 Yes,
653 No,
654}
655
656fn clean_generic_param<'tcx>(
657 cx: &mut DocContext<'tcx>,
658 generics: Option<&hir::Generics<'tcx>>,
659 param: &hir::GenericParam<'tcx>,
660) -> GenericParamDef {
661 let (name, kind) = match param.kind {
662 hir::GenericParamKind::Lifetime { .. } => {
663 let outlives = if let Some(generics) = generics {
664 generics
665 .outlives_for_param(param.def_id)
666 .filter(|bp| !bp.in_where_clause)
667 .flat_map(|bp| bp.bounds)
668 .map(|bound| match bound {
669 hir::GenericBound::Outlives(lt) => clean_lifetime(lt, cx),
670 _ => panic!(),
671 })
672 .collect()
673 } else {
674 ThinVec::new()
675 };
676 (param.name.ident().name, GenericParamDefKind::Lifetime { outlives })
677 }
678 hir::GenericParamKind::Type { ref default, synthetic } => {
679 let bounds = if let Some(generics) = generics {
680 generics
681 .bounds_for_param(param.def_id)
682 .filter(|bp| bp.origin != PredicateOrigin::WhereClause)
683 .flat_map(|bp| bp.bounds)
684 .filter_map(|x| clean_generic_bound(x, cx))
685 .collect()
686 } else {
687 ThinVec::new()
688 };
689 (
690 param.name.ident().name,
691 GenericParamDefKind::Type {
692 bounds,
693 default: default.map(|t| clean_ty(t, cx)).map(Box::new),
694 synthetic,
695 },
696 )
697 }
698 hir::GenericParamKind::Const { ty, default } => (
699 param.name.ident().name,
700 GenericParamDefKind::Const {
701 ty: Box::new(clean_ty(ty, cx)),
702 default: default.map(|ct| {
703 Box::new(
704 lower_const_arg_for_rustdoc(cx.tcx, ct, lower_ty(cx.tcx, ty)).to_string(),
705 )
706 }),
707 },
708 ),
709 };
710
711 GenericParamDef { name, def_id: param.def_id.to_def_id(), kind }
712}
713
714fn is_impl_trait(param: &hir::GenericParam<'_>) -> bool {
718 match param.kind {
719 hir::GenericParamKind::Type { synthetic, .. } => synthetic,
720 _ => false,
721 }
722}
723
724fn is_elided_lifetime(param: &hir::GenericParam<'_>) -> bool {
728 matches!(
729 param.kind,
730 hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::Elided(_) }
731 )
732}
733
734pub(crate) fn clean_generics<'tcx>(
735 gens: &hir::Generics<'tcx>,
736 cx: &mut DocContext<'tcx>,
737) -> Generics {
738 let impl_trait_params = gens
739 .params
740 .iter()
741 .filter(|param| is_impl_trait(param))
742 .map(|param| {
743 let param = clean_generic_param(cx, Some(gens), param);
744 match param.kind {
745 GenericParamDefKind::Lifetime { .. } => unreachable!(),
746 GenericParamDefKind::Type { ref bounds, .. } => {
747 cx.impl_trait_bounds.insert(param.def_id.into(), bounds.to_vec());
748 }
749 GenericParamDefKind::Const { .. } => unreachable!(),
750 }
751 param
752 })
753 .collect::<Vec<_>>();
754
755 let mut bound_predicates = FxIndexMap::default();
756 let mut region_predicates = FxIndexMap::default();
757 let mut eq_predicates = ThinVec::default();
758 for pred in gens.predicates.iter().filter_map(|x| clean_where_predicate(x, cx)) {
759 match pred {
760 WherePredicate::BoundPredicate { ty, bounds, bound_params } => {
761 match bound_predicates.entry(ty) {
762 IndexEntry::Vacant(v) => {
763 v.insert((bounds, bound_params));
764 }
765 IndexEntry::Occupied(mut o) => {
766 for bound in bounds {
768 if !o.get().0.contains(&bound) {
769 o.get_mut().0.push(bound);
770 }
771 }
772 for bound_param in bound_params {
773 if !o.get().1.contains(&bound_param) {
774 o.get_mut().1.push(bound_param);
775 }
776 }
777 }
778 }
779 }
780 WherePredicate::RegionPredicate { lifetime, bounds } => {
781 match region_predicates.entry(lifetime) {
782 IndexEntry::Vacant(v) => {
783 v.insert(bounds);
784 }
785 IndexEntry::Occupied(mut o) => {
786 for bound in bounds {
788 if !o.get().contains(&bound) {
789 o.get_mut().push(bound);
790 }
791 }
792 }
793 }
794 }
795 WherePredicate::ProjectionPredicate { lhs, rhs } => {
796 eq_predicates.push(WherePredicate::ProjectionPredicate { lhs, rhs });
797 }
798 }
799 }
800
801 let mut params = ThinVec::with_capacity(gens.params.len());
802 for p in gens.params.iter().filter(|p| !is_impl_trait(p) && !is_elided_lifetime(p)) {
806 let mut p = clean_generic_param(cx, Some(gens), p);
807 match &mut p.kind {
808 GenericParamDefKind::Lifetime { outlives } => {
809 if let Some(region_pred) = region_predicates.get_mut(&Lifetime(p.name)) {
810 for outlive in outlives.drain(..) {
812 let outlive = GenericBound::Outlives(outlive);
813 if !region_pred.contains(&outlive) {
814 region_pred.push(outlive);
815 }
816 }
817 }
818 }
819 GenericParamDefKind::Type { bounds, synthetic: false, .. } => {
820 if let Some(bound_pred) = bound_predicates.get_mut(&Type::Generic(p.name)) {
821 for bound in bounds.drain(..) {
823 if !bound_pred.0.contains(&bound) {
824 bound_pred.0.push(bound);
825 }
826 }
827 }
828 }
829 GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
830 }
832 }
833 params.push(p);
834 }
835 params.extend(impl_trait_params);
836
837 Generics {
838 params,
839 where_predicates: bound_predicates
840 .into_iter()
841 .map(|(ty, (bounds, bound_params))| WherePredicate::BoundPredicate {
842 ty,
843 bounds,
844 bound_params,
845 })
846 .chain(
847 region_predicates
848 .into_iter()
849 .map(|(lifetime, bounds)| WherePredicate::RegionPredicate { lifetime, bounds }),
850 )
851 .chain(eq_predicates)
852 .collect(),
853 }
854}
855
856fn clean_ty_generics<'tcx>(cx: &mut DocContext<'tcx>, def_id: DefId) -> Generics {
857 clean_ty_generics_inner(cx, cx.tcx.generics_of(def_id), cx.tcx.explicit_predicates_of(def_id))
858}
859
860fn clean_ty_generics_inner<'tcx>(
861 cx: &mut DocContext<'tcx>,
862 gens: &ty::Generics,
863 preds: ty::GenericPredicates<'tcx>,
864) -> Generics {
865 let mut impl_trait = BTreeMap::<u32, Vec<GenericBound>>::default();
868
869 let params: ThinVec<_> = gens
870 .own_params
871 .iter()
872 .filter(|param| match param.kind {
873 ty::GenericParamDefKind::Lifetime => !param.is_anonymous_lifetime(),
874 ty::GenericParamDefKind::Type { synthetic, .. } => {
875 if param.name == kw::SelfUpper {
876 debug_assert_eq!(param.index, 0);
877 return false;
878 }
879 if synthetic {
880 impl_trait.insert(param.index, vec![]);
881 return false;
882 }
883 true
884 }
885 ty::GenericParamDefKind::Const { .. } => true,
886 })
887 .map(|param| clean_generic_param_def(param, ParamDefaults::Yes, cx))
888 .collect();
889
890 let mut impl_trait_proj =
892 FxHashMap::<u32, Vec<(DefId, PathSegment, ty::Binder<'_, ty::Term<'_>>)>>::default();
893
894 let where_predicates = preds
895 .predicates
896 .iter()
897 .flat_map(|(pred, _)| {
898 let mut proj_pred = None;
899 let param_idx = {
900 let bound_p = pred.kind();
901 match bound_p.skip_binder() {
902 ty::ClauseKind::Trait(pred) if let ty::Param(param) = pred.self_ty().kind() => {
903 Some(param.index)
904 }
905 ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(ty, _reg))
906 if let ty::Param(param) = ty.kind() =>
907 {
908 Some(param.index)
909 }
910 ty::ClauseKind::Projection(p)
911 if let ty::Param(param) = p.projection_term.self_ty().kind() =>
912 {
913 proj_pred = Some(bound_p.rebind(p));
914 Some(param.index)
915 }
916 _ => None,
917 }
918 };
919
920 if let Some(param_idx) = param_idx
921 && let Some(bounds) = impl_trait.get_mut(¶m_idx)
922 {
923 let pred = clean_predicate(*pred, cx)?;
924
925 bounds.extend(pred.get_bounds().into_iter().flatten().cloned());
926
927 if let Some(pred) = proj_pred {
928 let lhs = clean_projection(pred.map_bound(|p| p.projection_term), cx, None);
929 impl_trait_proj.entry(param_idx).or_default().push((
930 lhs.trait_.unwrap().def_id(),
931 lhs.assoc,
932 pred.map_bound(|p| p.term),
933 ));
934 }
935
936 return None;
937 }
938
939 Some(pred)
940 })
941 .collect::<Vec<_>>();
942
943 for (idx, mut bounds) in impl_trait {
944 let mut has_sized = false;
945 bounds.retain(|b| {
946 if b.is_sized_bound(cx.tcx) {
947 has_sized = true;
948 false
949 } else if b.is_meta_sized_bound(cx.tcx) {
950 false
953 } else {
954 true
955 }
956 });
957 if !has_sized {
958 bounds.push(GenericBound::maybe_sized(cx));
959 }
960
961 bounds.sort_by_key(|b| !b.is_trait_bound());
963
964 if bounds.first().is_none_or(|b| !b.is_trait_bound()) {
967 bounds.insert(0, GenericBound::sized(cx));
968 }
969
970 if let Some(proj) = impl_trait_proj.remove(&idx) {
971 for (trait_did, name, rhs) in proj {
972 let rhs = clean_middle_term(rhs, cx);
973 simplify::merge_bounds(cx.tcx, &mut bounds, trait_did, name, &rhs);
974 }
975 }
976
977 cx.impl_trait_bounds.insert(idx.into(), bounds);
978 }
979
980 let where_predicates =
983 where_predicates.into_iter().flat_map(|p| clean_predicate(*p, cx)).collect();
984
985 let mut generics = Generics { params, where_predicates };
986 simplify::sizedness_bounds(cx, &mut generics);
987 generics.where_predicates = simplify::where_clauses(cx.tcx, generics.where_predicates);
988 generics
989}
990
991fn clean_ty_alias_inner_type<'tcx>(
992 ty: Ty<'tcx>,
993 cx: &mut DocContext<'tcx>,
994 ret: &mut Vec<Item>,
995) -> Option<TypeAliasInnerType> {
996 let ty::Adt(adt_def, args) = ty.kind() else {
997 return None;
998 };
999
1000 if !adt_def.did().is_local() {
1001 cx.with_param_env(adt_def.did(), |cx| {
1002 inline::build_impls(cx, adt_def.did(), None, ret);
1003 });
1004 }
1005
1006 Some(if adt_def.is_enum() {
1007 let variants: rustc_index::IndexVec<_, _> = adt_def
1008 .variants()
1009 .iter()
1010 .map(|variant| clean_variant_def_with_args(variant, args, cx))
1011 .collect();
1012
1013 if !adt_def.did().is_local() {
1014 inline::record_extern_fqn(cx, adt_def.did(), ItemType::Enum);
1015 }
1016
1017 TypeAliasInnerType::Enum {
1018 variants,
1019 is_non_exhaustive: adt_def.is_variant_list_non_exhaustive(),
1020 }
1021 } else {
1022 let variant = adt_def
1023 .variants()
1024 .iter()
1025 .next()
1026 .unwrap_or_else(|| bug!("a struct or union should always have one variant def"));
1027
1028 let fields: Vec<_> =
1029 clean_variant_def_with_args(variant, args, cx).kind.inner_items().cloned().collect();
1030
1031 if adt_def.is_struct() {
1032 if !adt_def.did().is_local() {
1033 inline::record_extern_fqn(cx, adt_def.did(), ItemType::Struct);
1034 }
1035 TypeAliasInnerType::Struct { ctor_kind: variant.ctor_kind(), fields }
1036 } else {
1037 if !adt_def.did().is_local() {
1038 inline::record_extern_fqn(cx, adt_def.did(), ItemType::Union);
1039 }
1040 TypeAliasInnerType::Union { fields }
1041 }
1042 })
1043}
1044
1045fn clean_proc_macro<'tcx>(
1046 item: &hir::Item<'tcx>,
1047 name: &mut Symbol,
1048 kind: MacroKind,
1049 tcx: TyCtxt<'tcx>,
1050) -> ItemKind {
1051 if kind != MacroKind::Derive {
1052 return ProcMacroItem(ProcMacro { kind, helpers: vec![] });
1053 }
1054 let attrs = tcx.hir_attrs(item.hir_id());
1055 let Some((trait_name, helper_attrs)) = find_attr!(attrs, ProcMacroDerive { trait_name, helper_attrs, ..} => (*trait_name, helper_attrs))
1056 else {
1057 return ProcMacroItem(ProcMacro { kind, helpers: vec![] });
1058 };
1059 *name = trait_name;
1060 let helpers = helper_attrs.iter().copied().collect();
1061
1062 ProcMacroItem(ProcMacro { kind, helpers })
1063}
1064
1065fn clean_fn_or_proc_macro<'tcx>(
1066 item: &hir::Item<'tcx>,
1067 sig: &hir::FnSig<'tcx>,
1068 generics: &hir::Generics<'tcx>,
1069 body_id: hir::BodyId,
1070 name: &mut Symbol,
1071 cx: &mut DocContext<'tcx>,
1072) -> ItemKind {
1073 let attrs = cx.tcx.hir_attrs(item.hir_id());
1074 let macro_kind = if find_attr!(attrs, ProcMacro) {
1075 Some(MacroKind::Bang)
1076 } else if find_attr!(attrs, ProcMacroDerive { .. }) {
1077 Some(MacroKind::Derive)
1078 } else if find_attr!(attrs, ProcMacroAttribute) {
1079 Some(MacroKind::Attr)
1080 } else {
1081 None
1082 };
1083
1084 match macro_kind {
1085 Some(kind) => clean_proc_macro(item, name, kind, cx.tcx),
1086 None => {
1087 let mut func = clean_function(
1088 cx,
1089 sig,
1090 generics,
1091 ParamsSrc::Body(body_id),
1092 item.owner_id.to_def_id(),
1093 );
1094 clean_fn_decl_legacy_const_generics(&mut func, attrs);
1095 FunctionItem(func)
1096 }
1097 }
1098}
1099
1100fn clean_fn_decl_legacy_const_generics(func: &mut Function, attrs: &[hir::Attribute]) {
1104 let Some(indexes) = find_attr!(attrs, RustcLegacyConstGenerics{fn_indexes,..} => fn_indexes)
1105 else {
1106 return;
1107 };
1108
1109 for (pos, (index, _)) in indexes.iter().enumerate() {
1110 let GenericParamDef { name, kind, .. } = func.generics.params.remove(0);
1111 if let GenericParamDefKind::Const { ty, .. } = kind {
1112 func.decl
1113 .inputs
1114 .insert(*index, Parameter { name: Some(name), type_: *ty, is_const: true });
1115 } else {
1116 panic!("unexpected non const in position {pos}");
1117 }
1118 }
1119}
1120
1121enum ParamsSrc<'tcx> {
1122 Body(hir::BodyId),
1123 Idents(&'tcx [Option<Ident>]),
1124}
1125
1126fn clean_function<'tcx>(
1127 cx: &mut DocContext<'tcx>,
1128 sig: &hir::FnSig<'tcx>,
1129 generics: &hir::Generics<'tcx>,
1130 params: ParamsSrc<'tcx>,
1131 def_id: DefId,
1132) -> Box<Function> {
1133 let (generics, decl) = enter_impl_trait(cx, |cx| {
1134 let generics = clean_generics(generics, cx);
1136 let decl = if sig.decl.opt_delegation_sig_id().is_some() {
1137 let sig = cx.tcx.fn_sig(def_id).instantiate_identity().skip_norm_wip();
1144 clean_poly_fn_sig(cx, Some(def_id), sig)
1145 } else {
1146 let params = match params {
1147 ParamsSrc::Body(body_id) => clean_params_via_body(cx, sig.decl.inputs, body_id),
1148 ParamsSrc::Idents(idents) => clean_params(cx, sig.decl.inputs, idents, |ident| {
1150 Some(ident.map_or(kw::Underscore, |ident| ident.name))
1151 }),
1152 };
1153 clean_fn_decl_with_params(cx, sig.decl, Some(&sig.header), params)
1154 };
1155 (generics, decl)
1156 });
1157 Box::new(Function { decl, generics })
1158}
1159
1160fn clean_params<'tcx>(
1161 cx: &mut DocContext<'tcx>,
1162 types: &[hir::Ty<'tcx>],
1163 idents: &[Option<Ident>],
1164 postprocess: impl Fn(Option<Ident>) -> Option<Symbol>,
1165) -> Vec<Parameter> {
1166 types
1167 .iter()
1168 .enumerate()
1169 .map(|(i, ty)| Parameter {
1170 name: postprocess(idents[i]),
1171 type_: clean_ty(ty, cx),
1172 is_const: false,
1173 })
1174 .collect()
1175}
1176
1177fn clean_params_via_body<'tcx>(
1178 cx: &mut DocContext<'tcx>,
1179 types: &[hir::Ty<'tcx>],
1180 body_id: hir::BodyId,
1181) -> Vec<Parameter> {
1182 types
1183 .iter()
1184 .zip(cx.tcx.hir_body(body_id).params)
1185 .map(|(ty, param)| Parameter {
1186 name: Some(name_from_pat(param.pat)),
1187 type_: clean_ty(ty, cx),
1188 is_const: false,
1189 })
1190 .collect()
1191}
1192
1193fn clean_fn_decl_with_params<'tcx>(
1194 cx: &mut DocContext<'tcx>,
1195 decl: &hir::FnDecl<'tcx>,
1196 header: Option<&hir::FnHeader>,
1197 params: Vec<Parameter>,
1198) -> FnDecl {
1199 let mut output = match decl.output {
1200 hir::FnRetTy::Return(typ) => clean_ty(typ, cx),
1201 hir::FnRetTy::DefaultReturn(..) => Type::Tuple(Vec::new()),
1202 };
1203 if let Some(header) = header
1204 && header.is_async()
1205 {
1206 output = output.sugared_async_return_type();
1207 }
1208 FnDecl { inputs: params, output, c_variadic: decl.c_variadic() }
1209}
1210
1211fn clean_poly_fn_sig<'tcx>(
1212 cx: &mut DocContext<'tcx>,
1213 did: Option<DefId>,
1214 sig: ty::PolyFnSig<'tcx>,
1215) -> FnDecl {
1216 let mut output = clean_middle_ty(sig.output(), cx, None, None);
1217
1218 if let Some(did) = did
1222 && let Type::ImplTrait(_) = output
1223 && cx.tcx.asyncness(did).is_async()
1224 {
1225 output = output.sugared_async_return_type();
1226 }
1227
1228 let mut idents = did.map(|did| cx.tcx.fn_arg_idents(did)).unwrap_or_default().iter().copied();
1229
1230 let fallback = did.map(|_| kw::Underscore);
1235
1236 let params = sig
1237 .inputs()
1238 .iter()
1239 .map(|ty| Parameter {
1240 name: idents.next().flatten().map(|ident| ident.name).or(fallback),
1241 type_: clean_middle_ty(ty.map_bound(|ty| *ty), cx, None, None),
1242 is_const: false,
1243 })
1244 .collect();
1245
1246 FnDecl { inputs: params, output, c_variadic: sig.skip_binder().c_variadic() }
1247}
1248
1249fn clean_trait_ref<'tcx>(trait_ref: &hir::TraitRef<'tcx>, cx: &mut DocContext<'tcx>) -> Path {
1250 let path = clean_path(trait_ref.path, cx);
1251 register_res(cx, path.res);
1252 path
1253}
1254
1255fn clean_poly_trait_ref<'tcx>(
1256 poly_trait_ref: &hir::PolyTraitRef<'tcx>,
1257 cx: &mut DocContext<'tcx>,
1258) -> PolyTrait {
1259 PolyTrait {
1260 trait_: clean_trait_ref(&poly_trait_ref.trait_ref, cx),
1261 generic_params: poly_trait_ref
1262 .bound_generic_params
1263 .iter()
1264 .filter(|p| !is_elided_lifetime(p))
1265 .map(|x| clean_generic_param(cx, None, x))
1266 .collect(),
1267 }
1268}
1269
1270fn clean_trait_item<'tcx>(trait_item: &hir::TraitItem<'tcx>, cx: &mut DocContext<'tcx>) -> Item {
1271 let local_did = trait_item.owner_id.to_def_id();
1272 cx.with_param_env(local_did, |cx| {
1273 let inner = match trait_item.kind {
1274 hir::TraitItemKind::Const(ty, Some(default)) => {
1275 ProvidedAssocConstItem(Box::new(Constant {
1276 generics: enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx)),
1277 kind: clean_const_item_rhs(default, local_did),
1278 type_: clean_ty(ty, cx),
1279 }))
1280 }
1281 hir::TraitItemKind::Const(ty, None) => {
1282 let generics = enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx));
1283 RequiredAssocConstItem(generics, Box::new(clean_ty(ty, cx)))
1284 }
1285 hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => {
1286 let m =
1287 clean_function(cx, sig, trait_item.generics, ParamsSrc::Body(body), local_did);
1288 MethodItem(m, Defaultness::from_trait_item(trait_item.defaultness))
1289 }
1290 hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Required(idents)) => {
1291 let m = clean_function(
1292 cx,
1293 sig,
1294 trait_item.generics,
1295 ParamsSrc::Idents(idents),
1296 local_did,
1297 );
1298 RequiredMethodItem(m, Defaultness::from_trait_item(trait_item.defaultness))
1299 }
1300 hir::TraitItemKind::Type(bounds, Some(default)) => {
1301 let generics = enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx));
1302 let bounds = bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect();
1303 let item_type =
1304 clean_middle_ty(ty::Binder::dummy(lower_ty(cx.tcx, default)), cx, None, None);
1305 AssocTypeItem(
1306 Box::new(TypeAlias {
1307 type_: clean_ty(default, cx),
1308 generics,
1309 inner_type: None,
1310 item_type: Some(item_type),
1311 }),
1312 bounds,
1313 )
1314 }
1315 hir::TraitItemKind::Type(bounds, None) => {
1316 let generics = enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx));
1317 let bounds = bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect();
1318 RequiredAssocTypeItem(generics, bounds)
1319 }
1320 };
1321 Item::from_def_id_and_parts(local_did, Some(trait_item.ident.name), inner, cx.tcx)
1322 })
1323}
1324
1325pub(crate) fn clean_impl_item<'tcx>(
1326 impl_: &hir::ImplItem<'tcx>,
1327 cx: &mut DocContext<'tcx>,
1328) -> Item {
1329 let local_did = impl_.owner_id.to_def_id();
1330 cx.with_param_env(local_did, |cx| {
1331 let inner = match impl_.kind {
1332 hir::ImplItemKind::Const(ty, expr) => ImplAssocConstItem(Box::new(Constant {
1333 generics: clean_generics(impl_.generics, cx),
1334 kind: clean_const_item_rhs(expr, local_did),
1335 type_: clean_ty(ty, cx),
1336 })),
1337 hir::ImplItemKind::Fn(ref sig, body) => {
1338 let m = clean_function(cx, sig, impl_.generics, ParamsSrc::Body(body), local_did);
1339 let defaultness = match impl_.impl_kind {
1340 hir::ImplItemImplKind::Inherent { .. } => hir::Defaultness::Final,
1341 hir::ImplItemImplKind::Trait { defaultness, .. } => defaultness,
1342 };
1343 MethodItem(m, Defaultness::from_impl_item(defaultness))
1344 }
1345 hir::ImplItemKind::Type(hir_ty) => {
1346 let type_ = clean_ty(hir_ty, cx);
1347 let generics = clean_generics(impl_.generics, cx);
1348 let item_type =
1349 clean_middle_ty(ty::Binder::dummy(lower_ty(cx.tcx, hir_ty)), cx, None, None);
1350 AssocTypeItem(
1351 Box::new(TypeAlias {
1352 type_,
1353 generics,
1354 inner_type: None,
1355 item_type: Some(item_type),
1356 }),
1357 Vec::new(),
1358 )
1359 }
1360 };
1361
1362 Item::from_def_id_and_parts(local_did, Some(impl_.ident.name), inner, cx.tcx)
1363 })
1364}
1365
1366pub(crate) fn clean_middle_assoc_item(assoc_item: &ty::AssocItem, cx: &mut DocContext<'_>) -> Item {
1367 let tcx = cx.tcx;
1368 let kind = match assoc_item.kind {
1369 ty::AssocKind::Const { .. } => {
1370 let ty = clean_middle_ty(
1371 ty::Binder::dummy(
1372 tcx.type_of(assoc_item.def_id).instantiate_identity().skip_norm_wip(),
1373 ),
1374 cx,
1375 Some(assoc_item.def_id),
1376 None,
1377 );
1378
1379 let mut generics = clean_ty_generics(cx, assoc_item.def_id);
1380 simplify::move_bounds_to_generic_parameters(&mut generics);
1381
1382 match assoc_item.container {
1383 ty::AssocContainer::InherentImpl | ty::AssocContainer::TraitImpl(_) => {
1384 ImplAssocConstItem(Box::new(Constant {
1385 generics,
1386 kind: ConstantKind::Extern { def_id: assoc_item.def_id },
1387 type_: ty,
1388 }))
1389 }
1390 ty::AssocContainer::Trait => {
1391 if tcx.defaultness(assoc_item.def_id).has_value() {
1392 ProvidedAssocConstItem(Box::new(Constant {
1393 generics,
1394 kind: ConstantKind::Extern { def_id: assoc_item.def_id },
1395 type_: ty,
1396 }))
1397 } else {
1398 RequiredAssocConstItem(generics, Box::new(ty))
1399 }
1400 }
1401 }
1402 }
1403 ty::AssocKind::Fn { has_self, .. } => {
1404 let mut item = inline::build_function(cx, assoc_item.def_id);
1405
1406 if has_self {
1407 let self_ty = match assoc_item.container {
1408 ty::AssocContainer::InherentImpl | ty::AssocContainer::TraitImpl(_) => tcx
1409 .type_of(assoc_item.container_id(tcx))
1410 .instantiate_identity()
1411 .skip_norm_wip(),
1412 ty::AssocContainer::Trait => tcx.types.self_param,
1413 };
1414 let self_param_ty = tcx
1415 .fn_sig(assoc_item.def_id)
1416 .instantiate_identity()
1417 .skip_norm_wip()
1418 .input(0)
1419 .skip_binder();
1420 if self_param_ty == self_ty {
1421 item.decl.inputs[0].type_ = SelfTy;
1422 } else if let ty::Ref(_, ty, _) = *self_param_ty.kind()
1423 && ty == self_ty
1424 {
1425 match item.decl.inputs[0].type_ {
1426 BorrowedRef { ref mut type_, .. } => **type_ = SelfTy,
1427 _ => unreachable!(),
1428 }
1429 }
1430 }
1431
1432 let defaultness = assoc_item.defaultness(tcx);
1433 let (provided, defaultness) = match assoc_item.container {
1434 ty::AssocContainer::Trait => {
1435 (defaultness.has_value(), Defaultness::from_trait_item(defaultness))
1436 }
1437 ty::AssocContainer::InherentImpl | ty::AssocContainer::TraitImpl(_) => {
1438 (true, Defaultness::from_impl_item(defaultness))
1439 }
1440 };
1441
1442 if provided {
1443 MethodItem(item, defaultness)
1444 } else {
1445 RequiredMethodItem(item, defaultness)
1446 }
1447 }
1448 ty::AssocKind::Type { .. } => {
1449 let my_name = assoc_item.name();
1450
1451 fn param_eq_arg(param: &GenericParamDef, arg: &GenericArg) -> bool {
1452 match (¶m.kind, arg) {
1453 (GenericParamDefKind::Type { .. }, GenericArg::Type(Type::Generic(ty)))
1454 if *ty == param.name =>
1455 {
1456 true
1457 }
1458 (GenericParamDefKind::Lifetime { .. }, GenericArg::Lifetime(Lifetime(lt)))
1459 if *lt == param.name =>
1460 {
1461 true
1462 }
1463 (GenericParamDefKind::Const { .. }, GenericArg::Const(c)) => match &**c {
1464 ConstantKind::TyConst { expr } => **expr == *param.name.as_str(),
1465 _ => false,
1466 },
1467 _ => false,
1468 }
1469 }
1470
1471 let mut predicates = tcx.explicit_predicates_of(assoc_item.def_id).predicates;
1472 if let ty::AssocContainer::Trait = assoc_item.container {
1473 let bounds = tcx
1474 .explicit_item_bounds(assoc_item.def_id)
1475 .iter_identity_copied()
1476 .map(Unnormalized::skip_norm_wip);
1477 predicates = tcx.arena.alloc_from_iter(bounds.chain(predicates.iter().copied()));
1478 }
1479 let mut generics = clean_ty_generics_inner(
1480 cx,
1481 tcx.generics_of(assoc_item.def_id),
1482 ty::GenericPredicates { parent: None, predicates },
1483 );
1484 simplify::move_bounds_to_generic_parameters(&mut generics);
1485
1486 if let ty::AssocContainer::Trait = assoc_item.container {
1487 let mut bounds: Vec<GenericBound> = Vec::new();
1492 generics.where_predicates.retain_mut(|pred| match *pred {
1493 WherePredicate::BoundPredicate {
1494 ty:
1495 QPath(QPathData {
1496 ref assoc, ref self_type, trait_: Some(ref trait_), ..
1497 }),
1498 bounds: ref mut pred_bounds,
1499 ..
1500 } => {
1501 if assoc.name != my_name {
1502 return true;
1503 }
1504 if trait_.def_id() != assoc_item.container_id(tcx) {
1505 return true;
1506 }
1507 if *self_type != SelfTy {
1508 return true;
1509 }
1510 match &assoc.args {
1511 GenericArgs::AngleBracketed { args, constraints } => {
1512 if !constraints.is_empty()
1513 || generics
1514 .params
1515 .iter()
1516 .zip(args.iter())
1517 .any(|(param, arg)| !param_eq_arg(param, arg))
1518 {
1519 return true;
1520 }
1521 }
1522 GenericArgs::Parenthesized { .. } => {
1523 }
1526 GenericArgs::ReturnTypeNotation => {
1527 }
1529 }
1530 bounds.extend(mem::take(pred_bounds));
1531 false
1532 }
1533 _ => true,
1534 });
1535
1536 bounds.retain(|b| {
1537 !b.is_meta_sized_bound(tcx)
1540 });
1541
1542 match bounds.iter().position(|b| b.is_sized_bound(tcx)) {
1548 Some(i) => {
1549 bounds.remove(i);
1550 }
1551 None => bounds.push(GenericBound::maybe_sized(cx)),
1552 }
1553
1554 if tcx.defaultness(assoc_item.def_id).has_value() {
1555 AssocTypeItem(
1556 Box::new(TypeAlias {
1557 type_: clean_middle_ty(
1558 ty::Binder::dummy(
1559 tcx.type_of(assoc_item.def_id)
1560 .instantiate_identity()
1561 .skip_norm_wip(),
1562 ),
1563 cx,
1564 Some(assoc_item.def_id),
1565 None,
1566 ),
1567 generics,
1568 inner_type: None,
1569 item_type: None,
1570 }),
1571 bounds,
1572 )
1573 } else {
1574 RequiredAssocTypeItem(generics, bounds)
1575 }
1576 } else {
1577 AssocTypeItem(
1578 Box::new(TypeAlias {
1579 type_: clean_middle_ty(
1580 ty::Binder::dummy(
1581 tcx.type_of(assoc_item.def_id)
1582 .instantiate_identity()
1583 .skip_norm_wip(),
1584 ),
1585 cx,
1586 Some(assoc_item.def_id),
1587 None,
1588 ),
1589 generics,
1590 inner_type: None,
1591 item_type: None,
1592 }),
1593 Vec::new(),
1596 )
1597 }
1598 }
1599 };
1600
1601 Item::from_def_id_and_parts(assoc_item.def_id, Some(assoc_item.name()), kind, tcx)
1602}
1603
1604fn first_non_private_clean_path<'tcx>(
1605 cx: &mut DocContext<'tcx>,
1606 path: &hir::Path<'tcx>,
1607 new_path_segments: &'tcx [hir::PathSegment<'tcx>],
1608 new_path_span: rustc_span::Span,
1609) -> Path {
1610 let new_hir_path =
1611 hir::Path { segments: new_path_segments, res: path.res, span: new_path_span };
1612 let mut new_clean_path = clean_path(&new_hir_path, cx);
1613 if let Some(path_last) = path.segments.last().as_ref()
1618 && let Some(new_path_last) = new_clean_path.segments[..].last_mut()
1619 && let Some(path_last_args) = path_last.args.as_ref()
1620 && path_last.args.is_some()
1621 {
1622 assert!(new_path_last.args.is_empty());
1623 new_path_last.args = clean_generic_args(None, path_last_args, cx);
1624 }
1625 new_clean_path
1626}
1627
1628fn first_non_private<'tcx>(
1633 cx: &mut DocContext<'tcx>,
1634 hir_id: hir::HirId,
1635 path: &hir::Path<'tcx>,
1636) -> Option<Path> {
1637 let target_def_id = path.res.opt_def_id()?;
1638 let (parent_def_id, ident) = match &path.segments {
1639 [] => return None,
1640 [leaf] => (cx.tcx.local_parent(hir_id.owner.def_id), leaf.ident),
1642 [parent, leaf] if parent.ident.name == kw::SelfLower => {
1644 (cx.tcx.local_parent(hir_id.owner.def_id), leaf.ident)
1645 }
1646 [parent, leaf] if matches!(parent.ident.name, kw::Crate | kw::PathRoot) => {
1648 (LOCAL_CRATE.as_def_id().as_local()?, leaf.ident)
1649 }
1650 [parent, leaf] if parent.ident.name == kw::Super => {
1651 let parent_mod = cx.tcx.parent_module(hir_id);
1652 if let Some(super_parent) = cx.tcx.opt_local_parent(parent_mod.to_local_def_id()) {
1653 (super_parent, leaf.ident)
1654 } else {
1655 (LOCAL_CRATE.as_def_id().as_local()?, leaf.ident)
1657 }
1658 }
1659 [.., parent, leaf] => (parent.res.opt_def_id()?.as_local()?, leaf.ident),
1661 };
1662 for child in
1664 cx.tcx.module_children_local(parent_def_id).iter().filter(move |c| c.ident == ident)
1665 {
1666 if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = child.res {
1667 continue;
1668 }
1669
1670 if let Some(def_id) = child.res.opt_def_id()
1671 && target_def_id == def_id
1672 {
1673 let mut last_path_res = None;
1674 'reexps: for reexp in child.reexport_chain.iter() {
1675 if let Some(use_def_id) = reexp.id()
1676 && let Some(local_use_def_id) = use_def_id.as_local()
1677 && let hir::Node::Item(item) = cx.tcx.hir_node_by_def_id(local_use_def_id)
1678 && let hir::ItemKind::Use(path, hir::UseKind::Single(_)) = item.kind
1679 {
1680 for res in path.res.present_items() {
1681 if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = res {
1682 continue;
1683 }
1684 if (cx.document_hidden() ||
1685 !cx.tcx.is_doc_hidden(use_def_id)) &&
1686 cx.tcx.local_visibility(local_use_def_id).is_public()
1690 {
1691 break 'reexps;
1692 }
1693 last_path_res = Some((path, res));
1694 continue 'reexps;
1695 }
1696 }
1697 }
1698 if !child.reexport_chain.is_empty() {
1699 if let Some((new_path, _)) = last_path_res {
1705 return Some(first_non_private_clean_path(
1706 cx,
1707 path,
1708 new_path.segments,
1709 new_path.span,
1710 ));
1711 }
1712 return None;
1717 }
1718 }
1719 }
1720 None
1721}
1722
1723fn clean_qpath<'tcx>(hir_ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> Type {
1724 let hir::Ty { hir_id, span, ref kind } = *hir_ty;
1725 let hir::TyKind::Path(qpath) = kind else { unreachable!() };
1726
1727 match qpath {
1728 hir::QPath::Resolved(None, path) => {
1729 if let Res::Def(DefKind::TyParam, did) = path.res {
1730 if let Some(new_ty) = cx.args.get(&did).and_then(|p| p.as_ty()).cloned() {
1731 return new_ty;
1732 }
1733 if let Some(bounds) = cx.impl_trait_bounds.remove(&did.into()) {
1734 return ImplTrait(bounds);
1735 }
1736 }
1737
1738 if let Some(expanded) = maybe_expand_private_type_alias(cx, path) {
1739 expanded
1740 } else {
1741 let path = if let Some(path) = first_non_private(cx, hir_id, path) {
1743 path
1744 } else {
1745 clean_path(path, cx)
1746 };
1747 resolve_type(cx, path)
1748 }
1749 }
1750 hir::QPath::Resolved(Some(qself), p) => {
1751 let ty = lower_ty(cx.tcx, hir_ty);
1753 if !ty.has_escaping_bound_vars()
1755 && let Some(normalized_value) = normalize(cx, ty::Binder::dummy(ty))
1756 {
1757 return clean_middle_ty(normalized_value, cx, None, None);
1758 }
1759
1760 let trait_segments = &p.segments[..p.segments.len() - 1];
1761 let trait_def = cx.tcx.parent(p.res.def_id());
1762 let trait_ = self::Path {
1763 res: Res::Def(DefKind::Trait, trait_def),
1764 segments: trait_segments.iter().map(|x| clean_path_segment(x, cx)).collect(),
1765 };
1766 register_res(cx, trait_.res);
1767 let self_def_id = DefId::local(qself.hir_id.owner.def_id.local_def_index);
1768 let self_type = clean_ty(qself, cx);
1769 let should_fully_qualify =
1770 should_fully_qualify_path(Some(self_def_id), &trait_, &self_type);
1771 Type::QPath(Box::new(QPathData {
1772 assoc: clean_path_segment(p.segments.last().expect("segments were empty"), cx),
1773 should_fully_qualify,
1774 self_type,
1775 trait_: Some(trait_),
1776 }))
1777 }
1778 hir::QPath::TypeRelative(qself, segment) => {
1779 let ty = lower_ty(cx.tcx, hir_ty);
1780 let self_type = clean_ty(qself, cx);
1781
1782 let (trait_, should_fully_qualify) = match ty.kind() {
1783 ty::Alias(proj @ ty::AliasTy { kind: ty::Projection { .. }, .. }) => {
1784 let res = Res::Def(DefKind::Trait, proj.trait_ref(cx.tcx).def_id);
1785 let trait_ = clean_path(&hir::Path { span, res, segments: &[] }, cx);
1786 register_res(cx, trait_.res);
1787 let self_def_id = res.opt_def_id();
1788 let should_fully_qualify =
1789 should_fully_qualify_path(self_def_id, &trait_, &self_type);
1790
1791 (Some(trait_), should_fully_qualify)
1792 }
1793 ty::Alias(ty::AliasTy { kind: ty::Inherent { .. }, .. }) => (None, false),
1794 ty::Error(_) => return Type::Infer,
1796 _ => bug!("clean: expected associated type, found `{ty:?}`"),
1797 };
1798
1799 Type::QPath(Box::new(QPathData {
1800 assoc: clean_path_segment(segment, cx),
1801 should_fully_qualify,
1802 self_type,
1803 trait_,
1804 }))
1805 }
1806 }
1807}
1808
1809fn maybe_expand_private_type_alias<'tcx>(
1810 cx: &mut DocContext<'tcx>,
1811 path: &hir::Path<'tcx>,
1812) -> Option<Type> {
1813 let Res::Def(DefKind::TyAlias, def_id) = path.res else { return None };
1814 let def_id = def_id.as_local()?;
1816 let alias = if !cx.cache.effective_visibilities.is_exported(cx.tcx, def_id.to_def_id())
1817 && !cx.current_type_aliases.contains_key(&def_id.to_def_id())
1818 {
1819 &cx.tcx.hir_expect_item(def_id).kind
1820 } else {
1821 return None;
1822 };
1823 let hir::ItemKind::TyAlias(_, generics, ty) = alias else { return None };
1824
1825 let final_seg = &path.segments.last().expect("segments were empty");
1826 let mut args = DefIdMap::default();
1827 let generic_args = final_seg.args();
1828
1829 let mut indices: hir::GenericParamCount = Default::default();
1830 for param in generics.params.iter() {
1831 match param.kind {
1832 hir::GenericParamKind::Lifetime { .. } => {
1833 let mut j = 0;
1834 let lifetime = generic_args.args.iter().find_map(|arg| match arg {
1835 hir::GenericArg::Lifetime(lt) => {
1836 if indices.lifetimes == j {
1837 return Some(lt);
1838 }
1839 j += 1;
1840 None
1841 }
1842 _ => None,
1843 });
1844 if let Some(lt) = lifetime {
1845 let lt = if !lt.is_anonymous() {
1846 clean_lifetime(lt, cx)
1847 } else {
1848 Lifetime::elided()
1849 };
1850 args.insert(param.def_id.to_def_id(), GenericArg::Lifetime(lt));
1851 }
1852 indices.lifetimes += 1;
1853 }
1854 hir::GenericParamKind::Type { ref default, .. } => {
1855 let mut j = 0;
1856 let type_ = generic_args.args.iter().find_map(|arg| match arg {
1857 hir::GenericArg::Type(ty) => {
1858 if indices.types == j {
1859 return Some(ty.as_unambig_ty());
1860 }
1861 j += 1;
1862 None
1863 }
1864 _ => None,
1865 });
1866 if let Some(ty) = type_.or(*default) {
1867 args.insert(param.def_id.to_def_id(), GenericArg::Type(clean_ty(ty, cx)));
1868 }
1869 indices.types += 1;
1870 }
1871 hir::GenericParamKind::Const { .. } => {}
1873 }
1874 }
1875
1876 Some(cx.enter_alias(args, def_id.to_def_id(), |cx| {
1877 cx.with_param_env(def_id.to_def_id(), |cx| clean_ty(ty, cx))
1878 }))
1879}
1880
1881pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> Type {
1882 use rustc_hir::*;
1883
1884 match ty.kind {
1885 TyKind::Never => Primitive(PrimitiveType::Never),
1886 TyKind::Ptr(ref m) => RawPointer(m.mutbl, Box::new(clean_ty(m.ty, cx))),
1887 TyKind::Ref(l, ref m) => {
1888 let lifetime = if l.is_anonymous() { None } else { Some(clean_lifetime(l, cx)) };
1889 BorrowedRef { lifetime, mutability: m.mutbl, type_: Box::new(clean_ty(m.ty, cx)) }
1890 }
1891 TyKind::Slice(ty) => Slice(Box::new(clean_ty(ty, cx))),
1892 TyKind::Pat(inner_ty, pat) => {
1893 let pat = match lower_ty(cx.tcx, ty).kind() {
1896 ty::Pat(_, pat) => format!("{pat:?}").into_boxed_str(),
1897 _ => format!("{pat:?}").into(),
1898 };
1899 Type::Pat(Box::new(clean_ty(inner_ty, cx)), pat)
1900 }
1901 TyKind::FieldOf(ty, hir::TyFieldPath { variant, field }) => {
1902 let field_str = if let Some(variant) = variant {
1903 format!("{variant}.{field}")
1904 } else {
1905 format!("{field}")
1906 };
1907 Type::FieldOf(Box::new(clean_ty(ty, cx)), field_str.into())
1908 }
1909 TyKind::Array(ty, const_arg) => {
1910 let length = match const_arg.kind {
1918 hir::ConstArgKind::Infer(..) | hir::ConstArgKind::Error(..) => "_".to_string(),
1919 hir::ConstArgKind::Anon(hir::AnonConst { def_id, .. }) => {
1920 let ct = lower_const_arg_for_rustdoc(cx.tcx, const_arg, cx.tcx.types.usize);
1921 let typing_env = ty::TypingEnv::post_analysis(cx.tcx, *def_id);
1922 let ct =
1923 cx.tcx.normalize_erasing_regions(typing_env, Unnormalized::new_wip(ct));
1924 print_const(cx.tcx, ct)
1925 }
1926 hir::ConstArgKind::Struct(..)
1927 | hir::ConstArgKind::Path(..)
1928 | hir::ConstArgKind::TupleCall(..)
1929 | hir::ConstArgKind::Tup(..)
1930 | hir::ConstArgKind::Array(..)
1931 | hir::ConstArgKind::Literal { .. } => {
1932 let ct = lower_const_arg_for_rustdoc(cx.tcx, const_arg, cx.tcx.types.usize);
1933 print_const(cx.tcx, ct)
1934 }
1935 };
1936 Array(Box::new(clean_ty(ty, cx)), length.into())
1937 }
1938 TyKind::Tup(tys) => Tuple(tys.iter().map(|ty| clean_ty(ty, cx)).collect()),
1939 TyKind::OpaqueDef(ty) => {
1940 ImplTrait(ty.bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect())
1941 }
1942 TyKind::Path(_) => clean_qpath(ty, cx),
1943 TyKind::TraitObject(bounds, lifetime) => {
1944 let bounds = bounds.iter().map(|bound| clean_poly_trait_ref(bound, cx)).collect();
1945 let lifetime = if !lifetime.is_elided() {
1946 Some(clean_lifetime(lifetime.pointer(), cx))
1947 } else {
1948 None
1949 };
1950 DynTrait(bounds, lifetime)
1951 }
1952 TyKind::FnPtr(barefn) => BareFunction(Box::new(clean_bare_fn_ty(barefn, cx))),
1953 TyKind::UnsafeBinder(unsafe_binder_ty) => {
1954 UnsafeBinder(Box::new(clean_unsafe_binder_ty(unsafe_binder_ty, cx)))
1955 }
1956 TyKind::Infer(())
1958 | TyKind::Err(_)
1959 | TyKind::InferDelegation(..)
1960 | TyKind::TraitAscription(_) => Infer,
1961 }
1962}
1963
1964fn normalize<'tcx>(
1966 cx: &DocContext<'tcx>,
1967 ty: ty::Binder<'tcx, Ty<'tcx>>,
1968) -> Option<ty::Binder<'tcx, Ty<'tcx>>> {
1969 if !cx.tcx.sess.opts.unstable_opts.normalize_docs {
1971 return None;
1972 }
1973
1974 use rustc_middle::traits::ObligationCause;
1975 use rustc_trait_selection::infer::TyCtxtInferExt;
1976 use rustc_trait_selection::traits::query::normalize::QueryNormalizeExt;
1977
1978 let infcx = cx.tcx.infer_ctxt().build(TypingMode::non_body_analysis());
1980 let normalized = infcx
1981 .at(&ObligationCause::dummy(), cx.param_env)
1982 .query_normalize(ty)
1983 .map(|resolved| infcx.resolve_vars_if_possible(resolved.value));
1984 match normalized {
1985 Ok(normalized_value) => {
1986 debug!("normalized {ty:?} to {normalized_value:?}");
1987 Some(normalized_value)
1988 }
1989 Err(err) => {
1990 debug!("failed to normalize {ty:?}: {err:?}");
1991 None
1992 }
1993 }
1994}
1995
1996fn clean_trait_object_lifetime_bound<'tcx>(
1997 region: ty::Region<'tcx>,
1998 container: Option<ContainerTy<'_, 'tcx>>,
1999 preds: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
2000 tcx: TyCtxt<'tcx>,
2001) -> Option<Lifetime> {
2002 if can_elide_trait_object_lifetime_bound(region, container, preds, tcx) {
2003 return None;
2004 }
2005
2006 match region.kind() {
2010 ty::ReStatic => Some(Lifetime::statik()),
2011 ty::ReEarlyParam(region) => Some(Lifetime(region.name)),
2012 ty::ReBound(_, ty::BoundRegion { kind: ty::BoundRegionKind::Named(def_id), .. }) => {
2013 Some(Lifetime(tcx.item_name(def_id)))
2014 }
2015 ty::ReBound(..)
2016 | ty::ReLateParam(_)
2017 | ty::ReVar(_)
2018 | ty::RePlaceholder(_)
2019 | ty::ReErased
2020 | ty::ReError(_) => None,
2021 }
2022}
2023
2024fn can_elide_trait_object_lifetime_bound<'tcx>(
2025 region: ty::Region<'tcx>,
2026 container: Option<ContainerTy<'_, 'tcx>>,
2027 preds: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
2028 tcx: TyCtxt<'tcx>,
2029) -> bool {
2030 let default = container
2035 .map_or(ObjectLifetimeDefault::Empty, |container| container.object_lifetime_default(tcx));
2036
2037 match default {
2040 ObjectLifetimeDefault::Static => return region.kind() == ty::ReStatic,
2041 ObjectLifetimeDefault::Arg(default) => {
2043 return region.get_name(tcx) == default.get_name(tcx);
2044 }
2045 ObjectLifetimeDefault::Ambiguous => return false,
2049 ObjectLifetimeDefault::Empty => {}
2051 }
2052
2053 match *object_region_bounds(tcx, preds) {
2055 [] => region.kind() == ty::ReStatic,
2063 [object_region] => object_region.get_name(tcx) == region.get_name(tcx),
2067 _ => false,
2071 }
2072}
2073
2074#[derive(Debug)]
2075pub(crate) enum ContainerTy<'a, 'tcx> {
2076 Ref(ty::Region<'tcx>),
2077 Regular {
2078 ty: DefId,
2079 args: ty::Binder<'tcx, &'a [ty::GenericArg<'tcx>]>,
2082 arg: usize,
2083 },
2084}
2085
2086impl<'tcx> ContainerTy<'_, 'tcx> {
2087 fn object_lifetime_default(self, tcx: TyCtxt<'tcx>) -> ObjectLifetimeDefault<'tcx> {
2088 match self {
2089 Self::Ref(region) => ObjectLifetimeDefault::Arg(region),
2090 Self::Regular { ty: container, args, arg: index } => {
2091 let (DefKind::Struct
2095 | DefKind::Union
2096 | DefKind::Enum
2097 | DefKind::TyAlias
2098 | DefKind::Trait) = tcx.def_kind(container)
2099 else {
2100 return ObjectLifetimeDefault::Empty;
2101 };
2102
2103 let generics = tcx.generics_of(container);
2104 debug_assert_eq!(generics.parent_count, 0);
2105
2106 let param = generics.own_params[index].def_id;
2107 let default = tcx.object_lifetime_default(param);
2108 match default {
2109 rbv::ObjectLifetimeDefault::Param(lifetime) => {
2110 let index = generics.param_def_id_to_index[&lifetime];
2113 let arg = args.skip_binder()[index as usize].expect_region();
2114 ObjectLifetimeDefault::Arg(arg)
2115 }
2116 rbv::ObjectLifetimeDefault::Empty => ObjectLifetimeDefault::Empty,
2117 rbv::ObjectLifetimeDefault::Static => ObjectLifetimeDefault::Static,
2118 rbv::ObjectLifetimeDefault::Ambiguous => ObjectLifetimeDefault::Ambiguous,
2119 }
2120 }
2121 }
2122 }
2123}
2124
2125#[derive(Debug, Clone, Copy)]
2126pub(crate) enum ObjectLifetimeDefault<'tcx> {
2127 Empty,
2128 Static,
2129 Ambiguous,
2130 Arg(ty::Region<'tcx>),
2131}
2132
2133#[instrument(level = "trace", skip(cx), ret)]
2134pub(crate) fn clean_middle_ty<'tcx>(
2135 bound_ty: ty::Binder<'tcx, Ty<'tcx>>,
2136 cx: &mut DocContext<'tcx>,
2137 parent_def_id: Option<DefId>,
2138 container: Option<ContainerTy<'_, 'tcx>>,
2139) -> Type {
2140 let bound_ty = normalize(cx, bound_ty).unwrap_or(bound_ty);
2141 match *bound_ty.skip_binder().kind() {
2142 ty::Never => Primitive(PrimitiveType::Never),
2143 ty::Bool => Primitive(PrimitiveType::Bool),
2144 ty::Char => Primitive(PrimitiveType::Char),
2145 ty::Int(int_ty) => Primitive(int_ty.into()),
2146 ty::Uint(uint_ty) => Primitive(uint_ty.into()),
2147 ty::Float(float_ty) => Primitive(float_ty.into()),
2148 ty::Str => Primitive(PrimitiveType::Str),
2149 ty::Slice(ty) => Slice(Box::new(clean_middle_ty(bound_ty.rebind(ty), cx, None, None))),
2150 ty::Pat(ty, pat) => Type::Pat(
2151 Box::new(clean_middle_ty(bound_ty.rebind(ty), cx, None, None)),
2152 format!("{pat:?}").into_boxed_str(),
2153 ),
2154 ty::Array(ty, n) => {
2155 let n = cx.tcx.normalize_erasing_regions(cx.typing_env(), Unnormalized::new_wip(n));
2156 let n = print_const(cx.tcx, n);
2157 Array(Box::new(clean_middle_ty(bound_ty.rebind(ty), cx, None, None)), n.into())
2158 }
2159 ty::RawPtr(ty, mutbl) => {
2160 RawPointer(mutbl, Box::new(clean_middle_ty(bound_ty.rebind(ty), cx, None, None)))
2161 }
2162 ty::Ref(r, ty, mutbl) => BorrowedRef {
2163 lifetime: clean_middle_region(r, cx.tcx),
2164 mutability: mutbl,
2165 type_: Box::new(clean_middle_ty(
2166 bound_ty.rebind(ty),
2167 cx,
2168 None,
2169 Some(ContainerTy::Ref(r)),
2170 )),
2171 },
2172 ty::FnDef(..) | ty::FnPtr(..) => {
2173 let sig = bound_ty.skip_binder().fn_sig(cx.tcx);
2175 let decl = clean_poly_fn_sig(cx, None, sig);
2176 let generic_params = clean_bound_vars(sig.bound_vars(), cx.tcx);
2177
2178 BareFunction(Box::new(BareFunctionDecl {
2179 safety: sig.safety(),
2180 generic_params,
2181 decl,
2182 abi: sig.abi(),
2183 }))
2184 }
2185 ty::UnsafeBinder(inner) => {
2186 let generic_params = clean_bound_vars(inner.bound_vars(), cx.tcx);
2187 let ty = clean_middle_ty(inner.into(), cx, None, None);
2188 UnsafeBinder(Box::new(UnsafeBinderTy { generic_params, ty }))
2189 }
2190 ty::Adt(def, args) => {
2191 let did = def.did();
2192 let kind = match def.adt_kind() {
2193 AdtKind::Struct => ItemType::Struct,
2194 AdtKind::Union => ItemType::Union,
2195 AdtKind::Enum => ItemType::Enum,
2196 };
2197 inline::record_extern_fqn(cx, did, kind);
2198 let path = clean_middle_path(cx, did, false, ThinVec::new(), bound_ty.rebind(args));
2199 Type::Path { path }
2200 }
2201 ty::Foreign(did) => {
2202 inline::record_extern_fqn(cx, did, ItemType::ForeignType);
2203 let path = clean_middle_path(
2204 cx,
2205 did,
2206 false,
2207 ThinVec::new(),
2208 ty::Binder::dummy(ty::GenericArgs::empty()),
2209 );
2210 Type::Path { path }
2211 }
2212 ty::Dynamic(obj, reg) => {
2213 let mut dids = obj.auto_traits();
2217 let did = obj
2218 .principal_def_id()
2219 .or_else(|| dids.next())
2220 .unwrap_or_else(|| panic!("found trait object `{bound_ty:?}` with no traits?"));
2221 let args = match obj.principal() {
2222 Some(principal) => principal.map_bound(|p| p.args),
2223 _ => ty::Binder::dummy(ty::GenericArgs::empty()),
2225 };
2226
2227 inline::record_extern_fqn(cx, did, ItemType::Trait);
2228
2229 let lifetime = clean_trait_object_lifetime_bound(reg, container, obj, cx.tcx);
2230
2231 let mut bounds = dids
2232 .map(|did| {
2233 let empty = ty::Binder::dummy(ty::GenericArgs::empty());
2234 let path = clean_middle_path(cx, did, false, ThinVec::new(), empty);
2235 inline::record_extern_fqn(cx, did, ItemType::Trait);
2236 PolyTrait { trait_: path, generic_params: Vec::new() }
2237 })
2238 .collect::<Vec<_>>();
2239
2240 let constraints = obj
2241 .projection_bounds()
2242 .map(|pb| AssocItemConstraint {
2243 assoc: projection_to_path_segment(
2244 pb.map_bound(|pb| {
2245 pb.with_self_ty(cx.tcx, cx.tcx.types.trait_object_dummy_self)
2246 .projection_term
2247 }),
2248 cx,
2249 ),
2250 kind: AssocItemConstraintKind::Equality {
2251 term: clean_middle_term(pb.map_bound(|pb| pb.term), cx),
2252 },
2253 })
2254 .collect();
2255
2256 let late_bound_regions: FxIndexSet<_> = obj
2257 .iter()
2258 .flat_map(|pred| pred.bound_vars())
2259 .filter_map(|var| match var {
2260 ty::BoundVariableKind::Region(ty::BoundRegionKind::Named(def_id)) => {
2261 let name = cx.tcx.item_name(def_id);
2262 if name != kw::UnderscoreLifetime {
2263 Some(GenericParamDef::lifetime(def_id, name))
2264 } else {
2265 None
2266 }
2267 }
2268 _ => None,
2269 })
2270 .collect();
2271 let late_bound_regions = late_bound_regions.into_iter().collect();
2272
2273 let path = clean_middle_path(cx, did, false, constraints, args);
2274 bounds.insert(0, PolyTrait { trait_: path, generic_params: late_bound_regions });
2275
2276 DynTrait(bounds, lifetime)
2277 }
2278 ty::Tuple(t) => {
2279 Tuple(t.iter().map(|t| clean_middle_ty(bound_ty.rebind(t), cx, None, None)).collect())
2280 }
2281
2282 ty::Alias(alias_ty @ ty::AliasTy { kind: ty::Projection { def_id }, args, .. }) => {
2283 if cx.tcx.is_impl_trait_in_trait(def_id) {
2284 clean_middle_opaque_bounds(cx, def_id, args)
2285 } else {
2286 Type::QPath(Box::new(clean_projection(
2287 bound_ty.rebind(alias_ty.into()),
2288 cx,
2289 parent_def_id,
2290 )))
2291 }
2292 }
2293
2294 ty::Alias(alias_ty @ ty::AliasTy { kind: ty::Inherent { def_id }, .. }) => {
2295 let alias_ty = bound_ty.rebind(alias_ty);
2296 let self_type = clean_middle_ty(alias_ty.map_bound(|ty| ty.self_ty()), cx, None, None);
2297
2298 Type::QPath(Box::new(QPathData {
2299 assoc: PathSegment {
2300 name: cx.tcx.item_name(def_id),
2301 args: GenericArgs::AngleBracketed {
2302 args: clean_middle_generic_args(
2303 cx,
2304 alias_ty.map_bound(|ty| ty.args.as_slice()),
2305 true,
2306 def_id,
2307 ),
2308 constraints: Default::default(),
2309 },
2310 },
2311 should_fully_qualify: false,
2312 self_type,
2313 trait_: None,
2314 }))
2315 }
2316
2317 ty::Alias(ty::AliasTy { kind: ty::Free { def_id }, args, .. }) => {
2318 if cx.tcx.features().lazy_type_alias() {
2319 let path =
2322 clean_middle_path(cx, def_id, false, ThinVec::new(), bound_ty.rebind(args));
2323 Type::Path { path }
2324 } else {
2325 let ty = cx.tcx.type_of(def_id).instantiate(cx.tcx, args).skip_norm_wip();
2326 clean_middle_ty(bound_ty.rebind(ty), cx, None, None)
2327 }
2328 }
2329
2330 ty::Param(ref p) => {
2331 if let Some(bounds) = cx.impl_trait_bounds.remove(&p.index.into()) {
2332 ImplTrait(bounds)
2333 } else if p.name == kw::SelfUpper {
2334 SelfTy
2335 } else {
2336 Generic(p.name)
2337 }
2338 }
2339
2340 ty::Bound(_, ref ty) => match ty.kind {
2341 ty::BoundTyKind::Param(def_id) => Generic(cx.tcx.item_name(def_id)),
2342 ty::BoundTyKind::Anon => panic!("unexpected anonymous bound type variable"),
2343 },
2344
2345 ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, args, .. }) => {
2346 if cx.current_type_aliases.contains_key(&def_id) {
2348 let path =
2349 clean_middle_path(cx, def_id, false, ThinVec::new(), bound_ty.rebind(args));
2350 Type::Path { path }
2351 } else {
2352 *cx.current_type_aliases.entry(def_id).or_insert(0) += 1;
2353 let ty = clean_middle_opaque_bounds(cx, def_id, args);
2356 if let Some(count) = cx.current_type_aliases.get_mut(&def_id) {
2357 *count -= 1;
2358 if *count == 0 {
2359 cx.current_type_aliases.remove(&def_id);
2360 }
2361 }
2362 ty
2363 }
2364 }
2365
2366 ty::Closure(..) => panic!("Closure"),
2367 ty::CoroutineClosure(..) => panic!("CoroutineClosure"),
2368 ty::Coroutine(..) => panic!("Coroutine"),
2369 ty::Placeholder(..) => panic!("Placeholder"),
2370 ty::CoroutineWitness(..) => panic!("CoroutineWitness"),
2371 ty::Infer(..) => panic!("Infer"),
2372
2373 ty::Error(_) => FatalError.raise(),
2374 }
2375}
2376
2377fn clean_middle_opaque_bounds<'tcx>(
2378 cx: &mut DocContext<'tcx>,
2379 impl_trait_def_id: DefId,
2380 args: ty::GenericArgsRef<'tcx>,
2381) -> Type {
2382 let mut has_sized = false;
2383
2384 let bounds: Vec<_> = cx
2385 .tcx
2386 .explicit_item_bounds(impl_trait_def_id)
2387 .iter_instantiated_copied(cx.tcx, args)
2388 .map(Unnormalized::skip_norm_wip)
2389 .collect();
2390
2391 let mut bounds = bounds
2392 .iter()
2393 .filter_map(|(bound, _)| {
2394 let bound_predicate = bound.kind();
2395 let trait_ref = match bound_predicate.skip_binder() {
2396 ty::ClauseKind::Trait(tr) => bound_predicate.rebind(tr.trait_ref),
2397 ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(_ty, reg)) => {
2398 return clean_middle_region(reg, cx.tcx).map(GenericBound::Outlives);
2399 }
2400 _ => return None,
2401 };
2402
2403 if cx.tcx.is_lang_item(trait_ref.def_id(), LangItem::MetaSized) {
2406 return None;
2407 }
2408
2409 if let Some(sized) = cx.tcx.lang_items().sized_trait()
2410 && trait_ref.def_id() == sized
2411 {
2412 has_sized = true;
2413 return None;
2414 }
2415
2416 let bindings: ThinVec<_> = bounds
2417 .iter()
2418 .filter_map(|(bound, _)| {
2419 let bound = bound.kind();
2420 if let ty::ClauseKind::Projection(proj_pred) = bound.skip_binder()
2421 && proj_pred.projection_term.trait_ref(cx.tcx) == trait_ref.skip_binder()
2422 {
2423 return Some(AssocItemConstraint {
2424 assoc: projection_to_path_segment(
2425 bound.rebind(proj_pred.projection_term),
2426 cx,
2427 ),
2428 kind: AssocItemConstraintKind::Equality {
2429 term: clean_middle_term(bound.rebind(proj_pred.term), cx),
2430 },
2431 });
2432 }
2433 None
2434 })
2435 .collect();
2436
2437 Some(clean_poly_trait_ref_with_constraints(cx, trait_ref, bindings))
2438 })
2439 .collect::<Vec<_>>();
2440
2441 if !has_sized {
2442 bounds.push(GenericBound::maybe_sized(cx));
2443 }
2444
2445 bounds.sort_by_key(|b| !b.is_trait_bound());
2447
2448 if bounds.first().is_none_or(|b| !b.is_trait_bound()) {
2451 bounds.insert(0, GenericBound::sized(cx));
2452 }
2453
2454 if let Some(args) = cx.tcx.rendered_precise_capturing_args(impl_trait_def_id) {
2455 bounds.push(GenericBound::Use(
2456 args.iter()
2457 .map(|arg| match arg {
2458 hir::PreciseCapturingArgKind::Lifetime(lt) => {
2459 PreciseCapturingArg::Lifetime(Lifetime(*lt))
2460 }
2461 hir::PreciseCapturingArgKind::Param(param) => {
2462 PreciseCapturingArg::Param(*param)
2463 }
2464 })
2465 .collect(),
2466 ));
2467 }
2468
2469 ImplTrait(bounds)
2470}
2471
2472pub(crate) fn clean_field<'tcx>(field: &hir::FieldDef<'tcx>, cx: &mut DocContext<'tcx>) -> Item {
2473 clean_field_with_def_id(
2474 field.def_id.to_def_id(),
2475 field.ident.name,
2476 clean_ty(field.ty, cx),
2477 cx.tcx,
2478 )
2479}
2480
2481pub(crate) fn clean_middle_field(field: &ty::FieldDef, cx: &mut DocContext<'_>) -> Item {
2482 clean_field_with_def_id(
2483 field.did,
2484 field.name,
2485 clean_middle_ty(
2486 ty::Binder::dummy(cx.tcx.type_of(field.did).instantiate_identity().skip_norm_wip()),
2487 cx,
2488 Some(field.did),
2489 None,
2490 ),
2491 cx.tcx,
2492 )
2493}
2494
2495pub(crate) fn clean_field_with_def_id(
2496 def_id: DefId,
2497 name: Symbol,
2498 ty: Type,
2499 tcx: TyCtxt<'_>,
2500) -> Item {
2501 Item::from_def_id_and_parts(def_id, Some(name), StructFieldItem(ty), tcx)
2502}
2503
2504pub(crate) fn clean_variant_def(variant: &ty::VariantDef, cx: &mut DocContext<'_>) -> Item {
2505 let discriminant = match variant.discr {
2506 ty::VariantDiscr::Explicit(def_id) => Some(Discriminant { expr: None, value: def_id }),
2507 ty::VariantDiscr::Relative(_) => None,
2508 };
2509
2510 let kind = match variant.ctor_kind() {
2511 Some(CtorKind::Const) => VariantKind::CLike,
2512 Some(CtorKind::Fn) => VariantKind::Tuple(
2513 variant.fields.iter().map(|field| clean_middle_field(field, cx)).collect(),
2514 ),
2515 None => VariantKind::Struct(VariantStruct {
2516 fields: variant.fields.iter().map(|field| clean_middle_field(field, cx)).collect(),
2517 }),
2518 };
2519
2520 Item::from_def_id_and_parts(
2521 variant.def_id,
2522 Some(variant.name),
2523 VariantItem(Variant { kind, discriminant }),
2524 cx.tcx,
2525 )
2526}
2527
2528pub(crate) fn clean_variant_def_with_args<'tcx>(
2529 variant: &ty::VariantDef,
2530 args: &GenericArgsRef<'tcx>,
2531 cx: &mut DocContext<'tcx>,
2532) -> Item {
2533 let discriminant = match variant.discr {
2534 ty::VariantDiscr::Explicit(def_id) => Some(Discriminant { expr: None, value: def_id }),
2535 ty::VariantDiscr::Relative(_) => None,
2536 };
2537
2538 use rustc_middle::traits::ObligationCause;
2539 use rustc_trait_selection::infer::TyCtxtInferExt;
2540 use rustc_trait_selection::traits::query::normalize::QueryNormalizeExt;
2541
2542 let infcx = cx.tcx.infer_ctxt().build(TypingMode::non_body_analysis());
2543 let kind = match variant.ctor_kind() {
2544 Some(CtorKind::Const) => VariantKind::CLike,
2545 Some(CtorKind::Fn) => VariantKind::Tuple(
2546 variant
2547 .fields
2548 .iter()
2549 .map(|field| {
2550 let ty = cx.tcx.type_of(field.did).instantiate(cx.tcx, args).skip_norm_wip();
2551
2552 let ty = infcx
2556 .at(&ObligationCause::dummy(), cx.param_env)
2557 .query_normalize(ty)
2558 .map(|normalized| normalized.value)
2559 .unwrap_or(ty);
2560
2561 clean_field_with_def_id(
2562 field.did,
2563 field.name,
2564 clean_middle_ty(ty::Binder::dummy(ty), cx, Some(field.did), None),
2565 cx.tcx,
2566 )
2567 })
2568 .collect(),
2569 ),
2570 None => VariantKind::Struct(VariantStruct {
2571 fields: variant
2572 .fields
2573 .iter()
2574 .map(|field| {
2575 let ty = cx.tcx.type_of(field.did).instantiate(cx.tcx, args).skip_norm_wip();
2576
2577 let ty = infcx
2581 .at(&ObligationCause::dummy(), cx.param_env)
2582 .query_normalize(ty)
2583 .map(|normalized| normalized.value)
2584 .unwrap_or(ty);
2585
2586 clean_field_with_def_id(
2587 field.did,
2588 field.name,
2589 clean_middle_ty(ty::Binder::dummy(ty), cx, Some(field.did), None),
2590 cx.tcx,
2591 )
2592 })
2593 .collect(),
2594 }),
2595 };
2596
2597 Item::from_def_id_and_parts(
2598 variant.def_id,
2599 Some(variant.name),
2600 VariantItem(Variant { kind, discriminant }),
2601 cx.tcx,
2602 )
2603}
2604
2605fn clean_variant_data<'tcx>(
2606 variant: &hir::VariantData<'tcx>,
2607 disr_expr: &Option<&hir::AnonConst>,
2608 cx: &mut DocContext<'tcx>,
2609) -> Variant {
2610 let discriminant = disr_expr
2611 .map(|disr| Discriminant { expr: Some(disr.body), value: disr.def_id.to_def_id() });
2612
2613 let kind = match variant {
2614 hir::VariantData::Struct { fields, .. } => VariantKind::Struct(VariantStruct {
2615 fields: fields.iter().map(|x| clean_field(x, cx)).collect(),
2616 }),
2617 hir::VariantData::Tuple(..) => {
2618 VariantKind::Tuple(variant.fields().iter().map(|x| clean_field(x, cx)).collect())
2619 }
2620 hir::VariantData::Unit(..) => VariantKind::CLike,
2621 };
2622
2623 Variant { discriminant, kind }
2624}
2625
2626fn clean_path<'tcx>(path: &hir::Path<'tcx>, cx: &mut DocContext<'tcx>) -> Path {
2627 Path {
2628 res: path.res,
2629 segments: path.segments.iter().map(|x| clean_path_segment(x, cx)).collect(),
2630 }
2631}
2632
2633fn clean_generic_args<'tcx>(
2634 trait_did: Option<DefId>,
2635 generic_args: &hir::GenericArgs<'tcx>,
2636 cx: &mut DocContext<'tcx>,
2637) -> GenericArgs {
2638 match generic_args.parenthesized {
2639 hir::GenericArgsParentheses::No => {
2640 let args = generic_args
2641 .args
2642 .iter()
2643 .map(|arg| match arg {
2644 hir::GenericArg::Lifetime(lt) if !lt.is_anonymous() => {
2645 GenericArg::Lifetime(clean_lifetime(lt, cx))
2646 }
2647 hir::GenericArg::Lifetime(_) => GenericArg::Lifetime(Lifetime::elided()),
2648 hir::GenericArg::Type(ty) => GenericArg::Type(clean_ty(ty.as_unambig_ty(), cx)),
2649 hir::GenericArg::Const(ct) => {
2650 GenericArg::Const(Box::new(clean_const(ct.as_unambig_ct())))
2651 }
2652 hir::GenericArg::Infer(_inf) => GenericArg::Infer,
2653 })
2654 .collect();
2655 let constraints = generic_args
2656 .constraints
2657 .iter()
2658 .map(|c| {
2659 clean_assoc_item_constraint(
2660 trait_did.expect("only trait ref has constraints"),
2661 c,
2662 cx,
2663 )
2664 })
2665 .collect::<ThinVec<_>>();
2666 GenericArgs::AngleBracketed { args, constraints }
2667 }
2668 hir::GenericArgsParentheses::ParenSugar => {
2669 let Some((inputs, output)) = generic_args.paren_sugar_inputs_output() else {
2670 bug!();
2671 };
2672 let inputs = inputs.iter().map(|x| clean_ty(x, cx)).collect();
2673 let output = match output.kind {
2674 hir::TyKind::Tup(&[]) => None,
2675 _ => Some(Box::new(clean_ty(output, cx))),
2676 };
2677 GenericArgs::Parenthesized { inputs, output }
2678 }
2679 hir::GenericArgsParentheses::ReturnTypeNotation => GenericArgs::ReturnTypeNotation,
2680 }
2681}
2682
2683fn clean_path_segment<'tcx>(
2684 path: &hir::PathSegment<'tcx>,
2685 cx: &mut DocContext<'tcx>,
2686) -> PathSegment {
2687 let trait_did = match path.res {
2688 hir::def::Res::Def(DefKind::Trait | DefKind::TraitAlias, did) => Some(did),
2689 _ => None,
2690 };
2691 PathSegment { name: path.ident.name, args: clean_generic_args(trait_did, path.args(), cx) }
2692}
2693
2694fn clean_bare_fn_ty<'tcx>(
2695 bare_fn: &hir::FnPtrTy<'tcx>,
2696 cx: &mut DocContext<'tcx>,
2697) -> BareFunctionDecl {
2698 let (generic_params, decl) = enter_impl_trait(cx, |cx| {
2699 let generic_params = bare_fn
2701 .generic_params
2702 .iter()
2703 .filter(|p| !is_elided_lifetime(p))
2704 .map(|x| clean_generic_param(cx, None, x))
2705 .collect();
2706 let filter = |ident: Option<Ident>| {
2710 ident.map(|ident| ident.name).filter(|&ident| ident != kw::Underscore)
2711 };
2712 let fallback =
2713 bare_fn.param_idents.iter().copied().find_map(filter).map(|_| kw::Underscore);
2714 let params = clean_params(cx, bare_fn.decl.inputs, bare_fn.param_idents, |ident| {
2715 filter(ident).or(fallback)
2716 });
2717 let decl = clean_fn_decl_with_params(cx, bare_fn.decl, None, params);
2718 (generic_params, decl)
2719 });
2720 BareFunctionDecl { safety: bare_fn.safety, abi: bare_fn.abi, decl, generic_params }
2721}
2722
2723fn clean_unsafe_binder_ty<'tcx>(
2724 unsafe_binder_ty: &hir::UnsafeBinderTy<'tcx>,
2725 cx: &mut DocContext<'tcx>,
2726) -> UnsafeBinderTy {
2727 let generic_params = unsafe_binder_ty
2728 .generic_params
2729 .iter()
2730 .filter(|p| !is_elided_lifetime(p))
2731 .map(|x| clean_generic_param(cx, None, x))
2732 .collect();
2733 let ty = clean_ty(unsafe_binder_ty.inner_ty, cx);
2734 UnsafeBinderTy { generic_params, ty }
2735}
2736
2737pub(crate) fn reexport_chain(
2738 tcx: TyCtxt<'_>,
2739 import_def_id: LocalDefId,
2740 target_def_id: DefId,
2741) -> &[Reexport] {
2742 for child in tcx.module_children_local(tcx.local_parent(import_def_id)) {
2743 if child.res.opt_def_id() == Some(target_def_id)
2744 && child.reexport_chain.first().and_then(|r| r.id()) == Some(import_def_id.to_def_id())
2745 {
2746 return &child.reexport_chain;
2747 }
2748 }
2749 &[]
2750}
2751
2752fn get_all_import_attributes<'hir>(
2754 cx: &mut DocContext<'hir>,
2755 import_def_id: LocalDefId,
2756 target_def_id: DefId,
2757 is_inline: bool,
2758) -> Vec<(Cow<'hir, hir::Attribute>, Option<DefId>)> {
2759 let mut attrs = Vec::new();
2760 let mut first = true;
2761 for def_id in reexport_chain(cx.tcx, import_def_id, target_def_id)
2762 .iter()
2763 .flat_map(|reexport| reexport.id())
2764 {
2765 let import_attrs = inline::load_attrs(cx.tcx, def_id);
2766 if first {
2767 attrs = import_attrs.iter().map(|attr| (Cow::Borrowed(attr), Some(def_id))).collect();
2769 first = false;
2770 } else if cx.document_hidden() || !cx.tcx.is_doc_hidden(def_id) {
2772 add_without_unwanted_attributes(&mut attrs, import_attrs, is_inline, Some(def_id));
2773 }
2774 }
2775 attrs
2776}
2777
2778fn add_without_unwanted_attributes<'hir>(
2799 attrs: &mut Vec<(Cow<'hir, hir::Attribute>, Option<DefId>)>,
2800 new_attrs: &'hir [hir::Attribute],
2801 is_inline: bool,
2802 import_parent: Option<DefId>,
2803) {
2804 for attr in new_attrs {
2805 match attr {
2806 hir::Attribute::Parsed(AttributeKind::DocComment { .. }) => {
2807 attrs.push((Cow::Borrowed(attr), import_parent));
2808 }
2809 hir::Attribute::Parsed(AttributeKind::Doc(d)) => {
2810 let DocAttribute {
2812 first_span: _,
2813 aliases,
2814 hidden,
2815 inline,
2816 cfg,
2817 auto_cfg: _,
2818 auto_cfg_change: _,
2819 fake_variadic: _,
2820 keyword: _,
2821 attribute: _,
2822 masked: _,
2823 notable_trait: _,
2824 search_unbox: _,
2825 html_favicon_url: _,
2826 html_logo_url: _,
2827 html_playground_url: _,
2828 html_root_url: _,
2829 html_no_source: _,
2830 issue_tracker_base_url: _,
2831 rust_logo: _,
2832 test_attrs: _,
2833 no_crate_inject: _,
2834 } = d;
2835 let mut attr = DocAttribute::default();
2836 if is_inline {
2837 attr.cfg = cfg.clone();
2838 } else {
2839 attr.inline = inline.clone();
2840 attr.hidden = hidden.clone();
2841 }
2842 attr.aliases = aliases.clone();
2843 attrs.push((
2844 Cow::Owned(hir::Attribute::Parsed(AttributeKind::Doc(Box::new(attr)))),
2845 import_parent,
2846 ));
2847 }
2848
2849 hir::Attribute::Parsed(AttributeKind::CfgTrace(..)) if !is_inline => {}
2851 _ => {
2853 attrs.push((Cow::Borrowed(attr), import_parent));
2854 }
2855 }
2856 }
2857}
2858
2859fn clean_maybe_renamed_item<'tcx>(
2860 cx: &mut DocContext<'tcx>,
2861 item: &hir::Item<'tcx>,
2862 renamed: Option<Symbol>,
2863 import_ids: &[LocalDefId],
2864) -> Vec<Item> {
2865 use hir::ItemKind;
2866 fn get_name(tcx: TyCtxt<'_>, item: &hir::Item<'_>, renamed: Option<Symbol>) -> Option<Symbol> {
2867 renamed.or_else(|| tcx.hir_opt_name(item.hir_id()))
2868 }
2869
2870 let def_id = item.owner_id.to_def_id();
2871 cx.with_param_env(def_id, |cx| {
2872 match item.kind {
2875 ItemKind::Impl(ref impl_) => {
2876 return clean_impl(impl_, item.owner_id.def_id, cx, renamed.is_some());
2880 }
2881 ItemKind::Use(path, kind) => {
2882 return clean_use_statement(
2883 item,
2884 get_name(cx.tcx, item, renamed),
2885 path,
2886 kind,
2887 cx,
2888 &mut FxHashSet::default(),
2889 );
2890 }
2891 _ => {}
2892 }
2893
2894 let mut name = get_name(cx.tcx, item, renamed).unwrap();
2895
2896 let kind = match item.kind {
2897 ItemKind::Static(mutability, _, ty, body_id) => StaticItem(Static {
2898 type_: Box::new(clean_ty(ty, cx)),
2899 mutability,
2900 expr: Some(body_id),
2901 }),
2902 ItemKind::Const(_, generics, ty, rhs) => ConstantItem(Box::new(Constant {
2903 generics: clean_generics(generics, cx),
2904 type_: clean_ty(ty, cx),
2905 kind: clean_const_item_rhs(rhs, def_id),
2906 })),
2907 ItemKind::TyAlias(_, generics, ty) => {
2908 *cx.current_type_aliases.entry(def_id).or_insert(0) += 1;
2909 let rustdoc_ty = clean_ty(ty, cx);
2910 let type_ =
2911 clean_middle_ty(ty::Binder::dummy(lower_ty(cx.tcx, ty)), cx, None, None);
2912 let generics = clean_generics(generics, cx);
2913 if let Some(count) = cx.current_type_aliases.get_mut(&def_id) {
2914 *count -= 1;
2915 if *count == 0 {
2916 cx.current_type_aliases.remove(&def_id);
2917 }
2918 }
2919
2920 let ty = cx.tcx.type_of(def_id).instantiate_identity().skip_norm_wip();
2921
2922 let mut ret = Vec::new();
2923 let inner_type = clean_ty_alias_inner_type(ty, cx, &mut ret);
2924
2925 ret.push(generate_item_with_correct_attrs(
2926 cx,
2927 TypeAliasItem(Box::new(TypeAlias {
2928 generics,
2929 inner_type,
2930 type_: rustdoc_ty,
2931 item_type: Some(type_),
2932 })),
2933 item.owner_id.def_id.to_def_id(),
2934 name,
2935 import_ids,
2936 renamed,
2937 ));
2938 return ret;
2939 }
2940 ItemKind::Enum(_, generics, def) => EnumItem(Enum {
2941 variants: def.variants.iter().map(|v| clean_variant(v, cx)).collect(),
2942 generics: clean_generics(generics, cx),
2943 }),
2944 ItemKind::TraitAlias(_, _, generics, bounds) => TraitAliasItem(TraitAlias {
2945 generics: clean_generics(generics, cx),
2946 bounds: bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(),
2947 }),
2948 ItemKind::Union(_, generics, variant_data) => UnionItem(Union {
2949 generics: clean_generics(generics, cx),
2950 fields: variant_data.fields().iter().map(|x| clean_field(x, cx)).collect(),
2951 }),
2952 ItemKind::Struct(_, generics, variant_data) => StructItem(Struct {
2953 ctor_kind: variant_data.ctor_kind(),
2954 generics: clean_generics(generics, cx),
2955 fields: variant_data.fields().iter().map(|x| clean_field(x, cx)).collect(),
2956 }),
2957 ItemKind::Macro(_, macro_def, kinds) => match kinds {
2958 MacroKinds::ATTR => clean_proc_macro(item, &mut name, MacroKind::Attr, cx.tcx),
2959 MacroKinds::DERIVE => clean_proc_macro(item, &mut name, MacroKind::Derive, cx.tcx),
2960 _ => MacroItem(
2961 Macro {
2962 source: display_macro_source(cx.tcx, name, macro_def),
2963 macro_rules: macro_def.macro_rules,
2964 },
2965 kinds,
2966 ),
2967 },
2968 ItemKind::Fn { ref sig, generics, body: body_id, .. } => {
2970 clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx)
2971 }
2972 ItemKind::Trait { generics, bounds, items: item_ids, .. } => {
2974 let items = item_ids
2975 .iter()
2976 .map(|&ti| clean_trait_item(cx.tcx.hir_trait_item(ti), cx))
2977 .collect();
2978
2979 TraitItem(Box::new(Trait {
2980 def_id,
2981 items,
2982 generics: clean_generics(generics, cx),
2983 bounds: bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(),
2984 }))
2985 }
2986 ItemKind::ExternCrate(orig_name, _) => {
2987 return clean_extern_crate(item, name, orig_name, cx);
2988 }
2989 _ => span_bug!(item.span, "not yet converted"),
2990 };
2991
2992 vec![generate_item_with_correct_attrs(
2993 cx,
2994 kind,
2995 item.owner_id.def_id.to_def_id(),
2996 name,
2997 import_ids,
2998 renamed,
2999 )]
3000 })
3001}
3002
3003fn clean_variant<'tcx>(variant: &hir::Variant<'tcx>, cx: &mut DocContext<'tcx>) -> Item {
3004 let kind = VariantItem(clean_variant_data(&variant.data, &variant.disr_expr, cx));
3005 Item::from_def_id_and_parts(variant.def_id.to_def_id(), Some(variant.ident.name), kind, cx.tcx)
3006}
3007
3008fn clean_impl<'tcx>(
3009 impl_: &hir::Impl<'tcx>,
3010 def_id: LocalDefId,
3011 cx: &mut DocContext<'tcx>,
3012 is_inlined: bool,
3016) -> Vec<Item> {
3017 let tcx = cx.tcx;
3018 let mut ret = Vec::new();
3019 let trait_ = match impl_.of_trait {
3020 Some(t) => {
3021 if is_inlined {
3022 return vec![Item::from_def_id_and_parts(
3023 def_id.to_def_id(),
3024 None,
3025 PlaceholderImplItem,
3026 tcx,
3027 )];
3028 }
3029 Some(clean_trait_ref(&t.trait_ref, cx))
3030 }
3031 None => None,
3032 };
3033 let items = impl_
3034 .items
3035 .iter()
3036 .map(|&ii| clean_impl_item(tcx.hir_impl_item(ii), cx))
3037 .collect::<Vec<_>>();
3038
3039 if trait_.as_ref().is_some_and(|t| tcx.lang_items().deref_trait() == Some(t.def_id()))
3042 && tcx.impl_polarity(def_id) != ty::ImplPolarity::Negative
3043 {
3044 build_deref_target_impls(cx, &items, &mut ret);
3045 }
3046
3047 let for_ = clean_ty(impl_.self_ty, cx);
3048 let type_alias =
3049 for_.def_id(&cx.cache).and_then(|alias_def_id: DefId| match tcx.def_kind(alias_def_id) {
3050 DefKind::TyAlias => Some(clean_middle_ty(
3051 ty::Binder::dummy(tcx.type_of(def_id).instantiate_identity().skip_norm_wip()),
3052 cx,
3053 Some(def_id.to_def_id()),
3054 None,
3055 )),
3056 _ => None,
3057 });
3058 let is_deprecated = tcx
3059 .lookup_deprecation(def_id.to_def_id())
3060 .is_some_and(|deprecation| deprecation.is_in_effect());
3061 let mut make_item = |trait_: Option<Path>, for_: Type, items: Vec<Item>| {
3062 let kind = ImplItem(Box::new(Impl {
3063 safety: match impl_.of_trait {
3064 Some(of_trait) => of_trait.safety,
3065 None => hir::Safety::Safe,
3066 },
3067 generics: clean_generics(impl_.generics, cx),
3068 trait_,
3069 for_,
3070 items,
3071 polarity: if impl_.of_trait.is_some() {
3072 tcx.impl_polarity(def_id)
3073 } else {
3074 ty::ImplPolarity::Positive
3075 },
3076 kind: if utils::has_doc_flag(tcx, def_id.to_def_id(), |d| d.fake_variadic.is_some()) {
3077 ImplKind::FakeVariadic
3078 } else {
3079 ImplKind::Normal
3080 },
3081 is_deprecated,
3082 }));
3083 Item::from_def_id_and_parts(def_id.to_def_id(), None, kind, tcx)
3084 };
3085 if let Some(type_alias) = type_alias {
3086 ret.push(make_item(trait_.clone(), type_alias, items.clone()));
3087 }
3088 ret.push(make_item(trait_, for_, items));
3089 ret
3090}
3091
3092fn clean_extern_crate<'tcx>(
3093 krate: &hir::Item<'tcx>,
3094 name: Symbol,
3095 orig_name: Option<Symbol>,
3096 cx: &mut DocContext<'tcx>,
3097) -> Vec<Item> {
3098 let cnum = cx.tcx.extern_mod_stmt_cnum(krate.owner_id.def_id).unwrap_or(LOCAL_CRATE);
3100 let crate_def_id = cnum.as_def_id();
3102 let attrs = cx.tcx.hir_attrs(krate.hir_id());
3103 let ty_vis = cx.tcx.visibility(krate.owner_id);
3104 let please_inline = ty_vis.is_public()
3105 && attrs.iter().any(|a| {
3106 matches!(
3107 a,
3108 hir::Attribute::Parsed(AttributeKind::Doc(d))
3109 if d.inline.first().is_some_and(|(i, _)| *i == DocInline::Inline))
3110 })
3111 && !cx.is_json_output();
3112
3113 let krate_owner_def_id = krate.owner_id.def_id;
3114
3115 if please_inline
3116 && let Some(items) = inline::try_inline(
3117 cx,
3118 Res::Def(DefKind::Mod, crate_def_id),
3119 name,
3120 Some((attrs, Some(krate_owner_def_id))),
3121 &mut Default::default(),
3122 )
3123 {
3124 return items;
3125 }
3126
3127 vec![Item::from_def_id_and_parts(
3128 krate_owner_def_id.to_def_id(),
3129 Some(name),
3130 ExternCrateItem { src: orig_name },
3131 cx.tcx,
3132 )]
3133}
3134
3135fn clean_use_statement<'tcx>(
3136 import: &hir::Item<'tcx>,
3137 name: Option<Symbol>,
3138 path: &hir::UsePath<'tcx>,
3139 kind: hir::UseKind,
3140 cx: &mut DocContext<'tcx>,
3141 inlined_names: &mut FxHashSet<(ItemType, Symbol)>,
3142) -> Vec<Item> {
3143 let mut items = Vec::new();
3144 let hir::UsePath { segments, ref res, span } = *path;
3145 for res in res.present_items() {
3146 let path = hir::Path { segments, res, span };
3147 items.append(&mut clean_use_statement_inner(import, name, &path, kind, cx, inlined_names));
3148 }
3149 items
3150}
3151
3152fn clean_use_statement_inner<'tcx>(
3153 import: &hir::Item<'tcx>,
3154 name: Option<Symbol>,
3155 path: &hir::Path<'tcx>,
3156 kind: hir::UseKind,
3157 cx: &mut DocContext<'tcx>,
3158 inlined_names: &mut FxHashSet<(ItemType, Symbol)>,
3159) -> Vec<Item> {
3160 if should_ignore_res(path.res) {
3161 return Vec::new();
3162 }
3163 if import.span.ctxt().outer_expn_data().kind == ExpnKind::AstPass(AstPass::StdImports) {
3167 return Vec::new();
3168 }
3169
3170 let visibility = cx.tcx.visibility(import.owner_id);
3171 let attrs = cx.tcx.hir_attrs(import.hir_id());
3172 let inline_attr = find_attr!(
3173 attrs,
3174 Doc(d) if d.inline.first().is_some_and(|(i, _)| *i == DocInline::Inline) => d
3175 )
3176 .and_then(|d| d.inline.first());
3177 let pub_underscore = visibility.is_public() && name == Some(kw::Underscore);
3178 let current_mod = cx.tcx.parent_module_from_def_id(import.owner_id.def_id);
3179 let import_def_id = import.owner_id.def_id;
3180
3181 let parent_mod = cx.tcx.parent_module_from_def_id(current_mod.to_local_def_id());
3185
3186 let is_visible_from_parent_mod =
3192 visibility.is_accessible_from(parent_mod, cx.tcx) && !current_mod.is_top_level_module();
3193
3194 if pub_underscore && let Some((_, inline_span)) = inline_attr {
3195 struct_span_code_err!(
3196 cx.tcx.dcx(),
3197 *inline_span,
3198 E0780,
3199 "anonymous imports cannot be inlined"
3200 )
3201 .with_span_label(import.span, "anonymous import")
3202 .emit();
3203 }
3204
3205 let mut denied = cx.is_json_output()
3210 || !(visibility.is_public() || (cx.document_private() && is_visible_from_parent_mod))
3211 || pub_underscore
3212 || attrs.iter().any(|a| matches!(
3213 a,
3214 hir::Attribute::Parsed(AttributeKind::Doc(d))
3215 if d.hidden.is_some() || d.inline.first().is_some_and(|(i, _)| *i == DocInline::NoInline)
3216 ));
3217
3218 let path = clean_path(path, cx);
3221 let inner = if kind == hir::UseKind::Glob {
3222 if !denied {
3223 let mut visited = DefIdSet::default();
3224 if let Some(items) = inline::try_inline_glob(
3225 cx,
3226 path.res,
3227 current_mod,
3228 &mut visited,
3229 inlined_names,
3230 import,
3231 ) {
3232 return items;
3233 }
3234 }
3235 Import::new_glob(resolve_use_source(cx, path), true)
3236 } else {
3237 let name = name.unwrap();
3238 if inline_attr.is_none()
3239 && let Res::Def(DefKind::Mod, did) = path.res
3240 && !did.is_local()
3241 && did.is_crate_root()
3242 {
3243 denied = true;
3246 }
3247 if !denied
3248 && let Some(mut items) = inline::try_inline(
3249 cx,
3250 path.res,
3251 name,
3252 Some((attrs, Some(import_def_id))),
3253 &mut Default::default(),
3254 )
3255 {
3256 items.push(Item::from_def_id_and_parts(
3257 import_def_id.to_def_id(),
3258 None,
3259 ImportItem(Import::new_simple(name, resolve_use_source(cx, path), false)),
3260 cx.tcx,
3261 ));
3262 return items;
3263 }
3264 Import::new_simple(name, resolve_use_source(cx, path), true)
3265 };
3266
3267 vec![Item::from_def_id_and_parts(import_def_id.to_def_id(), None, ImportItem(inner), cx.tcx)]
3268}
3269
3270fn clean_maybe_renamed_foreign_item<'tcx>(
3271 cx: &mut DocContext<'tcx>,
3272 item: &hir::ForeignItem<'tcx>,
3273 renamed: Option<Symbol>,
3274 import_id: Option<LocalDefId>,
3275) -> Item {
3276 let def_id = item.owner_id.to_def_id();
3277 cx.with_param_env(def_id, |cx| {
3278 let kind = match item.kind {
3279 hir::ForeignItemKind::Fn(sig, idents, generics) => ForeignFunctionItem(
3280 clean_function(cx, &sig, generics, ParamsSrc::Idents(idents), def_id),
3281 sig.header.safety(),
3282 ),
3283 hir::ForeignItemKind::Static(ty, mutability, safety) => ForeignStaticItem(
3284 Static { type_: Box::new(clean_ty(ty, cx)), mutability, expr: None },
3285 safety,
3286 ),
3287 hir::ForeignItemKind::Type => ForeignTypeItem,
3288 };
3289
3290 let mut clean_item = generate_item_with_correct_attrs(
3291 cx,
3292 kind,
3293 item.owner_id.def_id.to_def_id(),
3294 item.ident.name,
3295 import_id.as_slice(),
3296 renamed,
3297 );
3298 let mut attrs = Attributes::from_hir(inline::load_attrs(
3300 cx.tcx,
3301 cx.tcx.hir_owner_parent(item.owner_id).owner.to_def_id(),
3302 ));
3303 attrs.merge_with(std::mem::take(&mut clean_item.inner.attrs));
3304 clean_item.inner.attrs = attrs;
3305 clean_item
3306 })
3307}
3308
3309fn clean_assoc_item_constraint<'tcx>(
3310 trait_did: DefId,
3311 constraint: &hir::AssocItemConstraint<'tcx>,
3312 cx: &mut DocContext<'tcx>,
3313) -> AssocItemConstraint {
3314 AssocItemConstraint {
3315 assoc: PathSegment {
3316 name: constraint.ident.name,
3317 args: clean_generic_args(None, constraint.gen_args, cx),
3318 },
3319 kind: match constraint.kind {
3320 hir::AssocItemConstraintKind::Equality { ref term } => {
3321 let assoc_tag = match term {
3322 hir::Term::Ty(_) => ty::AssocTag::Type,
3323 hir::Term::Const(_) => ty::AssocTag::Const,
3324 };
3325 let assoc_item = cx
3326 .tcx
3327 .associated_items(trait_did)
3328 .find_by_ident_and_kind(cx.tcx, constraint.ident, assoc_tag, trait_did)
3329 .map(|item| item.def_id);
3330 AssocItemConstraintKind::Equality { term: clean_hir_term(assoc_item, term, cx) }
3331 }
3332 hir::AssocItemConstraintKind::Bound { bounds } => AssocItemConstraintKind::Bound {
3333 bounds: bounds.iter().filter_map(|b| clean_generic_bound(b, cx)).collect(),
3334 },
3335 },
3336 }
3337}
3338
3339fn clean_bound_vars<'tcx>(
3340 bound_vars: &ty::List<ty::BoundVariableKind<'tcx>>,
3341 tcx: TyCtxt<'tcx>,
3342) -> Vec<GenericParamDef> {
3343 bound_vars
3344 .into_iter()
3345 .filter_map(|var| match var {
3346 ty::BoundVariableKind::Region(ty::BoundRegionKind::Named(def_id)) => {
3347 let name = tcx.item_name(def_id);
3348 if name != kw::UnderscoreLifetime {
3349 Some(GenericParamDef::lifetime(def_id, name))
3350 } else {
3351 None
3352 }
3353 }
3354 ty::BoundVariableKind::Ty(ty::BoundTyKind::Param(def_id)) => {
3355 let name = tcx.item_name(def_id);
3356 Some(GenericParamDef {
3357 name,
3358 def_id,
3359 kind: GenericParamDefKind::Type {
3360 bounds: ThinVec::new(),
3361 default: None,
3362 synthetic: false,
3363 },
3364 })
3365 }
3366 ty::BoundVariableKind::Const => None,
3368 _ => None,
3369 })
3370 .collect()
3371}