Issue #2486: Raw pointers inside unsafe: which ones, and what the compiler checks (replacing ADR-302 D9)

Open and answer this issue on GitHub.

ffi, blocked, safety · opened 2026-11-21 · updated 2026-11-22 · 2 comments

Decision paper. Nothing here is decided, and no ADR or specification text is written yet.

Where the question comes from

The owner asked, in a session on Nikaia’s place as a systems language (2026-11-19 to 2026-11-21): should a program be able to do everything in Nikaia, raw pointers included, instead of switching to Rust for the last part? The owner’s view is that no line needs to be drawn: Rust allows pointers inside unsafe, Nikaia already has unsafe and already calls C, so a pointer in unsafe is no new kind of trust.

Two things already in the repository point the same way:

No program is blocked on this today. The answer decides which way the language grows.

From zero: what a program can do today

A call into C is written inside unsafe { … } (Part III 15.1, ADR-302 D3). A pointer appears only in an extern declaration: ref T, ref mut T, ref Array[T] and ref mut Array[T] live for the call (D5). A length beside a buffer is checked (NK1159, D6). A handle from a C library is an opaque type, never dereferenced, and released by a cleanup (D7). unsafe permits nothing else: “what is inside is checked as anything else is” (D3).

A raw pointer is refused. Run with target/fast/nikaia check (built 2026-11-19, tip e011699a):

fn main() throws {
    let p: Pointer[u8]? = null
    println("x")
}
error[NK1135]: There's no type called `Pointer`.
 2 |     let p: Pointer[u8]? = null

So no Nikaia program can build an allocator, an intrusive list, a ring buffer over raw memory, or hand C an address that C keeps past the call. Those live in a Rust crate (ADR-218) or in a described crate (ADR-rust-boundary-1).

Facts checked in the code

What makes a pointer hard in Nikaia is not the pointer itself. It is that the compiler chooses references and moves the program never writes, and Rust’s aliasing rules apply to the Rust it emits.

  1. A changing reference always comes from a written mut. A mut parameter, mut self, let mut, for mut, a door of SharedMut[T] (Part I 6.3, 6.5). A parameter the body changes without mut is NK1138. The call site shows nothing (fill(xs)), but the declaration does.
  2. A reading reference is often invisible. A lent parameter may lower to &T; a let over a place, a for, a view handed back are references the source does not spell. In Rust, writing through a raw pointer while a &T to the same memory is still used is undefined behaviour.
  3. The compiler moves a value’s header (a String or Vec struct, not its heap data) at points it chooses. Three cases, read in the lowered Rust:
    • at creation: let text = __keep.put_viewed(fs::read_to_string(…).await?); The name exists only as a reference into the anchor, so no pointer can be older than the move;
    • after the last change (late_puts, crates/nikaia-std/src/compiler/buffers.nika). This program, lowered with nikaia lower:

      struct View { head: String }
      
      fn make() -> String {
          let mut g = String()
          g.push_str("  hi  ")
          return g
      }
      
      fn first_word() -> View {
          let mut text = make()
          text.push_str(" end")
          let w: String = text.trim()
          return View { head: w }
      }
      
      fn main() throws {
          let v = first_word()
          println(v.head)
      }

      lowers first_word to

      let mut text = make();
      text.push_str(" end"); let text = __keep.put_viewed(text);
      let w: &str = text.trim();

      and prints hi end. The header moves after the name has existed for two lines. A pointer to text taken between them would dangle;

    • moves the program writes (Cursor { input: text }, a keep parameter), and assigning a variable a new value, where the old buffer moves into the anchor of what holds its views (Part III, NK2305).

    After put_viewed the header never moves again: the anchor holds an Arc, not a Box, so that moving the entry touches nothing it points at (Entry, crates/unsafe/tether/src/lib.rs).

  4. Every one of these points is known while the program is built. The compiler emits them; --tethers prints where each buffer lives. The analysis that refuses a change in place while a view lives (NK2305, ADR-views-3 D9) already follows views across these moves.
  5. While checking case 3 a lowering defect turned up: a let mut whose initialiser can throw gets no late put_viewed, and rustc refuses the code (NK1343). Filed as #2465. It does not change the decision, but the build of option C rests on that machinery.

Options

A. Nothing: ADR-302 D9 stands. Pointer code stays in Rust crates (ADR-218) or described crates.

B. Scoped pointers only. A pointer exists only inside a block over a place, as Swift’s withUnsafeMutablePointer(to:). Proposed syntax, not built:

unsafe {
    raw::with_address_mut(buffer) { p =>
        p.offset(4).write(0)
    }
}

C. A pointer type, checked where it points into Nikaia’s own values, unchecked where it points into raw memory. Proposed syntax, not built:

use std::raw

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

fn arena(n: i64) -> raw::PointerMut[u8]? {
    return unsafe { raw::alloc(n) }
}

The rules, each read off what the compiler already knows:

  1. Pointer[T] reads, PointerMut[T] reads and writes (the pair Shared / SharedMut already has). Both are never null; T? is the null pointer, one machine word, as for a handle (ADR-302 D11).
  2. Taking, reading, writing, offsetting and casting are written inside unsafe.
  3. address_mut takes only a mut place (fact 1).
  4. A pointer into a place is valid until the place’s next move or its next change by another path, whether the move is written or inserted (fact 3). Using it after that is refused, the message naming the move and, for an inserted one, the view that made it necessary.
  5. A write through a pointer is a change in place of its source: a view of that source still used afterwards is NK2305, as today (fact 2).
  6. A pointer into a place does not leave the place’s scope (returned, kept, given to a task). Refused for now; widening it later is possible, narrowing a free start would not be (ADR-302 D9’s own argument).
  7. A pointer from raw::alloc, from C or from an integer is not checked. That is the part that is unsafe in Rust too.
  8. A function that writes through a pointer has touches everything and locks may, as an extern declaration (ADR-302 D4), so the rest of the program stays sound. A Pointer does not cross tasks.
  9. In an extern declaration, a Pointer[T] is the address C may keep, beside ref T for the call only.

D. Pointers as in Rust, nothing checked.

How other languages do it

Recommendation

C. It is the owner’s direction (no line between Nikaia and systems work), and it is the only option where the programmer can actually keep the rules, because the compiler checks exactly the part the programmer cannot see. It also fits ADR-037 D7: what is observable is written, what is not is inferred, safe as the floor.

Questions the decision should also settle:

What changes if C is decided

To build

Done when

The owner has decided; the ADR and the specification text are on develop; the build issue is filed; or the owner chose A and this issue is closed naming ADR-302 D9.

Not decided here

The exact names (raw::address_mut, PointerMut), pointer arithmetic beyond offset, volatile access for device registers, and a bare-metal target (ADR-119).

Comments

keywan-ghadami, 2026-11-21

Owner’s answer, 2026-11-21, on the four questions under Recommendation: all four as proposed.

  1. Taking a pointer is written inside unsafe too, not only dereferencing it.
  2. A package must allow unsafe in its dependencies in the manifest, as ADR-raw-sinks-1 does for raw sinks, and nikaia reports how much unsafe a package holds.
  3. Programs with pointer unsafe run under Miri in CI, under both aliasing models, as the crates of ADR-218 D2 do.
  4. ADR-218 stays for the existing crates; whether each moves into Nikaia is decided per crate.

The main choice (A, B, C or D) is still to be confirmed.


Generated by Claude Code

keywan-ghadami, 2026-11-21

Owner’s decision, 2026-11-21: option C, a pointer type inside unsafe, with the four questions as proposed (comment above).

This issue stays open, blocked on the build.


Generated by Claude Code

Back to Open Issues