我正在研究一个过程宏,并且我发现编译器在程序宏恐慌时不提供有关proc宏包的信息。我试图重写panic!
打印位置:我可以使用自己的自定义宏从标准库中覆盖宏吗?
macro_rules! std_panic {
($($args:tt)+) => {{
panic!($($args)*);
}};
}
/// panic! with location reporting.
macro_rules! panic {
($($args:tt)+) => {{
std_panic!("{}\n --> {}:{}:{}", format_args!($($args)*), file!(), line!(), column!());
}};
}
但是,编译失败
error: recursion limit reached while expanding the macro `std_panic`
--> src/lib.rs:30:9
|
30 | std_panic!("{}\n --> {}:{}:{}", format_args!($($args)*), file!(), line!(), column!());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
54 | _ => unimplemented!("handling tuple struct"),
in this macro invocation
|
= help: consider adding a `#![recursion_limit="131072"]` attribute to your crate
我将限制设置为65536
,证明这是关系到递归扩展。
据the macros chapter of The Rust Programming Language, first edition,我自己panic!
不是std_panic!
可见的,所以它应该使用panic!
从标准库。
我也试过
#![feature(no_std)]
#![no_std]
#[macro_use(panic)]
extern crate std;
,但它不工作。
@kennytm谢谢。但它说同样的事情......并且' //在这里可见:'m1'。 macro_rules! m3 //这里可以看到:'m1','m3'。 '意味着'恐慌!'不应该从'std_panic!'看到(编辑:固定错字,我预计输入为
) – kdy