A similar path exists when using Macro Doc Example

I'm running into an issue when using a custom macro I am making. I tried to distill the problem using the following code:

pub mod A {
    pub mod B {
        pub fn foo() {
            println!("running foo()");
        }
    }

    pub mod C {
        /// # Examples
        ///
        /// ```
        /// mod_test::A::C::bar_macro!();
        /// ```
        #[macro_export]
        macro_rules! bar_macro {
            () => {{
                println!("using bar_macro!");
                crate::A::C::bar();
            }};
        }

        pub fn bar() {
            println!("running bar()");
            crate::A::B::foo();
        }

        pub use bar_macro;

        #[test]
        fn test1() {
            println!("testing macro");
            bar_macro!();
        }
    }
}

and my Cargo.toml

[package]
name = "mod_test"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

I get the following error from the docstring example:

---- src/lib.rs - A::C::bar_macro (line 11) stdout ----
error[E0433]: failed to resolve: unresolved import
 --> src/lib.rs:15:1
  |
7 | mod_test::A::C::bar_macro!();
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  | |
  | unresolved import
  | help: a similar path exists: `mod_test::A`
  |
  = note: this error originates in the macro `mod_test::A::C::bar_macro` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0433`.
Couldn't compile the test.

failures:
    src/lib.rs - A::C::bar_macro (line 11)

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.05s

error: test failed, to rerun pass '--doc'

Any ideas what is causing this issue?

Due to the way macros were implemented way back in Rust 1.0, macros are exported from your crate root instead of the module they were defined in.

You should be able to access it with mod_test::bar_macro!().

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.