Skip to content

Remove span in generated field getter#3725

Merged
Liamolucko merged 4 commits intowasm-bindgen:mainfrom
Liamolucko:fix-hygiene
Nov 29, 2023
Merged

Remove span in generated field getter#3725
Liamolucko merged 4 commits intowasm-bindgen:mainfrom
Liamolucko:fix-hygiene

Conversation

@Liamolucko
Copy link
Copy Markdown
Contributor

@Liamolucko Liamolucko commented Nov 28, 2023

Fixes #3707

Previously, (*js).borrow().#rust_name was being given the span of rust_name, which seems like a fairly harmless thing to do at first glance. However, it turns out that the span of a token also affects its hygiene - turns out proc macros have hygiene too, not just declarative macros!

This caused a problem because the declaration of js had a span of Span::call_site(), but it was being accessed with rust_name's span, which might be a different context where js doesn't exist.

Usually this is fine because Span::call_site() is in the same context as the struct definition: normal code. However, when you put #[wasm_bindgen] inside a macro_rules!, Span::call_site() is now in the context of that macro_rules!, while rust_name's span is still in normal code which can't access variables from inside macro_rules!.

To fix that I've just removed the span from (*js).borrow().#rust_name, making it Span::call_site(). I don't think this should have any effect on diagnostics anyway, since I don't see how that code could ever cause a compile error. Turns out it was used, so I just got rid of the span on js instead of the whole thing.

Copy link
Copy Markdown
Member

@daxpedda daxpedda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very funny business.

@daxpedda
Copy link
Copy Markdown
Member

Ah, could you add a note to the changelog as well?

Fixes wasm-bindgen#3707

Previously, `(*js).borrow().#rust_name` was being given the span of
`rust_name`, which seems like a fairly harmless thing to do at first
glance. However, it turns out that the span of a token also affects its
hygiene - turns out proc macros have hygiene too, not just declarative
macros!

This caused a problem because the declaration of `js` had a span of
`Span::call_site()`, but it was being accessed with `rust_name`'s span,
which might be a different context where `js` doesn't exist.

Usually this is fine because `Span::call_site()` is in the same context
as the struct definition: normal code. However, when you put
`#[wasm_bindgen]` inside a `macro_rules!`, `Span::call_site()` is now in
the context of that `macro_rules!`, while `rust_name`'s span is still in
normal code which can't access variables from inside `macro_rules!`.

To fix that I've just removed the span from `(*js).borrow().#rust_name`,
making it `Span::call_site()`. I don't think this should have any effect
on diagnostics anyway, since I don't see how that code could ever cause
a compile error.
@Liamolucko Liamolucko merged commit def9147 into wasm-bindgen:main Nov 29, 2023
@Liamolucko Liamolucko deleted the fix-hygiene branch November 29, 2023 23:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

#[wasm_bindgen] attribute not work in macro_rules

2 participants