Skip to content

Commit

Permalink
Merge pull request rust-lang#3589 from topecongiro/issue-3583
Browse files Browse the repository at this point in the history
Catch panics from the parser while rewriting macro calls
  • Loading branch information
scampi authored May 27, 2019
2 parents 1b02746 + 36dd49a commit 1c210eb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// and those with brackets will be formatted as array literals.

use std::collections::HashMap;
use std::panic::{catch_unwind, AssertUnwindSafe};

use syntax::parse::new_parser_from_tts;
use syntax::parse::parser::Parser;
Expand Down Expand Up @@ -216,12 +217,16 @@ pub(crate) fn rewrite_macro(
None
} else {
let guard = InsideMacroGuard::inside_macro_context(context);
let result =
rewrite_macro_inner(mac, extra_ident, context, shape, position, guard.is_nested);
if result.is_none() {
context.macro_rewrite_failure.replace(true);
let result = catch_unwind(AssertUnwindSafe(|| {
rewrite_macro_inner(mac, extra_ident, context, shape, position, guard.is_nested)
}));
match result {
Err(..) | Ok(None) => {
context.macro_rewrite_failure.replace(true);
None
}
Ok(rw) => rw,
}
result
}
}

Expand Down
3 changes: 3 additions & 0 deletions tests/source/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,6 @@ pub fn fold_abi<V: Fold + ?Sized>(_visitor: &mut V, _i: Abi) -> Abi {
// #3463
x ! {()}
x ! y {()}

// #3583
foo!(|x = y|);
3 changes: 3 additions & 0 deletions tests/target/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,3 +1052,6 @@ pub fn fold_abi<V: Fold + ?Sized>(_visitor: &mut V, _i: Abi) -> Abi {
// #3463
x! {()}
x! y {()}

// #3583
foo!(|x = y|);

0 comments on commit 1c210eb

Please sign in to comment.