Skip to content

[edition 2024] missing_unsafe_on_extern in macros with older editions #132425

Closed
@daxpedda

Description

@daxpedda

I tried this code:

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
unsafe extern {
    pub fn test();
}

I expected to see this happen: it should compile.
Instead, this happened: it fails to compile.

Meta

rustc --version --verbose:

rustc 1.84.0-nightly (1e4f10ba6 2024-10-29)
binary: rustc
commit-hash: 1e4f10ba6476e48a42a79b9f846a2d9366525b9e
commit-date: 2024-10-29
host: x86_64-unknown-linux-gnu
release: 1.84.0-nightly
LLVM version: 19.1.1
Compile Error

error: extern blocks must be unsafe
  --> src/main.rs:5:12
  |
5 |     pub fn test();
  |            ^^^^


Basically the problem is that the #[wasm-bindgen] proc-macro generates extern "C" blocks which aren't prefixed with the unsafe keyword.
However, my understanding is that proc-macros should carry the edition from the crate they are coming from, which in this case is edition 2021. The rules of edition 2024 should not be applied to the generated code from this edition 2021 proc-macro.

That said, this is easily fixable in wasm-bindgen, but the bug will probably break other libraries as well.

Cc rustwasm/wasm-bindgen#4218.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-edition-2024Area: The 2024 editionA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.F-unsafe_extern_blocks`#![feature(unsafe_extern_blocks)]`L-missing_unsafe_on_externLint: missing_unsafe_on_extern

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions