Skip to content

Commit 3d11d20

Browse files
committed
rustdoc: Add missing src links for generic impls on trait pages
`implementor2item` would return `None` for generic impls so instead this clones the entire `clean::Item` into the `implementors` map which simplifies some code.
1 parent 0efdfa1 commit 3d11d20

File tree

2 files changed

+35
-60
lines changed

2 files changed

+35
-60
lines changed

src/librustdoc/html/render.rs

+30-60
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,7 @@ pub enum ExternalLocation {
173173
Unknown,
174174
}
175175

176-
/// Metadata about an implementor of a trait.
177-
pub struct Implementor {
178-
pub def_id: DefId,
179-
pub stability: Option<clean::Stability>,
180-
pub impl_: clean::Impl,
181-
}
182-
183-
/// Metadata about implementations for a type.
176+
/// Metadata about implementations for a type or trait.
184177
#[derive(Clone)]
185178
pub struct Impl {
186179
pub impl_item: clean::Item,
@@ -279,7 +272,7 @@ pub struct Cache {
279272
/// When rendering traits, it's often useful to be able to list all
280273
/// implementors of the trait, and this mapping is exactly, that: a mapping
281274
/// of trait ids to the list of known implementors of the trait
282-
pub implementors: FxHashMap<DefId, Vec<Implementor>>,
275+
pub implementors: FxHashMap<DefId, Vec<Impl>>,
283276

284277
/// Cache of where external crate documentation can be found.
285278
pub extern_locations: FxHashMap<CrateNum, (String, PathBuf, ExternalLocation)>,
@@ -965,12 +958,12 @@ fn write_shared(cx: &Context,
965958
// there's no need to emit information about it (there's inlining
966959
// going on). If they're in different crates then the crate defining
967960
// the trait will be interested in our implementation.
968-
if imp.def_id.krate == did.krate { continue }
961+
if imp.impl_item.def_id.krate == did.krate { continue }
969962
// If the implementation is from another crate then that crate
970963
// should add it.
971-
if !imp.def_id.is_local() { continue }
964+
if !imp.impl_item.def_id.is_local() { continue }
972965
have_impls = true;
973-
write!(implementors, "{},", as_json(&imp.impl_.to_string())).unwrap();
966+
write!(implementors, "{},", as_json(&imp.inner_impl().to_string())).unwrap();
974967
}
975968
implementors.push_str("];");
976969

@@ -1202,10 +1195,8 @@ impl DocFolder for Cache {
12021195
if !self.masked_crates.contains(&item.def_id.krate) {
12031196
if let Some(did) = i.trait_.def_id() {
12041197
if i.for_.def_id().map_or(true, |d| !self.masked_crates.contains(&d.krate)) {
1205-
self.implementors.entry(did).or_insert(vec![]).push(Implementor {
1206-
def_id: item.def_id,
1207-
stability: item.stability.clone(),
1208-
impl_: i.clone(),
1198+
self.implementors.entry(did).or_insert(vec![]).push(Impl {
1199+
impl_item: item.clone(),
12091200
});
12101201
}
12111202
}
@@ -2332,18 +2323,6 @@ fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
23322323
document(w, cx, it)
23332324
}
23342325

2335-
fn implementor2item<'a>(cache: &'a Cache, imp : &Implementor) -> Option<&'a clean::Item> {
2336-
if let Some(t_did) = imp.impl_.for_.def_id() {
2337-
if let Some(impl_item) = cache.impls.get(&t_did).and_then(|i| i.iter()
2338-
.find(|i| i.impl_item.def_id == imp.def_id))
2339-
{
2340-
let i = &impl_item.impl_item;
2341-
return Some(i);
2342-
}
2343-
}
2344-
None
2345-
}
2346-
23472326
fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
23482327
t: &clean::Trait) -> fmt::Result {
23492328
let mut bounds = String::new();
@@ -2527,7 +2506,7 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
25272506
// if any Types with the same name but different DefId have been found.
25282507
let mut implementor_dups: FxHashMap<&str, (DefId, bool)> = FxHashMap();
25292508
for implementor in implementors {
2530-
match implementor.impl_.for_ {
2509+
match implementor.inner_impl().for_ {
25312510
clean::ResolvedPath { ref path, did, is_generic: false, .. } |
25322511
clean::BorrowedRef {
25332512
type_: box clean::ResolvedPath { ref path, did, is_generic: false, .. },
@@ -2544,7 +2523,7 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
25442523
}
25452524

25462525
let (local, foreign) = implementors.iter()
2547-
.partition::<Vec<_>, _>(|i| i.impl_.for_.def_id()
2526+
.partition::<Vec<_>, _>(|i| i.inner_impl().for_.def_id()
25482527
.map_or(true, |d| cache.paths.contains_key(&d)));
25492528

25502529
if !foreign.is_empty() {
@@ -2555,42 +2534,37 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
25552534
")?;
25562535

25572536
for implementor in foreign {
2558-
if let Some(i) = implementor2item(&cache, implementor) {
2559-
let impl_ = Impl { impl_item: i.clone() };
2560-
let assoc_link = AssocItemLink::GotoSource(
2561-
i.def_id, &implementor.impl_.provided_trait_methods
2562-
);
2563-
render_impl(w, cx, &impl_, assoc_link,
2564-
RenderMode::Normal, i.stable_since(), false)?;
2565-
}
2537+
let assoc_link = AssocItemLink::GotoSource(
2538+
implementor.impl_item.def_id, &implementor.inner_impl().provided_trait_methods
2539+
);
2540+
render_impl(w, cx, &implementor, assoc_link,
2541+
RenderMode::Normal, implementor.impl_item.stable_since(), false)?;
25662542
}
25672543
}
25682544

25692545
write!(w, "{}", impl_header)?;
25702546

25712547
for implementor in local {
25722548
write!(w, "<li>")?;
2573-
if let Some(item) = implementor2item(&cache, implementor) {
2574-
if let Some(l) = (Item { cx, item }).src_href() {
2575-
write!(w, "<div class='out-of-band'>")?;
2576-
write!(w, "<a class='srclink' href='{}' title='{}'>[src]</a>",
2577-
l, "goto source code")?;
2578-
write!(w, "</div>")?;
2579-
}
2549+
if let Some(l) = (Item { cx, item: &implementor.impl_item }).src_href() {
2550+
write!(w, "<div class='out-of-band'>")?;
2551+
write!(w, "<a class='srclink' href='{}' title='{}'>[src]</a>",
2552+
l, "goto source code")?;
2553+
write!(w, "</div>")?;
25802554
}
25812555
write!(w, "<code>")?;
25822556
// If there's already another implementor that has the same abbridged name, use the
25832557
// full path, for example in `std::iter::ExactSizeIterator`
2584-
let use_absolute = match implementor.impl_.for_ {
2558+
let use_absolute = match implementor.inner_impl().for_ {
25852559
clean::ResolvedPath { ref path, is_generic: false, .. } |
25862560
clean::BorrowedRef {
25872561
type_: box clean::ResolvedPath { ref path, is_generic: false, .. },
25882562
..
25892563
} => implementor_dups[path.last_name()].1,
25902564
_ => false,
25912565
};
2592-
fmt_impl_for_trait_page(&implementor.impl_, w, use_absolute)?;
2593-
for it in &implementor.impl_.items {
2566+
fmt_impl_for_trait_page(&implementor.inner_impl(), w, use_absolute)?;
2567+
for it in &implementor.inner_impl().items {
25942568
if let clean::TypedefItem(ref tydef, _) = it.inner {
25952569
write!(w, "<span class=\"where fmt-newline\"> ")?;
25962570
assoc_type(w, it, &vec![], Some(&tydef.type_), AssocItemLink::Anchor(None))?;
@@ -3880,20 +3854,16 @@ fn sidebar_trait(fmt: &mut fmt::Formatter, it: &clean::Item,
38803854

38813855
if let Some(implementors) = c.implementors.get(&it.def_id) {
38823856
let res = implementors.iter()
3883-
.filter(|i| i.impl_.for_.def_id()
3884-
.map_or(false, |d| !c.paths.contains_key(&d)))
3857+
.filter(|i| i.inner_impl().for_.def_id()
3858+
.map_or(false, |d| !c.paths.contains_key(&d)))
38853859
.filter_map(|i| {
3886-
if let Some(item) = implementor2item(&c, i) {
3887-
match extract_for_impl_name(&item) {
3888-
Some((ref name, ref url)) => {
3889-
Some(format!("<a href=\"#impl-{}\">{}</a>",
3890-
small_url_encode(url),
3891-
Escape(name)))
3892-
}
3893-
_ => None,
3860+
match extract_for_impl_name(&i.impl_item) {
3861+
Some((ref name, ref url)) => {
3862+
Some(format!("<a href=\"#impl-{}\">{}</a>",
3863+
small_url_encode(url),
3864+
Escape(name)))
38943865
}
3895-
} else {
3896-
None
3866+
_ => None,
38973867
}
38983868
})
38993869
.collect::<String>();

src/test/rustdoc/issue-43893.rs

+5
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,8 @@ impl SomeTrait for usize {}
2222
impl SomeTrait for SomeStruct {
2323
// deliberately multi-line impl
2424
}
25+
26+
pub trait AnotherTrait {}
27+
28+
// @has foo/trait.AnotherTrait.html '//a/@href' '../src/foo/issue-43893.rs.html#29'
29+
impl<T> AnotherTrait for T {}

0 commit comments

Comments
 (0)