问题描述
我有一个涉及宏的编译错误:
I have a compile error involving a macro:
<mdo macros>:6:19: 6:50 error: cannot move out of captured outer variable in an `FnMut` closure
<mdo macros>:6 bind ( $ e , move | $ p | mdo ! { $ ( $ t ) * } ) ) ; (
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<mdo macros>:1:1: 14:36 note: in expansion of mdo!
<mdo macros>:6:27: 6:50 note: expansion site
<mdo macros>:1:1: 14:36 note: in expansion of mdo!
<mdo macros>:6:27: 6:50 note: expansion site
<mdo macros>:1:1: 14:36 note: in expansion of mdo!
src/parser.rs:30:42: 37:11 note: expansion site
error: aborting due to previous error
不幸的是,宏是递归的,所以很难弄清楚编译器在抱怨什么,而且行号似乎是用于扩展宏而不是我的代码.
Unfortunately, the macro is recursive so it's hard to figure out what the compiler is complaining about, plus it seems like the line numbers are for the expanded macro rather than my code.
我怎样才能看到展开的宏?有没有我可以传递给 rustc(或者更好,cargo)的标志来倾倒它?
How can I see the expanded macro? Is there a flag I can pass to rustc (or even better, cargo) to dump this out?
(这个宏来自 rust-mdo,虽然我认为这并不重要.)
(This macro is from rust-mdo, though I don't think it matters.)
推荐答案
cargo rustc --profile=check -- -Zunpretty=expanded
,但更简洁的替代方案是 cargo-expand 箱子.它提供了一个 Cargo 子命令 cargo expand 来打印宏扩展的结果.它还通过 rustfmt
传递扩展代码,这通常会导致很多比 rustc 的默认输出更具可读性的代码.
cargo rustc --profile=check -- -Zunpretty=expanded
, but a more concise alternative is the cargo-expand crate. It provides a Cargo subcommand cargo expand
which prints the result of macro expansion. It also passes the expanded code through rustfmt
which generally results in much more readable code than the default output from rustc.
通过运行 cargo install cargo-expand
安装.
这篇关于如何查看导致编译错误的扩展宏代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!