Skip to content

Commit d755238

Browse files
Simplify deref impls for type aliases
1 parent e6ad49a commit d755238

File tree

2 files changed

+55
-39
lines changed

2 files changed

+55
-39
lines changed

src/librustdoc/clean/mod.rs

+29-2
Original file line numberDiff line numberDiff line change
@@ -2105,7 +2105,7 @@ impl Clean<Vec<Item>> for doctree::Impl<'_> {
21052105
build_deref_target_impls(cx, &items, &mut ret);
21062106
}
21072107

2108-
let provided = trait_
2108+
let provided: FxHashSet<String> = trait_
21092109
.def_id()
21102110
.map(|did| {
21112111
cx.tcx
@@ -2116,6 +2116,33 @@ impl Clean<Vec<Item>> for doctree::Impl<'_> {
21162116
})
21172117
.unwrap_or_default();
21182118

2119+
let for_ = self.for_.clean(cx);
2120+
let type_alias = for_.def_id().and_then(|did| match cx.tcx.def_kind(did) {
2121+
Some(DefKind::TyAlias) => Some(cx.tcx.type_of(did).clean(cx)),
2122+
_ => None,
2123+
});
2124+
if let Some(type_alias) = type_alias {
2125+
ret.push(Item {
2126+
name: None,
2127+
attrs: self.attrs.clean(cx),
2128+
source: self.whence.clean(cx),
2129+
def_id,
2130+
visibility: self.vis.clean(cx),
2131+
stability: cx.stability(self.id).clean(cx),
2132+
deprecation: cx.deprecation(self.id).clean(cx),
2133+
inner: ImplItem(Impl {
2134+
unsafety: self.unsafety,
2135+
generics: self.generics.clean(cx),
2136+
provided_trait_methods: provided.clone(),
2137+
trait_: trait_.clone(),
2138+
for_: type_alias,
2139+
items: items.clone(),
2140+
polarity: Some(cx.tcx.impl_polarity(def_id).clean(cx)),
2141+
synthetic: false,
2142+
blanket_impl: None,
2143+
}),
2144+
});
2145+
}
21192146
ret.push(Item {
21202147
name: None,
21212148
attrs: self.attrs.clean(cx),
@@ -2129,7 +2156,7 @@ impl Clean<Vec<Item>> for doctree::Impl<'_> {
21292156
generics: self.generics.clean(cx),
21302157
provided_trait_methods: provided,
21312158
trait_,
2132-
for_: self.for_.clean(cx),
2159+
for_,
21332160
items,
21342161
polarity: Some(cx.tcx.impl_polarity(def_id).clean(cx)),
21352162
synthetic: false,

src/librustdoc/html/render.rs

+26-37
Original file line numberDiff line numberDiff line change
@@ -2595,7 +2595,7 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait)
25952595
}
25962596

25972597
// If there are methods directly on this trait object, render them here.
2598-
render_assoc_items(w, cx, it, it.def_id, &AssocItemRender::All);
2598+
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All);
25992599

26002600
let mut synthetic_types = Vec::new();
26012601

@@ -2942,7 +2942,7 @@ fn item_struct(w: &mut Buffer, cx: &Context, it: &clean::Item, s: &clean::Struct
29422942
}
29432943
}
29442944
}
2945-
render_assoc_items(w, cx, it, it.def_id, &AssocItemRender::All)
2945+
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)
29462946
}
29472947

29482948
fn item_union(w: &mut Buffer, cx: &Context, it: &clean::Item, s: &clean::Union) {
@@ -2988,7 +2988,7 @@ fn item_union(w: &mut Buffer, cx: &Context, it: &clean::Item, s: &clean::Union)
29882988
document(w, cx, field);
29892989
}
29902990
}
2991-
render_assoc_items(w, cx, it, it.def_id, &AssocItemRender::All)
2991+
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)
29922992
}
29932993

29942994
fn item_enum(w: &mut Buffer, cx: &Context, it: &clean::Item, e: &clean::Enum) {
@@ -3130,7 +3130,7 @@ fn item_enum(w: &mut Buffer, cx: &Context, it: &clean::Item, e: &clean::Enum) {
31303130
render_stability_since(w, variant, it);
31313131
}
31323132
}
3133-
render_assoc_items(w, cx, it, it.def_id, &AssocItemRender::All)
3133+
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)
31343134
}
31353135

