Skip to content

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

Open
@ifsheldon

Description

@ifsheldon

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 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions