mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-01 09:33:26 +00:00
Merge pull request #2571 from topecongiro/issue-2569
Avoid panicking on macro call with a single comma
This commit is contained in:
commit
72b715bad4
@ -58,8 +58,7 @@ pub enum MacroArg {
|
||||
Expr(ptr::P<ast::Expr>),
|
||||
Ty(ptr::P<ast::Ty>),
|
||||
Pat(ptr::P<ast::Pat>),
|
||||
// `parse_item` returns `Option<ptr::P<ast::Item>>`.
|
||||
Item(Option<ptr::P<ast::Item>>),
|
||||
Item(ptr::P<ast::Item>),
|
||||
}
|
||||
|
||||
impl Rewrite for ast::Item {
|
||||
@ -78,14 +77,14 @@ impl Rewrite for MacroArg {
|
||||
MacroArg::Expr(ref expr) => expr.rewrite(context, shape),
|
||||
MacroArg::Ty(ref ty) => ty.rewrite(context, shape),
|
||||
MacroArg::Pat(ref pat) => pat.rewrite(context, shape),
|
||||
MacroArg::Item(ref item) => item.as_ref().and_then(|item| item.rewrite(context, shape)),
|
||||
MacroArg::Item(ref item) => item.rewrite(context, shape),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_macro_arg(parser: &mut Parser) -> Option<MacroArg> {
|
||||
macro_rules! parse_macro_arg {
|
||||
($macro_arg:ident, $parser:ident) => {
|
||||
($macro_arg:ident, $parser:ident, $f:expr) => {
|
||||
let mut cloned_parser = (*parser).clone();
|
||||
match cloned_parser.$parser() {
|
||||
Ok(x) => {
|
||||
@ -94,7 +93,7 @@ fn parse_macro_arg(parser: &mut Parser) -> Option<MacroArg> {
|
||||
} else {
|
||||
// Parsing succeeded.
|
||||
*parser = cloned_parser;
|
||||
return Some(MacroArg::$macro_arg(x.clone()));
|
||||
return Some(MacroArg::$macro_arg($f(x)?));
|
||||
}
|
||||
}
|
||||
Err(mut e) => {
|
||||
@ -105,10 +104,11 @@ fn parse_macro_arg(parser: &mut Parser) -> Option<MacroArg> {
|
||||
};
|
||||
}
|
||||
|
||||
parse_macro_arg!(Expr, parse_expr);
|
||||
parse_macro_arg!(Ty, parse_ty);
|
||||
parse_macro_arg!(Pat, parse_pat);
|
||||
parse_macro_arg!(Item, parse_item);
|
||||
parse_macro_arg!(Expr, parse_expr, |x: ptr::P<ast::Expr>| Some(x));
|
||||
parse_macro_arg!(Ty, parse_ty, |x: ptr::P<ast::Ty>| Some(x));
|
||||
parse_macro_arg!(Pat, parse_pat, |x: ptr::P<ast::Pat>| Some(x));
|
||||
// `parse_item` returns `Option<ptr::P<ast::Item>>`.
|
||||
parse_macro_arg!(Item, parse_item, |x: Option<ptr::P<ast::Item>>| x);
|
||||
|
||||
None
|
||||
}
|
||||
|
@ -187,7 +187,7 @@ impl Spanned for MacroArg {
|
||||
MacroArg::Expr(ref expr) => expr.span(),
|
||||
MacroArg::Ty(ref ty) => ty.span(),
|
||||
MacroArg::Pat(ref pat) => pat.span(),
|
||||
MacroArg::Item(ref item) => item.as_ref().unwrap().span(),
|
||||
MacroArg::Item(ref item) => item.span(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,8 @@ peg_file! modname ("mygrammarfile.rustpeg");
|
||||
fn main() {
|
||||
foo! ( );
|
||||
|
||||
foo!(,);
|
||||
|
||||
bar!( a , b , c );
|
||||
|
||||
bar!( a , b , c , );
|
||||
|
@ -15,6 +15,8 @@ peg_file! modname("mygrammarfile.rustpeg");
|
||||
fn main() {
|
||||
foo!();
|
||||
|
||||
foo!(,);
|
||||
|
||||
bar!(a, b, c);
|
||||
|
||||
bar!(a, b, c,);
|
||||
|
Loading…
Reference in New Issue
Block a user