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

Fix reexport of doc(hidden) item #106741

Merged
merged 2 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/librustdoc/passes/strip_priv_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ pub(crate) const STRIP_PRIV_IMPORTS: Pass = Pass {
};

pub(crate) fn strip_priv_imports(krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate {
ImportStripper { tcx: cx.tcx }.fold_crate(krate)
let is_json_output = cx.output_format.is_json() && !cx.show_coverage;
ImportStripper { tcx: cx.tcx, is_json_output }.fold_crate(krate)
}
3 changes: 2 additions & 1 deletion src/librustdoc/passes/strip_private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ pub(crate) fn strip_private(mut krate: clean::Crate, cx: &mut DocContext<'_>) ->
is_json_output,
tcx: cx.tcx,
};
krate = ImportStripper { tcx: cx.tcx }.fold_crate(stripper.fold_crate(krate));
krate =
ImportStripper { tcx: cx.tcx, is_json_output }.fold_crate(stripper.fold_crate(krate));
}

// strip all impls referencing private items
Expand Down
15 changes: 14 additions & 1 deletion src/librustdoc/passes/stripper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,25 @@ impl<'a> DocFolder for ImplStripper<'a, '_> {
/// This stripper discards all private import statements (`use`, `extern crate`)
pub(crate) struct ImportStripper<'tcx> {
pub(crate) tcx: TyCtxt<'tcx>,
pub(crate) is_json_output: bool,
}

impl<'tcx> ImportStripper<'tcx> {
fn import_should_be_hidden(&self, i: &Item, imp: &clean::Import) -> bool {
if self.is_json_output {
// FIXME: This should be handled the same way as for HTML output.
imp.imported_item_is_doc_hidden(self.tcx)
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm really unhappy about this. How the JSON output is handling the reexports is becoming problematic if we have to special case more things.

Copy link
Contributor

Choose a reason for hiding this comment

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

I suppose the correct way to handle this is similar to how stripped modules work — the pointee is kept around because of the import item referencing it, but is marked as hidden.

} else {
i.attrs.lists(sym::doc).has_word(sym::hidden)
}
}
}

impl<'tcx> DocFolder for ImportStripper<'tcx> {
fn fold_item(&mut self, i: Item) -> Option<Item> {
match *i.kind {
clean::ImportItem(imp) if imp.imported_item_is_doc_hidden(self.tcx) => None,
clean::ImportItem(imp) if self.import_should_be_hidden(&i, &imp) => None,
clean::ImportItem(_) if i.attrs.lists(sym::doc).has_word(sym::hidden) => None,
clean::ExternCrateItem { .. } | clean::ImportItem(..)
if i.visibility(self.tcx) != Some(Visibility::Public) =>
{
Expand Down
26 changes: 26 additions & 0 deletions tests/rustdoc/reexport-doc-hidden.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Part of <https://github.com/rust-lang/rust/issues/59368>.
// This test ensures that reexporting a `doc(hidden)` item will
// still show the reexport.

#![crate_name = "foo"]

#[doc(hidden)]
pub type Type = u32;

// @has 'foo/index.html'
// @has - '//*[@id="reexport.Type2"]/code' 'pub use crate::Type as Type2;'
pub use crate::Type as Type2;

// @count - '//*[@id="reexport.Type3"]' 0
#[doc(hidden)]
pub use crate::Type as Type3;

#[macro_export]
#[doc(hidden)]
macro_rules! foo {
() => {};
}

// This is a bug: https://github.com/rust-lang/rust/issues/59368
// @!has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;'
pub use crate::foo as Macro;