Currently procedual macros (like attribute macros) are only allowed to be placed on items.
However, from the book itself (not that the book is the source of all truth) it says that macros “allow creating syntax extensions”. And I think that this should also mean for things that aren’t just items since that might be a bit too broad.
So I propose that procedural macros should be able to be put onto within function blocks. This would allow for seemingly new syntax.
Example:
fn foobar() {
#[some_macro]
if check {
...
} else {
...
}
}
Problems:
- What exactly should be passed to the macro function?
- Could make it hard to see what the macro is modifying.
- When should there be run? Before or after any macros on the surrounding item (I would say before as the least surprising).
Solution to 1: Could be just be the immediately following block only which would also help with the problem of understanding scope of the macros.
Alternatives:
- Do nothing, such “syntax extensions” should only be done with macro_rules.
- Extend macro_rules (maybe with a new form) to expand how it might be used because of the following reasons:
- All three types of brackets work for all macros which is weird.
- No way to have some items / blocks / exprs before the block