From 225ac9efc10bfd4149fd6de3d09e70d6c2387a8b Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Mon, 25 Jul 2022 16:35:12 -0700 Subject: [PATCH 1/2] rustdoc: avoid inlining modules with duplicate names Fixes rust-lang/rust#99734 --- src/librustdoc/clean/mod.rs | 7 +++++-- src/test/rustdoc/auxiliary/issue-99734-aux.rs | 7 +++++++ .../issue-99734-multiple-mods-w-same-name.rs | 14 ++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 src/test/rustdoc/auxiliary/issue-99734-aux.rs create mode 100644 src/test/rustdoc/issue-99734-multiple-mods-w-same-name.rs diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index c9ef4748a4845..cf03979a934fb 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -51,19 +51,22 @@ pub(crate) trait Clean<'tcx, T> { impl<'tcx> Clean<'tcx, Item> for DocModule<'tcx> { fn clean(&self, cx: &mut DocContext<'tcx>) -> Item { let mut items: Vec = vec![]; + let mut inserted = FxHashSet::default(); items.extend( self.foreigns .iter() .map(|(item, renamed)| clean_maybe_renamed_foreign_item(cx, item, *renamed)), ); - items.extend(self.mods.iter().map(|x| x.clean(cx))); + items.extend(self.mods.iter().map(|x| { + inserted.insert((ItemType::Module, x.name)); + x.clean(cx) + })); // Split up imports from all other items. // // This covers the case where somebody does an import which should pull in an item, // but there's already an item with the same namespace and same name. Rust gives // priority to the not-imported one, so we should, too. - let mut inserted = FxHashSet::default(); items.extend(self.items.iter().flat_map(|(item, renamed)| { // First, lower everything other than imports. if matches!(item.kind, hir::ItemKind::Use(..)) { diff --git a/src/test/rustdoc/auxiliary/issue-99734-aux.rs b/src/test/rustdoc/auxiliary/issue-99734-aux.rs new file mode 100644 index 0000000000000..8f1f1ec89674f --- /dev/null +++ b/src/test/rustdoc/auxiliary/issue-99734-aux.rs @@ -0,0 +1,7 @@ +pub struct Option; +impl Option { + pub fn unwrap(self) {} +} + +/// [`Option::unwrap`] +pub mod task {} diff --git a/src/test/rustdoc/issue-99734-multiple-mods-w-same-name.rs b/src/test/rustdoc/issue-99734-multiple-mods-w-same-name.rs new file mode 100644 index 0000000000000..b2f9b8b46578b --- /dev/null +++ b/src/test/rustdoc/issue-99734-multiple-mods-w-same-name.rs @@ -0,0 +1,14 @@ +// aux-build:issue-99734-aux.rs +// build-aux-docs +// ignore-cross-compile + +#![crate_name = "foo"] + +#[macro_use] +extern crate issue_99734_aux; + +pub use issue_99734_aux::*; + +// @count foo/index.html '//a[@class="mod"][@title="foo::task mod"]' 1 + +pub mod task {} From 8724ca3114f2babefeee7989eadbb7d156503f37 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Tue, 2 Aug 2022 14:48:23 -0700 Subject: [PATCH 2/2] rustdoc: avoid inlining foreigns with duplicate names --- src/librustdoc/clean/mod.rs | 12 +++++++----- src/test/rustdoc/auxiliary/issue-99734-aux.rs | 4 ++++ .../issue-99734-multiple-foreigns-w-same-name.rs | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 src/test/rustdoc/issue-99734-multiple-foreigns-w-same-name.rs diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index cf03979a934fb..9251a7487a7d3 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -52,11 +52,13 @@ impl<'tcx> Clean<'tcx, Item> for DocModule<'tcx> { fn clean(&self, cx: &mut DocContext<'tcx>) -> Item { let mut items: Vec = vec![]; let mut inserted = FxHashSet::default(); - items.extend( - self.foreigns - .iter() - .map(|(item, renamed)| clean_maybe_renamed_foreign_item(cx, item, *renamed)), - ); + items.extend(self.foreigns.iter().map(|(item, renamed)| { + let item = clean_maybe_renamed_foreign_item(cx, item, *renamed); + if let Some(name) = item.name { + inserted.insert((item.type_(), name)); + } + item + })); items.extend(self.mods.iter().map(|x| { inserted.insert((ItemType::Module, x.name)); x.clean(cx) diff --git a/src/test/rustdoc/auxiliary/issue-99734-aux.rs b/src/test/rustdoc/auxiliary/issue-99734-aux.rs index 8f1f1ec89674f..234d55efb554d 100644 --- a/src/test/rustdoc/auxiliary/issue-99734-aux.rs +++ b/src/test/rustdoc/auxiliary/issue-99734-aux.rs @@ -5,3 +5,7 @@ impl Option { /// [`Option::unwrap`] pub mod task {} + +extern "C" { + pub fn main() -> std::ffi::c_int; +} diff --git a/src/test/rustdoc/issue-99734-multiple-foreigns-w-same-name.rs b/src/test/rustdoc/issue-99734-multiple-foreigns-w-same-name.rs new file mode 100644 index 0000000000000..3208fea05b376 --- /dev/null +++ b/src/test/rustdoc/issue-99734-multiple-foreigns-w-same-name.rs @@ -0,0 +1,16 @@ +// aux-build:issue-99734-aux.rs +// build-aux-docs +// ignore-cross-compile + +#![crate_name = "foo"] + +#[macro_use] +extern crate issue_99734_aux; + +pub use issue_99734_aux::*; + +// @count foo/index.html '//a[@class="fn"][@title="foo::main fn"]' 1 + +extern "C" { + pub fn main() -> std::ffi::c_int; +}