Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile error if function args are mut #96

Open
jherman3 opened this issue Dec 8, 2021 · 2 comments
Open

Compile error if function args are mut #96

jherman3 opened this issue Dec 8, 2021 · 2 comments

Comments

@jherman3
Copy link

jherman3 commented Dec 8, 2021

This is a really cool macro!
I noticed that it doesn't work if we declare mut function args:

use cached::proc_macro::cached;

#[cached]
fn foo(mut x: i32) -> i32 {
    x += 1;
    x
}

This gives:

error: expected expression, found keyword `mut`
 --> src/bin/foo.rs:4:8
  |
4 | fn foo(mut x: i32) -> i32 {
  |        ^^^ expected expression

This works:

use cached::proc_macro::cached;

#[cached]
fn foo(x: i32) -> i32 {
    let mut x = x;
    x += 1;
    x
}

It's easy to work around but it feels like a parsing bug that the former doesn't work.

@jaemk
Copy link
Owner

jaemk commented Dec 8, 2021

yeah, I'm going to guess it's this line being naive

FnArg::Typed(pat_type) => pat_type.pat.clone(),

@jaemk
Copy link
Owner

jaemk commented Jan 12, 2022

This should be fixed now, released in 0.29.0

cached/tests/cached.rs

Lines 1144 to 1155 in e24d7a7

#[cached]
fn mutable_args(mut a: i32, mut b: i32) -> (i32, i32) {
a += 1;
b += 1;
(a, b)
}
#[test]
fn test_mutable_args() {
assert_eq!((2, 2), mutable_args(1, 1));
assert_eq!((2, 2), mutable_args(1, 1));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants