Skip to content

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

Merged
merged 3 commits into from
Jul 25, 2018

Conversation

topecongiro
Copy link
Contributor

@topecongiro topecongiro commented Jul 9, 2018

Closes #2830.
Closes #2857.

Copy link
Member

@nrc nrc left a 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);
Copy link
Member

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?

Copy link
Contributor Author

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!

Copy link
Member

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

@topecongiro
Copy link
Contributor Author

Added commits to use RAII pattern and fix #2857.

@nrc nrc merged commit e3fea15 into rust-lang:master Jul 25, 2018
@topecongiro topecongiro deleted the issue-2830 branch July 25, 2018 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants