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

Cas*_*par 44 rust

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

<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
Run Code Online (Sandbox Code Playgroud)

不幸的是,宏是递归的,因此很难弄清楚编译器在抱怨什么,而且看起来线号是针对扩展宏而不是我的代码.

我怎样才能看到扩展的宏?是否有一面旗帜可以传递给rustc(甚至更好的货物)来解决这个问题?

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

dto*_*nay 51

更简洁的替代方案cargo rustc -- -Zunstable-options --pretty=expanded货物扩张板条箱.它提供了一个Cargo子命令cargo expand,用于打印宏扩展的结果.它还传递了扩展代码,通过rustfmt该代码通常会产生比rustc的默认输出更易读的代码.

通过运行安装cargo install cargo-expand.

  • 这看起来很酷很好!输出突出显示. (2认同)
  • 现在是 2021 年,事情发生了一些变化。您现在可以使用:`rustc -Zunpretty=expanded some.rs`。有关更多详细信息,请参阅 Enselic 的答案。 (2认同)

kri*_*hna 51

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

在此输入图像描述


Vla*_*eev 48

是的,您可以传递一个特殊标志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 ()
                                                                          {
                                                                          ()
                                                                          =>
                                                                          [],
                                                                      }));
}
Run Code Online (Sandbox Code Playgroud)

但是,你需要首先允许它通过-Z unstable-options.

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

cargo rustc -- -Z unstable-options --pretty=expanded
Run Code Online (Sandbox Code Playgroud)

  • 从1.19开始,这个命令`cargo rustc - -Z unstable-options --pretty = expanded`现在只能在夜间使用. (7认同)
  • 那么,三年来(并且还在继续),这种能力从未“稳定”过? (6认同)
  • @BogdanMart 因为它使用了不稳定的选项,但不用担心,您可以轻松使用它。运行 `rustup install nightly`,然后 `rustup run nightly Cargo rustc -- -Z不稳定选项 --pretty=expanded`。 (3认同)
  • 为什么只在晚上?( (2认同)

Ens*_*lic 16

从 开始nightly-2021-07-28,必须通过-Zunpretty=expanded而不是-Zunstable-options --pretty=expanded,如下所示:

% rustc -Zunpretty=expanded test.rs
Run Code Online (Sandbox Code Playgroud)

或者:

% cargo rustc -- -Zunpretty=expanded
Run Code Online (Sandbox Code Playgroud)

相关 rustc 提交

该参数已通过此提交--pretty删除。通过此提交添加了对的支持。nightly-2021-07-28-Zunpretty=expandednightly-2018-01-24