rust-lang / rust-lang/rust

Support `#[link(wasm_import_module)]` on "Rust" ABI

Open
#155,399 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-ABI C-bug needs-triage
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Extern blocks on wasm must specify wasm_import_module for a namespace. Currently, it seems only "C"-like ABI is accepted.

#[link(wasm_import_module = "not_env")]
unsafe extern "Rust" {
    fn foo();
}

#[unsafe(no_mangle)]
extern "Rust" fn bar() {
    unsafe { foo(); }
}

The produced wasm for the above should contain

(import "not_env" "foo" ...)
(export "bar" ...)

On current stable 1.95.0, the import is namespaced to "env" instead of "not_env". On nightly, a linker error is generated instead due to the removal of --allow-undefined in rust-lang/rust#149868.

I have tested the above with "C-unwind", which works as expected. No other ABIs come to mind for wasm.

Although the ABI is unstable, which makes it nearly impossible to import a js function in a defined manner, one can satisfy the import with an export from another rust compiled wasm module with matching ABI.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided unsafe extern "Rust" example on stable and nightly, then inspect the generated WebAssembly imports and exports. The change is done when wasm_import_module = "not_env" produces an import in the not_env namespace for the Rust ABI without the reported nightly linker error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, wasm
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.