Skip to content

Commit a786eaa

Browse files
committed
Simplify rustdoc handling of type aliases for associated types
The logic was very hard to follow before.
1 parent 18d27b2 commit a786eaa

File tree

5 files changed

+37
-31
lines changed

5 files changed

+37
-31
lines changed

src/librustdoc/clean/inline.rs

+3-17
Original file line numberDiff line numberDiff line change
@@ -261,26 +261,12 @@ fn build_union(cx: &DocContext<'_>, did: DefId) -> clean::Union {
261261

262262
fn build_type_alias(cx: &DocContext<'_>, did: DefId) -> clean::Typedef {
263263
let predicates = cx.tcx.explicit_predicates_of(did);
264+
let type_ = cx.tcx.type_of(did).clean(cx);
264265

265266
clean::Typedef {
266-
type_: cx.tcx.type_of(did).clean(cx),
267+
type_: type_.clone(),
267268
generics: (cx.tcx.generics_of(did), predicates).clean(cx),
268-
item_type: build_type_alias_type(cx, did),
269-
}
270-
}
271-
272-
fn build_type_alias_type(cx: &DocContext<'_>, did: DefId) -> Option<clean::Type> {
273-
let type_ = cx.tcx.type_of(did).clean(cx);
274-
type_.def_id().and_then(|did| build_ty(cx, did))
275-
}
276-
277-
crate fn build_ty(cx: &DocContext<'_>, did: DefId) -> Option<clean::Type> {
278-
match cx.tcx.def_kind(did) {
279-
DefKind::Struct | DefKind::Union | DefKind::Enum | DefKind::Const | DefKind::Static => {
280-
Some(cx.tcx.type_of(did).clean(cx))
281-
}
282-
DefKind::TyAlias => build_type_alias_type(cx, did),
283-
_ => None,
269+
item_type: Some(type_),
284270
}
285271
}
286272

src/librustdoc/clean/mod.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1118,10 +1118,10 @@ impl Clean<Item> for hir::ImplItem<'_> {
11181118
}
11191119
MethodItem(m, Some(self.defaultness))
11201120
}
1121-
hir::ImplItemKind::TyAlias(ref ty) => {
1122-
let type_ = ty.clean(cx);
1123-
let item_type = type_.def_id().and_then(|did| inline::build_ty(cx, did));
1124-
TypedefItem(Typedef { type_, generics: Generics::default(), item_type }, true)
1121+
hir::ImplItemKind::TyAlias(ref hir_ty) => {
1122+
let type_ = hir_ty.clean(cx);
1123+
let item_type = hir_ty_to_ty(cx.tcx, hir_ty).clean(cx);
1124+
TypedefItem(Typedef { type_, generics: Generics::default(), item_type: Some(item_type) }, true)
11251125
}
11261126
};
11271127
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx)
@@ -1267,13 +1267,13 @@ impl Clean<Item> for ty::AssocItem {
12671267

12681268
AssocTypeItem(bounds, ty.clean(cx))
12691269
} else {
1270+
// FIXME: when could this happen? ASsociated items in inherent impls?
12701271
let type_ = cx.tcx.type_of(self.def_id).clean(cx);
1271-
let item_type = type_.def_id().and_then(|did| inline::build_ty(cx, did));
12721272
TypedefItem(
12731273
Typedef {
1274-
type_,
1274+
type_: type_.clone(),
12751275
generics: Generics { params: Vec::new(), where_predicates: Vec::new() },
1276-
item_type,
1276+
item_type: Some(type_),
12771277
},
12781278
true,
12791279
)
@@ -1986,11 +1986,11 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Symbol>) {
19861986
bounds: ty.bounds.clean(cx),
19871987
generics: ty.generics.clean(cx),
19881988
}),
1989-
ItemKind::TyAlias(ty, ref generics) => {
1990-
let rustdoc_ty = ty.clean(cx);
1991-
let item_type = rustdoc_ty.def_id().and_then(|did| inline::build_ty(cx, did));
1989+
ItemKind::TyAlias(hir_ty, ref generics) => {
1990+
let rustdoc_ty = hir_ty.clean(cx);
1991+
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
19921992
TypedefItem(
1993-
Typedef { type_: rustdoc_ty, generics: generics.clean(cx), item_type },
1993+
Typedef { type_: rustdoc_ty, generics: generics.clean(cx), item_type: Some(ty.clean(cx)) },
19941994
false,
19951995
)
19961996
}

src/librustdoc/clean/types.rs

+4
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,10 @@ crate enum ItemKind {
334334
ProcMacroItem(ProcMacro),
335335
PrimitiveItem(PrimitiveType),
336336
AssocConstItem(Type, Option<String>),
337+
/// An associated item in a trait or trait impl.
338+
///
339+
/// The bounds may be non-empty if there is a `where` clause.
340+
/// The `Option<Type>` is the default concrete type (e.g. `trait Trait { type Target = usize; }`)
337341
AssocTypeItem(Vec<GenericBound>, Option<Type>),
338342
/// An item that has been stripped by a rustdoc pass
339343
StrippedItem(Box<ItemKind>),

src/librustdoc/html/render/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -4308,6 +4308,7 @@ fn sidebar_assoc_items(it: &clean::Item) -> String {
43084308
.filter(|i| i.inner_impl().trait_.is_some())
43094309
.find(|i| i.inner_impl().trait_.def_id() == c.deref_trait_did)
43104310
{
4311+
debug!("found Deref: {:?}", impl_);
43114312
if let Some((target, real_target)) =
43124313
impl_.inner_impl().items.iter().find_map(|item| match *item.kind {
43134314
clean::TypedefItem(ref t, true) => Some(match *t {
@@ -4317,6 +4318,7 @@ fn sidebar_assoc_items(it: &clean::Item) -> String {
43174318
_ => None,
43184319
})
43194320
{
4321+
debug!("found target, real_target: {:?} {:?}", target, real_target);
43204322
let deref_mut = v
43214323
.iter()
43224324
.filter(|i| i.inner_impl().trait_.is_some())
@@ -4328,6 +4330,7 @@ fn sidebar_assoc_items(it: &clean::Item) -> String {
43284330
.and_then(|prim| c.primitive_locations.get(&prim).cloned()))
43294331
.and_then(|did| c.impls.get(&did));
43304332
if let Some(impls) = inner_impl {
4333+
debug!("found inner_impl: {:?}", impls);
43314334
out.push_str("<a class=\"sidebar-title\" href=\"#deref-methods\">");
43324335
out.push_str(&format!(
43334336
"Methods from {}&lt;Target={}&gt;",

src/test/rustdoc/deref-typedef.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
#![crate_name = "foo"]
22

33
// @has 'foo/struct.Bar.html'
4-
// @has '-' '//*[@id="deref-methods"]' 'Methods from Deref<Target = FooC>'
4+
// @has '-' '//*[@id="deref-methods"]' 'Methods from Deref<Target = FooJ>'
55
// @has '-' '//*[@class="impl-items"]//*[@id="method.foo_a"]' 'pub fn foo_a(&self)'
66
// @has '-' '//*[@class="impl-items"]//*[@id="method.foo_b"]' 'pub fn foo_b(&self)'
77
// @has '-' '//*[@class="impl-items"]//*[@id="method.foo_c"]' 'pub fn foo_c(&self)'
8-
// @has '-' '//*[@class="sidebar-title"]' 'Methods from Deref<Target=FooC>'
8+
// @has '-' '//*[@class="impl-items"]//*[@id="method.foo_j"]' 'pub fn foo_j(&self)'
9+
// @has '-' '//*[@class="sidebar-title"]' 'Methods from Deref<Target=FooJ>'
910
// @has '-' '//*[@class="sidebar-links"]/a[@href="#method.foo_a"]' 'foo_a'
1011
// @has '-' '//*[@class="sidebar-links"]/a[@href="#method.foo_b"]' 'foo_b'
1112
// @has '-' '//*[@class="sidebar-links"]/a[@href="#method.foo_c"]' 'foo_c'
13+
// @has '-' '//*[@class="sidebar-links"]/a[@href="#method.foo_j"]' 'foo_j'
1214

1315
pub struct FooA;
1416
pub type FooB = FooA;
1517
pub type FooC = FooB;
18+
pub type FooD = FooC;
19+
pub type FooE = FooD;
20+
pub type FooF = FooE;
21+
pub type FooG = FooF;
22+
pub type FooH = FooG;
23+
pub type FooI = FooH;
24+
pub type FooJ = FooI;
1625

1726
impl FooA {
1827
pub fn foo_a(&self) {}
@@ -26,8 +35,12 @@ impl FooC {
2635
pub fn foo_c(&self) {}
2736
}
2837

38+
impl FooJ {
39+
pub fn foo_j(&self) {}
40+
}
41+
2942
pub struct Bar;
3043
impl std::ops::Deref for Bar {
31-
type Target = FooC;
44+
type Target = FooJ;
3245
fn deref(&self) -> &Self::Target { unimplemented!() }
3346
}

0 commit comments

Comments
 (0)