Issue #2575: A program can hold a raw pointer inside unsafe: raw::Pointer[T], raw::PointerMut[T] and their checks (ADR-pointers-v1)

Open and answer this issue on GitHub.

compiler, ffi, blocked, safety · opened 2026-11-21 · updated 2026-11-22 · 1 comment

Builds ADR-pointers-v1 and ADR-c-boundary-v1 D9, as Part III 15.1 states them.

Part of #2486 Needs #2465

The goal

A program can hold a raw pointer inside unsafe. Today raw::Pointer[u8] is refused (NK1135, “There’s no type called Pointer”, target/fast/nikaia check, tip e2c62d70), and an allocator, an intrusive structure or an address C keeps must be written in Rust. After this, Part III 15.1’s example runs:

use std::raw

fn fill(mut buf: Vec[u8]) {
    unsafe {
        let p: raw::PointerMut[u8] = raw::address_mut(buf[0])
        p.offset(1).write(7)
    }
}

fn main() throws {
    let mut buf: Vec[u8] = [0, 0, 0]
    fill(buf)
    println(f"{buf[1]}")
}

and prints 7.

The governing decision

ADR-pointers-v1 D1-D11 and ADR-c-boundary-v1 D9; the codes in Part III’s table: NK1143 (an operation outside unsafe), NK1138 (raw::address_mut of a place the function may not change), NK2305 (a write through a pointer is a change in place), NK2306 (used after its place moved or changed), NK2307 (leaves its place’s scope), NK1309 (a dependency’s pointer code without unsafe-pointers), NK2501 / NK2502 (a pointer does not cross a thread).

Written in Nikaia (CLAUDE.md, Written in Nikaia only): the checks in crates/nikaia-std/src/compiler/*.nika, the std::raw entries in std’s ledger, and the lowering in the emitter. The emitted Rust holds the unsafe (&raw const, &raw mut, std::alloc), which lowered code may hold (ADR-218 D5); no new crate under crates/unsafe/.

To build

  1. The types raw::Pointer[T] and raw::PointerMut[T] in std::raw, T? over them in one word, ==, p.reading(); lowered to Rust’s NonNull<T> (and Option<NonNull<T>> for ?). Test: a pointer stored in a struct field and compared, .stdout beside it.
  2. The operations of D2 (raw::address, raw::address_mut, read, write, offset, cast, to_address, raw::from_address, raw::alloc, raw::free), each refused outside unsafe with NK1143. Tests: the example above prints 7; raw::alloc / write / read / free over four i64 prints them; one .stderr per operation outside unsafe.
  3. NK1138 for raw::address_mut of a place the function may not change (a plain parameter, a let). Test .stderr.
  4. NK2306: a pointer into a place used after a written move, after a change other than through it, after the place ended, and after a move the compiler inserted into the anchor (the let mut / change / view-handed-back shape of #2465, with a pointer taken before the last change). The message names the place, the line that took the pointer, the line that moved or changed it, and for an inserted move the view. Tests: one .stderr per case, and a program that takes the pointer again after the move and runs.
  5. D5: a write through a raw::PointerMut[T] is a change in place for NK2305; handing a pointer into a place to a call is a mut hand-over or a lend of the place. Tests: a view used after a write through a pointer (.stderr); a call given a view and a pointer into the same place (.stderr).
  6. NK2307 for a pointer into a place returned, kept, given to a task, handed to an extern declaration’s raw::Pointer parameter, or converted to an integer. Tests: one .stderr per case.
  7. raw::Pointer[T] and raw::PointerMut[T] in an extern declaration as C’s const T * / T * (ADR-c-boundary-v1 D9). Test: malloc / free declared and called, a C function that keeps a raw::alloc pointer (setvbuf with a buffer from raw::alloc).
  8. D8: a function that reads or writes through a pointer from raw::alloc, C or raw::from_address has touches everything and locks may in the ledger, and so do its callers; a function whose pointers all point into its own places keeps its entry. Test: nikaia lower ledger of both shapes.
  9. D9: crosses = false for both types; NK2501 for a task that uses one, NK2502 for a call the compiler cannot see the end of. Tests .stderr.
  10. D10: a dependency’s pointer operation is NK1309 unless the program says unsafe-pointers = true under [allow.packages.NAME] or --allow=NAME:unsafe-pointers; --trust lists every pointer operation with its place and the number per package. Test: a project with a dependency, refused and then admitted.
  11. D11: the tests of items 1-10 that run run under Miri with Stacked Borrows and Tree Borrows in CI, as scripts/check-unsafe-crates.sh does for the crates of ADR-218.

Files expected

crates/nikaia-std/src/compiler/ (the checker for unsafe, the views and tether walks in buffers.nika, tether.nika, the ledger derivation, the emitter), std’s ledger for std::raw, the manifest reading of [allow.packages.NAME], tests/compiler/tests/, the CI workflow for Miri.

Done when

Every item’s tests are in tests/compiler/tests/ and green, Part III 15.1’s example prints 7, and the Miri job of item 11 is green on develop.

Comments

keywan-ghadami, 2026-11-21

Driver: not queued, this issue waits for #2465, which is still open (#1819). Set ready again when it is closed.


Generated by Claude Code

Back to Open Issues