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

Commit 1c210eb

Browse files
authored
Merge pull request rust-lang#3589 from topecongiro/issue-3583
Catch panics from the parser while rewriting macro calls
2 parents 1b02746 + 36dd49a commit 1c210eb

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/macros.rs

Lines changed: 10 additions & 5 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;
@@ -216,12 +217,16 @@ pub(crate) fn rewrite_macro(
216217
None
217218
} else {
218219
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);
220+
let result = catch_unwind(AssertUnwindSafe(|| {
221+
rewrite_macro_inner(mac, extra_ident, context, shape, position, guard.is_nested)
222+
}));
223+
match result {
224+
Err(..) | Ok(None) => {
225+
context.macro_rewrite_failure.replace(true);
226+
None
227+
}
228+
Ok(rw) => rw,
223229
}
224-
result
225230
}
226231
}
227232

tests/source/macros.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,3 +475,6 @@ pub fn fold_abi<V: Fold + ?Sized>(_visitor: &mut V, _i: Abi) -> Abi {
475475
// #3463
476476
x ! {()}
477477
x ! y {()}
478+
479+
// #3583
480+
foo!(|x = y|);

tests/target/macros.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,3 +1052,6 @@ pub fn fold_abi<V: Fold + ?Sized>(_visitor: &mut V, _i: Abi) -> Abi {
10521052
// #3463
10531053
x! {()}
10541054
x! y {()}
1055+
1056+
// #3583
1057+
foo!(|x = y|);

0 commit comments

Comments
 (0)