Skip to content

Commit

Permalink
try assuming macros are fn call-like, emit warning if not
Browse files Browse the repository at this point in the history
  • Loading branch information
alecmocatta committed Oct 15, 2019
1 parent 9b4a894 commit 10aa536
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
21 changes: 20 additions & 1 deletion serde_closure_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//! documentation.
#![doc(html_root_url = "https://docs.rs/serde_closure_derive/0.2.2")]
#![feature(proc_macro_diagnostic)]
#![allow(non_snake_case)] // due to proc-macro-hack can't apply this directly

extern crate proc_macro;
Expand All @@ -19,7 +20,7 @@ use proc_macro_hack::proc_macro_hack;
use quote::{quote, ToTokens};
use std::{collections::HashSet, iter, iter::successors, str};
use syn::{
parse::{Parse, ParseStream}, parse2, token::Bracket, Arm, Block, Error, Expr, ExprArray, ExprAssign, ExprAssignOp, ExprAsync, ExprAwait, ExprBinary, ExprBlock, ExprBox, ExprBreak, ExprCall, ExprCast, ExprClosure, ExprField, ExprForLoop, ExprGroup, ExprIf, ExprIndex, ExprLet, ExprLoop, ExprMatch, ExprMethodCall, ExprParen, ExprPath, ExprRange, ExprReference, ExprRepeat, ExprReturn, ExprStruct, ExprTry, ExprTryBlock, ExprTuple, ExprType, ExprUnary, ExprUnsafe, ExprWhile, ExprYield, FieldValue, Ident, Local, Member, Pat, PatBox, PatIdent, PatReference, PatSlice, PatTuple, PatTupleStruct, PatType, Path, PathArguments, PathSegment, Stmt, Type, TypeInfer, TypeReference, UnOp
parse::{Parse, ParseStream}, parse2, spanned::Spanned, token::Bracket, Arm, Block, Error, Expr, ExprArray, ExprAssign, ExprAssignOp, ExprAsync, ExprAwait, ExprBinary, ExprBlock, ExprBox, ExprBreak, ExprCall, ExprCast, ExprClosure, ExprField, ExprForLoop, ExprGroup, ExprIf, ExprIndex, ExprLet, ExprLoop, ExprMacro, ExprMatch, ExprMethodCall, ExprParen, ExprPath, ExprRange, ExprReference, ExprRepeat, ExprReturn, ExprStruct, ExprTry, ExprTryBlock, ExprTuple, ExprType, ExprUnary, ExprUnsafe, ExprWhile, ExprYield, FieldValue, Ident, Local, Member, Pat, PatBox, PatIdent, PatReference, PatSlice, PatTuple, PatTupleStruct, PatType, Path, PathArguments, PathSegment, Stmt, Type, TypeInfer, TypeReference, UnOp
};

#[proc_macro_hack]
Expand Down Expand Up @@ -730,6 +731,24 @@ impl<'a> State<'a> {
state.expr(cond);
state.block(&mut body.stmts);
}
Expr::Macro(ExprMacro { ref mut mac, .. }) => {
let tokens = &mac.tokens;
if let Ok(expr) = parse2::<ExprCall>(quote! { self::abc(#tokens) }) {
let mut expr = Expr::Call(expr);
self.expr(&mut expr);
let expr = if let Expr::Call(expr) = expr {
expr
} else {
unreachable!()
};
mac.tokens = expr.args.to_token_stream();
} else {
mac.span()
.unwrap()
.warning("See https://github.com/alecmocatta/serde_closure/issues/16")
.emit()
}
}
_ => (),
}
}
Expand Down
7 changes: 0 additions & 7 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ fn fnonce() {
a += *e;
*h += (b.clone() + f.as_str() + g.as_str()).as_str();
b += h.as_str();
let (a, b) = (&a, &b);
format!("{}{}{}{}{}{}{}{}", a, b, c, d, e, f, g, h)
}
);
Expand Down Expand Up @@ -159,7 +158,6 @@ fn fnmut() {
// *a += *e;
*h += (b.clone() + f.as_str() + g.as_str()).as_str();
// *b += h.as_str();
let (a, b) = (&a, &b);
format!("{}{}{}{}{}{}{}{}", a, b, c, d, e, f, g, h)
})(
0,
Expand All @@ -180,7 +178,6 @@ fn fnmut() {
a += *e;
*h += (b.clone() + f.as_str() + g.as_str()).as_str();
b += h.as_str();
let (a, b) = (&a, &b);
format!("{}{}{}{}{}{}{}{}", a, b, c, d, e, f, g, h)
}
);
Expand All @@ -201,7 +198,6 @@ fn fnmut() {
let _ = unfold(0_usize, FnMut!(|acc: &mut _| Some(*acc)));
let x = 123_usize;
let c = FnMut!(move |arg: String| {
let x = &x;
println!("{} {}", x, arg);
});
let _ = (c, c);
Expand Down Expand Up @@ -308,7 +304,6 @@ fn fnref() {
// *a += *e;
*h += (b.clone() + f.as_str() + g.as_str()).as_str();
// *b += h.as_str();
let (a, b) = (&a, &b);
format!("{}{}{}{}{}{}{}{}", a, b, c, d, e, f, g, h)
})(
0,
Expand Down Expand Up @@ -336,7 +331,6 @@ fn fnref() {
// *a += *e;
*h += (b.clone() + f.as_str() + g.as_str()).as_str();
// *b += h.as_str();
let (a, b) = (&a, &b);
format!("{}{}{}{}{}{}{}{}", a, b, c, d, e, f, g, h)
});
test(x);
Expand All @@ -356,7 +350,6 @@ fn fnref() {
let _ = unfold(0_usize, Fn!(|acc: &mut _| Some(*acc)));
let x = 123_usize;
let c = Fn!(move |arg: String| {
let x = &x;
println!("{} {}", x, arg);
});
let _ = (c, c);
Expand Down

0 comments on commit 10aa536

Please sign in to comment.