Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f138bbb

Browse files
rustdoc-json: discard non-local inherent impls
1 parent 80d8270 commit f138bbb

File tree

7 files changed

+28
-34
lines changed

7 files changed

+28
-34
lines changed

‎src/librustdoc/formats/cache.rs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -442,16 +442,16 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
442442
// `public_items` map, so we can skip inserting into the
443443
// paths map if there was already an entry present and we're
444444
// not a public item.
445-
if !self.cache.paths.contains_key(&item.item_id.expect_def_id())
445+
let item_def_id = item.item_id.expect_def_id();
446+
if !self.cache.paths.contains_key(&item_def_id)
446447
|| self
447448
.cache
448449
.effective_visibilities
449-
.is_directly_public(self.tcx, item.item_id.expect_def_id())
450+
.is_directly_public(self.tcx, item_def_id)
450451
{
451-
self.cache.paths.insert(
452-
item.item_id.expect_def_id(),
453-
(self.cache.stack.clone(), item.type_()),
454-
);
452+
self.cache
453+
.paths
454+
.insert(item_def_id, (self.cache.stack.clone(), item.type_()));
455455
}
456456
}
457457
}
@@ -514,9 +514,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
514514
&& adt.is_fundamental()
515515
{
516516
for ty in generics {
517-
if let Some(did) = ty.def_id(self.cache) {
518-
dids.insert(did);
519-
}
517+
dids.extend(ty.def_id(self.cache));
520518
}
521519
}
522520
}
@@ -529,33 +527,26 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
529527
.primitive_type()
530528
.and_then(|t| self.cache.primitive_locations.get(&t).cloned());
531529

532-
if let Some(did) = did {
533-
dids.insert(did);
534-
}
530+
dids.extend(did);
535531
}
536532
}
537533

538534
if let Some(generics) = i.trait_.as_ref().and_then(|t| t.generics()) {
539535
for bound in generics {
540-
if let Some(did) = bound.def_id(self.cache) {
541-
dids.insert(did);
542-
}
536+
dids.extend(bound.def_id(self.cache));
543537
}
544538
}
545539
let impl_item = Impl { impl_item: item };
546-
if impl_item.trait_did().map_or(true, |d| self.cache.traits.contains_key(&d)) {
540+
let impl_def_id = impl_item.def_id();
541+
let impl_trait_id = impl_item.trait_did();
542+
if impl_trait_id.map_or(true, |d| self.cache.traits.contains_key(&d)) {
547543
for did in dids {
548-
if self.impl_ids.entry(did).or_default().insert(impl_item.def_id()) {
549-
self.cache
550-
.impls
551-
.entry(did)
552-
.or_insert_with(Vec::new)
553-
.push(impl_item.clone());
544+
if self.impl_ids.entry(did).or_default().insert(impl_def_id) {
545+
self.cache.impls.entry(did).or_default().push(impl_item.clone());
554546
}
555547
}
556-
} else {
557-
let trait_did = impl_item.trait_did().expect("no trait did");
558-
self.cache.orphan_trait_impls.push((trait_did, dids, impl_item));
548+
} else if let Some(impl_trait_id) = impl_trait_id {
549+
self.cache.orphan_trait_impls.push((impl_trait_id, dids, impl_item));
559550
}
560551
None
561552
} else {

‎src/librustdoc/formats/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ pub(crate) mod cache;
22
pub(crate) mod item_type;
33
pub(crate) mod renderer;
44

5+
use cache::Cache;
56
pub(crate) use renderer::{run_format, FormatRenderer};
67
use rustc_hir::def_id::DefId;
8+
use rustc_middle::ty::TyCtxt;
79

810
use crate::clean::{self, ItemId};
9-
use crate::html::render::Context;
1011

1112
/// Metadata about implementations for a type or trait.
1213
#[derive(Clone, Debug)]
@@ -43,8 +44,7 @@ impl Impl {
4344
// Returns true if this is an implementation on a "local" type, meaning:
4445
// the type is in the current crate, or the type and the trait are both
4546
// re-exported by the current crate.
46-
pub(crate) fn is_on_local_type(&self, cx: &Context<'_>) -> bool {
47-
let cache = cx.cache();
47+
pub(crate) fn is_on_local_type(&self, cache: &Cache, tcx: TyCtxt<'_>) -> bool {
4848
let for_type = &self.inner_impl().for_;
4949
if let Some(for_type_did) = for_type.def_id(cache) {
5050
// The "for" type is local if it's in the paths for the current crate.
@@ -67,7 +67,7 @@ impl Impl {
6767
// a foreign type. To make sure that confusion doesn't pass on to
6868
// the reader, consider all implementations in std, core, and alloc
6969
// to be on local types.
70-
let crate_name = cx.tcx().crate_name(trait_did.krate);
70+
let crate_name = tcx.crate_name(trait_did.krate);
7171
if matches!(crate_name.as_str(), "std" | "core" | "alloc") {
7272
return true;
7373
}

‎src/librustdoc/formats/renderer.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ pub(crate) fn run_format<'tcx, T: FormatRenderer<'tcx>>(
8686
}
8787

8888
cx.mod_item_out()?;
89-
// FIXME: checking `item.name.is_some()` is very implicit and leads to lots of special
90-
// cases. Use an explicit match instead.
9189
} else if let Some(item_name) = item.name
9290
&& !item.is_extern_crate()
9391
{

‎src/librustdoc/html/render/print_item.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -980,8 +980,9 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
980980
}
981981
}
982982

983-
let (local, mut foreign) =
984-
implementors.iter().partition::<Vec<_>, _>(|i| i.is_on_local_type(cx));
983+
let (local, mut foreign) = implementors
984+
.iter()
985+
.partition::<Vec<_>, _>(|i| i.is_on_local_type(cx.cache(), cx.tcx()));
985986

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

‎src/librustdoc/html/render/sidebar.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ fn sidebar_trait<'a>(
199199
foreign_impls.extend(
200200
implementors
201201
.iter()
202-
.filter(|i| !i.is_on_local_type(cx))
202+
.filter(|i| !i.is_on_local_type(cx.cache(), cx.tcx()))
203203
.filter_map(|i| super::extract_for_impl_name(&i.impl_item, cx))
204204
.map(|(name, id)| Link::new(id, name)),
205205
);

‎src/librustdoc/json/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,12 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
217217
fn after_krate(&mut self) -> Result<(), Error> {
218218
debug!("Done with crate");
219219

220+
/*
220221
debug!("Adding Primitive impls");
221222
for primitive in Rc::clone(&self.cache).primitive_locations.values() {
222223
self.get_impls(*primitive);
223224
}
225+
*/
224226

225227
let e = ExternalCrate { crate_num: LOCAL_CRATE };
226228

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//@ count "$.index[*]" 1
2+
fn main() {}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /