Skip to content

Commit 52fa8fe

Browse files
authored
Rollup merge of #108241 - GuillaumeGomez:fix-reexported-macro-handling, r=notriddle
Fix handling of reexported macro in doc hidden items Fixes #108231. Fixes #59368. r? `@notriddle`
2 parents 8973049 + fc6a05c commit 52fa8fe

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

src/librustdoc/passes/strip_hidden.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl<'a, 'tcx> Stripper<'a, 'tcx> {
6262

6363
/// In case `i` is a non-hidden impl block, then we special-case it by changing the value
6464
/// of `is_in_hidden_item` to `true` because the impl children inherit its visibility.
65-
fn recurse_in_impl(&mut self, i: Item) -> Item {
65+
fn recurse_in_impl_or_exported_macro(&mut self, i: Item) -> Item {
6666
let prev = mem::replace(&mut self.is_in_hidden_item, false);
6767
let ret = self.fold_item_recur(i);
6868
self.is_in_hidden_item = prev;
@@ -73,9 +73,17 @@ impl<'a, 'tcx> Stripper<'a, 'tcx> {
7373
impl<'a, 'tcx> DocFolder for Stripper<'a, 'tcx> {
7474
fn fold_item(&mut self, i: Item) -> Option<Item> {
7575
let has_doc_hidden = i.attrs.lists(sym::doc).has_word(sym::hidden);
76-
let is_impl = matches!(*i.kind, clean::ImplItem(..));
76+
let is_impl_or_exported_macro = match *i.kind {
77+
clean::ImplItem(..) => true,
78+
// If the macro has the `#[macro_export]` attribute, it means it's accessible at the
79+
// crate level so it should be handled differently.
80+
clean::MacroItem(..) => {
81+
i.attrs.other_attrs.iter().any(|attr| attr.has_name(sym::macro_export))
82+
}
83+
_ => false,
84+
};
7785
let mut is_hidden = has_doc_hidden;
78-
if !is_impl {
86+
if !is_impl_or_exported_macro {
7987
is_hidden = self.is_in_hidden_item || has_doc_hidden;
8088
if !is_hidden && i.inline_stmt_id.is_none() {
8189
// We don't need to check if it's coming from a reexport since the reexport itself was
@@ -92,8 +100,8 @@ impl<'a, 'tcx> DocFolder for Stripper<'a, 'tcx> {
92100
if self.update_retained {
93101
self.retained.insert(i.item_id);
94102
}
95-
return Some(if is_impl {
96-
self.recurse_in_impl(i)
103+
return Some(if is_impl_or_exported_macro {
104+
self.recurse_in_impl_or_exported_macro(i)
97105
} else {
98106
self.set_is_in_hidden_item_and_fold(false, i)
99107
});

tests/rustdoc/issue-108231.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Regression test for <https://github.com/rust-lang/rust/issues/108231>.
2+
// Macros with `#[macro_export]` attribute should be visible at the top level
3+
// even if they are inside a doc hidden item.
4+
5+
#![crate_name = "foo"]
6+
7+
// @has 'foo/index.html'
8+
// @count - '//*[@id="main-content"]//a[@class="macro"]' 1
9+
// @has - '//*[@id="main-content"]//a[@class="macro"]' 'foo'
10+
11+
#[doc(hidden)]
12+
pub mod __internal {
13+
/// This one should be visible.
14+
#[macro_export]
15+
macro_rules! foo {
16+
() => {};
17+
}
18+
19+
/// This one should be hidden.
20+
macro_rules! bar {
21+
() => {};
22+
}
23+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Ensure that inlined reexport of hidden macros is working as expected.
2+
// Part of <https://github.com/rust-lang/rust/issues/59368>.
3+
4+
#![crate_name = "foo"]
5+
6+
// @has 'foo/index.html'
7+
// @has - '//*[@id="main-content"]//a[@href="macro.Macro2.html"]' 'Macro2'
8+
9+
// @has 'foo/macro.Macro2.html'
10+
// @has - '//*[@class="docblock"]' 'Displayed'
11+
12+
#[macro_export]
13+
#[doc(hidden)]
14+
macro_rules! foo {
15+
() => {};
16+
}
17+
18+
/// not displayed
19+
pub use crate::foo as Macro;
20+
/// Displayed
21+
#[doc(inline)]
22+
pub use crate::foo as Macro2;

0 commit comments

Comments
 (0)