Skip to content

Commit 6377c52

Browse files
committed
Fix comment handling in macros
1 parent 70e7716 commit 6377c52

File tree

3 files changed

+88
-24
lines changed

3 files changed

+88
-24
lines changed

src/macros.rs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,6 @@ pub fn rewrite_macro_def(
311311
let multi_branch_style = def.legacy || parsed_def.branches.len() != 1;
312312

313313
let mac_indent = if multi_branch_style {
314-
result += " {";
315314
indent.block_indent(context.config)
316315
} else {
317316
indent
@@ -322,28 +321,25 @@ pub fn rewrite_macro_def(
322321
let branch_items = itemize_list(
323322
context.codemap,
324323
parsed_def.branches.iter(),
325-
"",
326-
"",
327-
|branch| branch.args_span.lo(),
328-
|branch| branch.body.hi(),
324+
"}",
325+
";",
326+
|branch| {
327+
branch.span.lo()
328+
},
329+
|branch| {
330+
branch.span.hi()
331+
},
329332
|branch| {
330-
let mut result = String::new();
331-
332333
// Only attempt to format function-like macros.
333334
if branch.args_paren_kind != DelimToken::Paren {
334335
// FIXME(#1539): implement for non-sugared macros.
335336
return None;
336337
}
337338

338-
let args = format_macro_args(branch.args.clone())?;
339+
let mut result = format_macro_args(branch.args.clone())?;
339340

340341
if multi_branch_style {
341-
result += "\n";
342-
result += &mac_indent_str;
343-
result += &args;
344342
result += " =>";
345-
} else {
346-
result += &args;
347343
}
348344

349345
// The macro body is the most interesting part. It might end up as various
@@ -418,13 +414,14 @@ pub fn rewrite_macro_def(
418414
}
419415

420416
result += "}";
417+
421418
if def.legacy {
422419
result += ";";
423420
}
424-
result += "\n";
421+
425422
Some(result)
426423
},
427-
span.lo(),
424+
context.codemap.span_after(span, "{"),
428425
span.hi(),
429426
false
430427
).collect::<Vec<_>>();
@@ -439,14 +436,20 @@ pub fn rewrite_macro_def(
439436
trailing_separator: SeparatorTactic::Never,
440437
separator_place: SeparatorPlace::Back,
441438
shape: arm_shape,
442-
ends_with_newline: false,
439+
ends_with_newline: true,
443440
preserve_newline: true,
444441
config: context.config,
445442
};
446443

444+
if multi_branch_style {
445+
result += " {\n";
446+
result += &mac_indent_str;
447+
}
448+
447449
result += write_list(&branch_items, &fmt)?.as_str();
448450

449451
if multi_branch_style {
452+
result += "\n";
450453
result += &indent.to_string(context.config);
451454
result += "}";
452455
}
@@ -792,28 +795,29 @@ impl MacroParser {
792795
// `(` ... `)` `=>` `{` ... `}`
793796
fn parse_branch(&mut self) -> Option<MacroBranch> {
794797
let tok = self.toks.next()?;
795-
let (args_span, args_paren_kind) = match tok {
798+
let (lo, args_paren_kind) = match tok {
796799
TokenTree::Token(..) => return None,
797-
TokenTree::Delimited(sp, ref d) => (sp, d.delim),
800+
TokenTree::Delimited(sp, ref d) => (sp.lo(), d.delim),
798801
};
799802
let args = tok.joint().into();
800803
match self.toks.next()? {
801804
TokenTree::Token(_, Token::FatArrow) => {}
802805
_ => return None,
803806
}
804-
let body = match self.toks.next()? {
807+
let (mut hi, body) = match self.toks.next()? {
805808
TokenTree::Token(..) => return None,
806809
TokenTree::Delimited(sp, _) => {
807810
let data = sp.data();
808-
Span::new(data.lo + BytePos(1), data.hi - BytePos(1), data.ctxt)
811+
(data.hi, Span::new(data.lo + BytePos(1), data.hi - BytePos(1), data.ctxt))
809812
}
810813
};
811-
if let Some(TokenTree::Token(_, Token::Semi)) = self.toks.look_ahead(0) {
814+
if let Some(TokenTree::Token(sp, Token::Semi)) = self.toks.look_ahead(0) {
812815
self.toks.next();
816+
hi = sp.hi();
813817
}
814818
Some(MacroBranch {
819+
span: mk_sp(lo, hi),
815820
args_paren_kind,
816-
args_span,
817821
args,
818822
body,
819823
})
@@ -828,8 +832,8 @@ struct Macro {
828832
// FIXME: it would be more efficient to use references to the token streams
829833
// rather than clone them, if we can make the borrowing work out.
830834
struct MacroBranch {
835+
span: Span,
831836
args_paren_kind: DelimToken,
832-
args_span: Span,
833837
args: ThinTokenStream,
834838
body: Span,
835839
}

tests/source/macro_rules.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,39 @@ macro_rules! m {
1313

1414
() => {/* c */};
1515

16+
(@tag) =>
17+
{
18+
19+
};
20+
1621
// d
1722
( $item:ident ) => {
1823
mod macro_item { struct $item ; }
1924
};
2025
}
26+
27+
macro m2 {
28+
// a
29+
($expr :expr, $( $func : ident ) * ) => {
30+
{
31+
let x = $expr;
32+
$func (
33+
x
34+
)
35+
}
36+
}
37+
38+
/* b */
39+
40+
() => {/* c */}
41+
42+
(@tag) =>
43+
{
44+
45+
}
46+
47+
// d
48+
( $item:ident ) => {
49+
mod macro_item { struct $item ; }
50+
}
51+
}

tests/target/macro_rules.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,43 @@
11
macro_rules! m {
2+
// a
23
($expr: expr, $($func: ident)*) => {{
34
let x = $expr;
45
$func(x)
56
}};
67

7-
() => {};
8+
/* b */
9+
() => {
10+
/* c */
11+
};
12+
13+
(@tag) => {};
814

15+
// d
916
($item: ident) => {
1017
mod macro_item {
1118
struct $item;
1219
}
1320
};
1421
}
22+
23+
macro m2 {
24+
// a
25+
($expr: expr, $($func: ident)*) => {{
26+
let x = $expr;
27+
$func(x)
28+
}}
29+
30+
/* b */
31+
() => {
32+
/* c */
33+
}
34+
35+
(@tag) => {}
36+
37+
// d
38+
($item: ident) => {
39+
mod macro_item {
40+
struct $item;
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)