From d03b0f59d55324f373040d7636dffb7d9872c687 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 5 Jul 2023 18:36:10 +0200 Subject: [PATCH 1/6] If re-export is private, get the next item until a public one is found or expose the private item directly --- src/librustdoc/clean/mod.rs | 96 +++++++++++++++++++++++++++++++++++-- 1 file changed, 93 insertions(+), 3 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 0182d50773d77..507ac4f0c1a1d 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1479,8 +1479,93 @@ pub(crate) fn clean_middle_assoc_item<'tcx>( Item::from_def_id_and_parts(assoc_item.def_id, Some(assoc_item.name), kind, cx) } +/// The goal of this function is to return the first `Path` which is not private (ie not private +/// or `doc(hidden)`). If it's not possible, it'll return the "end type". +/// +/// If the path is not a re-export or is public, it'll return `None`. +fn first_not_private( + cx: &mut DocContext<'_>, + hir_id: hir::HirId, + path: &hir::Path<'_>, +) -> Option { + if path.segments.is_empty() { + return None; + } + let parent_def_id = if path.segments.len() == 1 { + // Then it's available in the same scope as the owner. + hir_id.owner.def_id + } else { + // It's not available in the same scope, so we start from the parent of the item. + path.segments[path.segments.len() - 2].res.opt_def_id()?.as_local()? + }; + let target_def_id = path.res.opt_def_id()?; + let mut ident = path.segments.last().unwrap().ident; + // First we try to get the `DefId` of the item. + for child in cx + .tcx + .module_children_local(cx.tcx.local_parent(parent_def_id)) + .iter() + .filter(move |c| c.ident == ident) + { + if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = child.res { + continue; + } + + if let Some(def_id) = child.res.opt_def_id() && target_def_id == def_id { + let mut last_path_res = None; + 'reexps: for reexp in child.reexport_chain.iter() { + if let Some(use_def_id) = reexp.id() && + let Some(local_use_def_id) = use_def_id.as_local() + { + let hir = cx.tcx.hir(); + // let parent_mod = hir.local_def_id_to_hir_id(); + for item_id in hir.module_items(cx.tcx.local_parent(local_use_def_id)) { + let item = hir.item(item_id); + if item.ident == ident { + match item.kind { + hir::ItemKind::Use(path, _) => { + for res in &path.res { + if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = res { + continue; + } + if !cx.tcx.is_doc_hidden(use_def_id) && + cx.tcx.local_visibility(local_use_def_id).is_public() { + break 'reexps; + } + ident = path.segments.last().unwrap().ident; + last_path_res = Some((path, res)); + continue 'reexps; + } + } + _ => {} + } + } + } + } + } + if !child.reexport_chain.is_empty() { + // So in here, we use the data we gathered from iterating the reexports. If + // `last_path_res` is set, it can mean two things: + // + // 1. We found a public reexport. + // 2. We didn't find a public reexport so it's the "end type" path. + if let Some((path, res)) = last_path_res { + let path = hir::Path { segments: path.segments, res: *res, span: path.span }; + return Some(clean_path(&path, cx)); + } + // If `last_path_res` is `None`, it can mean two things: + // + // 1. The re-export is public, no need to change anything, just use the path as is. + // 2. Nothing was found, so let's just return the original path. + return None; + } + } + } + None +} + fn clean_qpath<'tcx>(hir_ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> Type { - let hir::Ty { hir_id: _, span, ref kind } = *hir_ty; + let hir::Ty { hir_id, span, ref kind } = *hir_ty; let hir::TyKind::Path(qpath) = kind else { unreachable!() }; match qpath { @@ -1497,7 +1582,12 @@ fn clean_qpath<'tcx>(hir_ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> Type if let Some(expanded) = maybe_expand_private_type_alias(cx, path) { expanded } else { - let path = clean_path(path, cx); + // First we check if it's a private re-export. + let path = if let Some(path) = first_not_private(cx, hir_id, &path) { + path + } else { + clean_path(path, cx) + }; resolve_type(cx, path) } } @@ -1649,7 +1739,7 @@ fn maybe_expand_private_type_alias<'tcx>( } } - Some(cx.enter_alias(substs, def_id.to_def_id(), |cx| clean_ty(ty, cx))) + Some(cx.enter_alias(substs, def_id.to_def_id(), |cx| clean_ty(&ty, cx))) } pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> Type { From e3b7035e350b57baf9579c4c5c51befbe37a6df3 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 5 Jul 2023 18:45:42 +0200 Subject: [PATCH 2/6] Add regression test for #81141 --- ...ue-81141-private-reexport-in-public-api.rs | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tests/rustdoc/issue-81141-private-reexport-in-public-api.rs diff --git a/tests/rustdoc/issue-81141-private-reexport-in-public-api.rs b/tests/rustdoc/issue-81141-private-reexport-in-public-api.rs new file mode 100644 index 0000000000000..d6ef843542957 --- /dev/null +++ b/tests/rustdoc/issue-81141-private-reexport-in-public-api.rs @@ -0,0 +1,34 @@ +// This test ensures that if a private re-export is present in a public API, it'll be +// replaced by the first public item in the re-export chain or by the private item. + +#![crate_name = "foo"] + +use crate::bar::Bar as Alias; + +pub use crate::bar::Bar as Whatever; +use crate::Whatever as Whatever2; +use crate::Whatever2 as Whatever3; +pub use crate::bar::Inner as Whatever4; + +mod bar { + pub struct Bar; + pub use self::Bar as Inner; +} + +// @has 'foo/fn.bar.html' +// @has - '//*[@class="rust item-decl"]/code' 'pub fn bar() -> Bar' +pub fn bar() -> Alias { + Alias +} + +// @has 'foo/fn.bar2.html' +// @has - '//*[@class="rust item-decl"]/code' 'pub fn bar2() -> Whatever' +pub fn bar2() -> Whatever3 { + Whatever +} + +// @has 'foo/fn.bar3.html' +// @has - '//*[@class="rust item-decl"]/code' 'pub fn bar3() -> Whatever4' +pub fn bar3() -> Whatever4 { + Whatever +} From 52a7615d3bc0d77b1a2d5236fdeac1550455556b Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 7 Jul 2023 13:55:31 +0200 Subject: [PATCH 3/6] Improve code readability --- src/librustdoc/clean/mod.rs | 59 +++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 507ac4f0c1a1d..410d1a5f5643d 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1488,24 +1488,25 @@ fn first_not_private( hir_id: hir::HirId, path: &hir::Path<'_>, ) -> Option { - if path.segments.is_empty() { - return None; - } - let parent_def_id = if path.segments.len() == 1 { - // Then it's available in the same scope as the owner. - hir_id.owner.def_id - } else { - // It's not available in the same scope, so we start from the parent of the item. - path.segments[path.segments.len() - 2].res.opt_def_id()?.as_local()? + let (parent_def_id, mut ident) = match &path.segments[..] { + [] => return None, + // Relative paths are available in the same scope as the owner. + [leaf] => (cx.tcx.local_parent(hir_id.owner.def_id), leaf.ident), + // So are self paths. + [parent, leaf] if parent.ident.name == kw::SelfLower => { + (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 => { + (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), }; let target_def_id = path.res.opt_def_id()?; - let mut ident = path.segments.last().unwrap().ident; // First we try to get the `DefId` of the item. - for child in cx - .tcx - .module_children_local(cx.tcx.local_parent(parent_def_id)) - .iter() - .filter(move |c| c.ident == ident) + for child in + cx.tcx.module_children_local(parent_def_id).iter().filter(move |c| c.ident == ident) { if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = child.res { continue; @@ -1518,26 +1519,20 @@ fn first_not_private( let Some(local_use_def_id) = use_def_id.as_local() { let hir = cx.tcx.hir(); - // let parent_mod = hir.local_def_id_to_hir_id(); for item_id in hir.module_items(cx.tcx.local_parent(local_use_def_id)) { let item = hir.item(item_id); - if item.ident == ident { - match item.kind { - hir::ItemKind::Use(path, _) => { - for res in &path.res { - if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = res { - continue; - } - if !cx.tcx.is_doc_hidden(use_def_id) && - cx.tcx.local_visibility(local_use_def_id).is_public() { - break 'reexps; - } - ident = path.segments.last().unwrap().ident; - last_path_res = Some((path, res)); - continue 'reexps; - } + if item.ident == ident && let hir::ItemKind::Use(path, _) = item.kind { + for res in &path.res { + if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = res { + continue; + } + if !cx.tcx.is_doc_hidden(use_def_id) && + cx.tcx.local_visibility(local_use_def_id).is_public() { + break 'reexps; } - _ => {} + ident = path.segments.last().unwrap().ident; + last_path_res = Some((path, res)); + continue 'reexps; } } } From f37a6b0bf9ed9253e331ef3baa69fa463f1c200f Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 7 Jul 2023 13:55:47 +0200 Subject: [PATCH 4/6] Extend issue-81141-private-reexport-in-public-api test to cover more cases --- ...ue-81141-private-reexport-in-public-api.rs | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) 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 d6ef843542957..312146e795767 100644 --- a/tests/rustdoc/issue-81141-private-reexport-in-public-api.rs +++ b/tests/rustdoc/issue-81141-private-reexport-in-public-api.rs @@ -32,3 +32,83 @@ pub fn bar2() -> Whatever3 { pub fn bar3() -> Whatever4 { Whatever } + +// @has 'foo/fn.bar4.html' +// @has - '//*[@class="rust item-decl"]/code' 'pub fn bar4() -> Bar' +pub fn bar4() -> crate::Alias { + Alias +} + +// @has 'foo/fn.bar5.html' +// @has - '//*[@class="rust item-decl"]/code' 'pub fn bar5() -> Whatever' +pub fn bar5() -> crate::Whatever3 { + Whatever +} + +// @has 'foo/fn.bar6.html' +// @has - '//*[@class="rust item-decl"]/code' 'pub fn bar6() -> Whatever4' +pub fn bar6() -> crate::Whatever4 { + Whatever +} + + +// @has 'foo/fn.bar7.html' +// @has - '//*[@class="rust item-decl"]/code' 'pub fn bar7() -> Bar' +pub fn bar7() -> self::Alias { + Alias +} + +// @has 'foo/fn.bar8.html' +// @has - '//*[@class="rust item-decl"]/code' 'pub fn bar8() -> Whatever' +pub fn bar8() -> self::Whatever3 { + Whatever +} + +// @has 'foo/fn.bar9.html' +// @has - '//*[@class="rust item-decl"]/code' 'pub fn bar9() -> Whatever4' +pub fn bar9() -> self::Whatever4 { + Whatever +} + +mod nested { + pub(crate) use crate::Alias; + pub(crate) use crate::Whatever3; + pub(crate) use crate::Whatever4; + pub(crate) use crate::nested as nested2; +} + +// @has 'foo/fn.bar10.html' +// @has - '//*[@class="rust item-decl"]/code' 'pub fn bar10() -> Bar' +pub fn bar10() -> nested::Alias { + Alias +} + +// @has 'foo/fn.bar11.html' +// @has - '//*[@class="rust item-decl"]/code' 'pub fn bar11() -> Whatever' +pub fn bar11() -> nested::Whatever3 { + Whatever +} + +// @has 'foo/fn.bar12.html' +// @has - '//*[@class="rust item-decl"]/code' 'pub fn bar12() -> Whatever4' +pub fn bar12() -> nested::Whatever4 { + Whatever +} + +// @has 'foo/fn.bar13.html' +// @has - '//*[@class="rust item-decl"]/code' 'pub fn bar13() -> Bar' +pub fn bar13() -> nested::nested2::Alias { + Alias +} + +// @has 'foo/fn.bar14.html' +// @has - '//*[@class="rust item-decl"]/code' 'pub fn bar14() -> Whatever' +pub fn bar14() -> nested::nested2::Whatever3 { + Whatever +} + +// @has 'foo/fn.bar15.html' +// @has - '//*[@class="rust item-decl"]/code' 'pub fn bar15() -> Whatever4' +pub fn bar15() -> nested::nested2::Whatever4 { + Whatever +} From 1d1951bafaf113fdb2b07aba99f51365c4a22cb5 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 7 Jul 2023 16:56:21 +0200 Subject: [PATCH 5/6] Rename `first_not_private` into `first_non_private` --- src/librustdoc/clean/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 410d1a5f5643d..aa651bdb31474 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1483,7 +1483,7 @@ pub(crate) fn clean_middle_assoc_item<'tcx>( /// or `doc(hidden)`). If it's not possible, it'll return the "end type". /// /// If the path is not a re-export or is public, it'll return `None`. -fn first_not_private( +fn first_non_private( cx: &mut DocContext<'_>, hir_id: hir::HirId, path: &hir::Path<'_>, @@ -1578,7 +1578,7 @@ fn clean_qpath<'tcx>(hir_ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> Type expanded } else { // First we check if it's a private re-export. - let path = if let Some(path) = first_not_private(cx, hir_id, &path) { + let path = if let Some(path) = first_non_private(cx, hir_id, &path) { path } else { clean_path(path, cx) From 4a1e06b453d14b61d4fc19532918694f485bf51e Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 7 Jul 2023 17:55:33 +0200 Subject: [PATCH 6/6] 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 } +}