31363136
fn render_attribute(attr: &ast::MetaItem) -> Option<String> {
@@ -3344,7 +3344,7 @@ fn render_assoc_items(
33443344
cx: &Context,
33453345
containing_item: &clean::Item,
33463346
it: DefId,
3347-
what: &AssocItemRender<'_>,
3347+
what: AssocItemRender<'_>,
33483348
) {
33493349
let c = &cx.cache;
33503350
let v = match c.impls.get(&it) {
@@ -3377,7 +3377,7 @@ fn render_assoc_items(
33773377
trait_.print(),
33783378
type_.print()
33793379
);
3380-
RenderMode::ForDeref { mut_: *deref_mut_ }
3380+
RenderMode::ForDeref { mut_: deref_mut_ }
33813381
}
33823382
};
33833383
for i in &non_trait {
@@ -3461,19 +3461,6 @@ fn render_assoc_items(
34613461
}
34623462
}
34633463

3464-
fn get_def_id(real_target: &clean::Type, cx: &Context) -> Option<DefId> {
3465-
if let Some(did) = real_target.def_id() {
3466-
return Some(did);
3467-
} else {
3468-
if let Some(prim) = real_target.primitive_type() {
3469-
if let Some(&did) = cx.cache.primitive_locations.get(&prim) {
3470-
return Some(did);
3471-
}
3472-
}
3473-
}
3474-
None
3475-
}
3476-
34773464
fn render_deref_methods(
34783465
w: &mut Buffer,
34793466
cx: &Context,
@@ -3488,21 +3475,23 @@ fn render_deref_methods(
34883475
.iter()
34893476
.filter_map(|item| match item.inner {
34903477
clean::TypedefItem(ref t, true) => Some(match *t {
3491-
clean::Typedef { item_type: Some(ref type_), .. } => (&t.type_, Some(type_)),
3492-
_ => (&t.type_, None),
3478+
clean::Typedef { item_type: Some(ref type_), .. } => (type_, &t.type_),
3479+
_ => (&t.type_, &t.type_),
34933480
}),
34943481
_ => None,
34953482
})
34963483
.next()
34973484
.expect("Expected associated type binding");
3498-
let did = get_def_id(&target, cx);
34993485
let what =
3500-
AssocItemRender::DerefFor { trait_: deref_type, type_: target, deref_mut_: deref_mut };
3501-
if let Some(did) = did {
3502-
render_assoc_items(w, cx, container_item, did, &what);
3503-
}
3504-
if let Some(did) = real_target.and_then(|x| get_def_id(x, cx)) {
3505-
render_assoc_items(w, cx, container_item, did, &what);
3486+
AssocItemRender::DerefFor { trait_: deref_type, type_: real_target, deref_mut_: deref_mut };
3487+
if let Some(did) = target.def_id() {
3488+
render_assoc_items(w, cx, container_item, did, what);
3489+
} else {
3490+
if let Some(prim) = target.primitive_type() {
3491+
if let Some(&did) = cx.cache.primitive_locations.get(&prim) {
3492+
render_assoc_items(w, cx, container_item, did, what);
3493+
}
3494+
}
35063495
}
35073496
}
35083497

@@ -3884,7 +3873,7 @@ fn item_opaque_ty(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Opa
38843873
// won't be visible anywhere in the docs. It would be nice to also show
38853874
// associated items from the aliased type (see discussion in #32077), but
38863875
// we need #14072 to make sense of the generics.
3887-
render_assoc_items(w, cx, it, it.def_id, &AssocItemRender::All)
3876+
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)
38883877
}
38893878

38903879
fn item_trait_alias(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::TraitAlias) {
@@ -3905,7 +3894,7 @@ fn item_trait_alias(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::T
39053894
// won't be visible anywhere in the docs. It would be nice to also show
39063895
// associated items from the aliased type (see discussion in #32077), but
39073896
// we need #14072 to make sense of the generics.
3908-
render_assoc_items(w, cx, it, it.def_id, &AssocItemRender::All)
3897+
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)
39093898
}
39103899

39113900
fn item_typedef(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Typedef) {
@@ -3926,7 +3915,7 @@ fn item_typedef(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Typed
39263915
// won't be visible anywhere in the docs. It would be nice to also show
39273916
// associated items from the aliased type (see discussion in #32077), but
39283917
// we need #14072 to make sense of the generics.
3929-
render_assoc_items(w, cx, it, it.def_id, &AssocItemRender::All)
3918+
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)
39303919
}
39313920

39323921
fn item_foreign_type(w: &mut Buffer, cx: &Context, it: &clean::Item) {
@@ -3941,7 +3930,7 @@ fn item_foreign_type(w: &mut Buffer, cx: &Context, it: &clean::Item) {
39413930

39423931
document(w, cx, it);
39433932

3944-
render_assoc_items(w, cx, it, it.def_id, &AssocItemRender::All)
3933+
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)
39453934
}
39463935

39473936
fn print_sidebar(cx: &Context, it: &clean::Item, buffer: &mut Buffer) {
@@ -4137,20 +4126,20 @@ fn sidebar_assoc_items(it: &clean::Item) -> String {
41374126
.filter(|i| i.inner_impl().trait_.is_some())
41384127
.find(|i| i.inner_impl().trait_.def_id() == c.deref_trait_did)
41394128
{
4140-
if let Some((target, real_target)) = impl_
4129+
if let Some(target) = impl_
41414130
.inner_impl()
41424131
.items
41434132
.iter()
41444133
.filter_map(|item| match item.inner {
41454134
clean::TypedefItem(ref t, true) => Some(match *t {
4146-
clean::Typedef { item_type: Some(ref type_), .. } => (&t.type_, type_),
4147-
_ => (&t.type_, &t.type_),
4135+
clean::Typedef { item_type: Some(ref type_), .. } => type_,
4136+
_ => &t.type_,
41484137
}),
41494138
_ => None,
41504139
})
41514140
.next()
41524141
{
4153-
let inner_impl = real_target
4142+
let inner_impl = target
41544143
.def_id()
41554144
.or(target
41564145
.primitive_type()
@@ -4613,7 +4602,7 @@ fn item_proc_macro(w: &mut Buffer, cx: &Context, it: &clean::Item, m: &clean::Pr
46134602

46144603
fn item_primitive(w: &mut Buffer, cx: &Context, it: &clean::Item) {
46154604
document(w, cx, it);
4616-
render_assoc_items(w, cx, it, it.def_id, &AssocItemRender::All)
4605+
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)
46174606
}
46184607

46194608
fn item_keyword(w: &mut Buffer, cx: &Context, it: &clean::Item) {

0 commit comments

Comments
 (0)