Get type information in proc macro

I'm writing a function-like proc macro that takes an enum variant as an argument, e.g. my_macro!(Option::Some). Now, I want to get the information about the fields in the provided variant (in this case, Some(T)). So I want to get the type information of an identifier. Is this possible?

No, macros do not have access to type information.

Why? Do the compiler process them before doing semantic analysis?

Yes. More specifically, you wouldn't need full type information, but name resolution of the path Option::Some, as well as a way to access its definition. While there isn't anything fundamental preventing the compiler to allow this, no APIs or coherent design for a feature like that currently exists.

This Overview of the Rust compiler may help. You'll see that macro processing takes place during AST generation, before later phases like type checking.

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.