rust-lang / rust-lang/rust

fixing `wit_bindgen::generate!{}` during `cargo fix --edition` caused a compiler error

Open
#137,331 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-suggestion-diagnostics C-bug T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code

I wanted to migrate the code of a simple adder WASIp2 component to Edition 2024. The source code is in https://github.com/ifsheldon/wasi_mindmap/tree/main/guest-adder-rs and the code is https://github.com/ifsheldon/wasi_mindmap/blob/main/guest-adder-rs/src/lib.rs

wit_bindgen::generate!({
    world: "adder",
});

struct Adder;

impl Guest for Adder {
    fn add(a: i32, b: i32) -> i32 {
        a + b
    }
}

export!(Adder);
Meta

rustc --version --verbose:

rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: aarch64-apple-darwin
release: 1.85.0
LLVM version: 19.1.7
Error output

I tried both cargo fix --edition --target wasm32-wasip2 and cargo fix --edition.

The error message is confusing

(wasi) ~/offline_code/opensource/wasi_mindmap/guest-adder-rs git:[main]
cargo fix --edition --target wasm32-wasip2
warning: profiles for the non root package will be ignored, specify profiles at the workspace root:
package:   /Users/zhiqiu/offline_code/opensource/wasi_mindmap/host-command-component/Cargo.toml
workspace: /Users/zhiqiu/offline_code/opensource/wasi_mindmap/Cargo.toml
   Migrating guest-adder-rs/Cargo.toml from 2021 edition to 2024
warning: profiles for the non root package will be ignored, specify profiles at the workspace root:
package:   /Users/zhiqiu/offline_code/opensource/wasi_mindmap/host-command-component/Cargo.toml
workspace: /Users/zhiqiu/offline_code/opensource/wasi_mindmap/Cargo.toml
    Checking guest-adder-rs v0.1.0 (/Users/zhiqiu/offline_code/opensource/wasi_mindmap/guest-adder-rs)
   Migrating guest-adder-rs/src/lib.rs from 2021 edition to 2024
warning: failed to automatically apply fixes suggested by rustc to crate `guest_adder_rs`

after fixes were automatically applied the compiler reported errors within these files:

  * guest-adder-rs/src/lib.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error: expected item, found `{`
 --> guest-adder-rs/src/lib.rs:3:1
  |
3 | { unsafe wit_bindgen::generate!({
  | ^ expected item
  |
  = note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>

error: aborting due to 1 previous error

Original diagnostics will follow.

warning[E0133]: call to unsafe function `_export_add_cabi` is unsafe and requires unsafe block
  --> guest-adder-rs/src/lib.rs:3:1
   |
3  | / wit_bindgen::generate!({
4  | |     // the name of the world in the `*.wit-files` input file
5  | |     world: "adder",
6  | | });
   | |__^ call to unsafe function
...
21 |   export!(Adder);
   |   -------------- in this macro invocation
   |
   = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/unsafe-op-in-unsafe-fn.html>
   = note: consult the function's documentation for information on how to avoid undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default
  --> guest-adder-rs/src/lib.rs:3:1
   |
3  | / wit_bindgen::generate!({
4  | |     // the name of the world in the `*.wit-files` input file
5  | |     world: "adder",
6  | | });
   | |__^
...
21 |   export!(Adder);
   |   -------------- in this macro invocation
   = note: `--force-warn unsafe-op-in-unsafe-fn` implied by `--force-warn rust-2024-compatibility`
   = note: this warning originates in the macro `export` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0133`.
warning: `guest-adder-rs` (lib test) generated 1 warning (run `cargo fix --lib -p guest-adder-rs --tests` to apply 1 suggestion)
warning: failed to automatically apply fixes suggested by rustc to crate `guest_adder_rs`

after fixes were automatically applied the compiler reported errors within these files:

  * guest-adder-rs/src/lib.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error: expected item, found `{`
 --> guest-adder-rs/src/lib.rs:3:1
  |
3 | { unsafe wit_bindgen::generate!({
  | ^ expected item
  |
  = note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>

error: aborting due to 1 previous error

Original diagnostics will follow.

warning: `guest-adder-rs` (lib) generated 1 warning (1 duplicate)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.36s

RUST_BACKTRACE=1 cargo fix --edition --target wasm32-wasip2 didn't get me more information about this error.

As this is likely related to wit-bindgen, probably I should cc @alexcrichton ?

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

Reproduce the failure from guest-adder-rs/src/lib.rs with cargo fix --edition --target wasm32-wasip2, using the linked wasi_mindmap example and the reported Rust version. Inspect the edition migration output around wit_bindgen::generate!; done means the command no longer inserts invalid syntax and the crate completes migration without the reported compiler error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, wasm
Domain
compilers, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.