Skip to content

Keep the context that we are inside macro in nested macro #2834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,36 @@ 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<ast::Ident>,
context: &RewriteContext,
shape: Shape,
position: MacroPosition,
) -> Option<String> {
context.inside_macro.replace(true);
let result = rewrite_macro_inner(mac, extra_ident, context, shape, position);
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);
}
context.inside_macro.replace(false);
result
}

Expand All @@ -164,6 +181,7 @@ pub fn rewrite_macro_inner(
context: &RewriteContext,
shape: Shape,
position: MacroPosition,
is_nested_macro: bool,
) -> Option<String> {
if context.config.use_try_shorthand() {
if let Some(expr) = convert_try_mac(mac, context) {
Expand All @@ -176,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(&&macro_name[..]) {
let style = if FORCED_BRACKET_MACROS.contains(&&macro_name[..]) && !is_nested_macro {
DelimToken::Bracket
} else {
original_style
Expand Down Expand Up @@ -309,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);
Expand Down
33 changes: 33 additions & 0 deletions tests/source/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<GsvData>,
map_res!(
do_parse!(
number_of_sentences: map_res!(digit, parse_num::<u16>)
>> char!(',')
>> sentence_index: map_res!(digit, parse_num::<u16>)
>> char!(',')
>> total_number_of_sats: map_res!(digit, parse_num::<u16>)
>> 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));
33 changes: 33 additions & 0 deletions tests/target/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<GsvData>,
map_res!(
do_parse!(
number_of_sentences: map_res!(digit, parse_num::<u16>)
>> char!(',')
>> sentence_index: map_res!(digit, parse_num::<u16>)
>> char!(',')
>> total_number_of_sats: map_res!(digit, parse_num::<u16>)
>> 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));