How do I link to my macro whose name clashes with a struct and which is always in scope along with the struct in a doc comment of the struct in the module containing both?

I just released bevy_exclusive_components,

exclusive.rs contains a macro and a struct, both have the same name:

struct Exclusive {
    // -- SNIP --
}

macro_rules! Exclusive {
    // -- SNIP --
}

lib.rs reexports both

mod exclusive;
pub use exclusive::*;

and the issue is that the doc comments of Exclusive try to link to Exclusive!, which fails, even if I write [`macro@Exclusive`]

the current macro namespace is a bit tricky, since macros are not proper items, they have "textual scope", they are exported directly at the root of the crate, so you can write it like this:

/// use the [`Exclusive`](macro@crate::Exclusive) macro

see: Macros by example - The Rust Reference

Thanks, that worked.

I changed it a bit to make it consistent with my other imports and added a !:

/// use the [`Exclusive!`] macro
///
/// [`Exclusive!`]: macro@crate::Exclusive

You can opt in to a macro also having normal path scoping by useing it:

pub mod some_module {
    macro_rules! foo { () => {} }
    pub(crate) use foo; // causes `crate::some_module::foo` to exist
    
    #[macro_export]
    macro_rules! bar { () => {} }
    pub use bar; // causes `crate::some_module::bar` to exist
}

Note that if you want the macro to be visible outside the crate, as in bar above, you must also write #[macro_export], and that will export it from the crate root too.

If you would like the macro to be in a non-root module only, then that is not currently possible, but you can fake it with careful use of #[doc(hidden)] and #[doc(inline)]:[1]

pub mod some_module {
    #[doc(hidden)]
    #[macro_export]
    macro_rules! _some_module_private_baz { () => {} }
    
    /// Documentation for the macro blah blah blah.
    #[doc(inline)]
    pub use _some_module_private_baz as baz;
}

  1. Without the #[doc(inline)], the documentation will still appear in the crate’s documentation but not in cross-crate reexports, if I remember correctly. ↩︎