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.
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:
unsafe topic into a small Rust crate under crates/unsafe/ (16 crates today, from tether to task-block). So every new unsafe topic is new Rust, which runs against the rule Written in Nikaia only (CLAUDE.md).No program is blocked on this today. The answer decides which way the language grows.
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).
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.
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.&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.String or Vec struct, not its heap data) at points it chooses. Three cases, read in the lowered Rust:
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;
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).
--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.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.A. Nothing: ADR-302 D9 stands. Pointer code stays in Rust crates (ADR-218) or described crates.
unsafe and runs under Miri with both aliasing models (ADR-218 D2); the manifesto’s “without unsafe” stays trivially true.unsafe topic is new Rust, against Written in Nikaia only; a user doing systems work needs two languages; the owner’s stated direction is the opposite.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:
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).unsafe.address_mut takes only a mut place (fact 1).NK2305, as today (fact 2).raw::alloc, from C or from an integer is not checked. That is the part that is unsafe in Rust too.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.extern declaration, a Pointer[T] is the address C may keep, beside ref T for the call only.unsafe topics no longer need new Rust.D. Pointers as in Rust, nothing checked.
*const T / *mut T; taking one is safe, dereferencing is unsafe; nothing is checked; Miri finds undefined behaviour in tests. All references are visible to the programmer, which is what Nikaia does not have.unsafe; because the garbage collector moves objects, the address of a movable variable can be taken only inside fixed, which pins it. The same problem as our moved header, solved by scope.withUnsafePointer(to:) gives a pointer valid only inside the closure; escaping it is undefined behaviour. Option B.unsafe.Pointer with written rules for uintptr, because the runtime moves stacks.*T for one element and [*]T for many with arithmetic; no hidden moves, so nothing to track.@system / @trusted / @safe on functions; pointers free in @system.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:
unsafe too (proposed: yes, so all pointer code is visible and countable), or only dereferencing, as in Rust?unsafe in its dependencies in the manifest, as ADR-raw-sinks-1 does for raw sinks? Proposed: yes, and nikaia reports how much unsafe a package holds.unsafe run under Miri in CI, both aliasing models, as the crates of ADR-218 D2 do? Proposed: yes, their lowered Rust.std::raw gets its entries.unsafe” stays true but needs the unsafe count to stay checkable.std::raw entries, the new codes in Part III’s code table), every example run through scripts/probe.nika --check on stubs.ready issue with a numbered To build list and tests in tests/compiler/tests/, naming Needs #2465.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.
The exact names (raw::address_mut, PointerMut), pointer arithmetic beyond offset, volatile access for device registers, and a bare-metal target (ADR-119).
Owner’s answer, 2026-11-21, on the four questions under Recommendation: all four as proposed.
unsafe too, not only dereferencing it.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.unsafe run under Miri in CI, under both aliasing models, as the crates of ADR-218 D2 do.The main choice (A, B, C or D) is still to be confirmed.
Generated by Claude Code
Owner’s decision, 2026-11-21: option C, a pointer type inside unsafe, with the four questions as proposed (comment above).
raw::Pointer[T]; the view and the handle stay the forms to reach for. ADR-302 is in the history; every live citation moved.unsafe-pointers key under [allow.packages.NAME] (13.3), the code table (NK1138, NK1143, NK1309 widened; NK2306 and NK2307 new), Part I’s note on unsafe. Commit e2c62d70 on develop.raw::address_mut(buf) on a Vec[u8] is a pointer to the list’s header (PointerMut[Vec[u8]]), so the examples take raw::address_mut(buf[0]), a pointer to the first element. Option B’s block is written with a lambda, raw::with_address_mut(buffer[0], fn(p) { … }).ready, Needs #2465).This issue stays open, blocked on the build.
Generated by Claude Code