Skip to content

rustdoc: introduce glob shadowing rules to rustdoc #83872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions src/librustdoc/doctree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,34 @@ impl Module<'hir> {
}

pub(crate) fn push_item(&mut self, new_item: Item<'hir>) {
for item_iter in self.items.iter_mut() {
if item_iter.name() == new_item.name() {
if item_iter.from_glob {
debug!("push_item: {:?} shadowed by {:?}", *item_iter, new_item);
*item_iter = new_item;
if let Some(existed_item) =
self.items.iter_mut().find(|item| item.name() == new_item.name())
{
if existed_item.name() == new_item.name() {
if existed_item.from_glob {
debug!("push_item: {:?} shadowed by {:?}", *existed_item, new_item);
*existed_item = new_item;
return;
} else if new_item.from_glob {
return;
}
}
} else {
self.items.push(new_item);
}
self.items.push(new_item);
}

pub(crate) fn push_mod(&mut self, new_item: Module<'hir>) {
if let Some(shadowed_mod) = self.mods.iter_mut().find(|mod_| mod_.name == new_item.name) {
if item_iter.from_glob {
debug!("push_mod: {:?} shadowed by {:?}", item_iter.name, new_item.name);
*item_iter = new_item;
return;
} else if new_item.from_glob {
return;
}
if let Some(existed_mod) = self.mods.iter_mut().find(|mod_| mod_.name == new_item.name) {
if existed_mod.from_glob {
debug!("push_mod: {:?} shadowed by {:?}", existed_mod.name, new_item.name);
*existed_mod = new_item;
return;
} else if new_item.from_glob {
return;
}
} else {
self.mods.push(new_item);
}
self.mods.push(new_item);
}
}
10 changes: 4 additions & 6 deletions src/test/rustdoc/glob-shadowing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// @has - '//tr[@class="module-item"]' 'mod::prelude'
// @has - '//tr[@class="module-item"]' 'sub2::describe'

// @has 'glob_shadowing/fn.describe.html'
// @has - '//div[@class='docblock']' 'sub2::describe'

mod sub1 {
/// sub1::describe
pub fn describe() -> &'static str {
Expand All @@ -23,12 +26,7 @@ mod sub2 {
}

/// mod::prelude
pub mod prelude {
/// mod::prelude::describe
pub fn describe() -> &'static str {
"mod::describe"
}
}
pub mod prelude {}

#[doc(inline)]
pub use sub2::describe;
Expand Down