Skip to content

Commit 6ad7e7e

Browse files
committed
apply some suggestions from code reviewing
Signed-off-by: longfangsong <[email protected]>
1 parent 4b934bb commit 6ad7e7e

File tree

3 files changed

+47
-20
lines changed

3 files changed

+47
-20
lines changed

src/librustdoc/doctree.rs

+39-18
Original file line numberDiff line numberDiff line change
@@ -64,30 +64,51 @@ impl Module<'hir> {
6464
}
6565

6666
pub(crate) fn push_item(&mut self, new_item: Item<'hir>) {
67-
if let Some(existing_item) =
68-
self.items.iter_mut().find(|item| item.name() == new_item.name())
69-
{
70-
if existing_item.from_glob {
71-
debug!("push_item: {:?} shadowed by {:?}", *existing_item, new_item);
72-
*existing_item = new_item;
73-
return;
74-
} else if new_item.from_glob {
75-
return;
67+
if !new_item.name().is_empty() {
68+
// todo: also check namespace
69+
if let Some(existing_item) =
70+
self.items.iter_mut().find(|item| item.name() == new_item.name())
71+
{
72+
match (existing_item.from_glob, new_item.from_glob) {
73+
(true, _) => {
74+
// `existing_item` is from glob, no matter whether `new_item` is from glob
75+
// `new_item` should always shadow `existing_item`
76+
debug!("push_item: {:?} shadowed by {:?}", existing_item, new_item);
77+
*existing_item = new_item;
78+
return;
79+
}
80+
(false, true) => {
81+
// `existing_item` is not from glob but `new_item` is
82+
// just keep `existing_item` and return at once
83+
return;
84+
}
85+
(false, false) => unreachable!() // todo: how to handle this?
86+
}
7687
}
7788
}
89+
// no item with same name and namespace exists, just collect `new_item`
7890
self.items.push(new_item);
7991
}
8092

81-
pub(crate) fn push_mod(&mut self, new_item: Module<'hir>) {
82-
if let Some(existing_mod) = self.mods.iter_mut().find(|mod_| mod_.name == new_item.name) {
83-
if existing_mod.from_glob {
84-
debug!("push_mod: {:?} shadowed by {:?}", existing_mod.name, new_item.name);
85-
*existing_mod = new_item;
86-
return;
87-
} else if new_item.from_glob {
88-
return;
93+
pub(crate) fn push_mod(&mut self, new_mod: Module<'hir>) {
94+
if let Some(existing_mod) = self.mods.iter_mut().find(|mod_| mod_.name == new_mod.name) {
95+
match (existing_mod.from_glob, new_mod.from_glob) {
96+
(true, _) => {
97+
// `existing_mod` is from glob, no matter whether `new_mod` is from glob
98+
// `new_mod` should always shadow `existing_mod`
99+
debug!("push_mod: {:?} shadowed by {:?}", existing_mod.name, new_mod.name);
100+
*existing_mod = new_mod;
101+
return;
102+
},
103+
(false, true) => {
104+
// `existing_mod` is not from glob but `new_mod` is
105+
// just keep `existing_mod` and return at once
106+
return;
107+
},
108+
(false, false) => unreachable!(),
89109
}
90110
}
91-
self.mods.push(new_item);
111+
// no mod with same name exists, just collect `new_mod`
112+
self.mods.push(new_mod);
92113
}
93114
}

src/librustdoc/visit_ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
313313
}
314314
}
315315

316-
om.push_item(Item::new(item, renamed, is_glob))
316+
om.push_item(Item::new(item, renamed, from_glob))
317317
}
318318
hir::ItemKind::Mod(ref m) => {
319319
om.push_mod(self.visit_mod_contents(

src/test/rustdoc/glob-shadowing.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @has 'glob_shadowing/index.html'
2-
// @count - '//tr[@class="module-item"]' 2
2+
// @count - '//tr[@class="module-item"]' 4
33
// @has - '//tr[@class="module-item"]' 'mod::prelude'
44
// @!has - '//tr[@class="module-item"]' 'sub1::describe'
55
// @has - '//tr[@class="module-item"]' 'sub2::describe'
@@ -17,6 +17,9 @@ mod sub1 {
1717
pub mod prelude {
1818
pub use super::describe;
1919
}
20+
21+
/// sub1::Foo (struct)
22+
pub struct Foo;
2023
}
2124

2225
mod sub2 {
@@ -29,6 +32,9 @@ mod sub2 {
2932
/// mod::prelude
3033
pub mod prelude {}
3134

35+
/// mod::Foo (function)
36+
pub fn Foo() {}
37+
3238
#[doc(inline)]
3339
pub use sub2::describe;
3440

0 commit comments

Comments
 (0)