Skip to content

Conversation

@wyfo
Copy link
Contributor

@wyfo wyfo commented Nov 7, 2025

Unsafe declarations of traits, functions, methods, or extern blocks cannot cause undefined behavior by themselves — only unsafe blocks or implementations of unsafe traits can.

The unsafe_code lint previously applied to these declarations, preventing, for example, declarations of extern "C" blocks. See #108926.

This change removes all such unsafe declarations from unsafe_code coverage.

However, to maintain soundness, unsafe functions with bodies are only allowed when unsafe_op_in_unsafe_fn is set to forbid. This ensures unsafe operations inside such functions require an unsafe block. Declarations of unsafe functions without bodies (e.g., in traits) are always allowed.

Closes #108926

Unsafe declarations of traits, functions, methods, or extern blocks
cannot cause undefined behavior by themselves — only unsafe blocks
or implementations of unsafe traits can.

The `unsafe_code` lint previously applied to these declarations,
preventing, for example, declarations of `extern "C"` blocks.
See rust-lang#108926.

This change removes all such unsafe declarations from `unsafe_code`
coverage.

However, to maintain soundness, unsafe functions *with bodies* are
only allowed when `unsafe_op_in_unsafe_fn` is set to `forbid`. This
ensures unsafe operations inside such functions require an `unsafe`
block. Declarations of unsafe functions *without bodies* (e.g., in
traits) are always allowed.

Closes rust-lang#108926
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 7, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 7, 2025

r? @petrochenkov

rustbot has assigned @petrochenkov.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot
Copy link
Collaborator

rustbot commented Nov 7, 2025

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

@wyfo wyfo marked this pull request as draft November 7, 2025 14:27
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 7, 2025
@RalfJung
Copy link
Member

RalfJung commented Nov 7, 2025

Unsafe declarations of traits, functions, methods, or extern blocks cannot cause undefined behavior by themselves

At least for extern blocks, that is not correct. The mere presence of an incorrect extern block can cause UB. See #46188.

@rust-log-analyzer

This comment has been minimized.

@wyfo
Copy link
Contributor Author

wyfo commented Nov 7, 2025

At least for extern blocks, that is not correct. The mere presence of an incorrect extern block can cause UB. See #46188.

Indeed, my bad, I've rolled back this part.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unsafe_code lint fires for unsafe fn with safe body

5 participants