Skip to content

Commit 007d9e1

Browse files
camelidnotriddle
andcommitted
rustdoc: Re-add missing stripped_mod check; explain orphan impls
Co-authored-by: Michael Howell <[email protected]>
1 parent 220c2d8 commit 007d9e1

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/librustdoc/formats/cache.rs

+26-5
Original file line numberDiff line numberDiff line change
@@ -486,10 +486,30 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
486486
ParentStackItem::Type(item_id) => item_id.as_def_id(),
487487
};
488488
let Some(parent_did) = parent_did else { return };
489-
// The current stack not necessarily has correlation
490-
// for where the type was defined. On the other
491-
// hand, `paths` always has the right
492-
// information if present.
489+
// The current stack reflects the CacheBuilder's recursive
490+
// walk over HIR. For associated items, this is the module
491+
// where the `impl` block is defined. That's an implementation
492+
// detail that we don't want to affect the search engine.
493+
//
494+
// In particular, you can arrange things like this:
495+
//
496+
// #![crate_name="me"]
497+
// mod private_mod {
498+
// impl Clone for MyThing { fn clone(&self) -> MyThing { MyThing } }
499+
// }
500+
// pub struct MyThing;
501+
//
502+
// When that happens, we need to:
503+
// - ignore the `cache.stripped_mod` flag, since the Clone impl is actually
504+
// part of the public API even though it's defined in a private module
505+
// - present the method as `me::MyThing::clone`, its publicly-visible path
506+
// - deal with the fact that the recursive walk hasn't actually reached `MyThing`
507+
// until it's already past `private_mod`, since that's first, and doesn't know
508+
// yet if `MyThing` will actually be public or not (it could be re-exported)
509+
//
510+
// We accomplish the last two points by recording children of "orphan impls"
511+
// in a field of the cache whose elements are added to the search index later,
512+
// after cache building is complete (see `handle_orphan_impl_child`).
493513
match cache.paths.get(&parent_did) {
494514
Some((fqp, _)) => (Some(parent_did), &fqp[..fqp.len() - 1]),
495515
None => {
@@ -500,7 +520,8 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
500520
}
501521
_ => {
502522
// Don't index if item is crate root, which is inserted later on when serializing the index.
503-
if item_def_id.is_crate_root() {
523+
// Don't index if containing module is stripped (i.e., private),
524+
if item_def_id.is_crate_root() || cache.stripped_mod {
504525
return;
505526
}
506527
(None, &*cache.stack)

0 commit comments

Comments
 (0)