Skip to content

Commit 37d3638

Browse files
committed
Improve calculation of "Impls on Foreign Types"
1 parent 29e972d commit 37d3638

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

src/librustdoc/formats/mod.rs

+25
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_hir::def_id::DefId;
77
crate use renderer::{run_format, FormatRenderer};
88

99
use crate::clean::{self, ItemId};
10+
use cache::Cache;
1011

1112
/// Specifies whether rendering directly implemented trait items or ones from a certain Deref
1213
/// impl.
@@ -60,4 +61,28 @@ impl Impl {
6061
}
6162
}
6263
}
64+
65+
// Returns true if this is an implementation on a "local" type, meaning:
66+
// the type is in the current crate, or the type and the trait are both
67+
// re-exported by the current crate.
68+
pub(crate) fn is_on_local_type(&self, cache: &Cache) -> bool {
69+
let for_type = &self.inner_impl().for_;
70+
if let Some(for_type_did) = for_type.def_id(cache) {
71+
// The "for" type is local if it's in the paths for the current crate.
72+
if cache.paths.contains_key(&for_type_did) {
73+
return true;
74+
}
75+
if let Some(trait_did) = self.trait_did() {
76+
// The "for" type and the trait are from the same crate. That could
77+
// be different from the current crate, for instance when both were
78+
// re-exported from some other crate. But they are local with respect to
79+
// each other.
80+
if for_type_did.krate == trait_did.krate {
81+
return true;
82+
}
83+
}
84+
return false;
85+
};
86+
true
87+
}
6388
}

src/librustdoc/html/render/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -2287,9 +2287,7 @@ fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean
22872287
if let Some(implementors) = cache.implementors.get(&it.item_id.expect_def_id()) {
22882288
let mut res = implementors
22892289
.iter()
2290-
.filter(|i| {
2291-
i.inner_impl().for_.def_id(cache).map_or(false, |d| !cache.paths.contains_key(&d))
2292-
})
2290+
.filter(|i| !i.is_on_local_type(cache))
22932291
.filter_map(|i| extract_for_impl_name(&i.impl_item, cx))
22942292
.collect::<Vec<_>>();
22952293

src/librustdoc/html/render/print_item.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -818,9 +818,8 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
818818
}
819819
}
820820

821-
let (local, foreign) = implementors.iter().partition::<Vec<_>, _>(|i| {
822-
i.inner_impl().for_.def_id(cache).map_or(true, |d| cache.paths.contains_key(&d))
823-
});
821+
let (local, foreign) =
822+
implementors.iter().partition::<Vec<_>, _>(|i| i.is_on_local_type(cache));
824823

825824
let (mut synthetic, mut concrete): (Vec<&&Impl>, Vec<&&Impl>) =
826825
local.iter().partition(|i| i.inner_impl().kind.is_auto());

src/test/rustdoc/issue-75588.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ extern crate real_gimli;
1313
// @!has foo/trait.Deref.html '//*[@id="impl-Deref-for-EndianSlice"]//h3[@class="code-header in-band"]' 'impl Deref for EndianSlice'
1414
pub use realcore::Deref;
1515

16-
// @has foo/trait.Join.html '//*[@id="impl-Join-for-Foo"]//h3[@class="code-header in-band"]' 'impl Join for Foo'
16+
// @has foo/trait.Join.html '//*[@id="impl-Join"]//h3[@class="code-header in-band"]' 'impl Join for Foo'
1717
pub use realcore::Join;

0 commit comments

Comments
 (0)