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
Returning Option<Self> in #[wasm_bindgen] impl block gives error #3105
Comments
The error that arises is the following: E0401: Inner items do not inherit type or const parameters from the functions they are embedded in.. It hints at that an outer type parameter (in this case But how can that be when there isn't any inner function? Well, maybe there is. Lets take a look at this even simpler example: #[wasm_bindgen]
pub struct A();
#[wasm_bindgen]
impl A
{
#[wasm_bindgen]
pub fn foo() -> Option<Self>
{
None
}
} Using cargo-extend we can see what the impl A {
pub fn foo() -> Option<Self> {
#[automatically_derived]
const __wasm_bindgen_generated_A_f__const: () = {
pub unsafe extern "C" fn __wasm_bindgen_generated_A_f() -> <Option<
Self,
> as wasm_bindgen::convert::ReturnWasmAbi>::Abi {
let _ret = {
let _ret = A::f();
_ret
};
<Option<Self> as wasm_bindgen::convert::ReturnWasmAbi>::return_abi(_ret)
}
};
None
}
} And there you have it. The problem is that the pub unsafe extern "C" fn __wasm_bindgen_generated_A_f() -> <Option<
Self,
> An example that recreates the error without the pub struct A();
impl A
{
pub fn foo() -> Option<Self>
{
// `bar` is not allowed to reference `Self`.
fn bar() -> Option<Self> {
None
}
None
}
} |
Thank you for the detailed answer. It sounds like this is expected (at least if you are familiar with |
No, I think this should be fixable. |
That is great to hear! It would certainly remove a sharp corner for newcomers like myself to cut themselves on... 😅 Should the label be changed from question to enhancement then? |
Summary
This may not be the best example, but for simplicity I am adapting the code from this issue. The problem is as follows:
The following return types work for
new
with the#[wasm_bindgen]
attribute applied to the impl:Universe
,Self
,Option<Universe>
. However, this return type appears not to work:Option<Self>
. Is there some deep reason for this, or is this potentially a bug?This gives the error:
The same problem arises when attempting to use
Self
withResult
and other generics.The text was updated successfully, but these errors were encountered: