mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-15 17:50:51 +00:00
Handle the possibility that the class name is in its own Group (#2159)
* Handle the possibility that the class name is in its own Group rust-lang/rust#72388 makes this happen * Handle Groups in more places * fmt! * Add some functions to handle Groups As suggested by @alexcrichton [here](https://gist.github.com/alexcrichton/3c93ab2547d45d9caa3b72309cd4262b).
This commit is contained in:
parent
cf45d5b24a
commit
3dd8f3d2ac
@ -376,6 +376,14 @@ impl<'a> ConvertToAst<BindgenAttrs> for &'a mut syn::ItemStruct {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_ty(mut ty: &syn::Type) -> &syn::Type {
|
||||
while let syn::Type::Group(g) = ty {
|
||||
ty = &g.elem;
|
||||
}
|
||||
|
||||
ty
|
||||
}
|
||||
|
||||
impl<'a> ConvertToAst<(BindgenAttrs, &'a ast::ImportModule)> for syn::ForeignItemFn {
|
||||
type Target = ast::ImportKind;
|
||||
|
||||
@ -414,7 +422,7 @@ impl<'a> ConvertToAst<(BindgenAttrs, &'a ast::ImportModule)> for syn::ForeignIte
|
||||
let class = wasm.arguments.get(0).ok_or_else(|| {
|
||||
err_span!(self, "imported methods must have at least one argument")
|
||||
})?;
|
||||
let class = match &*class.ty {
|
||||
let class = match get_ty(&class.ty) {
|
||||
syn::Type::Reference(syn::TypeReference {
|
||||
mutability: None,
|
||||
elem,
|
||||
@ -425,7 +433,7 @@ impl<'a> ConvertToAst<(BindgenAttrs, &'a ast::ImportModule)> for syn::ForeignIte
|
||||
"first argument of method must be a shared reference"
|
||||
),
|
||||
};
|
||||
let class_name = match *class {
|
||||
let class_name = match get_ty(class) {
|
||||
syn::Type::Path(syn::TypePath {
|
||||
qself: None,
|
||||
ref path,
|
||||
@ -466,7 +474,7 @@ impl<'a> ConvertToAst<(BindgenAttrs, &'a ast::ImportModule)> for syn::ForeignIte
|
||||
Some(ref ty) => ty,
|
||||
_ => bail_span!(self, "constructor returns must be bare types"),
|
||||
};
|
||||
let class_name = match *class {
|
||||
let class_name = match get_ty(class) {
|
||||
syn::Type::Path(syn::TypePath {
|
||||
qself: None,
|
||||
ref path,
|
||||
@ -666,9 +674,9 @@ fn function_from_decl(
|
||||
Some(i) => i,
|
||||
None => return t,
|
||||
};
|
||||
let path = match t {
|
||||
syn::Type::Path(syn::TypePath { qself: None, path }) => path,
|
||||
other => return other,
|
||||
let path = match get_ty(&t) {
|
||||
syn::Type::Path(syn::TypePath { qself: None, path }) => path.clone(),
|
||||
other => return other.clone(),
|
||||
};
|
||||
let new_path = if path.segments.len() == 1 && path.segments[0].ident == "Self" {
|
||||
self_ty.clone().into()
|
||||
@ -869,7 +877,7 @@ impl<'a> MacroParse<BindgenAttrs> for &'a mut syn::ItemImpl {
|
||||
"#[wasm_bindgen] generic impls aren't supported"
|
||||
);
|
||||
}
|
||||
let name = match *self.self_ty {
|
||||
let name = match get_ty(&self.self_ty) {
|
||||
syn::Type::Path(syn::TypePath {
|
||||
qself: None,
|
||||
ref path,
|
||||
@ -1286,7 +1294,7 @@ fn extract_first_ty_param(ty: Option<&syn::Type>) -> Result<Option<syn::Type>, D
|
||||
Some(t) => t,
|
||||
None => return Ok(None),
|
||||
};
|
||||
let path = match *t {
|
||||
let path = match *get_ty(&t) {
|
||||
syn::Type::Path(syn::TypePath {
|
||||
qself: None,
|
||||
ref path,
|
||||
@ -1309,7 +1317,7 @@ fn extract_first_ty_param(ty: Option<&syn::Type>) -> Result<Option<syn::Type>, D
|
||||
syn::GenericArgument::Type(t) => t,
|
||||
other => bail_span!(other, "must be a type parameter"),
|
||||
};
|
||||
match ty {
|
||||
match get_ty(&ty) {
|
||||
syn::Type::Tuple(t) if t.elems.len() == 0 => return Ok(None),
|
||||
_ => {}
|
||||
}
|
||||
|
@ -43,10 +43,7 @@ pub fn wasm_bindgen_test(
|
||||
}
|
||||
}
|
||||
}
|
||||
let ident = match body.next() {
|
||||
Some(TokenTree::Ident(token)) => token,
|
||||
_ => panic!("expected a function name"),
|
||||
};
|
||||
let ident = find_ident(&mut body).expect("expected a function name");
|
||||
|
||||
let mut tokens = Vec::<TokenTree>::new();
|
||||
|
||||
@ -78,3 +75,13 @@ pub fn wasm_bindgen_test(
|
||||
|
||||
tokens.into_iter().collect::<TokenStream>().into()
|
||||
}
|
||||
|
||||
fn find_ident(iter: &mut token_stream::IntoIter) -> Option<Ident> {
|
||||
match iter.next()? {
|
||||
TokenTree::Ident(i) => Some(i),
|
||||
TokenTree::Group(g) if g.delimiter() == Delimiter::None => {
|
||||
find_ident(&mut g.stream().into_iter())
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user