-
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
Conversation
r? @ollie27 (rust-highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
Signed-off-by: longfangsong <longfangsong@icloud.com>
Co-authored-by: Joshua Nelson <joshua@yottadb.com>
Co-authored-by: Joshua Nelson <joshua@yottadb.com>
Co-authored-by: Joshua Nelson <joshua@yottadb.com>
This comment has been minimized.
This comment has been minimized.
Co-authored-by: Joshua Nelson <joshua@yottadb.com>
Co-authored-by: Joshua Nelson <joshua@yottadb.com>
Signed-off-by: longfangsong <longfangsong@icloud.com>
This comment has been minimized.
This comment has been minimized.
Signed-off-by: longfangsong <longfangsong@icloud.com>
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.
Just a few more nits :) this looks really good! Thank you for working on it.
This comment has been minimized.
This comment has been minimized.
Signed-off-by: longfangsong <longfangsong@icloud.com>
This comment has been minimized.
This comment has been minimized.
Can you add some tests for glob re-exports of unnamed items and items with different namespaces? |
Signed-off-by: longfangsong <longfangsong@icloud.com>
|
||
pub(crate) fn push_item(&mut self, ctx: &DocContext<'_>, new_item: Item<'hir>) { | ||
let new_item_ns = ctx.tcx.def_kind(new_item.hir_item.def_id).ns(); | ||
if !new_item.name().is_empty() && new_item_ns.is_some() { |
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 that new_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.
Is it possible for
new_item.name()
to be empty? And is it possible thatnew_item_ns
is empty?
The only situation I found which new_item.name().is_empty()
is std::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 is Debug
in src/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?
This comment has been minimized.
This comment has been minimized.
src/librustdoc/visit_ast.rs
Outdated
item.hir_id(), | ||
m, | ||
name, | ||
from_glob, |
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.
Hmm, are you sure from_glob
should apply recursively like this? What happens if you have
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 inner::X
take precedence over outer::X
?
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.
This could be set as a test case to prevent regression.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Signed-off-by: longfangsong <longfangsong@icloud.com>
This comment has been minimized.
This comment has been minimized.
I'm still looking for a way to fix the case in this comment, but I found another interesting case which might reveal a bug in rustc: // resolve.rs
mod sub4 {
pub const X: usize = 0;
pub mod other {
pub const X: usize = 1;
}
pub mod inner {
pub use other::*;
pub use super::*;
}
}
pub use sub4::inner::*;
// main.rs
println!("{:?}", resolve::X); Will give us a And // resolve.rs
mod sub4 {
pub const X: usize = 0;
pub mod other {
pub const X: usize = 1;
}
pub mod inner {
// just swap these `pub use` here
pub use super::*;
pub use other::*;
}
}
pub use sub4::inner::*;
// main.rs
println!("{:?}", resolve::X); Will cause: `X` is ambiguous (glob import vs glob import in the same module) Is this the expected behaviour? |
No, this is a bug: #47525 (and various other ordering bugs: https://github.com/rust-lang/rust/issues?q=is%3Aopen+is%3Aissue+label%3AA-resolve+order+). I wonder if there's a way to reuse what resolve decides here so we don't have three different opinions of what it should resolve to? Right now there are two, rustc_resolve and rust-analyzer. |
FWIW I think just passing |
Propagating Both for code: mod sub4 {
pub const X: usize = 0;
pub mod inner {
pub use super::*;
pub const X: usize = 1;
}
}
#[doc(inline)]
pub use sub4::inner::*; The call tree start from
|
Signed-off-by: longfangsong <longfangsong@icloud.com>
The job Click to see the possible cause of the failure (guessed by this bot)
|
I'm not quite following the code, but my opinion is we should keep neither since it's ambiguous. |
However rustc think |
Ok, I think I misunderstood, this is a different case from before. I think the issue is that |
☔ The latest upstream changes (presumably #84525) made this pull request unmergeable. Please resolve the merge conflicts. |
Ping from triage: thank you |
Close this, because this solution can't cover all cases and will cause regression and I have no idea how to fix it easily. |
Signed-off-by: longfangsong longfangsong@icloud.com
Fixes #83375.
This pr introduced the glob shadowing rules into the visit ast stage of rustdoc. So we can prevent glob reexport duplicate or wrong modules.