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 & before self. This works for self, &mut self, and &self, but not for self: &Self. So it previously interpreted self: &Self (a reference) as just self (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 whether self has an &, it now checks whether the type of self 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.
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.