1 //! Simplification of where-clauses and parameter bounds into a prettier and
2 //! more canonical form.
4 //! Currently all cross-crate-inlined function use `rustc::ty` to reconstruct
5 //! the AST (e.g., see all of `clean::inline`), but this is not always a
6 //! non-lossy transformation. The current format of storage for where-clauses
7 //! for functions and such is simply a list of predicates. One example of this
8 //! is that the AST predicate of: `where T: Trait<Foo = Bar>` is encoded as:
9 //! `where T: Trait, <T as Trait>::Foo = Bar`.
11 //! This module attempts to reconstruct the original where and/or parameter
12 //! bounds by special casing scenarios such as these. Fun!
15 use std::collections::BTreeMap;
17 use rustc::hir::def_id::DefId;
20 use clean::GenericArgs as PP;
21 use clean::WherePredicate as WP;
25 pub fn where_clauses(cx: &DocContext, clauses: Vec<WP>) -> Vec<WP> {
26 // First, partition the where clause into its separate components
27 let mut params: BTreeMap<_, Vec<_>> = BTreeMap::new();
28 let mut lifetimes = Vec::new();
29 let mut equalities = Vec::new();
30 let mut tybounds = Vec::new();
32 for clause in clauses {
34 WP::BoundPredicate { ty, bounds } => {
36 clean::Generic(s) => params.entry(s).or_default()
38 t => tybounds.push((t, ty_bounds(bounds))),
41 WP::RegionPredicate { lifetime, bounds } => {
42 lifetimes.push((lifetime, bounds));
44 WP::EqPredicate { lhs, rhs } => equalities.push((lhs, rhs)),
48 // Simplify the type parameter bounds on all the generics
49 let mut params = params.into_iter().map(|(k, v)| {
51 }).collect::<BTreeMap<_, _>>();
53 // Look for equality predicates on associated types that can be merged into
54 // general bound predicates
55 equalities.retain(|&(ref lhs, ref rhs)| {
56 let (self_, trait_, name) = match *lhs {
57 clean::QPath { ref self_type, ref trait_, ref name } => {
58 (self_type, trait_, name)
62 let generic = match **self_ {
63 clean::Generic(ref s) => s,
66 let trait_did = match **trait_ {
67 clean::ResolvedPath { did, .. } => did,
70 let bounds = match params.get_mut(generic) {
74 !bounds.iter_mut().any(|b| {
75 let trait_ref = match *b {
76 clean::GenericBound::TraitBound(ref mut tr, _) => tr,
77 clean::GenericBound::Outlives(..) => return false,
79 let (did, path) = match trait_ref.trait_ {
80 clean::ResolvedPath { did, ref mut path, ..} => (did, path),
83 // If this QPath's trait `trait_did` is the same as, or a supertrait
84 // of, the bound's trait `did` then we can keep going, otherwise
85 // this is just a plain old equality bound.
86 if !trait_is_same_or_supertrait(cx, did, trait_did) {
89 let last = path.segments.last_mut().expect("segments were empty");
91 PP::AngleBracketed { ref mut bindings, .. } => {
92 bindings.push(clean::TypeBinding {
97 PP::Parenthesized { ref mut output, .. } => {
98 assert!(output.is_none());
99 if *rhs != clean::Type::Tuple(Vec::new()) {
100 *output = Some(rhs.clone());
108 // And finally, let's reassemble everything
109 let mut clauses = Vec::new();
110 clauses.extend(lifetimes.into_iter().map(|(lt, bounds)| {
111 WP::RegionPredicate { lifetime: lt, bounds: bounds }
113 clauses.extend(params.into_iter().map(|(k, v)| {
115 ty: clean::Generic(k),
119 clauses.extend(tybounds.into_iter().map(|(ty, bounds)| {
120 WP::BoundPredicate { ty: ty, bounds: bounds }
122 clauses.extend(equalities.into_iter().map(|(lhs, rhs)| {
123 WP::EqPredicate { lhs: lhs, rhs: rhs }
128 pub fn ty_params(mut params: Vec<clean::GenericParamDef>) -> Vec<clean::GenericParamDef> {
129 for param in &mut params {
131 clean::GenericParamDefKind::Type { ref mut bounds, .. } => {
132 *bounds = ty_bounds(mem::replace(bounds, Vec::new()));
134 _ => panic!("expected only type parameters"),
140 fn ty_bounds(bounds: Vec<clean::GenericBound>) -> Vec<clean::GenericBound> {
144 fn trait_is_same_or_supertrait(cx: &DocContext, child: DefId,
145 trait_: DefId) -> bool {
149 let predicates = cx.tcx.super_predicates_of(child);
150 predicates.predicates.iter().filter_map(|(pred, _)| {
151 if let ty::Predicate::Trait(ref pred) = *pred {
152 if pred.skip_binder().trait_ref.self_ty().is_self() {
160 }).any(|did| trait_is_same_or_supertrait(cx, did, trait_))