Skip to content

Bad replacement for unsafe extern block suggestion #126756

Closed

Description

@ehuss

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:

Activity

  1. added
    C-bugCategory: This is a bug.
    D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.
    on Jun 20, 2024
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Jun 20, 2024
  3. self-assigned this
    on Jun 23, 2024
  4. traviscross commented on Jun 25, 2024

    @traviscross
    Contributor
  5. self-assigned this
    on Jun 25, 2024
  6. spastorino commented on Jun 25, 2024

    @spastorino
    Member

    @chenyukang if you want to take this one, go ahead. Also assigned myself for tracking purposes but I can fix this easily too.

  7. chenyukang commented on Jun 26, 2024

    @chenyukang
    Member

    @chenyukang if you want to take this one, go ahead. Also assigned myself for tracking purposes but I can fix this easily too.

    sorry for the delay, submitted a PR #126973.

  8. added 2 commits that reference this issue on Jun 26, 2024
    6b33843
    b272086
  9. added a commit that references this issue on Jun 26, 2024
    2e21862
  10. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Jun 29, 2024
  11. added a commit that references this issue on Aug 3, 2024
  12. added a commit that references this issue on Aug 3, 2024
  13. added a commit that references this issue on Aug 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.F-unsafe_extern_blocks`#![feature(unsafe_extern_blocks)]`

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions