如何使用同一个包中另一个宏定义的宏?

tga*_*tga 5 macros rust

我正在定义一个定义其他宏的宏,如下所示:

macros.rs

#[macro_export]
macro_rules! m1 {
    () => {
        #[macro_export]
        macro_rules! m2 {
            () => {}
        }
    }
}

m1!();
m2!(); // no problem;
Run Code Online (Sandbox Code Playgroud)

我可以使用m2!由另一个箱子use {{crate_name}}::macros::*,我可以使用m2!macros.rs,但我不知道如何使用m2!的是在同一箱文件.

lib.rs

#[macro_use]
pub mod macros;
pub mod test;
pub mod test2;
Run Code Online (Sandbox Code Playgroud)

test.rs(与macros.rs在同一个箱子里)

use crate::m1; // no problem
use crate::m2; // ERROR: macro-expanded `macro_export` macros from the current crate cannot be referred to by absolute paths

m1!(); // no problem
m2!(); // error, no m2
Run Code Online (Sandbox Code Playgroud)

test2.rs

use crate::*;
m2!(); // this works, but I don't really want to use crate::*
Run Code Online (Sandbox Code Playgroud)

实例/ yo.rs

use {{crate_name}}::m2;
m2!(); // no problem
Run Code Online (Sandbox Code Playgroud)

m2在同一个包中的其他文件中使用该宏的正确方法是什么?我正在使用Rust 1.31.1.

She*_*ter 3

阅读并遵循编译器的说明:

error[E0659]: `m2` is ambiguous (macro-expanded name vs less macro-expanded name from outer scope during import/macro resolution)
  --> src/lib.rs:22:5
   |
22 |     m2!();
   |     ^^ ambiguous name
   |
note: `m2` could refer to the macro defined here
  --> src/lib.rs:7:13
   |
7  | /             macro_rules! m2 {
8  | |                 () => {};
9  | |             }
   | |_____________^
...
21 |       m1!();
   |       ------ in this macro invocation
note: `m2` could also refer to the macro defined here
  --> src/lib.rs:7:13
   |
7  | /             macro_rules! m2 {
8  | |                 () => {};
9  | |             }
   | |_____________^
...
13 |       m1!();
   |       ------ in this macro invocation

error: a macro named `m2` has already been exported
  --> src/lib.rs:7:13
   |
7  | /             macro_rules! m2 {
8  | |                 () => {};
9  | |             }
   | |_____________^ `m2` already exported
...
21 |       m1!();
   |       ------ in this macro invocation
   |
   = note: #[deny(duplicate_macro_exports)] on by default
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in the 2018 edition!
   = note: for more information, see issue #35896 <https://github.com/rust-lang/rust/issues/35896>
note: previous macro export is now shadowed
  --> src/lib.rs:7:13
   |
7  | /             macro_rules! m2 {
8  | |                 () => {};
9  | |             }
   | |_____________^
...
13 |       m1!();
   |       ------ in this macro invocation

error: macro-expanded `macro_export` macros from the current crate cannot be referred to by absolute paths
  --> src/lib.rs:19:9
   |
19 |     use crate::m2; 
   |         ^^^^^^^^^
   |
   = note: #[deny(macro_expanded_macro_exports_accessed_by_absolute_paths)] on by default
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, see issue #52234 <https://github.com/rust-lang/rust/issues/52234>
note: the macro is defined here
  --> src/lib.rs:7:13
   |
7  | /             macro_rules! m2 {
8  | |                 () => {};
9  | |             }
   | |_____________^
...
21 |       m1!();
   |       ------ in this macro invocation
Run Code Online (Sandbox Code Playgroud)

具体来说:

错误:当前包中的宏扩展macro_export宏无法通过绝对路径引用

应用:

  1. 不要导入宏;没有必要。
  2. 不要打电话m1;这样做会创建第二个m2.

测试.rs

// use crate::m1;
// use crate::m2; 

// m1!();
m2!();
Run Code Online (Sandbox Code Playgroud)