rustdoc/clean/
simplify.rs1use rustc_data_structures::fx::FxIndexMap;
15use rustc_data_structures::thin_vec::ThinVec;
16use rustc_hir as hir;
17use rustc_hir::attrs::lang_items::LangItem;
18use rustc_hir::def_id::DefId;
19use rustc_middle::ty::{TyCtxt, Unnormalized};
20
21use crate::clean;
22use crate::clean::{GenericArgs as PP, WherePredicate as WP};
23use crate::core::DocContext;
24
25pub(crate) fn where_clauses(tcx: TyCtxt<'_>, clauses: ThinVec<WP>) -> ThinVec<WP> {
26 let mut tybounds = FxIndexMap::default();
31 let mut lifetimes = Vec::new();
32 let mut equalities = Vec::new();
33
34 for clause in clauses {
35 match clause {
36 WP::BoundPredicate { ty, bounds, bound_params } => {
37 let (b, p): &mut (Vec<_>, Vec<_>) = tybounds.entry(ty).or_default();
38 b.extend(bounds);
39 p.extend(bound_params);
40 }
41 WP::RegionPredicate { lifetime, bounds } => {
42 lifetimes.push((lifetime, bounds));
43 }
44 WP::ProjectionPredicate { lhs, rhs } => equalities.push((lhs, rhs)),
45 }
46 }
47
48 equalities.retain(|(lhs, rhs)| {
51 let Some((bounds, _)) = tybounds.get_mut(&lhs.self_type) else { return true };
52 merge_bounds(tcx, bounds, lhs.trait_.as_ref().unwrap().def_id(), lhs.assoc.clone(), rhs)
53 });
54
55 let mut clauses = ThinVec::with_capacity(lifetimes.len() + tybounds.len() + equalities.len());
57 clauses.extend(
58 lifetimes.into_iter().map(|(lt, bounds)| WP::RegionPredicate { lifetime: lt, bounds }),
59 );
60 clauses.extend(tybounds.into_iter().map(|(ty, (bounds, bound_params))| WP::BoundPredicate {
61 ty,
62 bounds,
63 bound_params,
64 }));
65 clauses.extend(equalities.into_iter().map(|(lhs, rhs)| WP::ProjectionPredicate { lhs, rhs }));
66 clauses
67}
68
69pub(crate) fn merge_bounds(
70 tcx: TyCtxt<'_>,
71 bounds: &mut [clean::GenericBound],
72 trait_did: DefId,
73 assoc: clean::PathSegment,
74 rhs: &clean::Term,
75) -> bool {
76 !bounds.iter_mut().any(|b| {
77 let trait_ref = match *b {
78 clean::GenericBound::TraitBound(ref mut tr, _) => tr,
79 clean::GenericBound::Outlives(..) | clean::GenericBound::Use(_) => return false,
80 };
81 if !trait_is_same_or_supertrait(tcx, trait_ref.trait_.def_id(), trait_did) {
85 return false;
86 }
87 let last = trait_ref.trait_.segments.last_mut().expect("segments were empty");
88
89 match last.args {
90 PP::AngleBracketed { ref mut constraints, .. } => {
91 constraints.push(clean::AssocItemConstraint {
92 assoc: assoc.clone(),
93 kind: clean::AssocItemConstraintKind::Equality { term: rhs.clone() },
94 });
95 }
96 PP::Parenthesized { ref mut output, .. } => match output {
97 Some(o) => assert_eq!(&clean::Term::Type(o.as_ref().clone()), rhs),
98 None => {
99 if *rhs != clean::Term::Type(clean::Type::Tuple(Vec::new())) {
100 *output = Some(Box::new(rhs.ty().unwrap().clone()));
101 }
102 }
103 },
104 PP::ReturnTypeNotation => {
105 return false;
107 }
108 };
109 true
110 })
111}
112
113fn trait_is_same_or_supertrait(tcx: TyCtxt<'_>, child: DefId, trait_: DefId) -> bool {
114 if child == trait_ {
115 return true;
116 }
117 let clauses = tcx.explicit_super_clauses_of(child);
118 clauses
119 .iter_identity_copied()
120 .map(Unnormalized::skip_norm_wip)
121 .filter_map(|(clause, _)| Some(clause.as_trait_clause()?.def_id()))
122 .any(|did| trait_is_same_or_supertrait(tcx, did, trait_))
123}
124
125pub(crate) fn sizedness_bounds(cx: &mut DocContext<'_>, generics: &mut clean::Generics) {
131 #[derive(PartialEq, Eq, PartialOrd, Ord)]
132 enum Sizedness {
133 PointeeSized,
134 MetaSized,
135 Sized,
136 }
137
138 let mut type_params: FxIndexMap<_, _> = generics
139 .params
140 .iter()
141 .filter(|param| matches!(param.kind, clean::GenericParamDefKind::Type { .. }))
142 .map(|param| (param.name, Sizedness::PointeeSized))
143 .collect();
144
145 generics.where_predicates.retain(|pred| {
146 let WP::BoundPredicate { ty: clean::Generic(param), bounds, .. } = pred else {
147 return true;
148 };
149
150 let [bound] = &*bounds else { unreachable!() };
153
154 let clean::GenericBound::TraitBound(trait_ref, hir::TraitBoundModifiers::NONE) = bound
155 else {
156 return true;
157 };
158
159 let Some(param_sizedness) = type_params.get_mut(param) else { return true };
163
164 let sizedness = match cx.tcx.as_lang_item(trait_ref.trait_.def_id()) {
165 Some(LangItem::Sized) => Sizedness::Sized,
166 Some(LangItem::MetaSized) => Sizedness::MetaSized,
167 _ => return true,
168 };
169
170 if sizedness > *param_sizedness {
171 *param_sizedness = sizedness;
172 }
173
174 false
175 });
176
177 for (param, sizedness) in type_params {
178 generics.where_predicates.push(WP::BoundPredicate {
179 ty: clean::Type::Generic(param),
180 bounds: vec![match sizedness {
181 Sizedness::MetaSized | Sizedness::PointeeSized => {
185 clean::GenericBound::maybe_sized(cx)
186 }
187 Sizedness::Sized => continue,
188 }],
189 bound_params: Vec::new(),
190 });
191 }
192}
193
194pub(crate) fn move_bounds_to_generic_parameters(generics: &mut clean::Generics) {
201 use clean::types::*;
202
203 let mut where_predicates = ThinVec::new();
204 for mut pred in generics.where_predicates.drain(..) {
205 if let WherePredicate::BoundPredicate { ty: Generic(arg), bounds, .. } = &mut pred
206 && let Some(GenericParamDef {
207 kind: GenericParamDefKind::Type { bounds: param_bounds, .. },
208 ..
209 }) = generics.params.iter_mut().find(|param| ¶m.name == arg)
210 {
211 param_bounds.extend(bounds.drain(..));
212 } else if let WherePredicate::RegionPredicate { lifetime: Lifetime(arg), bounds } =
213 &mut pred
214 && let Some(GenericParamDef {
215 kind: GenericParamDefKind::Lifetime { outlives: param_bounds },
216 ..
217 }) = generics.params.iter_mut().find(|param| ¶m.name == arg)
218 {
219 param_bounds.extend(bounds.drain(..).map(|bound| match bound {
220 GenericBound::Outlives(lifetime) => lifetime,
221 _ => unreachable!(),
222 }));
223 } else {
224 where_predicates.push(pred);
225 }
226 }
227 generics.where_predicates = where_predicates;
228}