Hello,
I wrote an attribute macro that essentially modifies a regular Rust function into one that can be called by Java/Kotlin using extern/native methods. The user writes the function like regular Rust, but the argument and return types are Java types or Rust primitives. The macro then converts these Java types to JObject.
Here is an example:
#[jni_fn("Test")]
pub fn test_jni_fn_2<'local>(obj: java.lang.Object) -> i32 {
3
}
The problem is that I get the following errors:
error: expected one of `:`, `@`, or `|`, found `.`
--> src/lib.rs:13:44
|
13 | pub fn test_jni_fn_2<'local>(obj: java.lang.Object) -> i32 {
| ^ expected one of `:`, `@`, or `|`
error: expected one of `!`, `(`, `)`, `+`, `,`, `::`, or `<`, found `.`
--> src/lib.rs:13:39
|
13 | pub fn test_jni_fn_2<'local>(obj: java.lang.Object) -> i32 {
| ^
| |
| expected one of 7 possible tokens
| help: missing `,`
Initially I thought that the macro was outputting both the input and the intended output,
but then I use cargo expand
and I get this.
#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
use ez_jni::{jni_fn, package};
#[no_mangle]
pub extern "system" fn Java_me_test_Test_test_1jni_1fn_12<'local>(
mut _env: ::jni::JNIEnv<'local>,
_class: ::jni::objects::JClass<'local>,
obj: ::jni::objects::JObject<'local>,
) -> i32 {
::ez_jni::__throw::catch_throw(&mut _env, move |env| { 3 })
}
If I use this instead, it will compile with no errors, so the macro is working fine.
Why does this happen? Is it because the compiler parses the function before the macro is expanded?