near / near-sdk-rs

Rust library for writing NEAR smart contracts
https://near-sdk.io
Apache License 2.0
470 stars 249 forks source link

`__abi-generate` issue for result of contract method with explicit lifetime parameter #1211

Open dj8yfo opened 5 months ago

dj8yfo commented 5 months ago

Originally highlighted by this test, but it was run under wrong feature flag abi instead of __abi-generate.

when doing cargo near build against this code

//! Method signature uses lifetime.

use near_sdk::near;

#[near(contract_state)]
#[derive(Default)]
struct Ident {
    value: u32,
}

#[near]
impl Ident {
    pub fn is_ident<'a>(&self, other: &'a u32) -> Option<&'a u32> {
        if *other == self.value {
            Some(other)
        } else {
            None
        }
    }
}
Cargo.toml

```toml [package] name = "test_contract" description = "cargo-near-new-project-description" version = "0.1.0" edition = "2021" # TODO: Fill out the repository field to help NEAR ecosystem tools to discover your project. # NEP-0330 is automatically implemented for all contracts built with https://github.com/near/cargo-near. # Link to the repository will be available via `contract_source_metadata` view-function. #repository = "https://github.com/xxx/xxx" [lib] crate-type = ["cdylib", "rlib"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] near-sdk = { version = "5.1.0", features = ["unit-testing"], git = "https://github.com/near/near-sdk-rs.git", rev = "c843e9aec747b9acc7e240a6dea80a14448a7bfd" } [dev-dependencies] near-sdk = { version = "5.1.0", features = ["unit-testing"], git = "https://github.com/near/near-sdk-rs.git", rev = "c843e9aec747b9acc7e240a6dea80a14448a7bfd" } near-workspaces = { version = "0.10.0", features = ["unstable"] } tokio = { version = "1.12.0", features = ["full"] } serde_json = "1" [profile.release] codegen-units = 1 # Tell `rustc` to optimize for small code size. opt-level = "z" lto = true debug = false panic = "abort" # Opt into extra safety checks on arithmetic operations https://stackoverflow.com/a/64136471/249801 overflow-checks = true ```

it results in following error:

• Checking the host environment...
• done

• Collecting cargo project metadata...
• done

• Generating ABI
 │    Compiling test_contract v0.1.0 (/home/user/Documents/code/test_contract)
 │ error[E0261]: use of undeclared lifetime name `'a`
 │   --> src/lib.rs:13:59
 │    |
 │ 13 |     pub fn is_ident<'a>(&self, other: &'a u32) -> Option<&'a u32> {
 │    |                    -                                      ^^ undeclared lifetime
 │    |                    |
 │    |                    help: consider introducing lifetime `'a` here: `<'a>`
 │
 │ For more information about this error, try `rustc --explain E0261`.
 │ error: could not compile `test_contract` (lib) due to 1 previous error
Here is the console command if you ever need to re-run it again:
cargo near build --no-locked --no-docker

Error:
   0: `cd "/home/user/Documents/code/test_contract" && CARGO_NEAR_ABI_GENERATION="true" CARGO_PROFILE_DEV_DEBUG="0" CARGO_PROFILE_DEV_LTO="off" CARGO_PROFILE_DEV_OPT_LEVEL="0" RUSTFLAGS="-Awarnings" "/home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/cargo" "build" "--message-format=json-render-diagnostics" "--features" "near-sdk/__abi-generate" "--color" "auto"` failed with exit code: Some(101)

expansion by cargo expand --features near-sdk/__abi-generate has this:

#[cfg(not(target_arch = "wasm32"))]
const _: () = {
    #[no_mangle]
    pub extern "C" fn __near_abi_is_ident() -> (*const u8, usize) {
        ...
        let functions = <[_]>::into_vec(
            #[rustc_box]
            ::alloc::boxed::Box::new([
                ::near_sdk::__private::AbiFunction {
                    ...
                    result: ::std::option::Option::Some(::near_sdk::__private::AbiType::Json {
                        type_schema: gen.subschema_for::<Option<&'a u32>>(),
                    }),
                },
            ]),
        );
        ...
    }
};    

Code, that produces the expansion is somewhere here https://github.com/near/near-sdk-rs/blob/c843e9aec747b9acc7e240a6dea80a14448a7bfd/near-sdk-macros/src/core_impl/abi/abi_generator.rs#L135-L146 and here https://github.com/near/near-sdk-rs/blob/c843e9aec747b9acc7e240a6dea80a14448a7bfd/near-sdk-macros/src/core_impl/abi/abi_generator.rs#L258-L272

Sravya-212 commented 2 months ago

@dj8yfo can I work on this?

onlydustapp[bot] commented 2 months ago

Hey @Sravya-212! Thanks for showing interest. We've created an application for you to contribute to near-sdk-rs. Go check it out on OnlyDust!