-
Notifications
You must be signed in to change notification settings - Fork 927
Keep the context that we are inside macro in nested macro #2834
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
src/macros.rs
Outdated
@@ -149,12 +149,12 @@ pub fn rewrite_macro( | |||
shape: Shape, | |||
position: MacroPosition, | |||
) -> Option<String> { | |||
context.inside_macro.replace(true); | |||
let old_value = context.inside_macro.replace(true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use an RAII pattern here so there is no change of missing the replace
at the end of the function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I am sorry, but I am not sure how to apply an RAII pattern here. Does RefCell
provides such thing? Either way, I have tried to mimic the pattern here. Please let me know if this is not what you expected, or there is a better way to do this!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RAII in Rust would be something like:
struct InsideMacroRestore<'a> {
context: &RewriteContext,
old_value: bool,
}
impl<'a> InsideMacroRestore<'a> {
// Call this as `let _imr = InsideMacroRestore(context, true);` which sets
// the value and stores the old one.
fn replace(context: &'a RewriteContext, new_value: bool) -> InsideMacroRestore<'a> {
let old_value = context.inside_macro.replace(new_value);
InsideMacroRestore { context, old_value }
}
}
impl<'a> Drop for InsideMacroRestore<'a> {
// Called automatically when `_imr` goes out of scope, which restores the old
// value of inside_macro
fn drop(&mut self) {
self.context.inside_macro.replace(self.old_value)
}
}
MutexLock
is an example of such thing, although there the object is actually useful, here we just want the Drop
impl
Added commits to use RAII pattern and fix #2857. |
Closes #2830.
Closes #2857.