Expanding macros that do not compile

I'm looking for approaches to debug declarative macros that produce compile errors. Currently, the only way I've found to do that is to use macro expansion functionality in RustRover, which uses rust-analyzer internally. I believe it should also be available in VSCode.

I wonder if there are other approaches?

UPDATE

Sample code:

macro_rules! buggy_macro {
    () => {
        Not valid Rust at all
    };
}

fn main() {
    buggy_macro!();
}

cargo expand should also work.

3 Likes

I've tried it, but for me it doesn't work. It prints the errors produced by the macro and omits the entire body of the macro from the output.

For instance if we take this code:

macro_rules! buggy_macro {
    () => {
        Not valid Rust at all
    };
}

fn main() {
    buggy_macro!();
}

cargo expand would output this:

#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
fn main() {
    (/*ERROR*/);
}

And also the compile-errors.

But RustRover's "Show Macro Expansion" would output this: