Skip to content

Implement wasm proc-macros using wasmi - #157709

Draft
bjorn3 wants to merge 7 commits into
rust-lang:mainfrom
bjorn3:proc_macro_refactors5
Draft

Implement wasm proc-macros using wasmi#157709
bjorn3 wants to merge 7 commits into
rust-lang:mainfrom
bjorn3:proc_macro_refactors5

Conversation

@bjorn3

@bjorn3 bjorn3 commented Jun 10, 2026

Copy link
Copy Markdown
Member

This is an alternative approach to rust-lang/all-hands-2026#73. Unlike #157590 this uses wasmi + wasm32-wasip1. The advantage of this that it allows us to unconditionally depend on support for wasm proc-macros, allowing us to use them instead of the -Zdual-proc-macro hack as well as using proc-macros in the standard library without breaking cross-compilation. And by removing the last commit this can even use wasm32-unknown-unknown to avoid depending on wasi-sdk (which currently has a bootstrap cycle with rustc and thus might not be a great dependency to have for building rustc as it makes the lives of distros harder)

Once wasmtime supports all architectures for which we support host tools, swapping wasi for wasmtime is trivial. After that at a future point we can switch to the component model to allow using wasm32-wasip2 once we deem the advantages of this to outweigh the code duplication cost for the extra proc-macro ABI, extra code for handling the crate metadata for wasm components and the hack necessary to get wit-bindgen to work within the context of libproc_macro. This PR should not make this harder, while allowing us to get (some of) the benefits of wasm proc-macros earlier.

cc @Mark-Simulacrum @alexcrichton

@rustbot rustbot added A-meta Area: Issues & PRs about the rust-lang/rust repository itself S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue. labels Jun 10, 2026
@rust-log-analyzer

This comment has been minimized.

@bjorn3
bjorn3 force-pushed the proc_macro_refactors5 branch from 7f2358a to 3572b0e Compare June 11, 2026 11:23
@rustbot rustbot added A-tidy Area: The tidy tool T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) labels Jun 11, 2026
@bjorn3
bjorn3 force-pushed the proc_macro_refactors5 branch from 3572b0e to 3f33572 Compare June 12, 2026 12:50
@rust-bors

This comment has been minimized.

@bjorn3
bjorn3 force-pushed the proc_macro_refactors5 branch from 3f33572 to 3f346c6 Compare June 22, 2026 09:06
@bjorn3
bjorn3 force-pushed the proc_macro_refactors5 branch 2 times, most recently from 45afede to 439c9eb Compare June 22, 2026 14:21
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 22, 2026
…=Mark-Simulacrum

Fix linking for wasm with crate metadata included

On wasm the crate metadata ends up in a custom section. It is not possible to refer to create symbols inside a custom section, so attempting to export it during linking will result in a linker error. This keeps the target spec flag to deny compiling rust dylibs for wasm, but may theoretically allow compiling wasm rust dylibs with a custom target spec. It will help with wasm proc-macros. And in the future we can try to flip the only-cdylib option to false on wasm.

Extracted out of rust-lang#157709, but also helps with rust-lang#157590.

r? @Mark-Simulacrum
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 22, 2026
…=Mark-Simulacrum

Fix linking for wasm with crate metadata included

On wasm the crate metadata ends up in a custom section. It is not possible to refer to create symbols inside a custom section, so attempting to export it during linking will result in a linker error. This keeps the target spec flag to deny compiling rust dylibs for wasm, but may theoretically allow compiling wasm rust dylibs with a custom target spec. It will help with wasm proc-macros. And in the future we can try to flip the only-cdylib option to false on wasm.

Extracted out of rust-lang#157709, but also helps with rust-lang#157590.

r? @Mark-Simulacrum
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 22, 2026
…=Mark-Simulacrum

Fix linking for wasm with crate metadata included

On wasm the crate metadata ends up in a custom section. It is not possible to refer to create symbols inside a custom section, so attempting to export it during linking will result in a linker error. This keeps the target spec flag to deny compiling rust dylibs for wasm, but may theoretically allow compiling wasm rust dylibs with a custom target spec. It will help with wasm proc-macros. And in the future we can try to flip the only-cdylib option to false on wasm.

Extracted out of rust-lang#157709, but also helps with rust-lang#157590.

