Use `macro_name` doesn't work in the same crate?


Here is sample code

mod foo {
    macro_rules! bar {
        () => { println!("123") };

use crate::foo::bar;

fn main() {

Which build fails with

error[E0432]: unresolved import `crate::foo::bar`
 --> src/
7 | use crate::foo::bar;
  |     ^^^^^^^^^^^^^^^ no `bar` in `foo`

error: cannot determine resolution for the macro `bar`
  --> src/
10 |     bar!();
   |     ^^^
   = note: import resolution is stuck, try simplifying macro imports

Since 2018 introduces modules clarification I expected this code to work normally. But it seems that use crate::macro only works for topmost macros declared in other crates. I was sure that macro_use and so on are obsolete and completely replaced by simple use, but it seems that it’s not the case.


As far as I remember, you still need to #[macro_export].

Or make sure it is defined before you use it, even without useing it.

eg. in some codebase of mine, I have a macro_rules! … before any mod and in each of the child modules I can use that macro without even importing it.


Correct! See