Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoc: Cleanup CacheBuilder code for building search index #128578

Merged
merged 7 commits into from
Aug 4, 2024

Conversation

camelid
Copy link
Member

@camelid camelid commented Aug 3, 2024

This code was very convoluted and hard to reason about. It is now (I hope) much
clearer and more suitable for both future enhancements and future cleanups.

I'm doing this as a precursor, with no UI changes, to changing rustdoc to
ignore blanket impls in type-based search.

r? @notriddle

@rustbot rustbot added A-rustdoc-json Area: Rustdoc JSON backend S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Aug 3, 2024
@camelid camelid marked this pull request as draft August 3, 2024 00:57
@camelid camelid marked this pull request as ready for review August 3, 2024 01:02
@camelid
Copy link
Member Author

camelid commented Aug 3, 2024

I realized that these changes are actually completely independent, so this is no longer based on the Self type PR. It will have a small conflict once the other is merged, though, since there is one function that is changed in both.

@camelid camelid added A-rustdoc-search Area: Rustdoc's search feature and removed A-rustdoc-json Area: Rustdoc JSON backend labels Aug 3, 2024
@rust-log-analyzer

This comment has been minimized.

@camelid
Copy link
Member Author

camelid commented Aug 3, 2024

Well that's weird... those tests passed locally.

Copy link
Contributor

@notriddle notriddle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a suggested fix, along with a much longer comment for a remaining, ugly fact of the search engine builder:

diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs
index d2d0f5a4380..6454bc58cd6 100644
--- a/src/librustdoc/formats/cache.rs
+++ b/src/librustdoc/formats/cache.rs
@@ -486,10 +486,26 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
                 ParentStackItem::Type(item_id) => item_id.as_def_id(),
             };
             let Some(parent_did) = parent_did else { return };
-            // The current stack not necessarily has correlation
-            // for where the type was defined. On the other
-            // hand, `paths` always has the right
-            // information if present.
+            // The current stack reflects the CacheBuilder's recursive
+            // walk over HIR. For associated items, this is the module
+            // where the `impl` block is defined. That's an implementation
+            // detail that we don't want to affect the search engine.
+            //
+            // In particular, you can arrange things like this:
+            //
+            //     #![crate_name="me"]
+            //     mod private_mod {
+            //         impl Clone for MyThing { fn clone(&self) -> MyThing { MyThing } }
+            //     }
+            //     pub struct MyThing;
+            //
+            // When that happens, we need to:
+            // - ignore the `cache.stripped_mod` flag, since the Clone impl is actually
+            //   part of the public API even though it's defined in a private module
+            // - present the method as `me::MyThing::clone`, its publicly-visible path
+            // - deal with the fact that the recursive walk hasn't actually reached `MyThing`
+            //   until it's already past `private_mod`, since that's first, and doesn't know
+            //   yet if `MyThing` will actually be public or not (it could be re-exported)
             match cache.paths.get(&parent_did) {
                 Some((fqp, _)) => (Some(parent_did), &fqp[..fqp.len() - 1]),
                 None => {
@@ -500,7 +516,8 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
         }
         _ => {
             // Don't index if item is crate root, which is inserted later on when serializing the index.
-            if item_def_id.is_crate_root() {
+            // Don't index if containing module is stripped (i.e., private),
+            if item_def_id.is_crate_root() || cache.stripped_mod {
                 return;
             }
             (None, &*cache.stack)

src/librustdoc/formats/cache.rs Show resolved Hide resolved
Co-authored-by: Michael Howell <michael@notriddle.com>
@notriddle
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Aug 4, 2024

📌 Commit 007d9e1 has been approved by notriddle

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 4, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Aug 4, 2024
…riddle

rustdoc: Cleanup `CacheBuilder` code for building search index

This code was very convoluted and hard to reason about. It is now (I hope) much
clearer and more suitable for both future enhancements and future cleanups.

I'm doing this as a precursor, with no UI changes, to changing rustdoc to
[ignore blanket impls][1] in type-based search.

[1]: rust-lang#128471 (comment)

r? `@notriddle`
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 4, 2024
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#128362 (add test for symbol visibility of `#[naked]` functions)
 - rust-lang#128526 (time.rs: remove "Basic usage text")
 - rust-lang#128531 (Miri: add a flag to do recursive validity checking)
 - rust-lang#128578 (rustdoc: Cleanup `CacheBuilder` code for building search index)
 - rust-lang#128589 (allow setting `link-shared` and `static-libstdcpp` with CI LLVM)
 - rust-lang#128615 (rustdoc: make the hover trail for doc anchors a bit bigger)
 - rust-lang#128620 (Update rinja version to 0.3.0)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 4, 2024
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#128305 (improve error message when `global_asm!` uses `asm!` operands)
 - rust-lang#128526 (time.rs: remove "Basic usage text")
 - rust-lang#128531 (Miri: add a flag to do recursive validity checking)
 - rust-lang#128578 (rustdoc: Cleanup `CacheBuilder` code for building search index)
 - rust-lang#128589 (allow setting `link-shared` and `static-libstdcpp` with CI LLVM)
 - rust-lang#128615 (rustdoc: make the hover trail for doc anchors a bit bigger)
 - rust-lang#128620 (Update rinja version to 0.3.0)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 8c82692 into rust-lang:master Aug 4, 2024
6 checks passed
@rustbot rustbot added this to the 1.82.0 milestone Aug 4, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Aug 4, 2024
Rollup merge of rust-lang#128578 - camelid:cache-index-cleanup, r=notriddle

rustdoc: Cleanup `CacheBuilder` code for building search index

This code was very convoluted and hard to reason about. It is now (I hope) much
clearer and more suitable for both future enhancements and future cleanups.

I'm doing this as a precursor, with no UI changes, to changing rustdoc to
[ignore blanket impls][1] in type-based search.

[1]: rust-lang#128471 (comment)

r? ``@notriddle``
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-rustdoc-search Area: Rustdoc's search feature S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants