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