From 4a1e06b453d14b61d4fc19532918694f485bf51e Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 7 Jul 2023 17:55:33 +0200 Subject: [PATCH] Correctly handle `super` and `::` --- src/librustdoc/clean/mod.rs | 11 ++++++++++- .../issue-81141-private-reexport-in-public-api-2.rs | 13 +++++++++++++ .../issue-81141-private-reexport-in-public-api.rs | 10 ++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 tests/rustdoc/issue-81141-private-reexport-in-public-api-2.rs diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index aa651bdb31474..f663b58c7380e 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1497,9 +1497,18 @@ fn first_non_private( (cx.tcx.local_parent(hir_id.owner.def_id), leaf.ident) } // Crate paths are not. We start from the crate root. - [parent, leaf] if parent.ident.name == kw::Crate => { + [parent, leaf] if matches!(parent.ident.name, kw::Crate | kw::PathRoot) => { (LOCAL_CRATE.as_def_id().as_local()?, leaf.ident) } + [parent, leaf] if parent.ident.name == kw::Super => { + let parent_mod = cx.tcx.parent_module(hir_id); + if let Some(super_parent) = cx.tcx.opt_local_parent(parent_mod) { + (super_parent, leaf.ident) + } else { + // If we can't find the parent of the parent, then the parent is already the crate. + (LOCAL_CRATE.as_def_id().as_local()?, leaf.ident) + } + } // Absolute paths are not. We start from the parent of the item. [.., parent, leaf] => (parent.res.opt_def_id()?.as_local()?, leaf.ident), }; diff --git a/tests/rustdoc/issue-81141-private-reexport-in-public-api-2.rs b/tests/rustdoc/issue-81141-private-reexport-in-public-api-2.rs new file mode 100644 index 0000000000000..4e9d188bbf8d1 --- /dev/null +++ b/tests/rustdoc/issue-81141-private-reexport-in-public-api-2.rs @@ -0,0 +1,13 @@ +// edition:2015 + +#![crate_name = "foo"] + +use external::Public as Private; + +pub mod external { + pub struct Public; + + // @has 'foo/external/fn.make.html' + // @has - '//*[@class="rust item-decl"]/code' 'pub fn make() -> Public' + pub fn make() -> ::Private { super::Private } +} diff --git a/tests/rustdoc/issue-81141-private-reexport-in-public-api.rs b/tests/rustdoc/issue-81141-private-reexport-in-public-api.rs index 312146e795767..bd54d02c6ec8f 100644 --- a/tests/rustdoc/issue-81141-private-reexport-in-public-api.rs +++ b/tests/rustdoc/issue-81141-private-reexport-in-public-api.rs @@ -112,3 +112,13 @@ pub fn bar14() -> nested::nested2::Whatever3 { pub fn bar15() -> nested::nested2::Whatever4 { Whatever } + +use external::Public as Private; + +pub mod external { + pub struct Public; + + // @has 'foo/external/fn.make.html' + // @has - '//*[@class="rust item-decl"]/code' 'pub fn make() -> Public' + pub fn make() -> super::Private { super::Private } +}