Skip to content

Commit def9147

Browse files
authored
Remove span in generated field getter (#3725)
1 parent 4fea53b commit def9147

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
* Add bindings for `UserActivation`.
99
[#3719](https://github.com/rustwasm/wasm-bindgen/pull/3719)
1010

11+
### Fixed
12+
13+
* Fixed a compiler error when using `#[wasm_bindgen]` inside `macro_rules!`.
14+
[#3725](https://github.com/rustwasm/wasm-bindgen/pull/3725)
15+
1116
### Removed
1217

1318
* Removed Gecko-only `InstallTriggerData` and Gecko-internal `FlexLineGrowthState`, `GridDeclaration`, `GridTrackState`,

crates/backend/src/codegen.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,14 @@ impl ToTokens for ast::StructField {
389389
};
390390
let maybe_assert_copy = respan(maybe_assert_copy, ty);
391391

392-
let mut val = quote_spanned!(self.rust_name.span()=> (*js).borrow().#rust_name);
392+
// Split this out so that it isn't affected by `quote_spanned!`.
393+
//
394+
// If we don't do this, it might end up being unable to reference `js`
395+
// properly because it doesn't have the same span.
396+
//
397+
// See https://github.com/rustwasm/wasm-bindgen/pull/3725.
398+
let js_token = quote! { js };
399+
let mut val = quote_spanned!(self.rust_name.span()=> (*#js_token).borrow().#rust_name);
393400
if let Some(span) = self.getter_with_clone {
394401
val = quote_spanned!(span=> <#ty as Clone>::clone(&#val) );
395402
}

tests/wasm/classes.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,10 @@ exports.js_test_inspectable_classes_can_override_generated_methods = () => {
241241
assert.strictEqual(overridden_inspectable.toString(), 'string was overwritten');
242242
overridden_inspectable.free();
243243
};
244+
245+
exports.js_test_class_defined_in_macro = () => {
246+
const macroClass = new wasm.InsideMacro();
247+
assert.strictEqual(macroClass.a, 3);
248+
macroClass.a = 5;
249+
assert.strictEqual(macroClass.a, 5);
250+
};

tests/wasm/classes.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ extern "C" {
3434
fn js_test_option_classes();
3535
fn js_test_inspectable_classes();
3636
fn js_test_inspectable_classes_can_override_generated_methods();
37+
fn js_test_class_defined_in_macro();
3738
}
3839

3940
#[wasm_bindgen_test]
@@ -608,3 +609,27 @@ impl OverriddenInspectable {
608609
String::from("string was overwritten")
609610
}
610611
}
612+
613+
macro_rules! make_struct {
614+
($field:ident) => {
615+
#[wasm_bindgen]
616+
pub struct InsideMacro {
617+
pub $field: u32,
618+
}
619+
};
620+
}
621+
622+
make_struct!(a);
623+
624+
#[wasm_bindgen]
625+
impl InsideMacro {
626+
#[wasm_bindgen(constructor)]
627+
pub fn new() -> Self {
628+
Self { a: 3 }
629+
}
630+
}
631+
632+
#[wasm_bindgen_test]
633+
fn class_defined_in_macro() {
634+
js_test_class_defined_in_macro();
635+
}

0 commit comments

Comments
 (0)