86

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.

How can I see the expanded macro? Is there a flag I can pass to rustc (or even better, cargo) to dump this out?

(This macro is from rust-mdo, though I don't think it matters.)

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
Caspar
  • 7,039
  • 4
  • 29
  • 41

4 Answers4

102

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.

Install by running cargo install cargo-expand.

dtolnay
  • 9,621
  • 5
  • 41
  • 62
  • cargo install cargo-expand —features=prettyprint/regex-fancy will be slightly slower but avoids a c++ dependency so is more likely to build cleanly. – Squirrel Apr 26 '20 at 07:04
  • cargo-expand does not depend on prettyprint or any C++ dependencies. – dtolnay Apr 26 '20 at 09:00
  • You should mention you are the author of the crate – SharedRory Oct 21 '21 at 18:05
  • 2
    It's 2021 and things have changed a bit. You can now use: `rustc -Zunpretty=expanded some.rs`. See Enselic's answer for more details. – at54321 Nov 03 '21 at 22:00
51

Yes, you can pass a special flag to rustc, called --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 ()
                                                                          {
                                                                          ()
                                                                          =>
                                                                          [],
                                                                      }));
}

You need to allow it first, however, by passing -Z unstable-options.

Since Rust 1.1 you can pass these arguments to Cargo, like this:

cargo rustc -- -Z unstable-options --pretty=expanded
Vladimir Matveev
  • 120,085
  • 34
  • 287
  • 296
  • 8
    As of 1.19 this command `cargo rustc -- -Z unstable-options --pretty=expanded` is now only available on nightly. – lncr Aug 09 '17 at 10:03
  • 2
    Why it's only in nightly?( – Bogdan Mart Jan 24 '19 at 00:28
  • 3
    @BogdanMart Because it uses an unstable option, but fear not, you can easily use it. Run `rustup install nightly`, then `rustup run nightly cargo rustc -- -Z unstable-options --pretty=expanded`. – grooveplex Jul 29 '19 at 18:08
  • 7
    So, for three (and counting) years, this capability has never been "stabilized"? – jhfrontz May 29 '20 at 21:09
  • 1
    Likely less about stabilization and more that they don't want to commit to supporting it. – Tumbleweed53 Jan 04 '21 at 18:16
  • @jhfrontz A lot of functionality isn't stabilized, because it means it's always available even if they deprecate it, and isn't meant to be used except as a debugging or internal feature. `-Z timings`, `-Z unstable-options --pretty=expanded`, most `rustfmt` lints are all nightly only. – Alex Huszagh May 07 '21 at 20:56
36

If you want to see the expanded code even before compiling, you can use Expand Macro Recursively feature of rust-analyzer (a vscode extension for Rust language).

enter image description here

Krishna
  • 6,107
  • 2
  • 40
  • 43
14

Starting with nightly-2021-07-28, one must pass -Zunpretty=expanded instead of -Zunstable-options --pretty=expanded, like this:

% rustc -Zunpretty=expanded test.rs

or:

% cargo rustc -- -Zunpretty=expanded

Relevant rustc commits

The --pretty argument was removed from nightly-2021-07-28 via this commit. Support for -Zunpretty=expanded was added to nightly-2018-01-24 via this commit.

Enselic
  • 4,434
  • 2
  • 30
  • 42