1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::collections::BTreeMap;
use rustc_hir::def_id::DefId;
use rustc_middle::ty;
use crate::clean;
use crate::clean::GenericArgs as PP;
use crate::clean::WherePredicate as WP;
use crate::core::DocContext;
crate fn where_clauses(cx: &DocContext<'_>, clauses: Vec<WP>) -> Vec<WP> {
let mut params: BTreeMap<_, Vec<_>> = BTreeMap::new();
let mut lifetimes = Vec::new();
let mut equalities = Vec::new();
let mut tybounds = Vec::new();
for clause in clauses {
match clause {
WP::BoundPredicate { ty, bounds } => match ty {
clean::Generic(s) => params.entry(s).or_default().extend(bounds),
t => tybounds.push((t, bounds)),
},
WP::RegionPredicate { lifetime, bounds } => {
lifetimes.push((lifetime, bounds));
}
WP::EqPredicate { lhs, rhs } => equalities.push((lhs, rhs)),
}
}
equalities.retain(|&(ref lhs, ref rhs)| {
let (self_, trait_did, name) = if let Some(p) = lhs.projection() {
p
} else {
return true;
};
let generic = match self_ {
clean::Generic(s) => s,
_ => return true,
};
let bounds = match params.get_mut(generic) {
Some(bound) => bound,
None => return true,
};
merge_bounds(cx, bounds, trait_did, name, rhs)
});
let mut clauses = Vec::new();
clauses.extend(
lifetimes.into_iter().map(|(lt, bounds)| WP::RegionPredicate { lifetime: lt, bounds }),
);
clauses.extend(
params.into_iter().map(|(k, v)| WP::BoundPredicate { ty: clean::Generic(k), bounds: v }),
);
clauses.extend(tybounds.into_iter().map(|(ty, bounds)| WP::BoundPredicate { ty, bounds }));
clauses.extend(equalities.into_iter().map(|(lhs, rhs)| WP::EqPredicate { lhs, rhs }));
clauses
}
crate fn merge_bounds(
cx: &clean::DocContext<'_>,
bounds: &mut Vec<clean::GenericBound>,
trait_did: DefId,
name: &str,
rhs: &clean::Type,
) -> bool {
!bounds.iter_mut().any(|b| {
let trait_ref = match *b {
clean::GenericBound::TraitBound(ref mut tr, _) => tr,
clean::GenericBound::Outlives(..) => return false,
};
let (did, path) = match trait_ref.trait_ {
clean::ResolvedPath { did, ref mut path, .. } => (did, path),
_ => return false,
};
if !trait_is_same_or_supertrait(cx, did, trait_did) {
return false;
}
let last = path.segments.last_mut().expect("segments were empty");
match last.args {
PP::AngleBracketed { ref mut bindings, .. } => {
bindings.push(clean::TypeBinding {
name: name.to_string(),
kind: clean::TypeBindingKind::Equality { ty: rhs.clone() },
});
}
PP::Parenthesized { ref mut output, .. } => match output {
Some(o) => assert_eq!(o, rhs),
None => {
if *rhs != clean::Type::Tuple(Vec::new()) {
*output = Some(rhs.clone());
}
}
},
};
true
})
}
fn trait_is_same_or_supertrait(cx: &DocContext<'_>, child: DefId, trait_: DefId) -> bool {
if child == trait_ {
return true;
}
let predicates = cx.tcx.super_predicates_of(child);
debug_assert!(cx.tcx.generics_of(child).has_self);
let self_ty = cx.tcx.types.self_param;
predicates
.predicates
.iter()
.filter_map(|(pred, _)| {
if let ty::PredicateAtom::Trait(pred, _) = pred.skip_binders() {
if pred.trait_ref.self_ty() == self_ty { Some(pred.def_id()) } else { None }
} else {
None
}
})
.any(|did| trait_is_same_or_supertrait(cx, did, trait_))
}