Macro in doc test has confusing path

I have something similar to this case, but deeper.

I have a setup I tried to illustrate in playground but obviously this macro is in some of the inner subdirectories.

For some reason doc test on macro cannot find the macro even if it is imported from root.

I get errors like:

unresolved import:
help: a similar path exists: `mycrate::innermodule-with-macro`

Like integration tests and unlike unit tests, you can't use local imports in in your doctests:

/// # Examples
///
/// ```
/// use playground::sum; // can't import macro via local path, need to refer to
//7                      // exporting crate through extern name inside doctest
///
/// sum!(1, 1, u8);
/// ```
///
#[macro_export]
macro_rules! sum {
    ($a:expr, $b:expr, $ty:ty) => {
        $a as $ty + $b as $ty
    }
}

Playground.

Macros are not normal items and exporting them works a little different. For exported macros the module they are in does not matter, they are always exported from the top-level scope of the crate:

mod mod1 {
    mod mod2 {
        /// # Examples
        ///
        /// ```
        /// use playground::sum; // note how we still import from top-level, even
        ///                      // though the macro is in some other module
        ///
        /// sum!(1, 1, u8);
        /// ```
        ///
        #[macro_export]
        macro_rules! sum {
            ($a:expr, $b:expr, $ty:ty) => {
                $a as $ty + $b as $ty
            };
        }
    }
}

Playground.

That much I understand, what I don't understand is the fact that it works using it around the crate imported, as you noted, from the root, but specifically in docttest it fails to find it, imported like use mycrate::sum; or used directly let s = mycrate::sum!(

If you #[macro_export]ed sum you should be able to doctest it by importing it as mycrate::sum. So I assume there's something else causing you troubles, though I can't think of anything that might be the culprit.

Ok thanks for confirming my thoughts, I have no idea why it wont import either.
Welp, seems like I'll have to leave it with ignored

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.