Skip to content

Commit 833b021

Browse files
authored
Attempt to handle references from macro_rules #30
1 parent 41aae56 commit 833b021

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/expand.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,18 @@ fn transform_sig(sig: &mut Signature, args: &RecursionArgs) {
6161

6262
for arg in &mut sig.inputs {
6363
if let FnArg::Typed(pt) = arg {
64-
if let Type::Reference(tr) = pt.ty.as_mut() {
65-
ref_arguments.push(tr);
64+
match pt.ty.as_mut() {
65+
// rustc can give us a None-delimited group if this type comes from
66+
// a macro_rules macro. I don't this can happen for code the user has written.
67+
Type::Group(tg) => {
68+
if let Type::Reference(tr) = &mut *tg.elem {
69+
ref_arguments.push(tr);
70+
}
71+
}
72+
Type::Reference(tr) => {
73+
ref_arguments.push(tr);
74+
}
75+
_ => {}
6676
}
6777
} else if let FnArg::Receiver(recv) = arg {
6878
if let Some((_, slt)) = &mut recv.reference {

tests/macros_all_the_way_down.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use futures_executor::block_on;
2+
3+
macro_rules! recurse {
4+
($name:ident, $param:ty) => {
5+
#[::async_recursion::async_recursion]
6+
async fn $name<F>(param: $param, f: &F)
7+
where
8+
F: Fn($param) + Sync + Send,
9+
{
10+
f(param);
11+
}
12+
};
13+
}
14+
15+
recurse!(owned, usize);
16+
recurse!(by_ref, &usize);
17+
recurse!(by_ref_mut, &mut usize);
18+
19+
#[test]
20+
fn async_in_macro() {
21+
block_on(async move {
22+
owned(5, &|_| ()).await;
23+
by_ref(&5, &|_| ()).await;
24+
by_ref_mut(&mut 5, &|_| ()).await;
25+
});
26+
}

0 commit comments

Comments
 (0)