Skip to content

Commit 72b715b

Browse files
authored
Merge pull request #2571 from topecongiro/issue-2569
Avoid panicking on macro call with a single comma
2 parents 1644b17 + a49e00b commit 72b715b

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

src/macros.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ pub enum MacroArg {
5858
Expr(ptr::P<ast::Expr>),
5959
Ty(ptr::P<ast::Ty>),
6060
Pat(ptr::P<ast::Pat>),
61-
// `parse_item` returns `Option<ptr::P<ast::Item>>`.
62-
Item(Option<ptr::P<ast::Item>>),
61+
Item(ptr::P<ast::Item>),
6362
}
6463

6564
impl Rewrite for ast::Item {
@@ -78,14 +77,14 @@ impl Rewrite for MacroArg {
7877
MacroArg::Expr(ref expr) => expr.rewrite(context, shape),
7978
MacroArg::Ty(ref ty) => ty.rewrite(context, shape),
8079
MacroArg::Pat(ref pat) => pat.rewrite(context, shape),
81-
MacroArg::Item(ref item) => item.as_ref().and_then(|item| item.rewrite(context, shape)),
80+
MacroArg::Item(ref item) => item.rewrite(context, shape),
8281
}
8382
}
8483
}
8584

8685
fn parse_macro_arg(parser: &mut Parser) -> Option<MacroArg> {
8786
macro_rules! parse_macro_arg {
88-
($macro_arg:ident, $parser:ident) => {
87+
($macro_arg:ident, $parser:ident, $f:expr) => {
8988
let mut cloned_parser = (*parser).clone();
9089
match cloned_parser.$parser() {
9190
Ok(x) => {
@@ -94,7 +93,7 @@ fn parse_macro_arg(parser: &mut Parser) -> Option<MacroArg> {
9493
} else {
9594
// Parsing succeeded.
9695
*parser = cloned_parser;
97-
return Some(MacroArg::$macro_arg(x.clone()));
96+
return Some(MacroArg::$macro_arg($f(x)?));
9897
}
9998
}
10099
Err(mut e) => {
@@ -105,10 +104,11 @@ fn parse_macro_arg(parser: &mut Parser) -> Option<MacroArg> {
105104
};
106105
}
107106

108-
parse_macro_arg!(Expr, parse_expr);
109-
parse_macro_arg!(Ty, parse_ty);
110-
parse_macro_arg!(Pat, parse_pat);
111-
parse_macro_arg!(Item, parse_item);
107+
parse_macro_arg!(Expr, parse_expr, |x: ptr::P<ast::Expr>| Some(x));
108+
parse_macro_arg!(Ty, parse_ty, |x: ptr::P<ast::Ty>| Some(x));
109+
parse_macro_arg!(Pat, parse_pat, |x: ptr::P<ast::Pat>| Some(x));
110+
// `parse_item` returns `Option<ptr::P<ast::Item>>`.
111+
parse_macro_arg!(Item, parse_item, |x: Option<ptr::P<ast::Item>>| x);
112112

113113
None
114114
}

src/spanned.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl Spanned for MacroArg {
187187
MacroArg::Expr(ref expr) => expr.span(),
188188
MacroArg::Ty(ref ty) => ty.span(),
189189
MacroArg::Pat(ref pat) => pat.span(),
190-
MacroArg::Item(ref item) => item.as_ref().unwrap().span(),
190+
MacroArg::Item(ref item) => item.span(),
191191
}
192192
}
193193
}

tests/source/macros.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ peg_file! modname ("mygrammarfile.rustpeg");
1010
fn main() {
1111
foo! ( );
1212

13+
foo!(,);
14+
1315
bar!( a , b , c );
1416

1517
bar!( a , b , c , );

tests/target/macros.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ peg_file! modname("mygrammarfile.rustpeg");
1515
fn main() {
1616
foo!();
1717

18+
foo!(,);
19+
1820
bar!(a, b, c);
1921

2022
bar!(a, b, c,);

0 commit comments

Comments
 (0)