How to check a code is valid or else in proc-macro?

I want to call a method in a field, but this method ask this field impl some trait. How can I check this call is valid?

such I have these codes:

let code = quote!{
    #ident.name();
}

how do I check the code whether is a valid code?

You don't.

Macros don't have access to the information that would be needed to determine if the code is valid or not. Neither does the compiler itself. Macros are expanded before things like names or traits are resolved, so no one can tell if the code you expand to is valid yet.

4 Likes

User-defined macros won't check anything except tokens passed in, and they only emit code that can be written out by hand, which is they are designed for.
If the emitted code is invalid as Rust code, the compiler will check and report the error.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.