rustdoc/clean/
simplify.rs1use rustc_data_structures::fx::FxIndexMap;
15use rustc_data_structures::unord::UnordSet;
16use rustc_hir::def_id::DefId;
17use thin_vec::ThinVec;
18
19use crate::clean;
20use crate::clean::{GenericArgs as PP, WherePredicate as WP};
21use crate::core::DocContext;
22
23pub(crate) fn where_clauses(cx: &DocContext<'_>, clauses: ThinVec<WP>) -> ThinVec<WP> {
24 let mut tybounds = FxIndexMap::default();
29 let mut lifetimes = Vec::new();
30 let mut equalities = Vec::new();
31
32 for clause in clauses {
33 match clause {
34 WP::BoundPredicate { ty, bounds, bound_params } => {
35 let (b, p): &mut (Vec<_>, Vec<_>) = tybounds.entry(ty).or_default();
36 b.extend(bounds);
37 p.extend(bound_params);
38 }
39 WP::RegionPredicate { lifetime, bounds } => {
40 lifetimes.push((lifetime, bounds));
41 }
42 WP::EqPredicate { lhs, rhs } => equalities.push((lhs, rhs)),
43 }
44 }
45
46 equalities.retain(|(lhs, rhs)| {
49 let Some((bounds, _)) = tybounds.get_mut(&lhs.self_type) else { return true };
50 merge_bounds(cx, bounds, lhs.trait_.as_ref().unwrap().def_id(), lhs.assoc.clone(), rhs)
51 });
52
53 let mut clauses = ThinVec::with_capacity(lifetimes.len() + tybounds.len() + equalities.len());
55 clauses.extend(
56 lifetimes.into_iter().map(|(lt, bounds)| WP::RegionPredicate { lifetime: lt, bounds }),
57 );
58 clauses.extend(tybounds.into_iter().map(|(ty, (bounds, bound_params))| WP::BoundPredicate {
59 ty,
60 bounds,
61 bound_params,
62 }));
63 clauses.extend(equalities.into_iter().map(|(lhs, rhs)| WP::EqPredicate { lhs, rhs }));
64 clauses
65}
66
67pub(crate) fn merge_bounds(
68 cx: &clean::DocContext<'_>,
69 bounds: &mut [clean::GenericBound],
70 trait_did: DefId,
71 assoc: clean::PathSegment,
72 rhs: &clean::Term,
73) -> bool {
74 !bounds.iter_mut().any(|b| {
75 let trait_ref = match *b {
76 clean::GenericBound::TraitBound(ref mut tr, _) => tr,
77 clean::GenericBound::Outlives(..) | clean::GenericBound::Use(_) => return false,
78 };
79 if !trait_is_same_or_supertrait(cx, trait_ref.trait_.def_id(), trait_did) {
83 return false;
84 }
85 let last = trait_ref.trait_.segments.last_mut().expect("segments were empty");
86
87 match last.args {
88 PP::AngleBracketed { ref mut constraints, .. } => {
89 constraints.push(clean::AssocItemConstraint {
90 assoc: assoc.clone(),
91 kind: clean::AssocItemConstraintKind::Equality { term: rhs.clone() },
92 });
93 }
94 PP::Parenthesized { ref mut output, .. } => match output {
95 Some(o) => assert_eq!(&clean::Term::Type(o.as_ref().clone()), rhs),
96 None => {
97 if *rhs != clean::Term::Type(clean::Type::Tuple(Vec::new())) {
98 *output = Some(Box::new(rhs.ty().unwrap().clone()));
99 }
100 }
101 },
102 PP::ReturnTypeNotation => {
103 return false;
105 }
106 };
107 true
108 })
109}
110
111fn trait_is_same_or_supertrait(cx: &DocContext<'_>, child: DefId, trait_: DefId) -> bool {
112 if child == trait_ {
113 return true;
114 }
115 let predicates = cx.tcx.explicit_super_predicates_of(child);
116 predicates
117 .iter_identity_copied()
118 .filter_map(|(pred, _)| Some(pred.as_trait_clause()?.def_id()))
119 .any(|did| trait_is_same_or_supertrait(cx, did, trait_))
120}
121
122pub(crate) fn sized_bounds(cx: &mut DocContext<'_>, generics: &mut clean::Generics) {
123 let mut sized_params = UnordSet::new();
124
125 generics.where_predicates.retain(|pred| {
138 let WP::BoundPredicate { ty: clean::Generic(param), bounds, .. } = pred else {
139 return true;
140 };
141
142 if bounds.iter().any(|b| b.is_sized_bound(cx)) {
143 sized_params.insert(*param);
144 false
145 } else if bounds.iter().any(|b| b.is_meta_sized_bound(cx)) {
146 false
149 } else {
150 true
151 }
152 });
153
154 for param in &generics.params {
157 if let clean::GenericParamDefKind::Type { .. } = param.kind
158 && !sized_params.contains(¶m.name)
159 {
160 generics.where_predicates.push(WP::BoundPredicate {
161 ty: clean::Type::Generic(param.name),
162 bounds: vec![clean::GenericBound::maybe_sized(cx)],
163 bound_params: Vec::new(),
164 })
165 }
166 }
167}
168
169pub(crate) fn move_bounds_to_generic_parameters(generics: &mut clean::Generics) {
176 use clean::types::*;
177
178 let mut where_predicates = ThinVec::new();
179 for mut pred in generics.where_predicates.drain(..) {
180 if let WherePredicate::BoundPredicate { ty: Generic(arg), bounds, .. } = &mut pred
181 && let Some(GenericParamDef {
182 kind: GenericParamDefKind::Type { bounds: param_bounds, .. },
183 ..
184 }) = generics.params.iter_mut().find(|param| ¶m.name == arg)
185 {
186 param_bounds.extend(bounds.drain(..));
187 } else if let WherePredicate::RegionPredicate { lifetime: Lifetime(arg), bounds } =
188 &mut pred
189 && let Some(GenericParamDef {
190 kind: GenericParamDefKind::Lifetime { outlives: param_bounds },
191 ..
192 }) = generics.params.iter_mut().find(|param| ¶m.name == arg)
193 {
194 param_bounds.extend(bounds.drain(..).map(|bound| match bound {
195 GenericBound::Outlives(lifetime) => lifetime,
196 _ => unreachable!(),
197 }));
198 } else {
199 where_predicates.push(pred);
200 }
201 }
202 generics.where_predicates = where_predicates;
203}