Skip to content

Commit 9beff38

Browse files
committed
Associated type bound for inlined impl Trait doc
1 parent b78367d commit 9beff38

File tree

4 files changed

+131
-68
lines changed

4 files changed

+131
-68
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 70 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,7 +1698,7 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics,
16981698

16991699
// Don't populate `cx.impl_trait_bounds` before `clean`ning `where` clauses,
17001700
// since `Clean for ty::Predicate` would consume them.
1701-
let mut impl_trait = FxHashMap::<ImplTraitParam, Vec<_>>::default();
1701+
let mut impl_trait = FxHashMap::<ImplTraitParam, Vec<GenericBound>>::default();
17021702

17031703
// Bounds in the type_params and lifetimes fields are repeated in the
17041704
// predicates field (see rustc_typeck::collect::ty_generics), so remove
@@ -1720,41 +1720,73 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics,
17201720
ty::GenericParamDefKind::Const { .. } => None,
17211721
}).collect::<Vec<GenericParamDef>>();
17221722

1723+
// (param index, def id of trait) -> (name, type)
1724+
let mut impl_trait_proj = FxHashMap::<(u32, DefId), Vec<(String, Type)>>::default();
1725+
17231726
let mut where_predicates = preds.predicates.iter()
17241727
.flat_map(|(p, _)| {
1725-
let param_idx = if let Some(trait_ref) = p.to_opt_poly_trait_ref() {
1726-
if let ty::Param(param) = trait_ref.self_ty().sty {
1727-
Some(param.index)
1728-
} else {
1729-
None
1730-
}
1731-
} else if let Some(outlives) = p.to_opt_type_outlives() {
1732-
if let ty::Param(param) = outlives.skip_binder().0.sty {
1733-
Some(param.index)
1734-
} else {
1735-
None
1728+
let param_idx = (|| {
1729+
if let Some(trait_ref) = p.to_opt_poly_trait_ref() {
1730+
if let ty::Param(param) = trait_ref.self_ty().sty {
1731+
return Some(param.index);
1732+
}
1733+
} else if let Some(outlives) = p.to_opt_type_outlives() {
1734+
if let ty::Param(param) = outlives.skip_binder().0.sty {
1735+
return Some(param.index);
1736+
}
1737+
} else if let ty::Predicate::Projection(proj) = p {
1738+
if let ty::Param(param) = proj.skip_binder().projection_ty.self_ty().sty {
1739+
return Some(param.index);
1740+
}
17361741
}
1737-
} else {
1742+
17381743
None
1739-
};
1744+
})();
17401745

17411746
let p = p.clean(cx)?;
17421747

1743-
if let Some(b) = param_idx.and_then(|i| impl_trait.get_mut(&i.into())) {
1744-
b.extend(
1745-
p.get_bounds()
1746-
.into_iter()
1747-
.flatten()
1748-
.cloned()
1749-
.filter(|b| !b.is_sized_bound(cx))
1750-
);
1751-
return None;
1748+
if let Some(param_idx) = param_idx {
1749+
if let Some(b) = impl_trait.get_mut(&param_idx.into()) {
1750+
b.extend(
1751+
p.get_bounds()
1752+
.into_iter()
1753+
.flatten()
1754+
.cloned()
1755+
.filter(|b| !b.is_sized_bound(cx))
1756+
);
1757+
1758+
let proj = match &p {
1759+
WherePredicate::EqPredicate { lhs, rhs } => Some((lhs, rhs))
1760+
.and_then(|(lhs, rhs)| Some((lhs.projection()?, rhs))),
1761+
_ => None,
1762+
};
1763+
if let Some(((_, trait_did, name), rhs)) = proj {
1764+
impl_trait_proj
1765+
.entry((param_idx, trait_did))
1766+
.or_default()
1767+
.push((name.to_string(), rhs.clone()));
1768+
}
1769+
1770+
return None;
1771+
}
17521772
}
17531773

17541774
Some(p)
17551775
})
17561776
.collect::<Vec<_>>();
17571777

1778+
for ((param_idx, trait_did), bounds) in impl_trait_proj {
1779+
for (name, rhs) in bounds {
1780+
simplify::merge_bounds(
1781+
cx,
1782+
impl_trait.get_mut(&param_idx.into()).unwrap(),
1783+
trait_did,
1784+
&name,
1785+
&rhs,
1786+
);
1787+
}
1788+
}
1789+
17581790
// Move `TraitPredicate`s to the front.
17591791
for (_, bounds) in impl_trait.iter_mut() {
17601792
bounds.sort_by_key(|b| if let GenericBound::TraitBound(..) = b {
@@ -2664,6 +2696,21 @@ impl Type {
26642696
_ => false,
26652697
}
26662698
}
2699+
2700+
pub fn projection(&self) -> Option<(&Type, DefId, &str)> {
2701+
let (self_, trait_, name) = match self {
2702+
QPath { ref self_type, ref trait_, ref name } => {
2703+
(self_type, trait_, name)
2704+
}
2705+
_ => return None,
2706+
};
2707+
let trait_did = match **trait_ {
2708+
ResolvedPath { did, .. } => did,
2709+
_ => return None,
2710+
};
2711+
Some((&self_, trait_did, name))
2712+
}
2713+
26672714
}
26682715

26692716
impl GetDefId for Type {

src/librustdoc/clean/simplify.rs

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -53,58 +53,21 @@ pub fn where_clauses(cx: &DocContext<'_>, clauses: Vec<WP>) -> Vec<WP> {
5353
// Look for equality predicates on associated types that can be merged into
5454
// general bound predicates
5555
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)
59-
}
60-
_ => return true,
61-
};
62-
let generic = match **self_ {
63-
clean::Generic(ref s) => s,
64-
_ => return true,
56+
let (self_, trait_did, name) = if let Some(p) = lhs.projection() {
57+
p
58+
} else {
59+
return true;
6560
};
66-
let trait_did = match **trait_ {
67-
clean::ResolvedPath { did, .. } => did,
61+
let generic = match self_ {
62+
clean::Generic(s) => s,
6863
_ => return true,
6964
};
7065
let bounds = match params.get_mut(generic) {
7166
Some(bound) => bound,
7267
None => return true,
7368
};
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,
78-
};
79-
let (did, path) = match trait_ref.trait_ {
80-
clean::ResolvedPath { did, ref mut path, ..} => (did, path),
81-
_ => return false,
82-
};
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) {
87-
return false
88-
}
89-
let last = path.segments.last_mut().expect("segments were empty");
90-
match last.args {
91-
PP::AngleBracketed { ref mut bindings, .. } => {
92-
bindings.push(clean::TypeBinding {
93-
name: name.clone(),
94-
kind: clean::TypeBindingKind::Equality {
95-
ty: rhs.clone(),
96-
},
97-
});
98-
}
99-
PP::Parenthesized { ref mut output, .. } => {
100-
assert!(output.is_none());
101-
if *rhs != clean::Type::Tuple(Vec::new()) {
102-
*output = Some(rhs.clone());
103-
}
104-
}
105-
};
106-
true
107-
})
69+
70+
merge_bounds(cx, bounds, trait_did, name, rhs)
10871
});
10972

