Skip to content

Commit 975b375

Browse files
committed
Keep the inside macro context in nested macro call
1 parent d512240 commit 975b375

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/macros.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,36 @@ fn return_original_snippet_with_failure_marked(
142142
Some(context.snippet(span).to_owned())
143143
}
144144

145+
struct InsideMacroGuard<'a> {
146+
context: &'a RewriteContext<'a>,
147+
is_nested: bool,
148+
}
149+
150+
impl<'a> InsideMacroGuard<'a> {
151+
fn inside_macro_context(context: &'a RewriteContext) -> InsideMacroGuard<'a> {
152+
let is_nested = context.inside_macro.replace(true);
153+
InsideMacroGuard { context, is_nested }
154+
}
155+
}
156+
157+
impl<'a> Drop for InsideMacroGuard<'a> {
158+
fn drop(&mut self) {
159+
self.context.inside_macro.replace(self.is_nested);
160+
}
161+
}
162+
145163
pub fn rewrite_macro(
146164
mac: &ast::Mac,
147165
extra_ident: Option<ast::Ident>,
148166
context: &RewriteContext,
149167
shape: Shape,
150168
position: MacroPosition,
151169
) -> Option<String> {
152-
context.inside_macro.replace(true);
170+
let guard = InsideMacroGuard::inside_macro_context(context);
153171
let result = rewrite_macro_inner(mac, extra_ident, context, shape, position);
154172
if result.is_none() {
155173
context.macro_rewrite_failure.replace(true);
156174
}
157-
context.inside_macro.replace(false);
158175
result
159176
}
160177

0 commit comments

Comments
 (0)