Skip to content

Improving Unsafe Code Documentation in the Rust Standard Library #640

Open

Description

@tomassedovic
Metadata
Contact @hxuhack
Team(s) libs, opsem
Goal document 2026/improve-std-unsafe

Summary

Review unsafe code documentation in the Rust standard library, identify inconsistencies, and fix them. Based on the lessons learned during this review process, we also aim to establish explicit conventions for code safety reasoning and unsafe code documentation.


This issue is intended for status updates only.

For general questions or comments, please contact a goal owner or post in the Zulip topic.

Activity

  1. added this to the 2026 milestone on May 5, 2026
  2. tomassedovic commented on May 5, 2026

    @tomassedovic
    ContributorAuthor

    This issue is intended for status updates only.

    For general questions or comments, please contact the owner(s) directly.

  3. locked and limited conversation to collaborators on May 5, 2026
  4. hxuhack commented on May 6, 2026

    @hxuhack
    Contributor

    We have built a website that continuously tracks the safety doc of unsafe APIs in the standard library. Our effort starts with non-intrinsic unsafe APIs, aiming to ensure that all such APIs are equipped with explicit safety sections as a first step.

  5. added
    ex-2025h2Goals that were previously in 2025h2 (for reporting)
    and removed
    ex-2025h2Goals that were previously in 2025h2 (for reporting)
    on May 11, 2026
  6. hxuhack commented on Jun 29, 2026

    @hxuhack
    Contributor

    Trying to fix all methods without safety docs. Currently, there are 55/643 left. Some of these methods implement the same traits, so we only need to add safety docs to the trait methods and can then obtain the safety docs from the traits.

    Currently, the main challenge is that adding some safety docs involves ACP (154665), even when the change only duplicates existing sentences or makes minor syntax adjustments. See 158383.

  7. hxuhack commented on Aug 28, 2026

    @hxuhack
    Contributor

    The following PRs have been submitted and merged since July as part of this effort:

    Pull Request API Info Contributor
    158810 *const[T]::get_unchecked, NonNull<[T]>::get_unchecked_mut Add supplementary information yilin0518
    2183 _mm_stream_si32, _mm_stream_si64 Add supplementary information yilin0518
    158804 *mut T::as_uninit_mut Add supplementary information yilin0518
    159322 intrinsic::simd::simd_gather, intrinsic::simd::simd_scatter Fix safety section yilin0518
    159402 intrinsic::simd::simd_shl/shr, intrinsic::simd::simd_funnel_shl/shr, intrinsic::simd::simd_masked_load, intrinsic::simd::simd_masked_store Fix safety section yilin0518
    161271 CodePoint::from_u32_unchecked, Wtf8::from_bytes_unchecked, Wtf8::from_mut_bytes_unchecked Add safety section yilin0518
    161292 alloc::Wtf8 Add safety section and discharge yilin0518
  8. hxuhack commented on Sep 25, 2026

    @hxuhack
    Contributor

    The following PRs have been submitted during the past month:

    Pull Request API Info Contributor
    162854 atomic_load, atomic_store merged yilin0518
    163008 core::mem::transmute_neo merged kiana1kaslana
    163140 atomic_(cxchg, cxchgweak, xchg, xadd, xsub, and. nand, or, xor, max, min, umax, umin) under review yilin0518
    163141 disjoint_bitor, unchecked_funnel_shl, unchecked_funnel_shr under review yilin0518

    Since text duplication is a major concern for some unsafe APIs that currently do not have safety documentation, we have proposed ACP: Referencing existing safety contracts in unsafe API documentation. The ACP is currently under review.

  9. hxuhack commented on Oct 12, 2026

    @hxuhack
    Contributor

    Current Results (based on nightly-2026-10-11)

    Overall, unsafe APIs in the Rust standard library fall into four categories.

    • Free functions: There are 5,159 free functions, of which 3,106 have no # Safety section.

      • Of these, 3,006 are in the core::arch module.

        • hexagon: 1,853/1,853 APIs.
        • x86: 544/559 APIs.
        • s390x: 196/196 APIs.
        • powerpc: 118/118 APIs.
        • x86_64: 75/75 APIs.
        • riscv_shared: 38/38 APIs.
        • loongarch64: 34/34 APIs.
        • ...
      • Some architectures have more standardized safety documentation:

        • aarch64: Only 13 of 1,411 unsafe functions lack a # Safety section. However, more than 200 neon APIs are documented only with the generic statement Neon intrinsic unsafe.
        • arm_shared: Only 3 of 550 unsafe functions lack a # Safety section. However, 547 neon APIs are documented only with the generic statement Neon intrinsic unsafe.
    • Methods: There are 506 methods, excluding trait method implementations, of which 40 have no # Safety section. But, most of them already refer to other methods with the same safety requirements.

    • Trait methods: Ideally, the safety requirements of unsafe methods declared in a trait should be documented at the trait definition rather than separately at each implementation site. This is because the safety requirements should be the same regardless of the type implementing the trait. There are 25 such unsafe methods, of which 2 have no # Safety section.

    • Unsafe traits: There are 28 unsafe traits, of which 9 have no safety documentation. Ideally, each unsafe trait should document its safety requirements, which specify the invariants that implementors must uphold.

    Conclusion and Future Work

    • Methods (including struct methods and trait methods): The documentation is already in good shape structurally. We will address the remaining undocumented APIs.
    • Unsafe traits: We hope to complete the missing safety documentation for unsafe traits by the end of this year.
    • Free functions: These appear to be the most challenging category. Some require an in-depth understanding of the instruction set architecture (ISA) and LLVM intrinsics. We will further classify these APIs into categories to make the remaining work more manageable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions