Skip to content

Commit e3fea15

Browse files
authored
Merge pull request #2834 from topecongiro/issue-2830
Keep the context that we are inside macro in nested macro
2 parents 2f03180 + 339fa20 commit e3fea15

File tree

3 files changed

+89
-5
lines changed

3 files changed

+89
-5
lines changed

src/macros.rs

+23-5
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,36 @@ fn return_original_snippet_with_failure_marked(
142142
Some(context.snippet(span).to_owned())
143143
}
144144

145+
struct InsideMacroGuard<'a> {
146+
context: &'a RewriteContext<'a>,
147+
is_nested: bool,
148+
}
149+
150+
impl<'a> InsideMacroGuard<'a> {
151+
fn inside_macro_context(context: &'a RewriteContext) -> InsideMacroGuard<'a> {
152+
let is_nested = context.inside_macro.replace(true);
153+
InsideMacroGuard { context, is_nested }
154+
}
155+
}
156+
157+
impl<'a> Drop for InsideMacroGuard<'a> {
158+
fn drop(&mut self) {
159+
self.context.inside_macro.replace(self.is_nested);
160+
}
161+
}
162+
145163
pub fn rewrite_macro(
146164
mac: &ast::Mac,
147165
extra_ident: Option<ast::Ident>,
148166
context: &RewriteContext,
149167
shape: Shape,
150168
position: MacroPosition,
151169
) -> Option<String> {
152-
context.inside_macro.replace(true);
153-
let result = rewrite_macro_inner(mac, extra_ident, context, shape, position);
170+
let guard = InsideMacroGuard::inside_macro_context(context);
171+
let result = rewrite_macro_inner(mac, extra_ident, context, shape, position, guard.is_nested);
154172
if result.is_none() {
155173
context.macro_rewrite_failure.replace(true);
156174
}
157-
context.inside_macro.replace(false);
158175
result
159176
}
160177

@@ -164,6 +181,7 @@ pub fn rewrite_macro_inner(
164181
context: &RewriteContext,
165182
shape: Shape,
166183
position: MacroPosition,
184+
is_nested_macro: bool,
167185
) -> Option<String> {
168186
if context.config.use_try_shorthand() {
169187
if let Some(expr) = convert_try_mac(mac, context) {
@@ -176,7 +194,7 @@ pub fn rewrite_macro_inner(
176194

177195
let macro_name = rewrite_macro_name(context, &mac.node.path, extra_ident);
178196

179-
let style = if FORCED_BRACKET_MACROS.contains(&&macro_name[..]) {
197+
let style = if FORCED_BRACKET_MACROS.contains(&&macro_name[..]) && !is_nested_macro {
180198
DelimToken::Bracket
181199
} else {
182200
original_style
@@ -309,7 +327,7 @@ pub fn rewrite_macro_inner(
309327
} else {
310328
Some(SeparatorTactic::Never)
311329
};
312-
if FORCED_BRACKET_MACROS.contains(macro_name) {
330+
if FORCED_BRACKET_MACROS.contains(macro_name) && !is_nested_macro {
313331
context.inside_macro.replace(false);
314332
if context.use_block_indent() {
315333
force_trailing_comma = Some(SeparatorTactic::Vertical);

tests/source/macros.rs

+33
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,36 @@ macro_rules! bar {
393393
$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]);
394394
};
395395
}
396+
397+
// #2830
398+
// Preserve trailing comma-less/ness inside nested macro.
399+
named!(
400+
do_parse_gsv<GsvData>,
401+
map_res!(
402+
do_parse!(
403+
number_of_sentences: map_res!(digit, parse_num::<u16>)
404+
>> char!(',')
405+
>> sentence_index: map_res!(digit, parse_num::<u16>)
406+
>> char!(',')
407+
>> total_number_of_sats: map_res!(digit, parse_num::<u16>)
408+
>> char!(',')
409+
>> sat0: opt!(complete!(parse_gsv_sat_info))
410+
>> sat1: opt!(complete!(parse_gsv_sat_info))
411+
>> sat2: opt!(complete!(parse_gsv_sat_info))
412+
>> sat3: opt!(complete!(parse_gsv_sat_info))
413+
>> (
414+
number_of_sentences,
415+
sentence_index,
416+
total_number_of_sats,
417+
sat0,
418+
sat1,
419+
sat2,
420+
sat3
421+
)
422+
),
423+
construct_gsv_data
424+
)
425+
);
426+
427+
// #2857
428+
convert_args!(vec!(1, 2, 3));

tests/target/macros.rs

+33
Original file line numberDiff line numberDiff line change
@@ -972,3 +972,36 @@ macro_rules! bar {
972972
$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]);
973973
};
974974
}
975+
976+
// #2830
977+
// Preserve trailing comma-less/ness inside nested macro.
978+
named!(
979+
do_parse_gsv<GsvData>,
980+
map_res!(
981+
do_parse!(
982+
number_of_sentences: map_res!(digit, parse_num::<u16>)
983+
>> char!(',')
984+
>> sentence_index: map_res!(digit, parse_num::<u16>)
985+
>> char!(',')
986+
>> total_number_of_sats: map_res!(digit, parse_num::<u16>)
987+
>> char!(',')
988+
>> sat0: opt!(complete!(parse_gsv_sat_info))
989+
>> sat1: opt!(complete!(parse_gsv_sat_info))
990+
>> sat2: opt!(complete!(parse_gsv_sat_info))
991+
>> sat3: opt!(complete!(parse_gsv_sat_info))
992+
>> (
993+
number_of_sentences,
994+
sentence_index,
995+
total_number_of_sats,
996+
sat0,
997+
sat1,
998+
sat2,
999+
sat3
1000+
)
1001+
),
1002+
construct_gsv_data
1003+
)
1004+
);
1005+
1006+
// #2857
1007+
convert_args!(vec!(1, 2, 3));

0 commit comments

Comments
 (0)