Skip to content

Commit 3dd8f3d

Browse files
authored
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).
1 parent cf45d5b commit 3dd8f3d

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

crates/macro-support/src/parser.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,14 @@ impl<'a> ConvertToAst<BindgenAttrs> for &'a mut syn::ItemStruct {
376376
}
377377
}
378378

379+
fn get_ty(mut ty: &syn::Type) -> &syn::Type {
380+
while let syn::Type::Group(g) = ty {
381+
ty = &g.elem;
382+
}
383+
384+
ty
385+
}
386+
379387
impl<'a> ConvertToAst<(BindgenAttrs, &'a ast::ImportModule)> for syn::ForeignItemFn {
380388
type Target = ast::ImportKind;
381389

@@ -414,7 +422,7 @@ impl<'a> ConvertToAst<(BindgenAttrs, &'a ast::ImportModule)> for syn::ForeignIte
414422
let class = wasm.arguments.get(0).ok_or_else(|| {
415423
err_span!(self, "imported methods must have at least one argument")
416424
})?;
417-
let class = match &*class.ty {
425+
let class = match get_ty(&class.ty) {
418426
syn::Type::Reference(syn::TypeReference {
419427
mutability: None,
420428
elem,
@@ -425,7 +433,7 @@ impl<'a> ConvertToAst<(BindgenAttrs, &'a ast::ImportModule)> for syn::ForeignIte
425433
"first argument of method must be a shared reference"
426434
),
427435
};
428-
let class_name = match *class {
436+
let class_name = match get_ty(class) {
429437
syn::Type::Path(syn::TypePath {
430438
qself: None,
431439
ref path,
@@ -466,7 +474,7 @@ impl<'a> ConvertToAst<(BindgenAttrs, &'a ast::ImportModule)> for syn::ForeignIte
466474
Some(ref ty) => ty,
467475
_ => bail_span!(self, "constructor returns must be bare types"),
468476
};
469-
let class_name = match *class {
477+
let class_name = match get_ty(class) {
470478
syn::Type::Path(syn::TypePath {
471479
qself: None,
472480
ref path,
@@ -666,9 +674,9 @@ fn function_from_decl(
666674
Some(i) => i,
667675
None => return t,
668676
};
669-
let path = match t {
670-
syn::Type::Path(syn::TypePath { qself: None, path }) => path,
671-
other => return other,
677+
let path = match get_ty(&t) {
678+
syn::Type::Path(syn::TypePath { qself: None, path }) => path.clone(),
679+
other => return other.clone(),
672680
};
673681
let new_path = if path.segments.len() == 1 && path.segments[0].ident == "Self" {
674682
self_ty.clone().into()
@@ -869,7 +877,7 @@ impl<'a> MacroParse<BindgenAttrs> for &'a mut syn::ItemImpl {
869877
"#[wasm_bindgen] generic impls aren't supported"
870878
);
871879
}
872-
let name = match *self.self_ty {
880+
let name = match get_ty(&self.self_ty) {
873881
syn::Type::Path(syn::TypePath {
874882
qself: None,
875883
ref path,
@@ -1286,7 +1294,7 @@ fn extract_first_ty_param(ty: Option<&syn::Type>) -> Result<Option<syn::Type>, D
12861294
Some(t) => t,
12871295
None => return Ok(None),
12881296
};
1289-
let path = match *t {
1297+
let path = match *get_ty(&t) {
12901298
syn::Type::Path(syn::TypePath {
12911299
qself: None,
12921300
ref path,
@@ -1309,7 +1317,7 @@ fn extract_first_ty_param(ty: Option<&syn::Type>) -> Result<Option<syn::Type>, D
13091317
syn::GenericArgument::Type(t) => t,
13101318
other => bail_span!(other, "must be a type parameter"),
13111319
};
1312-
match ty {
1320+
match get_ty(&ty) {
13131321
syn::Type::Tuple(t) if t.elems.len() == 0 => return Ok(None),
13141322
_ => {}
13151323
}

crates/test-macro/src/lib.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ pub fn wasm_bindgen_test(
4343
}
4444
}
4545
}
46-
let ident = match body.next() {
47-
Some(TokenTree::Ident(token)) => token,
48-
_ => panic!("expected a function name"),
49-
};
46+
let ident = find_ident(&mut body).expect("expected a function name");
5047

5148
let mut tokens = Vec::<TokenTree>::new();
5249

@@ -78,3 +75,13 @@ pub fn wasm_bindgen_test(
7875

7976
tokens.into_iter().collect::<TokenStream>().into()
8077
}
78+
79+
fn find_ident(iter: &mut token_stream::IntoIter) -> Option<Ident> {
80+
match iter.next()? {
81+
TokenTree::Ident(i) => Some(i),
82+
TokenTree::Group(g) if g.delimiter() == Delimiter::None => {
83+
find_ident(&mut g.stream().into_iter())
84+
}
85+
_ => None,
86+
}
87+
}

0 commit comments

Comments
 (0)