I’m currently working on a syntax extension to implement trait mocking, though I stumbled over a problem with disabling macro hygienics.
Basically all macro code in Rust is hygienic, i.e., identifiers generated by a macro will not conflict with identifiers of the same name outside the macro (except for items). Usually that is something you want to have … except when you don’t.
macro_rules! lets you interact with outside identifiers by passing it to the macro from the outside context. Reading a bit online about the MTWT algorithm and the
libsyntax docs got me so far that for syntax extensions I’d have to change the SyntaxContext of the Idents, e.g., using Ident::unhygienize.
Though doing so is very cumbersome as you have to walk the generated AST and replace the identifiers (Visitor is not able to modify the AST) on your own. I need to disable hygienics for for the whole macro, making this approach very heavy weight (AST structure is very complex to traverse and subject to changes in nightly).
Another option I came up with is to parse the whole function inside a macro and handle special syntax there as everything would share the same syntax context, but I think that’s hardly better. Though I’d prefer to do that if no other options come up (syn + synom makes that easier).
Has anyone an idea of some practical way to solve this?