-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Replace all instances of Self
in methods
#3631
Conversation
Currently the `wasm_bindgen` macro only replaces top level occurrences of the generic parameter `Self` in exported methods. This leads to an error when returning parametrized types containing `Self` due to Rust inner items rules. This PR fixes the issue by drilling down the return type `TokenStream` and replacing all instances of `Self` with the actual type name. Signed-off-by: Oliver T <geronimooliver00@gmail.com>
} | ||
} | ||
|
||
let path_str = quote::quote! { #path }.to_string(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let path_str = quote::quote! { #path }.to_string(); | |
let path_str = path.to_token_stream().to_string(); |
} | ||
|
||
let path_str = quote::quote! { #path }.to_string(); | ||
if path_str.contains("Self") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't we just call walk_path_change_self()
and let it just do it's thing anyway? I'm assuming to_token_stream()
and to_string()
have to go through the Path
to build it anyway, so it wouldn't be more expensive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah now that you mentioned it, seems like there is no way around it. Will push an update later today!
syn::Type::Path(syn::TypePath { ref mut path, .. }) => { | ||
if path.segments.len() == 1 && path.segments[0].ident == "Self" { | ||
path.segments[0].ident = self_ty.clone(); | ||
} else { | ||
walk_path_change_self(path, self_ty) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should handle types other than paths too. For example, Box<[Self]>
is still broken because Type::Slice
is ignored.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! Yeah I'll try to handle all possible types. AFAIK references are not allowed in return types, but I did noticed this function is also called for types in arguments which do allow references. So maybe I'll handle function args differently.
let mut path = match get_ty(&ty) { | ||
syn::Type::Path(syn::TypePath { qself: None, path }) => path.clone(), | ||
other => return other.clone(), | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should also handle types other than paths.
I was also thinking of turning the type into a |
I guess this could work and would obviously simplify the code a lot. The only thing I can think of that could use the word "Self" in a type is a macro or a const generic, Rust only supports integers right now. My impression is that the additional code required to cover the remaining bases shouldn't be too bad. I'm not too worried about what else Rust might add in the future, it would be most likely a breaking change in |
That would also catch types that contain |
That's so sad! Oh well, will try to have an update on this later today. Thank you both for your comments! |
Currently the
wasm_bindgen
macro only replaces top level occurrences of the generic parameterSelf
in exported methods. This leads to an error when returning parametrized types containingSelf
due to Rust inner items rules.This PR fixes the issue by drilling down the return type
TokenStream
and replacing all instances ofSelf
with the actual type name.Fixes #3105