Skip to content

Commit d6825ad

Browse files
authored
Handle Expr::Group in the macro parser (#2395)
We explicitly match on `Expr` in a few locations so it's necessary to peel back `Expr::Group` like we do with `Type::Group`. Closes #2393
1 parent b584407 commit d6825ad

File tree

2 files changed

+54
-39
lines changed

2 files changed

+54
-39
lines changed

crates/macro-support/src/parser.rs

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,14 @@ fn get_ty(mut ty: &syn::Type) -> &syn::Type {
436436
ty
437437
}
438438

439+
fn get_expr(mut expr: &syn::Expr) -> &syn::Expr {
440+
while let syn::Expr::Group(g) = expr {
441+
expr = &g.expr;
442+
}
443+
444+
expr
445+
}
446+
439447
impl<'a> ConvertToAst<(BindgenAttrs, &'a ast::ImportModule)> for syn::ForeignItemFn {
440448
type Target = ast::ImportKind;
441449

@@ -1074,24 +1082,24 @@ fn import_enum(enum_: syn::ItemEnum, program: &mut ast::Program) -> Result<(), D
10741082
_ => bail_span!(v.fields, "only C-Style enums allowed with #[wasm_bindgen]"),
10751083
}
10761084

1077-
match &v.discriminant {
1078-
Some((
1079-
_,
1080-
syn::Expr::Lit(syn::ExprLit {
1081-
attrs: _,
1082-
lit: syn::Lit::Str(str_lit),
1083-
}),
1084-
)) => {
1085+
let (_, expr) = match &v.discriminant {
1086+
Some(pair) => pair,
1087+
None => {
1088+
bail_span!(v, "all variants must have a value");
1089+
}
1090+
};
1091+
match get_expr(expr) {
1092+
syn::Expr::Lit(syn::ExprLit {
1093+
attrs: _,
1094+
lit: syn::Lit::Str(str_lit),
1095+
}) => {
10851096
variants.push(v.ident.clone());
10861097
variant_values.push(str_lit.value());
10871098
}
1088-
Some((_, expr)) => bail_span!(
1099+
expr => bail_span!(
10891100
expr,
10901101
"enums with #[wasm_bidngen] cannot mix string and non-string values",
10911102
),
1092-
None => {
1093-
bail_span!(v, "all variants must have a value");
1094-
}
10951103
}
10961104
}
10971105

@@ -1122,18 +1130,17 @@ impl<'a> MacroParse<(&'a mut TokenStream, BindgenAttrs)> for syn::ItemEnum {
11221130
let generate_typescript = opts.skip_typescript().is_none();
11231131

11241132
// Check if the first value is a string literal
1125-
match self.variants[0].discriminant {
1126-
Some((
1127-
_,
1133+
if let Some((_, expr)) = &self.variants[0].discriminant {
1134+
match get_expr(expr) {
11281135
syn::Expr::Lit(syn::ExprLit {
11291136
attrs: _,
11301137
lit: syn::Lit::Str(_),
1131-
}),
1132-
)) => {
1133-
opts.check_used()?;
1134-
return import_enum(self, program);
1138+
}) => {
1139+
opts.check_used()?;
1140+
return import_enum(self, program);
1141+
}
1142+
_ => {}
11351143
}
1136-
_ => {}
11371144
}
11381145
let js_name = opts
11391146
.js_name()
@@ -1169,28 +1176,27 @@ impl<'a> MacroParse<(&'a mut TokenStream, BindgenAttrs)> for syn::ItemEnum {
11691176
}
11701177

11711178
let value = match &v.discriminant {
1172-
Some((
1173-
_,
1179+
Some((_, expr)) => match get_expr(expr) {
11741180
syn::Expr::Lit(syn::ExprLit {
11751181
attrs: _,
11761182
lit: syn::Lit::Int(int_lit),
1177-
}),
1178-
)) => match int_lit.base10_digits().parse::<u32>() {
1179-
Ok(v) => v,
1180-
Err(_) => {
1181-
bail_span!(
1182-
int_lit,
1183-
"enums with #[wasm_bindgen] can only support \
1183+
}) => match int_lit.base10_digits().parse::<u32>() {
1184+
Ok(v) => v,
1185+
Err(_) => {
1186+
bail_span!(
1187+
int_lit,
1188+
"enums with #[wasm_bindgen] can only support \
11841189
numbers that can be represented as u32"
1185-
);
1186-
}
1190+
);
1191+
}
1192+
},
1193+
expr => bail_span!(
1194+
expr,
1195+
"enums with #[wasm_bidngen] may only have \
1196+
number literal values",
1197+
),
11871198
},
11881199
None => i as u32,
1189-
Some((_, expr)) => bail_span!(
1190-
expr,
1191-
"enums with #[wasm_bidngen] may only have \
1192-
number literal values",
1193-
),
11941200
};
11951201

11961202
let comments = extract_doc_comments(&v.attrs);
@@ -1242,15 +1248,15 @@ impl MacroParse<BindgenAttrs> for syn::ItemConst {
12421248
bail_span!(self, "#[wasm_bindgen] will not work on constants unless you are defining a #[wasm_bindgen(typescript_custom_section)].");
12431249
}
12441250

1245-
match *self.expr {
1251+
match get_expr(&self.expr) {
12461252
syn::Expr::Lit(syn::ExprLit {
12471253
lit: syn::Lit::Str(litstr),
12481254
..
12491255
}) => {
12501256
program.typescript_custom_sections.push(litstr.value());
12511257
}
1252-
_ => {
1253-
bail_span!(self, "Expected a string literal to be used with #[wasm_bindgen(typescript_custom_section)].");
1258+
expr => {
1259+
bail_span!(expr, "Expected a string literal to be used with #[wasm_bindgen(typescript_custom_section)].");
12541260
}
12551261
}
12561262

crates/typescript-tests/src/omit_definition.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,12 @@ pub enum MyEnum {
3232
Two,
3333
Three,
3434
}
35+
36+
macro_rules! generate_ts {
37+
($lit:literal) => {
38+
#[wasm_bindgen(typescript_custom_section)]
39+
const _: &str = $lit;
40+
};
41+
}
42+
43+
generate_ts!("");

0 commit comments

Comments
 (0)