Skip to content
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: Avoid duplicating macros in sidebar #94002

Merged
merged 2 commits into from
Feb 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 11 additions & 6 deletions src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ impl<'tcx> Context<'tcx> {
fn build_sidebar_items(&self, m: &clean::Module) -> BTreeMap<String, Vec<NameDoc>> {
// BTreeMap instead of HashMap to get a sorted output
let mut map: BTreeMap<_, Vec<_>> = BTreeMap::new();
let mut inserted: FxHashMap<ItemType, FxHashSet<Symbol>> = FxHashMap::default();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a map to skip duplicates doesn't seem like the correct approach to me. We shouldn't have two macros with the same name in the module in the first place, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible because of reexports. Initially, I had rewrote using a BTreeMap but then I discovered that we had an option to keep the declaration order. So I had to revert that and instead using a Hashmap alongside...


for item in &m.items {
if item.is_stripped() {
continue;
Expand All @@ -258,13 +260,16 @@ impl<'tcx> Context<'tcx> {
let short = item.type_();
let myname = match item.name {
None => continue,
Some(ref s) => s.to_string(),
Some(s) => s,
};
let short = short.to_string();
map.entry(short).or_default().push((
myname,
Some(item.doc_value().map_or_else(String::new, |s| plain_text_summary(&s))),
));
if inserted.entry(short).or_default().insert(myname) {
let short = short.to_string();
let myname = myname.to_string();
map.entry(short).or_default().push((
myname,
Some(item.doc_value().map_or_else(String::new, |s| plain_text_summary(&s))),
));
}
}

if self.shared.sort_modules_alphabetically {
Expand Down
14 changes: 14 additions & 0 deletions src/test/rustdoc-gui/duplicate-macro-reexport.goml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// This test ensures that there is no macro duplicates in the sidebar.
goto: file://|DOC_PATH|/test_docs/macro.a.html
// Waiting for the elements in the sidebar to be rendered.
wait-for: ".sidebar-elems .others .macro"
// Check there is only one macro named "a" listed in the sidebar.
assert-count: (
"//*[@class='sidebar-elems']//*[@class='others']/*[@class='block macro']//li/a[text()='a']",
1,
)
// Check there is only one macro named "b" listed in the sidebar.
assert-count: (
"//*[@class='sidebar-elems']//*[@class='others']/*[@class='block macro']//li/a[text()='b']",
1,
)
Comment on lines +1 to +14
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a regular HTML test using @count, not a GUI test.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's generated by the JS, so can't do that. ^^'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is? Somewhat off-topic, but why?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering the same thing... Will need to dive into this to understand.

3 changes: 3 additions & 0 deletions src/test/rustdoc-gui/src/test_docs/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,6 @@ impl EmptyTrait1 for HasEmptyTraits {}
impl EmptyTrait2 for HasEmptyTraits {}
#[doc(cfg(feature = "some-feature"))]
impl EmptyTrait3 for HasEmptyTraits {}

mod macros;
pub use macros::*;
4 changes: 4 additions & 0 deletions src/test/rustdoc-gui/src/test_docs/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[macro_export]
macro_rules! a{ () => {}}
#[macro_export]
macro_rules! b{ () => {}}