Repository navigation
Description
Summary
The raw-pointer spec now says passing a raw pointer to a function is not unsafe by itself. The obligation lives in the callee's contract. A function that dereferences, retains, mutates through, converts, or otherwise relies on caller-guaranteed validity of a raw-pointer parameter has a safety precondition that the raw pointer type does not encode, so it must be an unsafe fn unless the compiler or binding provides a proven safe wrapper.
Current behavior permits a safe function to dereference a raw pointer internally under an unsafe block, then be called safely by users with no indication that the caller must provide a valid pointer.
Repro
fn touch(p: *mut i32) -> i32:
unsafe *p
fn main:
var value = 1
let p = (&raw mut value) as *mut i32
let x = touch(p) // currently safe call
let _ = x
Observed with ./out/bin/with check: currently reports ok.
Expected
The compiler should not let raw-pointer validity preconditions hide behind a safe callable surface.
Possible implementation shape:
- A function that performs an unsafe raw-pointer memory access or raw-to-safe conversion using a raw pointer parameter is inferred/required to be
unsafe fnunless it proves or models the precondition internally. - A genuinely safe wrapper can remain safe when it validates/models nullability, bounds, lifetime, ownership, retention, mutation, and permissions before exposing safe behavior.
- Calling an unsafe raw-pointer-precondition function requires
unsafeat the call site. - A function that only computes, compares, observes, or relabels raw pointer values remains safe.
Root cause / 5 Whys
- Why can callers invoke
touch(p)safely? The unsafe operation is contained inside the callee body, so the public callable type stays safe. - Why is that wrong? The caller still supplies the validity fact needed by
unsafe *p. - Why doesn't
*mut Tencode that fact? Raw pointers are validity-less values; they do not encode liveness, alignment, initialization, ownership, permissions, or provenance. - Why can't an internal unsafe block be enough? It localizes the memory access but hides the caller-facing safety precondition.
- Root cause: function safety currently tracks explicit unsafe operations syntactically, not whether a callable's public contract relies on caller-guaranteed raw-pointer validity.
Acceptance criteria
- Safe functions may accept raw pointers when they only compute/compare/relabel them or when they prove/model validity internally.
- Functions whose correctness relies on caller-provided raw-pointer validity are
unsafe fnor rejected until marked/converted. - Manual
externand raw/unmodeled ABI functions with raw-pointer contracts remain unsafe by default unless a safe wrapper models the contract. - Tests cover safe raw-pointer computation helpers, unsafe raw-pointer dereference helpers, and a safe wrapper that validates/models its contract.
with build,with build :fixpoint,with build :test, andwith build :test-greenpass.
Activity
ehartford commentedon Jun 9, 2026
Implemented in 0030c68 (pushed to main). Verified with with build, with build :fixpoint, with build :test, and with build :test-green on the rebased main.