Skip to content

Commit 8d0de3a

Browse files
authored
Rollup merge of #97735 - jsha:no-foreign-std, r=GuillaumeGomez
Don't generate "Impls on Foreign Types" for std Hack: many traits and types in std are re-exported from core or alloc. In general, rustdoc is capable of recognizing these implementations as being on local types. However, in at least one case, rustdoc gets confused and labels an implementation as being on a foreign type. To make sure that confusion doesn't pass on to the reader, consider all implementations in std, core, and alloc to be on local types. Demo: https://rustdoc.crud.net/jsha/no-foreign-std/std/clone/trait.Clone.html
2 parents c857265 + 784eebc commit 8d0de3a

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

Diff for: src/librustdoc/formats/mod.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_hir::def_id::DefId;
77
pub(crate) use renderer::{run_format, FormatRenderer};
88

99
use crate::clean::{self, ItemId};
10-
use cache::Cache;
10+
use crate::html::render::Context;
1111

1212
/// Specifies whether rendering directly implemented trait items or ones from a certain Deref
1313
/// impl.
@@ -65,7 +65,8 @@ impl Impl {
6565
// Returns true if this is an implementation on a "local" type, meaning:
6666
// the type is in the current crate, or the type and the trait are both
6767
// re-exported by the current crate.
68-
pub(crate) fn is_on_local_type(&self, cache: &Cache) -> bool {
68+
pub(crate) fn is_on_local_type(&self, cx: &Context<'_>) -> bool {
69+
let cache = cx.cache();
6970
let for_type = &self.inner_impl().for_;
7071
if let Some(for_type_did) = for_type.def_id(cache) {
7172
// The "for" type is local if it's in the paths for the current crate.
@@ -80,6 +81,18 @@ impl Impl {
8081
if for_type_did.krate == trait_did.krate {
8182
return true;
8283
}
84+
// Hack: many traits and types in std are re-exported from
85+
// core or alloc. In general, rustdoc is capable of recognizing
86+
// these implementations as being on local types. However, in at
87+
// least one case (https://github.com/rust-lang/rust/issues/97610),
88+
// rustdoc gets confused and labels an implementation as being on
89+
// a foreign type. To make sure that confusion doesn't pass on to
90+
// the reader, consider all implementations in std, core, and alloc
91+
// to be on local types.
92+
let crate_name = cx.tcx().crate_name(trait_did.krate);
93+
if matches!(crate_name.as_str(), "std" | "core" | "alloc") {
94+
return true;
95+
}
8396
}
8497
return false;
8598
};

Diff for: src/librustdoc/html/render/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -2281,11 +2281,10 @@ fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean
22812281
|sym| format!("<a href=\"#{1}.{0}\">{0}</a>", sym, ItemType::Method),
22822282
);
22832283

2284-
let cache = cx.cache();
2285-
if let Some(implementors) = cache.implementors.get(&it.item_id.expect_def_id()) {
2284+
if let Some(implementors) = cx.cache().implementors.get(&it.item_id.expect_def_id()) {
22862285
let mut res = implementors
22872286
.iter()
2288-
.filter(|i| !i.is_on_local_type(cache))
2287+
.filter(|i| !i.is_on_local_type(cx))
22892288
.filter_map(|i| extract_for_impl_name(&i.impl_item, cx))
22902289
.collect::<Vec<_>>();
22912290

Diff for: src/librustdoc/html/render/print_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
823823
}
824824

825825
let (local, foreign) =
826-
implementors.iter().partition::<Vec<_>, _>(|i| i.is_on_local_type(cache));
826+
implementors.iter().partition::<Vec<_>, _>(|i| i.is_on_local_type(cx));
827827

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

0 commit comments

Comments
 (0)