Is it possible to check for repr(C) inside a procedural macro?

The title says it all: is it possible with a procedural macro (derive) on a structure to check whether the structure is repr(C)?

1 Like

Macros don't have any information other than the literal source code you give them. They run before any types are known, so there's no information about repr(C). A macro (derive or attribute) on the type would be able to check for the repr(C) (and repr(transparent)) attribute, though. There may be tricks for sharing info between macros, but I haven't looked into it. The only similar case I needed was for deriving a trait that would also imply repr(C) or repr(transparent).

Apparently, it's trivial:

That requires the macro to be on the structure and I thought you didn't want that. :eyes: But if that's fine, then it's about as easy as it looks.

I think you had a very unfortunate typo here, saying without :smile:

Oh man. I'm so sorry.

2 Likes

No worries, it just made the answer more indirect :sweat_smile:

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.