Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upBorrow #run idea from JAI #57641
Comments
This comment has been minimized.
This comment has been minimized.
|
You can already do this, just not inline in the crate being built: https://doc.rust-lang.org/book/ch19-06-macros.html#how-to-write-a-custom-derive-macro |
This comment has been minimized.
This comment has been minimized.
|
Can you clarify one thing for me? Are you able to walk backwards through the |
This comment has been minimized.
This comment has been minimized.
|
You're given access to whatever the macro is applied to: #[my_custom_macro] // this can read and modify the module declaration and everything in it
mod foo {
...
}
my_custom_macro! {
// this can only modify the stuff in the block here
...
} |
This comment has been minimized.
This comment has been minimized.
|
That's what I thought. OK, how do I handle the following cases from within a macro?
That said, it looks like I need to re-read everything about macros. The last time I looked at them was more than a year ago, when I started working on developing macros for marshaling crate. |
Centril
added
A-macros
T-lang
needs-rfc
labels
Jan 16, 2019
This comment has been minimized.
This comment has been minimized.
|
If you want to continue this conversation I recommend doing so on http://internals.rust-lang.org/. This is not the place for speculative new designs. |
Centril
closed this
Jan 16, 2019
This comment has been minimized.
This comment has been minimized.
|
Understood, I'll move the conversation over there and reference this issue. |
ckaran commentedJan 15, 2019
I've been trying to figure out macros in Rust, including some of the tracking issues for Macros 2.0, and have come to the conclusion that JAI's
#runmechanism may be the better way to go (see https://github.com/BSVino/JaiPrimer/blob/master/JaiPrimer.md, and search for#run).The basic idea is that the compiler has an interpreter for the language built into itself. Whenever you reach some code marked with
#run, the compiler executes the code immediately as a part of the compilation process. My thought is that this may be a good way to deal with macros. The process would be something like the following:#runis encountered. This crate provides the communications link between the#runinterpreted code and the compiler.#run, it starts up a MIRI interpreter instance which takes over. The code has access to the current state of the compiler (including the abstract syntax tree, etc.) via the fake crate's features. The code can mutate the compiler's state (including the AST) via the crate.#runis a no-op.#runis encountered before the first#runis finished, then a new MIRI interpreter instance is spun up, and the steps above follow suit. This might be used in cases where you have avec!()within the interpreted code.Advantages to this approach
#runyour entire program (although probably more slowly than if it were compiled).Issues I can think off off-hand:
The only real issue that I see is the middle one; it is a security issue. However, it would have limited effect as it we're talking about compile-time execution, which means on a developer's machine rather than across numerous machines on the internet.
Thoughts, critique, comments?