Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit f7cd789

Browse files
committed
Catch panics from the parser while rewriting macro calls
1 parent 1c1b4f7 commit f7cd789

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/macros.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// and those with brackets will be formatted as array literals.
1111

1212
use std::collections::HashMap;
13+
use std::panic::{catch_unwind, AssertUnwindSafe};
1314

1415
use syntax::parse::new_parser_from_tts;
1516
use syntax::parse::parser::Parser;
@@ -208,21 +209,24 @@ pub(crate) fn rewrite_macro(
208209
shape: Shape,
209210
position: MacroPosition,
210211
) -> Option<String> {
211-
let should_skip = context
212-
.skip_macro_names
213-
.borrow()
214-
.contains(&context.snippet(mac.node.path.span).to_owned());
215-
if should_skip {
216-
None
217-
} else {
218-
let guard = InsideMacroGuard::inside_macro_context(context);
219-
let result =
220-
rewrite_macro_inner(mac, extra_ident, context, shape, position, guard.is_nested);
221-
if result.is_none() {
222-
context.macro_rewrite_failure.replace(true);
212+
catch_unwind(AssertUnwindSafe(|| {
213+
let should_skip = context
214+
.skip_macro_names
215+
.borrow()
216+
.contains(&context.snippet(mac.node.path.span).to_owned());
217+
if should_skip {
218+
None
219+
} else {
220+
let guard = InsideMacroGuard::inside_macro_context(context);
221+
let result =
222+
rewrite_macro_inner(mac, extra_ident, context, shape, position, guard.is_nested);
223+
if result.is_none() {
224+
context.macro_rewrite_failure.replace(true);
225+
}
226+
result
223227
}
224-
result
225-
}
228+
}))
229+
.ok()?
226230
}
227231

228232
fn check_keyword<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option<MacroArg> {

0 commit comments

Comments
 (0)