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

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

Open
dj8yfo opened this issue Jun 28, 2024 · 2 comments

Comments

@dj8yfo
Copy link
Collaborator

dj8yfo commented Jun 28, 2024

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

[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

BindgenArgType::CallbackResultArg => {
let typ = if let Some(ok_type) = utils::extract_ok_type(typ) {
ok_type
} else {
return syn::Error::new_spanned(
&arg.ty,
"Function parameters marked with \
#[callback_result] should have type Result<T, PromiseError>",
)
.into_compile_error();
};
callbacks.push(generate_abi_type(typ, &arg.serializer_ty));

and here
fn generate_abi_type(ty: &Type, serializer_type: &SerializerType) -> TokenStream2 {
let schema = generate_schema(ty, serializer_type);
match serializer_type {
SerializerType::JSON => quote! {
::near_sdk::__private::AbiType::Json {
type_schema: #schema,
}
},
SerializerType::Borsh => quote! {
::near_sdk::__private::AbiType::Borsh {
type_schema: #schema,
}
},
}
}

dj8yfo pushed a commit to dj8yfo/near-sdk-rs that referenced this issue Jun 28, 2024
@Sravya-212
Copy link

@dj8yfo can I work on this?

Copy link

onlydustapp bot commented Sep 11, 2024

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!

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

No branches or pull requests

2 participants