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((ty, trait_did, name)) = lhs.projection() else {
50 return true;
51 };
52 let Some((bounds, _)) = tybounds.get_mut(ty) else { return true };
53 merge_bounds(cx, bounds, trait_did, name, rhs)
54 });
55
56 let mut clauses = ThinVec::with_capacity(lifetimes.len() + tybounds.len() + equalities.len());
58 clauses.extend(
59 lifetimes.into_iter().map(|(lt, bounds)| WP::RegionPredicate { lifetime: lt, bounds }),
60 );
61 clauses.extend(tybounds.into_iter().map(|(ty, (bounds, bound_params))| WP::BoundPredicate {
62 ty,
63 bounds,
64 bound_params,
65 }));
66 clauses.extend(equalities.into_iter().map(|(lhs, rhs)| WP::EqPredicate { lhs, rhs }));
67 clauses
68}
69
70pub(crate) fn merge_bounds(
71 cx: &clean::DocContext<'_>,
72 bounds: &mut [clean::GenericBound],
73 trait_did: DefId,
74 assoc: clean::PathSegment,
75 rhs: &clean::Term,
76) -> bool {
77 !bounds.iter_mut().any(|b| {
78 let trait_ref = match *b {
79 clean::GenericBound::TraitBound(ref mut tr, _) => tr,
80 clean::GenericBound::Outlives(..) | clean::GenericBound::Use(_) => return false,
81 };
82 if !trait_is_same_or_supertrait(cx, trait_ref.trait_.def_id(), trait_did) {
86 return false;
87 }
88 let last = trait_ref.trait_.segments.last_mut().expect("segments were empty");
89
90 match last.args {
91 PP::AngleBracketed { ref mut constraints, .. } => {
92 constraints.push(clean::AssocItemConstraint {
93 assoc: assoc.clone(),
94 kind: clean::AssocItemConstraintKind::Equality { term: rhs.clone() },
95 });
96 }
97 PP::Parenthesized { ref mut output, .. } => match output {
98 Some(o) => assert_eq!(&clean::Term::Type(o.as_ref().clone()), rhs),
99 None => {
100 if *rhs != clean::Term::Type(clean::Type::Tuple(Vec::new())) {
101 *output = Some(Box::new(rhs.ty().unwrap().clone()));
102 }
103 }
104 },
105 PP::ReturnTypeNotation => {
106 return false;
108 }
109 };
110 true
111 })
112}
113
114fn trait_is_same_or_supertrait(cx: &DocContext<'_>, child: DefId, trait_: DefId) -> bool {
115 if child == trait_ {
116 return true;
117 }
118 let predicates = cx.tcx.explicit_super_predicates_of(child);
119 predicates
120 .iter_identity_copied()
121 .filter_map(|(pred, _)| Some(pred.as_trait_clause()?.def_id()))
122 .any(|did| trait_is_same_or_supertrait(cx, did, trait_))
123}
124
125pub(crate) fn sized_bounds(cx: &mut DocContext<'_>, generics: &mut clean::Generics) {
126 let mut sized_params = UnordSet::new();
127
128 generics.where_predicates.retain(|pred| {
141 if let WP::BoundPredicate { ty: clean::Generic(param), bounds, .. } = pred
142 && bounds.iter().any(|b| b.is_sized_bound(cx))
143 {
144 sized_params.insert(*param);
145 false
146 } else {
147 true
148 }
149 });
150
151 for param in &generics.params {
154 if let clean::GenericParamDefKind::Type { .. } = param.kind
155 && !sized_params.contains(¶m.name)
156 {
157 generics.where_predicates.push(WP::BoundPredicate {
158 ty: clean::Type::Generic(param.name),
159 bounds: vec![clean::GenericBound::maybe_sized(cx)],
160 bound_params: Vec::new(),
161 })
162 }
163 }
164}
165
166pub(crate) fn move_bounds_to_generic_parameters(generics: &mut clean::Generics) {
173 use clean::types::*;
174
175 let mut where_predicates = ThinVec::new();
176 for mut pred in generics.where_predicates.drain(..) {
177 if let WherePredicate::BoundPredicate { ty: Generic(arg), bounds, .. } = &mut pred
178 && let Some(GenericParamDef {
179 kind: GenericParamDefKind::Type { bounds: param_bounds, .. },
180 ..
181 }) = generics.params.iter_mut().find(|param| ¶m.name == arg)
182 {
183 param_bounds.extend(bounds.drain(..));
184 } else if let WherePredicate::RegionPredicate { lifetime: Lifetime(arg), bounds } =
185 &mut pred
186 && let Some(GenericParamDef {
187 kind: GenericParamDefKind::Lifetime { outlives: param_bounds },
188 ..
189 }) = generics.params.iter_mut().find(|param| ¶m.name == arg)
190 {
191 param_bounds.extend(bounds.drain(..).map(|bound| match bound {
192 GenericBound::Outlives(lifetime) => lifetime,
193 _ => unreachable!(),
194 }));
195 } else {
196 where_predicates.push(pred);
197 }
198 }
199 generics.where_predicates = where_predicates;
200}