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

#1

Here is sample code

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

use crate::foo::bar;

fn main() {
    bar!();
}

Which build fails with

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

error: cannot determine resolution for the macro `bar`
  --> src/main.rs:10:5
   |
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.

#2

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.

#3

Correct! See https://doc.rust-lang.org/edition-guide/rust-2018/macros/macro-changes.html