From 1c1b4f76bd2a6ef5cb64eaec4b4d56f76c88c292 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Tue, 28 May 2019 00:03:42 +0900 Subject: [PATCH 1/3] Add a test for #3583 --- tests/source/macros.rs | 3 +++ tests/target/macros.rs | 3 +++ 2 files changed, 6 insertions(+) diff --git a/tests/source/macros.rs b/tests/source/macros.rs index 66012e4d69d4d..ef5b1efa5b967 100644 --- a/tests/source/macros.rs +++ b/tests/source/macros.rs @@ -475,3 +475,6 @@ pub fn fold_abi(_visitor: &mut V, _i: Abi) -> Abi { // #3463 x ! {()} x ! y {()} + +// #3583 +foo!(|x = y|); diff --git a/tests/target/macros.rs b/tests/target/macros.rs index 8f86b79f9225e..f19b5d11feef7 100644 --- a/tests/target/macros.rs +++ b/tests/target/macros.rs @@ -1052,3 +1052,6 @@ pub fn fold_abi(_visitor: &mut V, _i: Abi) -> Abi { // #3463 x! {()} x! y {()} + +// #3583 +foo!(|x = y|); From f7cd789ebc135cb165f00e64a7e5edaf821c0c62 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Tue, 28 May 2019 00:03:50 +0900 Subject: [PATCH 2/3] Catch panics from the parser while rewriting macro calls --- src/macros.rs | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index a42b839f9c507..d25106c49616e 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -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; @@ -208,21 +209,24 @@ pub(crate) fn rewrite_macro( shape: Shape, position: MacroPosition, ) -> Option { - let should_skip = context - .skip_macro_names - .borrow() - .contains(&context.snippet(mac.node.path.span).to_owned()); - if should_skip { - 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); + catch_unwind(AssertUnwindSafe(|| { + let should_skip = context + .skip_macro_names + .borrow() + .contains(&context.snippet(mac.node.path.span).to_owned()); + if should_skip { + 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); + } + result } - result - } + })) + .ok()? } fn check_keyword<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option { From 36dd49ae5f4fd0e0d54111533f1577fccfef979a Mon Sep 17 00:00:00 2001 From: topecongiro Date: Tue, 28 May 2019 00:14:36 +0900 Subject: [PATCH 3/3] Set macro_rewrite_failure to true on catching panic --- src/macros.rs | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index d25106c49616e..077e6a32d62c8 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -209,24 +209,25 @@ pub(crate) fn rewrite_macro( shape: Shape, position: MacroPosition, ) -> Option { - catch_unwind(AssertUnwindSafe(|| { - let should_skip = context - .skip_macro_names - .borrow() - .contains(&context.snippet(mac.node.path.span).to_owned()); - if should_skip { - 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() { + let should_skip = context + .skip_macro_names + .borrow() + .contains(&context.snippet(mac.node.path.span).to_owned()); + if should_skip { + None + } else { + let guard = InsideMacroGuard::inside_macro_context(context); + 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 } - result + Ok(rw) => rw, } - })) - .ok()? + } } fn check_keyword<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option {