Fixed methods with self: &Self
consuming the object
#4178
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
fixes #3958
Rust allows type annotations for the
self
parameter, so it can have many forms. E.g.self
,&self
,&mut self
,self: &Self
,self: Self
,self: Box<Self>
, etc. Wasm bindgen needs to know whether a value is consumed (pass by value) or referenced to generate the correct JS shim.Previously, the parser only looked at whether the self parameter had an
&
beforeself
. This works forself
,&mut self
, and&self
, but not forself: &Self
. So it previously interpretedself: &Self
(a reference) as justself
(pass by value), because it ignored any and all type annotations, which caused #3958.This PR fixes the issue by looking at the type of
self
instead. Helpfully, syn always gives us a type even for&self
. See their docs for more details. So instead of checking whetherself
has an&
, it now checks whether the type ofself
is a reference type. This is the same approach wasm bindgen uses for all other arguments to figure out whether they are passed by referece or by value.