rustdoc: fix duplicate Re-exports sections

When a module contains both `pub extern crate` and `pub use` items,
they were being rendered under two separate "Re-exports" section
headers instead of one. This happened because the code iterated over
each ItemType separately and rendered a section header for each,
even though both ExternCrate and Import map to the same ItemSection.

This fix tracks the current section and only renders a new header
when the section actually changes, consolidating items that belong
to the same section under a single header.
This commit is contained in:
Heath Dutton🕴️ 2025-12-24 23:41:40 -05:00
parent e7d44143a1
commit b2f19eac2f
2 changed files with 48 additions and 10 deletions

View file

@ -398,18 +398,34 @@ fn item_module(cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) -> i
let mut types = not_stripped_items.keys().copied().collect::<Vec<_>>();
types.sort_unstable_by(|a, b| reorder(*a).cmp(&reorder(*b)));
let mut last_section: Option<super::ItemSection> = None;
for type_ in types {
let my_section = item_ty_to_section(type_);
let tag = if my_section == super::ItemSection::Reexports {
REEXPORTS_TABLE_OPEN
} else {
ITEM_TABLE_OPEN
};
write!(
w,
"{}",
write_section_heading(my_section.name(), &cx.derive_id(my_section.id()), None, tag)
)?;
// Only render section heading if the section changed
if last_section != Some(my_section) {
// Close the previous section if there was one
if last_section.is_some() {
w.write_str(ITEM_TABLE_CLOSE)?;
}
let tag = if my_section == super::ItemSection::Reexports {
REEXPORTS_TABLE_OPEN
} else {
ITEM_TABLE_OPEN
};
write!(
w,
"{}",
write_section_heading(
my_section.name(),
&cx.derive_id(my_section.id()),
None,
tag
)
)?;
last_section = Some(my_section);
}
for (_, myitem) in &not_stripped_items[&type_] {
match myitem.kind {
@ -526,6 +542,9 @@ fn item_module(cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) -> i
}
}
}
}
// Close the final section
if last_section.is_some() {
w.write_str(ITEM_TABLE_CLOSE)?;
}

View file

@ -0,0 +1,19 @@
//@ aux-build:pub-extern-crate.rs
// Regression test for issue <https://github.com/rust-lang/rust/issues/150211>.
// When a module has both `pub extern crate` and `pub use` items,
// they should both appear under a single "Re-exports" section,
// not two separate sections.
//@ has duplicate_reexports_section_150211/index.html
// Verify there's exactly one Re-exports section header
//@ count - '//h2[@id="reexports"]' 1
//@ has - '//h2[@id="reexports"]' 'Re-exports'
// Verify both the extern crate and the use item are present
//@ has - '//code' 'pub extern crate inner;'
//@ has - '//code' 'pub use inner::SomeStruct;'
pub extern crate inner;
#[doc(no_inline)]
pub use inner::SomeStruct;