Skip to content

Commit 0d990a3

Browse files
authored
Rollup merge of #89867 - Urgau:fix-double-definition, r=GuillaumeGomez
Fix macro_rules! duplication when reexported in the same module This can append if within the same module a `#[macro_export] macro_rules!` is declared but also a reexport of itself producing two export of the same macro in the same module. In that case we only want to document it once. Before: ``` Module { is_crate: true, items: [ Id("0:4"), // pub use crate::repro as repro2; Id("0:3"), // macro_rules! repro Id("0:3"), // duplicate, same as above ], } ``` After: ``` Module { is_crate: true, items: [ Id("0:4"), // pub use crate::repro as repro2; Id("0:3"), // macro_rules! repro ], } ``` Fixes #89852
2 parents 54aa547 + db5b64a commit 0d990a3

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

src/librustdoc/visit_ast.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,21 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
8787
// the rexport defines the path that a user will actually see. Accordingly,
8888
// we add the rexport as an item here, and then skip over the original
8989
// definition in `visit_item()` below.
90+
//
91+
// We also skip `#[macro_export] macro_rules!` that have already been inserted,
92+
// it can happen if within the same module a `#[macro_export] macro_rules!`
93+
// is declared but also a reexport of itself producing two exports of the same
94+
// macro in the same module.
95+
let mut inserted = FxHashSet::default();
9096
for export in self.cx.tcx.module_exports(CRATE_DEF_ID).unwrap_or(&[]) {
9197
if let Res::Def(DefKind::Macro(_), def_id) = export.res {
9298
if let Some(local_def_id) = def_id.as_local() {
9399
if self.cx.tcx.has_attr(def_id, sym::macro_export) {
94-
let hir_id = self.cx.tcx.hir().local_def_id_to_hir_id(local_def_id);
95-
let item = self.cx.tcx.hir().expect_item(hir_id);
96-
top_level_module.items.push((item, None));
100+
if inserted.insert(def_id) {
101+
let hir_id = self.cx.tcx.hir().local_def_id_to_hir_id(local_def_id);
102+
let item = self.cx.tcx.hir().expect_item(hir_id);
103+
top_level_module.items.push((item, None));
104+
}
97105
}
98106
}
99107
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// edition:2018
2+
3+
#![no_core]
4+
#![feature(no_core)]
5+
6+
// @count macro.json "$.index[*][?(@.name=='macro')].inner.items[*]" 2
7+
8+
// @set repro_id = macro.json "$.index[*][?(@.name=='repro')].id"
9+
// @has - "$.index[*][?(@.name=='macro')].inner.items[*]" $repro_id
10+
#[macro_export]
11+
macro_rules! repro {
12+
() => {};
13+
}
14+
15+
// @set repro2_id = macro.json "$.index[*][?(@.inner.name=='repro2')].id"
16+
// @has - "$.index[*][?(@.name=='macro')].inner.items[*]" $repro2_id
17+
pub use crate::repro as repro2;

src/test/rustdoc/issue-89852.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// edition:2018
2+
3+
#![no_core]
4+
#![feature(no_core)]
5+
6+
// @matches 'issue_89852/sidebar-items.js' '"repro"'
7+
// @!matches 'issue_89852/sidebar-items.js' '"repro".*"repro"'
8+
9+
#[macro_export]
10+
macro_rules! repro {
11+
() => {};
12+
}
13+
14+
pub use crate::repro as repro2;

0 commit comments

Comments
 (0)