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 generation fails with Self return types #972

Closed
miraclx opened this issue Nov 23, 2022 · 4 comments · Fixed by #1001
Closed

ABI generation fails with Self return types #972

miraclx opened this issue Nov 23, 2022 · 4 comments · Fixed by #1001
Assignees
Labels
abi ABI related issues

Comments

@miraclx
Copy link
Contributor

miraclx commented Nov 23, 2022

#[near_bindgen]
impl MyType {
    pub fn new() -> Self { ... }
}
$ cargo build --features near-sdk/__abi-generate
   Compiling adder v0.1.0 (/Users/miraclx/git/near/near-sdk-rs/examples/adder)
error[E0411]: cannot find type `Self` in this scope
 --> src/lib.rs:3:12
  |
3 |     pub fn new() -> Self {
  |            ---      ^^^^ `Self` is only available in impls, traits, and type definitions
  |            |
  |            `Self` not allowed in a function

For more information about this error, try `rustc --explain E0411`.
error: could not compile `adder` due to 2 previous errors
@miraclx miraclx self-assigned this Nov 23, 2022
@miraclx miraclx added the abi ABI related issues label Nov 24, 2022
@itegulov
Copy link
Contributor

Is there a common use for Self outside of init functions though? Your particular example works if you mark the function with #[init].

@austinabell
Copy link
Contributor

Is there a common use for Self outside of init functions though? Your particular example works if you mark the function with #[init].

Not a common one, but I think it is a valid one. I've seen devs try this, and it only doesn't really work when there are storage collections in the state. But if everything is in-memory and you just want to return all of the state, there could be a reason to do so.

new might be a confusing naming for the use case, though.

To confirm, this fails also, right?

#[near_bindgen]
// derive default, serde, ....
struct MyType {
  data: u8,
}

#[near_bindgen]
impl MyType {
  pub fn get_state(self) -> Self { self }
  // or
  pub fn get_state(&self) -> &Self { self }
}

@itegulov
Copy link
Contributor

Not a common one, but I think it is a valid one. I've seen devs try this, and it only doesn't really work when there are storage collections in the state. But if everything is in-memory and you just want to return all of the state, there could be a reason to do so.

Makes sense.

I have taken a look at what ink! does with Self and they just ban its usage in non-constructor functions altogether:
latest-screenshot

So maybe banning all occurrences of Self makes more sense from the maintenance point of view.

@austinabell
Copy link
Contributor

So maybe banning all occurrences of Self makes more sense from the maintenance point of view.

My intuition would agree with this. If they really wanted to serialize the state as a return value, they could use the type name explicitly or a number of other ways to work around.

Might be best to nudge devs away from this, as I've noticed a few times devs have accidentally forgotten to mark the fn as init

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

Successfully merging a pull request may close this issue.

3 participants