[rustdoc] hide item that is not marked as doc(inline) and whose src is doc(hidden)
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
This commit is contained in:
parent
96cfc75584
commit
133705c402
9 changed files with 60 additions and 26 deletions
|
@ -7,9 +7,8 @@ use rustc_middle::ty::TyCtxt;
|
|||
use rustc_span::symbol::sym;
|
||||
use tracing::debug;
|
||||
|
||||
use crate::clean;
|
||||
use crate::clean::utils::inherits_doc_hidden;
|
||||
use crate::clean::{Item, ItemIdSet};
|
||||
use crate::clean::{self, Item, ItemIdSet, reexport_chain};
|
||||
use crate::core::DocContext;
|
||||
use crate::fold::{DocFolder, strip_item};
|
||||
use crate::passes::{ImplStripper, Pass};
|
||||
|
@ -89,6 +88,25 @@ impl Stripper<'_, '_> {
|
|||
impl DocFolder for Stripper<'_, '_> {
|
||||
fn fold_item(&mut self, i: Item) -> Option<Item> {
|
||||
let has_doc_hidden = i.is_doc_hidden();
|
||||
|
||||
if let clean::ImportItem(clean::Import { source, .. }) = &i.kind
|
||||
&& let Some(source_did) = source.did
|
||||
&& let Some(import_def_id) = i.def_id().and_then(|def_id| def_id.as_local())
|
||||
{
|
||||
let reexports = reexport_chain(self.tcx, import_def_id, source_did);
|
||||
|
||||
// Check if any reexport in the chain has a hidden source
|
||||
let has_hidden_source = reexports
|
||||
.iter()
|
||||
.filter_map(|reexport| reexport.id())
|
||||
.any(|reexport_did| self.tcx.is_doc_hidden(reexport_did))
|
||||
|| self.tcx.is_doc_hidden(source_did);
|
||||
|
||||
if has_hidden_source {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
let is_impl_or_exported_macro = match i.kind {
|
||||
clean::ImplItem(..) => true,
|
||||
// If the macro has the `#[macro_export]` attribute, it means it's accessible at the
|
||||
|
|
|
@ -26,21 +26,21 @@ pub mod single_reexport {
|
|||
//@ has 'foo/single_reexport/index.html'
|
||||
|
||||
// First we check that we have 4 type aliases.
|
||||
//@ count - '//*[@id="main-content"]/*[@class="item-table reexports"]//code' 4
|
||||
//@ count - '//*[@id="main-content"]/*[@class="item-table reexports"]//code' 0
|
||||
|
||||
// Then we check that we have the correct link for each re-export.
|
||||
|
||||
//@ !has - '//*[@href="struct.Foo.html"]' 'Foo'
|
||||
//@ has - '//*[@id="reexport.Foo"]/code' 'pub use crate::private_module::Public as Foo;'
|
||||
//@ !has - '//*[@id="reexport.Foo"]/code' 'pub use crate::private_module::Public as Foo;'
|
||||
pub use crate::private_module::Public as Foo;
|
||||
//@ !has - '//*[@href="type.Foo2.html"]' 'Foo2'
|
||||
//@ has - '//*[@id="reexport.Foo2"]/code' 'pub use crate::private_module::Bar as Foo2;'
|
||||
//@ !has - '//*[@id="reexport.Foo2"]/code' 'pub use crate::private_module::Bar as Foo2;'
|
||||
pub use crate::private_module::Bar as Foo2;
|
||||
//@ !has - '//*[@href="type.Yo.html"]' 'Yo'
|
||||
//@ has - '//*[@id="reexport.Yo"]/code' 'pub use crate::Bar3 as Yo;'
|
||||
//@ !has - '//*[@id="reexport.Yo"]/code' 'pub use crate::Bar3 as Yo;'
|
||||
pub use crate::Bar3 as Yo;
|
||||
//@ !has - '//*[@href="struct.Yo2.html"]' 'Yo2'
|
||||
//@ has - '//*[@id="reexport.Yo2"]/code' 'pub use crate::FooFoo as Yo2;'
|
||||
//@ !has - '//*[@id="reexport.Yo2"]/code' 'pub use crate::FooFoo as Yo2;'
|
||||
pub use crate::FooFoo as Yo2;
|
||||
|
||||
// Checking that each file is also created as expected.
|
||||
|
@ -70,19 +70,19 @@ pub mod single_reexport_no_inline {
|
|||
//@ has - '//*[@id="main-content"]/*[@class="section-header"]' 'Re-exports'
|
||||
|
||||
// Now we check that we don't have links to the items, just `pub use`.
|
||||
//@ has - '//*[@id="main-content"]//*' 'pub use crate::private_module::Public as XFoo;'
|
||||
//@ !has - '//*[@id="main-content"]//*' 'pub use crate::private_module::Public as XFoo;'
|
||||
//@ !has - '//*[@id="main-content"]//a' 'XFoo'
|
||||
#[doc(no_inline)]
|
||||
pub use crate::private_module::Public as XFoo;
|
||||
//@ has - '//*[@id="main-content"]//*' 'pub use crate::private_module::Bar as Foo2;'
|
||||
//@ !has - '//*[@id="main-content"]//*' 'pub use crate::private_module::Bar as Foo2;'
|
||||
//@ !has - '//*[@id="main-content"]//a' 'Foo2'
|
||||
#[doc(no_inline)]
|
||||
pub use crate::private_module::Bar as Foo2;
|
||||
//@ has - '//*[@id="main-content"]//*' 'pub use crate::Bar3 as Yo;'
|
||||
//@ !has - '//*[@id="main-content"]//*' 'pub use crate::Bar3 as Yo;'
|
||||
//@ !has - '//*[@id="main-content"]//a' 'Yo'
|
||||
#[doc(no_inline)]
|
||||
pub use crate::Bar3 as Yo;
|
||||
//@ has - '//*[@id="main-content"]//*' 'pub use crate::FooFoo as Yo2;'
|
||||
//@ !has - '//*[@id="main-content"]//*' 'pub use crate::FooFoo as Yo2;'
|
||||
//@ !has - '//*[@id="main-content"]//a' 'Yo2'
|
||||
#[doc(no_inline)]
|
||||
pub use crate::FooFoo as Yo2;
|
||||
|
|
16
tests/rustdoc/doc-hidden-source.rs
Normal file
16
tests/rustdoc/doc-hidden-source.rs
Normal file
|
@ -0,0 +1,16 @@
|
|||
// Test for <https://github.com/rust-lang/rust/issues/137342>.
|
||||
|
||||
#![crate_name = "foo"]
|
||||
|
||||
//@ has 'foo/index.html'
|
||||
//@ !has - '//*[@id="main-content"]//*[@class="struct"]' 'Bar'
|
||||
#[doc(hidden)]
|
||||
pub struct Bar;
|
||||
|
||||
//@ !has - '//*' 'pub use crate::Bar as A;'
|
||||
pub use crate::Bar as A;
|
||||
//@ !has - '//*' 'pub use crate::A as B;'
|
||||
pub use crate::A as B;
|
||||
//@ has - '//dt/a[@class="struct"]' 'C'
|
||||
#[doc(inline)]
|
||||
pub use crate::Bar as C;
|
|
@ -6,7 +6,7 @@ extern crate rustdoc_hidden;
|
|||
|
||||
//@ has inline_hidden/index.html
|
||||
// Ensures this item is not inlined.
|
||||
//@ has - '//*[@id="reexport.Foo"]/code' 'pub use rustdoc_hidden::Foo;'
|
||||
//@ !has - '//*[@id="reexport.Foo"]/code' 'pub use rustdoc_hidden::Foo;'
|
||||
#[doc(no_inline)]
|
||||
pub use rustdoc_hidden::Foo;
|
||||
|
||||
|
@ -16,7 +16,7 @@ pub use rustdoc_hidden::Foo;
|
|||
pub use rustdoc_hidden::Foo as Inlined;
|
||||
|
||||
// Even with this import, we should not see `Foo`.
|
||||
//@ count - '//dt' 4
|
||||
//@ count - '//dt' 3
|
||||
//@ has - '//dt/a[@class="struct"]' 'Bar'
|
||||
//@ has - '//dt/a[@class="fn"]' 'foo'
|
||||
pub use rustdoc_hidden::*;
|
||||
|
|
|
@ -18,7 +18,7 @@ pub use Foo1 as Foo2;
|
|||
|
||||
// First we ensure that only the reexport `Bar2` and the inlined struct `Bar`
|
||||
// are inlined.
|
||||
//@ count - '//a[@class="struct"]' 2
|
||||
//@ count - '//a[@class="struct"]' 1
|
||||
// Then we check that `cfg` is displayed for base item, but not for intermediate re-exports.
|
||||
//@ has - '//*[@class="stab portability"]' 'foo'
|
||||
//@ !has - '//*[@class="stab portability"]' 'bar'
|
||||
|
@ -29,5 +29,5 @@ pub use Foo2 as Bar;
|
|||
|
||||
// This one should appear but `Bar2` won't be linked because there is no
|
||||
// `#[doc(inline)]`.
|
||||
//@ has - '//*[@id="reexport.Bar2"]' 'pub use Foo2 as Bar2;'
|
||||
//@ !has - '//*[@id="reexport.Bar2"]' 'pub use Foo2 as Bar2;'
|
||||
pub use Foo2 as Bar2;
|
||||
|
|
|
@ -9,8 +9,8 @@ mod private_module {
|
|||
}
|
||||
|
||||
//@ has 'foo/index.html'
|
||||
//@ has - '//*[@id="reexport.Foo"]/code' 'pub use crate::private_module::Public as Foo;'
|
||||
//@ !has - '//*[@id="reexport.Foo"]/code' 'pub use crate::private_module::Public as Foo;'
|
||||
pub use crate::private_module::Public as Foo;
|
||||
// Glob re-exports with no visible items should not be displayed.
|
||||
//@ count - '//*[@class="item-table reexports"]/dt' 1
|
||||
//@ count - '//*[@class="item-table reexports"]/dt' 0
|
||||
pub use crate::private_module::*;
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
pub type Type = u32;
|
||||
|
||||
//@ has 'foo/index.html'
|
||||
//@ has - '//*[@id="reexport.Type2"]/code' 'pub use crate::Type as Type2;'
|
||||
//@ !has - '//*[@id="reexport.Type2"]/code' 'pub use crate::Type as Type2;'
|
||||
pub use crate::Type as Type2;
|
||||
|
||||
//@ count - '//*[@id="reexport.Type3"]' 0
|
||||
|
@ -21,5 +21,5 @@ macro_rules! foo {
|
|||
() => {};
|
||||
}
|
||||
|
||||
//@ has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;'
|
||||
//@ !has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;'
|
||||
pub use crate::foo as Macro;
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
//@ has 'foo/index.html'
|
||||
//@ has - '//*[@id="main-content"]//a[@href="macro.Macro2.html"]' 'Macro2'
|
||||
//@ has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;'
|
||||
//@ !has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;'
|
||||
|
||||
//@ has 'foo/macro.Macro2.html'
|
||||
//@ has - '//*[@class="docblock"]' 'Displayed'
|
||||
|
|
|
@ -12,13 +12,13 @@ macro_rules! foo {
|
|||
}
|
||||
|
||||
//@ has 'foo/index.html'
|
||||
//@ has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;'
|
||||
//@ !has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;'
|
||||
pub use crate::foo as Macro;
|
||||
//@ has - '//*[@id="reexport.Macro2"]/code' 'pub use crate::foo as Macro2;'
|
||||
//@ !has - '//*[@id="reexport.Macro2"]/code' 'pub use crate::foo as Macro2;'
|
||||
pub use crate::foo as Macro2;
|
||||
//@ has - '//*[@id="reexport.Boo"]/code' 'pub use crate::Bar as Boo;'
|
||||
//@ !has - '//*[@id="reexport.Boo"]/code' 'pub use crate::Bar as Boo;'
|
||||
pub use crate::Bar as Boo;
|
||||
//@ has - '//*[@id="reexport.Boo2"]/code' 'pub use crate::Bar as Boo2;'
|
||||
//@ !has - '//*[@id="reexport.Boo2"]/code' 'pub use crate::Bar as Boo2;'
|
||||
pub use crate::Bar as Boo2;
|
||||
|
||||
pub fn fofo() {}
|
||||
|
@ -30,9 +30,9 @@ pub use crate::fofo as f2;
|
|||
|
||||
pub mod sub {
|
||||
//@ has 'foo/sub/index.html'
|
||||
//@ has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;'
|
||||
//@ !has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;'
|
||||
pub use crate::foo as Macro;
|
||||
//@ has - '//*[@id="reexport.Macro2"]/code' 'pub use crate::foo as Macro2;'
|
||||
//@ !has - '//*[@id="reexport.Macro2"]/code' 'pub use crate::foo as Macro2;'
|
||||
pub use crate::foo as Macro2;
|
||||
|
||||
//@ has - '//*[@id="reexport.f1"]/code' 'pub use crate::fofo as f1;'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue