From d512240206f1f5bf7ce5edeffaa1728a381e2fbb Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Wed, 25 Jul 2018 07:13:51 +0900 Subject: [PATCH 1/3] Add tests for #2830 and #2857 --- tests/source/macros.rs | 33 +++++++++++++++++++++++++++++++++ tests/target/macros.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/tests/source/macros.rs b/tests/source/macros.rs index 10a207009b73..09affb8a33fe 100644 --- a/tests/source/macros.rs +++ b/tests/source/macros.rs @@ -393,3 +393,36 @@ macro_rules! bar { $m!([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]); }; } + +// #2830 +// Preserve trailing comma-less/ness inside nested macro. +named!( + do_parse_gsv, + map_res!( + do_parse!( + number_of_sentences: map_res!(digit, parse_num::) + >> char!(',') + >> sentence_index: map_res!(digit, parse_num::) + >> char!(',') + >> total_number_of_sats: map_res!(digit, parse_num::) + >> char!(',') + >> sat0: opt!(complete!(parse_gsv_sat_info)) + >> sat1: opt!(complete!(parse_gsv_sat_info)) + >> sat2: opt!(complete!(parse_gsv_sat_info)) + >> sat3: opt!(complete!(parse_gsv_sat_info)) + >> ( + number_of_sentences, + sentence_index, + total_number_of_sats, + sat0, + sat1, + sat2, + sat3 + ) + ), + construct_gsv_data + ) +); + +// #2857 +convert_args!(vec!(1, 2, 3)); diff --git a/tests/target/macros.rs b/tests/target/macros.rs index ac076615f99a..26ccdd9a1b52 100644 --- a/tests/target/macros.rs +++ b/tests/target/macros.rs @@ -972,3 +972,36 @@ macro_rules! bar { $m!([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]); }; } + +// #2830 +// Preserve trailing comma-less/ness inside nested macro. +named!( + do_parse_gsv, + map_res!( + do_parse!( + number_of_sentences: map_res!(digit, parse_num::) + >> char!(',') + >> sentence_index: map_res!(digit, parse_num::) + >> char!(',') + >> total_number_of_sats: map_res!(digit, parse_num::) + >> char!(',') + >> sat0: opt!(complete!(parse_gsv_sat_info)) + >> sat1: opt!(complete!(parse_gsv_sat_info)) + >> sat2: opt!(complete!(parse_gsv_sat_info)) + >> sat3: opt!(complete!(parse_gsv_sat_info)) + >> ( + number_of_sentences, + sentence_index, + total_number_of_sats, + sat0, + sat1, + sat2, + sat3 + ) + ), + construct_gsv_data + ) +); + +// #2857 +convert_args!(vec!(1, 2, 3)); From 975b3753ba9ef60889daa654b0f96db85be30e2a Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Wed, 25 Jul 2018 07:15:33 +0900 Subject: [PATCH 2/3] Keep the inside macro context in nested macro call --- src/macros.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index b55375b949c1..c97ef18d65e9 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -142,6 +142,24 @@ fn return_original_snippet_with_failure_marked( Some(context.snippet(span).to_owned()) } +struct InsideMacroGuard<'a> { + context: &'a RewriteContext<'a>, + is_nested: bool, +} + +impl<'a> InsideMacroGuard<'a> { + fn inside_macro_context(context: &'a RewriteContext) -> InsideMacroGuard<'a> { + let is_nested = context.inside_macro.replace(true); + InsideMacroGuard { context, is_nested } + } +} + +impl<'a> Drop for InsideMacroGuard<'a> { + fn drop(&mut self) { + self.context.inside_macro.replace(self.is_nested); + } +} + pub fn rewrite_macro( mac: &ast::Mac, extra_ident: Option, @@ -149,12 +167,11 @@ pub fn rewrite_macro( shape: Shape, position: MacroPosition, ) -> Option { - context.inside_macro.replace(true); + let guard = InsideMacroGuard::inside_macro_context(context); let result = rewrite_macro_inner(mac, extra_ident, context, shape, position); if result.is_none() { context.macro_rewrite_failure.replace(true); } - context.inside_macro.replace(false); result } From 339fa20973c8dabe35a8c2ce65b2e1ee47e7d478 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Wed, 25 Jul 2018 07:16:43 +0900 Subject: [PATCH 3/3] Veto converting delimiters inside nested macro --- src/macros.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index c97ef18d65e9..f00bb339cb09 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -168,7 +168,7 @@ pub fn rewrite_macro( position: MacroPosition, ) -> Option { let guard = InsideMacroGuard::inside_macro_context(context); - let result = rewrite_macro_inner(mac, extra_ident, context, shape, position); + let result = rewrite_macro_inner(mac, extra_ident, context, shape, position, guard.is_nested); if result.is_none() { context.macro_rewrite_failure.replace(true); } @@ -181,6 +181,7 @@ pub fn rewrite_macro_inner( context: &RewriteContext, shape: Shape, position: MacroPosition, + is_nested_macro: bool, ) -> Option { if context.config.use_try_shorthand() { if let Some(expr) = convert_try_mac(mac, context) { @@ -193,7 +194,7 @@ pub fn rewrite_macro_inner( let macro_name = rewrite_macro_name(context, &mac.node.path, extra_ident); - let style = if FORCED_BRACKET_MACROS.contains(&¯o_name[..]) { + let style = if FORCED_BRACKET_MACROS.contains(&¯o_name[..]) && !is_nested_macro { DelimToken::Bracket } else { original_style @@ -326,7 +327,7 @@ pub fn rewrite_macro_inner( } else { Some(SeparatorTactic::Never) }; - if FORCED_BRACKET_MACROS.contains(macro_name) { + if FORCED_BRACKET_MACROS.contains(macro_name) && !is_nested_macro { context.inside_macro.replace(false); if context.use_block_indent() { force_trailing_comma = Some(SeparatorTactic::Vertical);