]>
Commit | Line | Data |
---|---|---|
9fa01778 | 1 | //! Simplification of where-clauses and parameter bounds into a prettier and |
9346a6ac AL |
2 | //! more canonical form. |
3 | //! | |
ba9703b0 | 4 | //! Currently all cross-crate-inlined function use `rustc_middle::ty` to reconstruct |
0731742a | 5 | //! the AST (e.g., see all of `clean::inline`), but this is not always a |
9fa01778 | 6 | //! non-lossy transformation. The current format of storage for where-clauses |
9346a6ac | 7 | //! for functions and such is simply a list of predicates. One example of this |
9fa01778 | 8 | //! is that the AST predicate of: `where T: Trait<Foo = Bar>` is encoded as: |
ea8adc8c | 9 | //! `where T: Trait, <T as Trait>::Foo = Bar`. |
9346a6ac AL |
10 | //! |
11 | //! This module attempts to reconstruct the original where and/or parameter | |
12 | //! bounds by special casing scenarios such as these. Fun! | |
13 | ||
a7813a04 | 14 | use std::collections::BTreeMap; |
9346a6ac | 15 | |
dfeec247 | 16 | use rustc_hir::def_id::DefId; |
ba9703b0 | 17 | use rustc_middle::ty; |
fc512014 | 18 | use rustc_span::Symbol; |
9346a6ac | 19 | |
dfeec247 | 20 | use crate::clean; |
9fa01778 XL |
21 | use crate::clean::GenericArgs as PP; |
22 | use crate::clean::WherePredicate as WP; | |
9fa01778 | 23 | use crate::core::DocContext; |
9346a6ac | 24 | |
fc512014 | 25 | crate fn where_clauses(cx: &DocContext<'_>, clauses: Vec<WP>) -> Vec<WP> { |
9346a6ac | 26 | // First, partition the where clause into its separate components |
136023e0 | 27 | let mut params: BTreeMap<_, (Vec<_>, Vec<_>)> = BTreeMap::new(); |
9346a6ac AL |
28 | let mut lifetimes = Vec::new(); |
29 | let mut equalities = Vec::new(); | |
30 | let mut tybounds = Vec::new(); | |
ff7c6d11 | 31 | |
9346a6ac AL |
32 | for clause in clauses { |
33 | match clause { | |
136023e0 XL |
34 | WP::BoundPredicate { ty, bounds, bound_params } => match ty { |
35 | clean::Generic(s) => { | |
36 | let (b, p) = params.entry(s).or_default(); | |
37 | b.extend(bounds); | |
38 | p.extend(bound_params); | |
39 | } | |
40 | t => tybounds.push((t, (bounds, bound_params))), | |
dfeec247 | 41 | }, |
9346a6ac AL |
42 | WP::RegionPredicate { lifetime, bounds } => { |
43 | lifetimes.push((lifetime, bounds)); | |
44 | } | |
45 | WP::EqPredicate { lhs, rhs } => equalities.push((lhs, rhs)), | |
46 | } | |
47 | } | |
48 | ||
9346a6ac AL |
49 | // Look for equality predicates on associated types that can be merged into |
50 | // general bound predicates | |
51 | equalities.retain(|&(ref lhs, ref rhs)| { | |
e1599b0c XL |
52 | let (self_, trait_did, name) = if let Some(p) = lhs.projection() { |
53 | p | |
54 | } else { | |
55 | return true; | |
9346a6ac | 56 | }; |
e1599b0c XL |
57 | let generic = match self_ { |
58 | clean::Generic(s) => s, | |
9346a6ac AL |
59 | _ => return true, |
60 | }; | |
136023e0 | 61 | let (bounds, _) = match params.get_mut(generic) { |
9346a6ac AL |
62 | Some(bound) => bound, |
63 | None => return true, | |
64 | }; | |
e1599b0c XL |
65 | |
66 | merge_bounds(cx, bounds, trait_did, name, rhs) | |
9346a6ac AL |
67 | }); |
68 | ||
69 | // And finally, let's reassemble everything | |
70 | let mut clauses = Vec::new(); | |
dfeec247 XL |
71 | clauses.extend( |
72 | lifetimes.into_iter().map(|(lt, bounds)| WP::RegionPredicate { lifetime: lt, bounds }), | |
73 | ); | |
136023e0 XL |
74 | clauses.extend(params.into_iter().map(|(k, (bounds, params))| WP::BoundPredicate { |
75 | ty: clean::Generic(k), | |
76 | bounds, | |
77 | bound_params: params, | |
78 | })); | |
79 | clauses.extend(tybounds.into_iter().map(|(ty, (bounds, bound_params))| WP::BoundPredicate { | |
80 | ty, | |
81 | bounds, | |
82 | bound_params, | |
83 | })); | |
dfeec247 | 84 | clauses.extend(equalities.into_iter().map(|(lhs, rhs)| WP::EqPredicate { lhs, rhs })); |
9346a6ac AL |
85 | clauses |
86 | } | |
87 | ||
fc512014 | 88 | crate fn merge_bounds( |
e1599b0c XL |
89 | cx: &clean::DocContext<'_>, |
90 | bounds: &mut Vec<clean::GenericBound>, | |
91 | trait_did: DefId, | |
fc512014 | 92 | name: Symbol, |
e1599b0c XL |
93 | rhs: &clean::Type, |
94 | ) -> bool { | |
95 | !bounds.iter_mut().any(|b| { | |
96 | let trait_ref = match *b { | |
97 | clean::GenericBound::TraitBound(ref mut tr, _) => tr, | |
98 | clean::GenericBound::Outlives(..) => return false, | |
99 | }; | |
100 | let (did, path) = match trait_ref.trait_ { | |
dfeec247 | 101 | clean::ResolvedPath { did, ref mut path, .. } => (did, path), |
e1599b0c XL |
102 | _ => return false, |
103 | }; | |
104 | // If this QPath's trait `trait_did` is the same as, or a supertrait | |
105 | // of, the bound's trait `did` then we can keep going, otherwise | |
106 | // this is just a plain old equality bound. | |
107 | if !trait_is_same_or_supertrait(cx, did, trait_did) { | |
dfeec247 | 108 | return false; |
e1599b0c XL |
109 | } |
110 | let last = path.segments.last_mut().expect("segments were empty"); | |
111 | match last.args { | |
112 | PP::AngleBracketed { ref mut bindings, .. } => { | |
113 | bindings.push(clean::TypeBinding { | |
fc512014 | 114 | name, |
dfeec247 | 115 | kind: clean::TypeBindingKind::Equality { ty: rhs.clone() }, |
e1599b0c XL |
116 | }); |
117 | } | |
e74abb32 XL |
118 | PP::Parenthesized { ref mut output, .. } => match output { |
119 | Some(o) => assert_eq!(o, rhs), | |
dfeec247 XL |
120 | None => { |
121 | if *rhs != clean::Type::Tuple(Vec::new()) { | |
122 | *output = Some(rhs.clone()); | |
123 | } | |
e1599b0c | 124 | } |
dfeec247 | 125 | }, |
e1599b0c XL |
126 | }; |
127 | true | |
128 | }) | |
129 | } | |
130 | ||
dfeec247 | 131 | fn trait_is_same_or_supertrait(cx: &DocContext<'_>, child: DefId, trait_: DefId) -> bool { |
9346a6ac | 132 | if child == trait_ { |
dfeec247 | 133 | return true; |
9346a6ac | 134 | } |
a1dfa0c6 | 135 | let predicates = cx.tcx.super_predicates_of(child); |
e1599b0c XL |
136 | debug_assert!(cx.tcx.generics_of(child).has_self); |
137 | let self_ty = cx.tcx.types.self_param; | |
dfeec247 XL |
138 | predicates |
139 | .predicates | |
140 | .iter() | |
141 | .filter_map(|(pred, _)| { | |
5869c6ff | 142 | if let ty::PredicateKind::Trait(pred, _) = pred.kind().skip_binder() { |
3dfed10e | 143 | if pred.trait_ref.self_ty() == self_ty { Some(pred.def_id()) } else { None } |
9e0c209e SL |
144 | } else { |
145 | None | |
9346a6ac | 146 | } |
dfeec247 XL |
147 | }) |
148 | .any(|did| trait_is_same_or_supertrait(cx, did, trait_)) | |
9346a6ac | 149 | } |