r? @Mark-Simulacrum
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 22, 2026
…=Mark-Simulacrum

Fix linking for wasm with crate metadata included

On wasm the crate metadata ends up in a custom section. It is not possible to refer to create symbols inside a custom section, so attempting to export it during linking will result in a linker error. This keeps the target spec flag to deny compiling rust dylibs for wasm, but may theoretically allow compiling wasm rust dylibs with a custom target spec. It will help with wasm proc-macros. And in the future we can try to flip the only-cdylib option to false on wasm.

Extracted out of rust-lang#157709, but also helps with rust-lang#157590.

r? @Mark-Simulacrum
rust-timer added a commit that referenced this pull request Jun 22, 2026
Rollup merge of #158242 - bjorn3:wasm_rust_dylib_metadata, r=Mark-Simulacrum

Fix linking for wasm with crate metadata included

On wasm the crate metadata ends up in a custom section. It is not possible to refer to create symbols inside a custom section, so attempting to export it during linking will result in a linker error. This keeps the target spec flag to deny compiling rust dylibs for wasm, but may theoretically allow compiling wasm rust dylibs with a custom target spec. It will help with wasm proc-macros. And in the future we can try to flip the only-cdylib option to false on wasm.

Extracted out of #157709, but also helps with #157590.

r? @Mark-Simulacrum
@bjorn3
bjorn3 force-pushed the proc_macro_refactors5 branch 2 times, most recently from eadf95b to 1c04273 Compare June 25, 2026 09:41
@bjorn3
bjorn3 force-pushed the proc_macro_refactors5 branch from 1c04273 to 4c6ae5a Compare June 30, 2026 12:38
@rust-bors

This comment has been minimized.

@bjorn3
bjorn3 force-pushed the proc_macro_refactors5 branch from 4c6ae5a to fb5a3f2 Compare July 1, 2026 11:23
@rust-bors

This comment has been minimized.

@bjorn3
bjorn3 force-pushed the proc_macro_refactors5 branch 2 times, most recently from 528e86a to 279919d Compare July 2, 2026 09:22
@rust-bors

This comment has been minimized.

@bjorn3
bjorn3 force-pushed the proc_macro_refactors5 branch from 279919d to 14b2d8a Compare July 9, 2026 11:45
@rust-bors

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@bjorn3
bjorn3 force-pushed the proc_macro_refactors5 branch from 4258ad0 to 1cf1d47 Compare August 10, 2026 14:40
@rust-bors

This comment has been minimized.

@bjorn3
bjorn3 force-pushed the proc_macro_refactors5 branch from 1cf1d47 to 3a2ce85 Compare August 12, 2026 10:00
@rustbot rustbot added A-compiletest Area: The compiletest test runner A-testsuite Area: The testsuite used to check the correctness of rustc labels Aug 12, 2026
@bjorn3
bjorn3 force-pushed the proc_macro_refactors5 branch 2 times, most recently from 5608fe2 to 15ca38a Compare August 12, 2026 10:17
@bjorn3

bjorn3 commented Aug 12, 2026

Copy link
Copy Markdown
Member Author

All proc-macro tests that don't involve panicking seem to work with the exception of two proc-macro that have an auxiliary dependency, where compiletest builds it for the host rather than wasm target.

@bjorn3
bjorn3 force-pushed the proc_macro_refactors5 branch 3 times, most recently from 38786f8 to 17873af Compare August 12, 2026 13:32
@rust-bors

This comment has been minimized.

bjorn3 added 7 commits August 13, 2026 15:47
For building this currently only removes the crate type check and
doesn't implement crate metadata support for wasip2 or the proc-macro
ABI. And for locating wasip2 needs support for loading crate metadata.
Crate metadata building and loading should work for wasip1.
This can be used by cargo to determine the target for which wasm
proc-macros should be compiled.
@bjorn3
bjorn3 force-pushed the proc_macro_refactors5 branch from 17873af to 25004b4 Compare August 13, 2026 13:53
@rustbot rustbot added the A-run-make Area: port run-make Makefiles to rmake.rs label Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-compiletest Area: The compiletest test runner A-meta Area: Issues & PRs about the rust-lang/rust repository itself A-run-make Area: port run-make Makefiles to rmake.rs A-testsuite Area: The testsuite used to check the correctness of rustc A-tidy Area: The tidy tool S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants