Looking up syntax for struct based on identifier passed to procedural macro

I am learning syn and was wondering if I am able to look up struct syntax based only on the struct identifier as syntax input to a procedural macro without using a derive or attribute proc macro.

For example:

struct Ab {
a: f32,
b: u32 }

my_macro!(Ab)

Should look up if the struct Ab exists in the file and then turn the syntax into a TokenStream.

Which would be equivalent to

my_macro!(
struct Ab {
a: f32,
b: u32 }
)

You want reflection, in other words. Nope, not gonna happen. Not anytime soon. Macros work with tokens, nothing else exist at this stage of the compilation process.

1 Like

No, this is not possible. Macros run interleaved with name resolution and parsing of items (such as structs), and before processing the items’ tokens into actual definitions, so the information you are looking for does not even reliably exist yet.

2 Likes

This kind of problem is usually solved with an attribute macro, which would look like this:

#[my_macro]
struct Ab {
  a: f32,
  b: u32
}