Rust docs with declarative macros

50 views Asked by At

I have two macros that build the same struct. a! matches the field name using a generic identifier, the b! matches the specific field name.

In VS Code, though, only a! let's the documentation comments work (i.e., hover over name inside the macro call and see the doc comment.

How can I get b! to also show the documentation comments on hover?

struct Hello {
    /// Name docs
    name: String
}

macro_rules! a {
    ($name:ident: $value:expr) => {
        Hello { $name: $value }
    };
}

macro_rules! b {
    (name: $expr:expr) => {
        Hello { name: $expr }
    };
}

fn main() {
    let hello_alice = a!(name: String::from("Alice"));
    let hello_bob = b!(name: String::from("Bob"));
}

enter image description here

I'm guessing the issue comes from the fact that in b!'s definition, Rust doesn't know that name refers to an identifier, and that the name in the matcher is the same name as the output.

0

There are 0 answers