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:
Rahul Butani
2020-05-26 15:08:56 -05:00
committed by GitHub
parent cf45d5b24a
commit 3dd8f3d2ac
2 changed files with 28 additions and 13 deletions

View File

@ -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),
_ => {}
}