如何查看导致编译错误的扩展宏代码?

How do I see the expanded macro code that's causing my compile error?

我有一个涉及宏的编译错误:

<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

不幸的是,宏是递归的,所以很难弄清楚编译器在抱怨什么,而且行号似乎是针对扩展宏而不是我的代码的。

如何查看展开后的宏?有没有我可以传递给 rustc(或者更好的是 cargo)的标志来转储它?

(这个宏来自 rust-mdo,虽然我认为它不重要。)

是的,您可以将一个特殊标志传递给 rustc,称为 --pretty=expanded:

% cat test.rs
fn main() {
    println!("Hello world");
}
% rustc -Z unstable-options --pretty=expanded test.rs
#![feature(no_std)]
#![no_std]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate "std" as std;
fn main() {
    ::std::old_io::stdio::println_args(::std::fmt::Arguments::new_v1({
                                                                         static __STATIC_FMTSTR:
                                                                                &'static [&'static str]
                                                                                =
                                                                             &["Hello world"];
                                                                         __STATIC_FMTSTR
                                                                     },
                                                                     &match ()
                                                                          {
                                                                          ()
                                                                          =>
                                                                          [],
                                                                      }));
}

但是,您需要先允许它,方法是传递 -Z unstable-options

从 Rust 1.1 开始,您可以将这些参数传递给 Cargo,如下所示:

cargo rustc -- -Z unstable-options --pretty=expanded

cargo rustc --profile=check -- -Zunpretty=expanded,但更简洁的替代方法是 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,它通常比 rustc 的默认输出更易读。

通过 运行 cargo install cargo-expand.

安装

nightly-2021-07-28开始,必须通过-Zunpretty=expanded而不是-Zunstable-options --pretty=expanded,像这样:

% rustc -Zunpretty=expanded test.rs

或:

% cargo rustc -- -Zunpretty=expanded

相关的 rustc 提交

--pretty 参数已通过 this commitnightly-2021-07-28 中删除。 通过 this commit.

将对 -Zunpretty=expanded 的支持添加到 nightly-2018-01-24

如果您想在编译之前查看扩展代码,可以使用 rust-analyzerExpand Macro Recursively 功能(Rust 语言的 vscode 扩展)。