Bad replacement for unsafe extern block suggestion · Issue #126756 · rust-lang/rust (original) (raw)

The span for the suggestion to add an unsafe qualifier to an extern block ends up deleting the entire extern token.

#![feature(unsafe_extern_blocks)]

extern "C" { unsafe fn foo(); }

Generates this output:

error: items in unadorned `extern` blocks cannot have safety qualifiers
 --> src/main.rs:4:5
  |
3 | extern "C" {
  | ---------- help: add unsafe to this `extern` block
4 |     unsafe fn foo();
  |     ^^^^^^^^^^^^^^^^

When the suggestion is applied, the code results in:

#![feature(unsafe_extern_blocks)]

{ unsafe fn foo(); }

Although the suggestion is currently marked MaybeIncorrect, I think it should still provide something that is closer to being correct than to delete it. It also seems like this should be a relatively simple replacement that should support MachineApplicable. Ref:

#[suggestion(code = "", applicability = "maybe-incorrect")]
pub block: Span,

I expected to see this happen: Suggestion adds the unsafe keyword to the front of extern

Instead, this happened: Deletes the extern

Meta

rustc 1.81.0-nightly (59e2c01c2 2024-06-17)
binary: rustc
commit-hash: 59e2c01c2217a01546222e4d9ff4e6695ee8a1db
commit-date: 2024-06-17
host: aarch64-apple-darwin
release: 1.81.0-nightly
LLVM version: 18.1.7

Tracking: