Skip to content

Commit 47a572a

Browse files
committed
Fix comment handling in macros
1 parent 36685f2 commit 47a572a

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
@@ -415,13 +411,14 @@ pub fn rewrite_macro_def(
415411
}
416412

417413
result += "}";
414+
418415
if def.legacy {
419416
result += ";";
420417
}
421-
result += "\n";
418+
422419
Some(result)
423420
},
424-
span.lo(),
421+
context.codemap.span_after(span, "{"),
425422
span.hi(),
426423
false
427424
).collect::<Vec<_>>();
@@ -436,14 +433,20 @@ pub fn rewrite_macro_def(
436433
trailing_separator: SeparatorTactic::Never,
437434
separator_place: SeparatorPlace::Back,
438435
shape: arm_shape,
439-
ends_with_newline: false,
436+
ends_with_newline: true,
440437
preserve_newline: true,
441438
config: context.config,
442439
};
443440

441+
if multi_branch_style {
442+
result += " {\n";
443+
result += &mac_indent_str;
444+
}
445+
444446
result += write_list(&branch_items, &fmt)?.as_str();
445447

446448
if multi_branch_style {
449+
result += "\n";
447450
result += &indent.to_string(context.config);
448451
result += "}";
449452
}
@@ -786,28 +789,29 @@ impl MacroParser {
786789
// `(` ... `)` `=>` `{` ... `}`
787790
fn parse_branch(&mut self) -> Option<MacroBranch> {
788791
let tok = self.toks.next()?;
789-
let (args_span, args_paren_kind) = match tok {
792+
let (lo, args_paren_kind) = match tok {
790793
TokenTree::Token(..) => return None,
791-
TokenTree::Delimited(sp, ref d) => (sp, d.delim),
794+
TokenTree::Delimited(sp, ref d) => (sp.lo(), d.delim),
792795
};
793796
let args = tok.joint().into();
794797
match self.toks.next()? {
795798
TokenTree::Token(_, Token::FatArrow) => {}
796799
_ => return None,
797800
}
798-
let body = match self.toks.next()? {
801+
let (mut hi, body) = match self.toks.next()? {
799802
TokenTree::Token(..) => return None,
800803
TokenTree::Delimited(sp, _) => {
801804
let data = sp.data();
802-
Span::new(data.lo + BytePos(1), data.hi - BytePos(1), data.ctxt)
805+
(data.hi, Span::new(data.lo + BytePos(1), data.hi - BytePos(1), data.ctxt))
803806
}
804807
};
805-
if let Some(TokenTree::Token(_, Token::Semi)) = self.toks.look_ahead(0) {
808+
if let Some(TokenTree::Token(sp, Token::Semi)) = self.toks.look_ahead(0) {
806809
self.toks.next();
810+
hi = sp.hi();
807811
}
808812
Some(MacroBranch {
813+
span: mk_sp(lo, hi),
809814
args_paren_kind,
810-
args_span,
811815
args,
812816
body,
813817
})
@@ -822,8 +826,8 @@ struct Macro {
822826
// FIXME: it would be more efficient to use references to the token streams
823827
// rather than clone them, if we can make the borrowing work out.
824828
struct MacroBranch {
829+
span: Span,
825830
args_paren_kind: DelimToken,
826-
args_span: Span,
827831
args: ThinTokenStream,
828832
body: Span,
829833
}

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)