Skip to content

Commit a49e00b

Browse files
committed
Avoid panicking on macro call with a single comma
`parse_item` from libsyntax may return `None`, so we need to discard the result in that case.
1 parent 0f55350 commit a49e00b

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
@@ -76,8 +76,7 @@ pub enum MacroArg {
7676
Expr(ptr::P<ast::Expr>),
7777
Ty(ptr::P<ast::Ty>),
7878
Pat(ptr::P<ast::Pat>),
79-
// `parse_item` returns `Option<ptr::P<ast::Item>>`.
80-
Item(Option<ptr::P<ast::Item>>),
79+
Item(ptr::P<ast::Item>),
8180
}
8281

8382
impl Rewrite for ast::Item {
@@ -96,14 +95,14 @@ impl Rewrite for MacroArg {
9695
MacroArg::Expr(ref expr) => expr.rewrite(context, shape),
9796
MacroArg::Ty(ref ty) => ty.rewrite(context, shape),
9897
MacroArg::Pat(ref pat) => pat.rewrite(context, shape),
99-
MacroArg::Item(ref item) => item.as_ref().and_then(|item| item.rewrite(context, shape)),
98+
MacroArg::Item(ref item) => item.rewrite(context, shape),
10099
}
101100
}
102101
}
103102

104103
fn parse_macro_arg(parser: &mut Parser) -> Option<MacroArg> {
105104
macro_rules! parse_macro_arg {
106-
($macro_arg:ident, $parser:ident) => {
105+
($macro_arg:ident, $parser:ident, $f:expr) => {
107106
let mut cloned_parser = (*parser).clone();
108107
match cloned_parser.$parser() {
109108
Ok(x) => {
@@ -112,7 +111,7 @@ fn parse_macro_arg(parser: &mut Parser) -> Option<MacroArg> {
112111
} else {
113112
// Parsing succeeded.
114113
*parser = cloned_parser;
115-
return Some(MacroArg::$macro_arg(x.clone()));
114+
return Some(MacroArg::$macro_arg($f(x)?));
116115
}
117116
}
118117
Err(mut e) => {
@@ -123,10 +122,11 @@ fn parse_macro_arg(parser: &mut Parser) -> Option<MacroArg> {
123122
};
124123
}
125124

126-
parse_macro_arg!(Expr, parse_expr);
127-
parse_macro_arg!(Ty, parse_ty);
128-
parse_macro_arg!(Pat, parse_pat);
129-
parse_macro_arg!(Item, parse_item);
125+
parse_macro_arg!(Expr, parse_expr, |x: ptr::P<ast::Expr>| Some(x));
126+
parse_macro_arg!(Ty, parse_ty, |x: ptr::P<ast::Ty>| Some(x));
127+
parse_macro_arg!(Pat, parse_pat, |x: ptr::P<ast::Pat>| Some(x));
128+
// `parse_item` returns `Option<ptr::P<ast::Item>>`.
129+
parse_macro_arg!(Item, parse_item, |x: Option<ptr::P<ast::Item>>| x);
130130

131131
None
132132
}

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)