11073
// And finally, let's reassemble everything
@@ -127,6 +90,49 @@ pub fn where_clauses(cx: &DocContext<'_>, clauses: Vec<WP>) -> Vec<WP> {
12790
clauses
12891
}
12992

93+
pub fn merge_bounds(
94+
cx: &clean::DocContext<'_>,
95+
bounds: &mut Vec<clean::GenericBound>,
96+
trait_did: DefId,
97+
name: &str,
98+
rhs: &clean::Type,
99+
) -> bool {
100+
!bounds.iter_mut().any(|b| {
101+
let trait_ref = match *b {
102+
clean::GenericBound::TraitBound(ref mut tr, _) => tr,
103+
clean::GenericBound::Outlives(..) => return false,
104+
};
105+
let (did, path) = match trait_ref.trait_ {
106+
clean::ResolvedPath { did, ref mut path, ..} => (did, path),
107+
_ => return false,
108+
};
109+
// If this QPath's trait `trait_did` is the same as, or a supertrait
110+
// of, the bound's trait `did` then we can keep going, otherwise
111+
// this is just a plain old equality bound.
112+
if !trait_is_same_or_supertrait(cx, did, trait_did) {
113+
return false
114+
}
115+
let last = path.segments.last_mut().expect("segments were empty");
116+
match last.args {
117+
PP::AngleBracketed { ref mut bindings, .. } => {
118+
bindings.push(clean::TypeBinding {
119+
name: name.to_string(),
120+
kind: clean::TypeBindingKind::Equality {
121+
ty: rhs.clone(),
122+
},
123+
});
124+
}
125+
PP::Parenthesized { ref mut output, .. } => {
126+
assert!(output.is_none());
127+
if *rhs != clean::Type::Tuple(Vec::new()) {
128+
*output = Some(rhs.clone());
129+
}
130+
}
131+
};
132+
true
133+
})
134+
}
135+
130136
pub fn ty_params(mut params: Vec<clean::GenericParamDef>) -> Vec<clean::GenericParamDef> {
131137
for param in &mut params {
132138
match param.kind {

src/test/rustdoc/inline_cross/auxiliary/impl_trait_aux.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
use std::ops::Deref;
2+
13
pub fn func<'a>(_x: impl Clone + Into<Vec<u8>> + 'a) {}
24

5+
pub fn func2<T>(_x: impl Deref<Target = Option<T>> + Iterator<Item = T>, _y: impl Iterator<Item = u8>) {}
6+
37
pub struct Foo;
48

59
impl Foo {

src/test/rustdoc/inline_cross/impl_trait.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ extern crate impl_trait_aux;
77
// @!has - '//pre[@class="rust fn"]' 'where'
88
pub use impl_trait_aux::func;
99

10+
// @has impl_trait/fn.func2.html
11+
// @has - '//pre[@class="rust fn"]' "_x: impl Deref<Target = Option<T>> + Iterator<Item = T>,"
12+
// @has - '//pre[@class="rust fn"]' "_y: impl Iterator<Item = u8>)"
13+
// @!has - '//pre[@class="rust fn"]' 'where'
14+
pub use impl_trait_aux::func2;
15+
1016
// @has impl_trait/struct.Foo.html
1117
// @has - '//code[@id="method.v"]' "pub fn method<'a>(_x: impl Clone + Into<Vec<u8>> + 'a)"
1218
// @!has - '//code[@id="method.v"]' 'where'

0 commit comments

Comments
 (0)