From 1a515e69490cdc3cee5325c4fc8d18baa502f38a Mon Sep 17 00:00:00 2001 From: Alona Enraght-Moony Date: Wed, 30 Jul 2025 19:12:31 +0000 Subject: [PATCH 1/2] rustdoc-json: Add test for `#[macro_use]` attribute --- tests/rustdoc-json/attrs/macro_export.rs | 40 ++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/rustdoc-json/attrs/macro_export.rs diff --git a/tests/rustdoc-json/attrs/macro_export.rs b/tests/rustdoc-json/attrs/macro_export.rs new file mode 100644 index 0000000000000..1472aabb6ff56 --- /dev/null +++ b/tests/rustdoc-json/attrs/macro_export.rs @@ -0,0 +1,40 @@ +//@ compile-flags: --document-private-items + +//@ set exported_id = "$.index[?(@.name=='exported')].id" +//@ is "$.index[?(@.name=='exported')].attrs" '[{"other": "#[macro_export]"}]' +//@ is "$.index[?(@.name=='exported')].visibility" '"public"' + +#[macro_export] +macro_rules! exported { + () => {}; +} + +//@ set not_exported_id = "$.index[?(@.name=='not_exported')].id" +//@ is "$.index[?(@.name=='not_exported')].attrs" [] +//@ is "$.index[?(@.name=='not_exported')].visibility" '"crate"' +macro_rules! not_exported { + () => {}; +} + +//@ set module_id = "$.index[?(@.name=='module')].id" +pub mod module { + //@ set exported_from_mod_id = "$.index[?(@.name=='exported_from_mod')].id" + //@ is "$.index[?(@.name=='exported_from_mod')].attrs" '[{"other": "#[macro_export]"}]' + //@ is "$.index[?(@.name=='exported_from_mod')].visibility" '"public"' + #[macro_export] + macro_rules! exported_from_mod { + () => {}; + } + + //@ set not_exported_from_mod_id = "$.index[?(@.name=='not_exported_from_mod')].id" + //@ is "$.index[?(@.name=='not_exported_from_mod')].attrs" [] + //@ is "$.index[?(@.name=='not_exported_from_mod')].visibility" '"crate"' + macro_rules! not_exported_from_mod { + () => {}; + } +} +// The non-exported macro's are left in place, but the #[macro_export]'d ones +// are moved to the crate root. + +//@ is "$.index[?(@.name=='module')].inner.module.items[*]" $not_exported_from_mod_id +//@ ismany "$.index[?(@.name=='macro_export')].inner.module.items[*]" $exported_id $not_exported_id $module_id $exported_from_mod_id From a33e084afe698e0a025211abd6dc1c9a4bb22e9d Mon Sep 17 00:00:00 2001 From: Alona Enraght-Moony Date: Wed, 30 Jul 2025 19:57:32 +0000 Subject: [PATCH 2/2] rustdoc-json: Move `#[macro_export]` from `Other` to it's own variant --- src/librustdoc/json/conversions.rs | 8 ++++++-- src/rustdoc-json-types/lib.rs | 7 +++++-- tests/rustdoc-json/attrs/macro_export.rs | 4 ++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs index 08bc0bb19505f..fe69a5abee1ad 100644 --- a/src/librustdoc/json/conversions.rs +++ b/src/librustdoc/json/conversions.rs @@ -908,8 +908,12 @@ fn maybe_from_hir_attr( hir::Attribute::Parsed(kind) => kind, hir::Attribute::Unparsed(_) => { - // FIXME: We should handle `#[doc(hidden)]`. - return Some(other_attr(tcx, attr)); + return Some(if attr.has_name(sym::macro_export) { + Attribute::MacroExport + // FIXME: We should handle `#[doc(hidden)]`. + } else { + other_attr(tcx, attr) + }); } }; diff --git a/src/rustdoc-json-types/lib.rs b/src/rustdoc-json-types/lib.rs index 6235b0e8576f1..40f89009a431b 100644 --- a/src/rustdoc-json-types/lib.rs +++ b/src/rustdoc-json-types/lib.rs @@ -37,8 +37,8 @@ pub type FxHashMap = HashMap; // re-export for use in src/librustdoc // will instead cause conflicts. See #94591 for more. (This paragraph and the "Latest feature" line // are deliberately not in a doc comment, because they need not be in public docs.) // -// Latest feature: Structured Attributes -pub const FORMAT_VERSION: u32 = 54; +// Latest feature: Add Attribute::MacroUse +pub const FORMAT_VERSION: u32 = 55; /// The root of the emitted JSON blob. /// @@ -216,6 +216,9 @@ pub enum Attribute { /// `#[must_use]` MustUse { reason: Option }, + /// `#[macro_export]` + MacroExport, + /// `#[export_name = "name"]` ExportName(String), diff --git a/tests/rustdoc-json/attrs/macro_export.rs b/tests/rustdoc-json/attrs/macro_export.rs index 1472aabb6ff56..5d487cf6a56e8 100644 --- a/tests/rustdoc-json/attrs/macro_export.rs +++ b/tests/rustdoc-json/attrs/macro_export.rs @@ -1,7 +1,7 @@ //@ compile-flags: --document-private-items //@ set exported_id = "$.index[?(@.name=='exported')].id" -//@ is "$.index[?(@.name=='exported')].attrs" '[{"other": "#[macro_export]"}]' +//@ is "$.index[?(@.name=='exported')].attrs" '["macro_export"]' //@ is "$.index[?(@.name=='exported')].visibility" '"public"' #[macro_export] @@ -19,7 +19,7 @@ macro_rules! not_exported { //@ set module_id = "$.index[?(@.name=='module')].id" pub mod module { //@ set exported_from_mod_id = "$.index[?(@.name=='exported_from_mod')].id" - //@ is "$.index[?(@.name=='exported_from_mod')].attrs" '[{"other": "#[macro_export]"}]' + //@ is "$.index[?(@.name=='exported_from_mod')].attrs" '["macro_export"]' //@ is "$.index[?(@.name=='exported_from_mod')].visibility" '"public"' #[macro_export] macro_rules! exported_from_mod {