IssueRaiser

Crate local state for procedural macros?

I'm tinkering a bit with procedural macros and encountered a problem that can be solved by keeping state in between proc macro invocations. Example from my real application: assume my `proc-macro` crate exposes two macros: `config! {}` and `do_it {}`. The user of my lib is supposed to call `config! {}` only once, but may call `do_it! {}` multiple times. But `do_it!{}` needs data from the `config!{}` invocation. Another example: we want to write a `macro_unique_id!()` macro returning a `u64` by counting internally. --- How am I supposed to solve those problems? I know that somewhat-global state is usually bad. But I do see applications for crate-local state for proc macros.

Posted by LukasKalbertodt on Aug 22, 2017, 12:34 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.4,500 issues0 comments

github (Other)

Crate local state for procedural macros?

LukasKalbertodtAug 22, 2017, 12:34 PM

I'm tinkering a bit with procedural macros and encountered a problem that can be solved by keeping state in between proc macro invocations. Example from my real application: assume my `proc-macro` crate exposes two macros: `config! {}` and `do_it {}`. The user of my lib is supposed to call `config! {}` only once, but may call `do_it! {}` multiple times. But `do_it!{}` needs data from the `config!{}` invocation. Another example: we want to write a `macro_unique_id!()` macro returning a `u64` by counting internally. --- How am I supposed to solve those problems? I know that somewhat-global state is usually bad. But I do see applications for crate-local state for proc macros.

Comments (0)

No comments yet. Start the discussion.

Comment on this issue