-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
macro_rules
that "captures" outter variable fails to compile if used inside a function, but not if used inside a method
#15504
Comments
cc @jbclements |
Okay, let's see. First things first: you're right that methods are currently different from functions; that's because hygiene Next, Hygiene. The basic idea is that in general, macros such as your In this instance, the easiest fix would simply be to put the definition of the Thanks for your question! |
That makes sense.
With these hygiene rules, would it be possible for the compiler to catch these kind of errors in the macro definiton? Because, right now, all errors point to the macro expansions. If you used a bad macro ten times, you'll get ten errors - it'll be better (less noisy) if the compiler pointed directly at the culprit: the macro definition.
In that case, I would end with two equal macro definitions: one in
Something like this? macro_rules! check_len {
($collection:ident) => { $collection.len() == 0 }
} @jbclements Thanks for your answer! (Should I leave this issue open until the method hygiene patch lands, or just close it?) |
Re: catching the errors in the definition: the problem here is that there are things that macros don't know about their contexts. There are certain macros that can be used as patterns or as expressions or as statements; the compiler doesn't know how it'll be used until it plugs it in. Another way of saying that: this is still an area of active research! You can go ahead and close this now, if you like. Naturally, you can also wait, if you prefer that. |
@jbclements 👍 |
minor: Sync from downstream
STR:
Output:
Expansion:
Version:
P.S. I got the impression that we are moving towards macros that can't "capture" outter variables (like
xs
in this case) for "hygienic" reasons. Is that correct? And, if that's the case, may I ask what is this "hygiene" about? (Is a concept related to compilers (only)?)The text was updated successfully, but these errors were encountered: