Skip to content

Commit 237e524

Browse files
Fix impl being removed excessively
1 parent fe1bf8e commit 237e524

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

src/librustdoc/passes/collect_trait_impls.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,29 @@ crate fn collect_trait_impls(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
8888

8989
let mut cleaner = BadImplStripper { prims, items: crate_items };
9090

91+
let sized_trait = cx.tcx.lang_items().sized_trait();
92+
let deref_trait = cx.tcx.lang_items().deref_trait();
93+
9194
let mut type_did_to_deref_target: FxHashMap<DefId, &Type> = FxHashMap::default();
9295
// Gather all type to `Deref` target edges.
9396
for it in &new_items {
9497
if let ImplItem(Impl { ref for_, ref trait_, ref items, .. }) = *it.kind {
95-
if trait_.def_id() == cx.tcx.lang_items().deref_trait() {
96-
let target = items.iter().find_map(|item| match *item.kind {
97-
TypedefItem(ref t, true) => Some(&t.type_),
98-
_ => None,
99-
});
100-
if let (Some(for_did), Some(target)) = (for_.def_id(), target) {
101-
type_did_to_deref_target.insert(for_did, target);
98+
match trait_.def_id() {
99+
did if sized_trait == did => {}
100+
Some(did) => {
101+
if Some(did) == deref_trait {
102+
let target = items.iter().find_map(|item| match *item.kind {
103+
TypedefItem(ref t, true) => Some(&t.type_),
104+
_ => None,
105+
});
106+
if let (Some(for_did), Some(target)) = (for_.def_id(), target) {
107+
type_did_to_deref_target.insert(for_did, target);
108+
}
109+
} else {
110+
cleaner.items.insert(did);
111+
}
102112
}
113+
_ => {}
103114
}
104115
}
105116
}

src/test/rustdoc/impl-on-foreign.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#![crate_name = "foo"]
2+
3+
use std::convert::AsRef;
4+
pub struct Gus;
5+
6+
// @has 'foo/struct.Gus.html'
7+
// @has - '//h3[@class="impl"]/code' 'impl AsRef<str> for Gus'
8+
impl AsRef<str> for Gus {
9+
fn as_ref(&self) -> &str {
10+
todo!()
11+
}
12+
}
13+
14+
// @has - '//h3[@class="impl"]/code' 'impl AsRef<Gus> for str'
15+
impl AsRef<Gus> for str {
16+
fn as_ref(&self) -> &Gus {
17+
todo!()
18+
}
19+
}
20+
21+
// @has - '//h3[@class="impl"]/code' 'impl AsRef<Gus> for String'
22+
impl AsRef<Gus> for String {
23+
fn as_ref(&self) -> &Gus {
24+
todo!()
25+
}
26+
}

0 commit comments

Comments
 (0)