-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
rustdoc: introduce glob shadowing rules to rustdoc #83872
Changes from 16 commits
b83ad87
17b908e
791c371
29ebc1e
8c031ca
58bb799
0005dc3
09cb285
4b934bb
6ad7e7e
8100884
981f38e
4076106
7ee35c6
f9df01a
06b0273
18f031f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { | |
hir::CRATE_HIR_ID, | ||
&krate.item, | ||
self.cx.tcx.crate_name, | ||
false, | ||
); | ||
top_level_module.is_crate = true; | ||
// Attach the crate's exported macros to the top-level module. | ||
|
@@ -134,17 +135,19 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { | |
id: hir::HirId, | ||
m: &'tcx hir::Mod<'tcx>, | ||
name: Symbol, | ||
from_glob: bool, | ||
) -> Module<'tcx> { | ||
let mut om = Module::new(name); | ||
om.where_outer = span; | ||
om.where_inner = m.inner; | ||
om.id = id; | ||
om.from_glob = from_glob; | ||
// Keep track of if there were any private modules in the path. | ||
let orig_inside_public_path = self.inside_public_path; | ||
self.inside_public_path &= vis.node.is_pub(); | ||
for &i in m.item_ids { | ||
let item = self.cx.tcx.hir().item(i); | ||
self.visit_item(item, None, &mut om); | ||
self.visit_item(item, None, &mut om, from_glob); | ||
} | ||
self.inside_public_path = orig_inside_public_path; | ||
om | ||
|
@@ -225,14 +228,14 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { | |
let prev = mem::replace(&mut self.inlining, true); | ||
for &i in m.item_ids { | ||
let i = self.cx.tcx.hir().item(i); | ||
self.visit_item(i, None, om); | ||
self.visit_item(i, None, om, glob); | ||
} | ||
self.inlining = prev; | ||
true | ||
} | ||
Node::Item(it) if !glob => { | ||
let prev = mem::replace(&mut self.inlining, true); | ||
self.visit_item(it, renamed, om); | ||
self.visit_item(it, renamed, om, glob); | ||
self.inlining = prev; | ||
true | ||
} | ||
|
@@ -257,6 +260,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { | |
item: &'tcx hir::Item<'_>, | ||
renamed: Option<Symbol>, | ||
om: &mut Module<'tcx>, | ||
from_glob: bool, | ||
) { | ||
debug!("visiting item {:?}", item); | ||
let name = renamed.unwrap_or(item.ident.name); | ||
|
@@ -309,10 +313,17 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { | |
} | ||
} | ||
|
||
om.items.push((item, renamed)) | ||
om.push_item(self.cx, Item::new(item, renamed, from_glob)) | ||
} | ||
hir::ItemKind::Mod(ref m) => { | ||
om.mods.push(self.visit_mod_contents(item.span, &item.vis, item.hir_id(), m, name)); | ||
om.push_mod(self.visit_mod_contents( | ||
item.span, | ||
&item.vis, | ||
item.hir_id(), | ||
m, | ||
name, | ||
from_glob, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, are you sure pub mod outer {
pub const X: usize = 0;
pub mod inner {
pub use super::*;
pub const X: usize = 1;
pub const Y: usize = 2;
}
}
pub use outer::inner::*; Shouldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be set as a test case to prevent regression. |
||
)); | ||
} | ||
hir::ItemKind::Fn(..) | ||
| hir::ItemKind::ExternCrate(..) | ||
|
@@ -323,19 +334,21 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { | |
| hir::ItemKind::OpaqueTy(..) | ||
| hir::ItemKind::Static(..) | ||
| hir::ItemKind::Trait(..) | ||
| hir::ItemKind::TraitAlias(..) => om.items.push((item, renamed)), | ||
| hir::ItemKind::TraitAlias(..) => { | ||
om.push_item(self.cx, Item::new(item, renamed, from_glob)) | ||
} | ||
hir::ItemKind::Const(..) => { | ||
// Underscore constants do not correspond to a nameable item and | ||
// so are never useful in documentation. | ||
if name != kw::Underscore { | ||
om.items.push((item, renamed)); | ||
om.push_item(self.cx, Item::new(item, renamed, from_glob)); | ||
} | ||
} | ||
hir::ItemKind::Impl(ref impl_) => { | ||
// Don't duplicate impls when inlining or if it's implementing a trait, we'll pick | ||
// them up regardless of where they're located. | ||
if !self.inlining && impl_.of_trait.is_none() { | ||
om.items.push((item, None)); | ||
om.push_item(self.cx, Item::new(item, None, from_glob)); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// @has 'glob_shadowing/index.html' | ||
// @count - '//tr[@class="module-item"]' 4 | ||
// @!has - '//tr[@class="module-item"]' 'sub1::describe' | ||
// @!has - '//tr[@class="module-item"]' 'sub1::describe2' | ||
// @has - '//tr[@class="module-item"]' 'mod::prelude' | ||
// @has - '//tr[@class="module-item"]' 'sub2::describe' | ||
// @has - '//tr[@class="module-item"]' 'sub1::Foo (struct)' | ||
// @has - '//tr[@class="module-item"]' 'mod::Foo (function)' | ||
// @has 'glob_shadowing/fn.describe.html' | ||
// @has - '//div[@class="docblock"]' 'sub2::describe' | ||
|
||
mod sub1 { | ||
// this should be shadowed by sub2::describe | ||
/// sub1::describe | ||
pub fn describe() -> &'static str { | ||
"sub1::describe" | ||
} | ||
|
||
// this should be shadowed by mod::prelude | ||
/// sub1::prelude | ||
pub mod prelude { | ||
pub use super::describe; | ||
} | ||
|
||
// this should *not* be shadowed, because sub1::Foo and mod::Foo are in different namespace | ||
/// sub1::Foo (struct) | ||
pub struct Foo; | ||
|
||
// this should be shadowed, | ||
// because both sub1::describe2 and sub3::describe2 are from glob reexport | ||
/// sub1::describe2 | ||
pub fn describe2() -> &'static str { | ||
"sub1::describe2" | ||
} | ||
} | ||
|
||
mod sub2 { | ||
/// sub2::describe | ||
pub fn describe() -> &'static str { | ||
"sub2::describe" | ||
} | ||
} | ||
|
||
mod sub3 { | ||
// this should be shadowed | ||
// because both sub1::describe2 and sub3::describe2 are from glob reexport | ||
/// sub3::describe2 | ||
pub fn describe2() -> &'static str { | ||
"sub3::describe2" | ||
} | ||
} | ||
|
||
/// mod::prelude | ||
pub mod prelude {} | ||
|
||
/// mod::Foo (function) | ||
pub fn Foo() {} | ||
|
||
#[doc(inline)] | ||
pub use sub2::describe; | ||
longfangsong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#[doc(inline)] | ||
pub use sub1::*; | ||
|
||
longfangsong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[doc(inline)] | ||
pub use sub3::*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible for
new_item.name()
to be empty? And is it possible thatnew_item_ns
is empty?If both is not possible maybe we can remove this branch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only situation I found which
new_item.name().is_empty()
isstd::prelude
(but IMO we don't need to care about it), remove this check seems won't affect the test result. But this comment mentioned some possible situation. @ollie27 can you give a more detailed example?On the other hand,
new_item_ns
can be empty in many situations like macro derives (a minimal example isDebug
insrc/test/rustdoc/wrapping.rs
). If we remove this check, then adding those items may cause panic on L88.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new_item.name
can be empty for impl blocks - or if it's not and it's using{{impl}}
or something, that's another bug, because impl blocks shouldn't use globbing logic, they don't have have a namespace.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So means in this case both checks are still necessary? Maybe we should leave a comment or it's not necessary?
And should the test includes these cases?