Skip to content

rustdoc: Correctly handle reexports when --document-private-items is used - #161362

Open
GuillaumeGomez wants to merge 2 commits into
rust-lang:mainfrom
GuillaumeGomez:reexports-document-private
Open

rustdoc: Correctly handle reexports when --document-private-items is used#161362
GuillaumeGomez wants to merge 2 commits into
rust-lang:mainfrom
GuillaumeGomez:reexports-document-private

Conversation

@GuillaumeGomez

@GuillaumeGomez GuillaumeGomez commented Aug 19, 2026

Copy link
Copy Markdown
Member

Fixes #159109.

This PR makes the (private) reexport work like the public ones when the --document-private-items option is used. I still differentiate between imports (use) and reexports (pub(...) use) in this PR as I don't think we should display imports.

r? @Urgau

@rustbot rustbot added 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. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. labels Aug 19, 2026
//@ hasraw - "Mod1Public"
//@ !hasraw - "Mod1Private"
//@ !hasraw - "mod2"
//@ hasraw - "Mod1Private"

@GuillaumeGomez GuillaumeGomez Aug 19, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fun fact: this test was added in 2016 in #37773. :)

View changes since the review

@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez
GuillaumeGomez force-pushed the reexports-document-private branch from bdb8873 to 9baaae5 Compare August 19, 2026 16:10
@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez
GuillaumeGomez force-pushed the reexports-document-private branch from 9baaae5 to 7d595c2 Compare August 19, 2026 16:23
// a `pub(...)` part with its `vis_span` field.
let is_pub = tcx.visibility(def_id).is_public()
|| (self.cx.document_private()
&& (!matches!(item.kind, hir::ItemKind::Use(..)) || !item.vis_span.is_empty()));

@Urgau Urgau Aug 19, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't like this hack. I think we would be better of checking if the visibility of the use item is different from where the use item is defined.

I'm thinking of something like this:

let parent_module = tcx.parent_module_from_def_id(def_id);
let is_exported = tcx.visibility(def_id) != Visibility::Restricted(parent_module.to_mod_id());
// or maybe call Visibility::greater_than

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That's... strange. With this code:

pub(crate) mod foo {
    pub(crate) use self::x::Y;

    #[doc(hidden)]
    mod x {
        struct Y;
    }
}

It means we would not see Y. I think the only thing we want to care about is whether it's a reexport or not. And the only way to see that is if there is a pub(), which we know with vis_span. Not a trick imo.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It means we would not see Y

I think we would, parent_module should be mod foo and tcx.visibility(def_id) should be CRATE_DEF_ID and those do not match, implying that there is a pub(...) token. It's possible my logic is wrong and should be adjusted, but I think something like that should work.

And the only way to see that is if there is a pub(), which we know with vis_span. Not a trick imo.

Spans can be arbitrarily manipulated by proc-macros, a proc-macro could assign a empty span to the pub(...) tokens and your check would be bypassed. We should never rely on spans for correctness.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fair point. I'll test locally and we'll see if use without visibility are correctly handled.

@Urgau

Urgau commented Aug 19, 2026

Copy link
Copy Markdown
Member

I was wondering if this was a breaking change, but our documentation clearly states:

--document-private-items includes all non-public items in the generated documentation except for #[doc(hidden)] items.

as such, this seems clearly like a bug fix.

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job aarch64-gnu-llvm-21-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--document-private-items does not document private reexports

4 participants