-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
decl_macro: scoping and other issues #42448
Comments
@r3c0d3x #![feature(decl_macro)]
macro m($f:ident) {
#[derive(Debug)]
struct S;
fn $f() { println!("{:?}", S); }
}
fn main() {
struct S; // This does not conflict with `S` from the macro
m!(f);
f();
} Note that this is how local variables work today: macro_rules! m {
() => { let created_local = 0; }
}
fn main() {
m!();
println!("{}", created_local); // resolution error
} Macros 2.0 extends this behavior to all item/fields/methods/etc., not just locals. If you want your macro to define macro m($i:ident) {
struct $i;
}
fn main() {
m!(CreatedStruct);
CreatedStruct; // resolves
} We plan to allow "opting out" of hygiene soon. @SergioBenitez has proposed using macro m() {
struct #CreatedStruct; // This is exempt from hygiene, so is usable outside the macro.
let #created_local = 0; // Same with this local.
}
fn main() {
m!();
CreatedStruct; // resolves
created_local; // resolves
} This hasn't been implemented yet (but I'd be happy to mentor), so you'll need to pass in the ident as an argument for now. #42337 only applies to macros from across crates (I believe it's a bug in how macros are encoded/decoded in the crate metadata, which needs improving). |
I was getting familiar with the implementation to work on #35853 but it sounds like future work should be in macros 2.0, so I'm available to work on this. |
@spearman Great! I'm not sure this issue needs any work though -- everything is working as expected here. @r3c0d3x @spearman did my explanation make sense? If so, I think we should close this. |
It makes sense, but macros 2.0 won't be very useful until the opt-out syntax is implemented. I have macros that expand to several items. Without the opt-out syntax, the name of each item, their members, variants, and method impls will need to be provided as part of the macro invocation:
|
@spearman Agreed, I think implementing opt-out syntax should be the highest priority now that declarative macros 2.0 have landed. |
Sorry about the late response. I understood the explanation and I probably should've looked more deeply into the functionality instead of opening an issue. |
@jseyfried Is anyone working on hygiene opt-out yet? If not, I'd be happy to give it a go, with your mentorship (although I admit I'm pretty inexperienced with the compiler). |
The current implementation of
decl_macro
seems to have some issues, as shown in this example (playground):I expected this to compile and just print out "
CreatedStruct
" (from its derivedDebug
implementation), but it throws the following errors instead:That's strange because our example should be equivalent to this (playground):
which works just fine.
The scoping issue is likely caused by whatever is causing #42337, but I'm not sure what is happening with the trait implementation.
CC @jseyfried
The text was updated successfully, but these errors were encountered: