You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
&raw mut local: offset-0 store dropped when local is later passed to a function (-O1 miscompile) #888
A raw store through &raw mut local (writing a field at offset 0) is dropped
when the local is later passed to a function; the store at offset 8 is kept.
The callee then receives a half-updated aggregate.
The compiler appears to constant-propagate local's offset-0 field from its
initializer past the opaque *mut store — i.e. &raw mut local does not
invalidate value assumptions about local's fields.
Minimal repro
use std.builtins.print_i32
fn show_len(s: &str) -> i32: s.len() as i32
fn show_b0(s: &str) -> i32: s.byte_at(0)
fn main:
var chunk: str = "" // str = {ptr@0, len@8}
let sp = &raw mut chunk as *mut u8
var target: [u8; 4] = [65u8, 66u8, 67u8, 0u8]
unsafe:
*(sp as *mut u64) = &target[0] as u64 // write ptr (offset 0)
*((sp + 8u64) as *mut i64) = 3 // write len (offset 8)
print_i32(show_len(chunk)) // prints 3 — offset-8 store honored across call
print_i32(show_b0(chunk)) // CRASHES — offset-0 (ptr) store dropped; stale ""-ptr
show_len(chunk) returns 3 (len store visible), but show_b0(chunk) dereferences
a stalechunk.ptr (the constant "" pointer, not &target) and segfaults.
Reading chunk.len()/chunk.byte_at(0)directly in main (no call) works —
the divergence appears only when chunk is marshalled across a call boundary.
Impact
lib/std/http.w's recv loop punned a str over a stack buffer via exactly this
pattern and passed it to push_str; the callee read a stale pointer and crashed
once TLS actually delivered data (unmasked by the #883 GCM fix). Fixed there by
dropping the punning and appending bytes directly.
Expected
&raw mut local must escape/alias local fully: any later use of local
(including passing it to a function) must reload its fields from memory, honoring
raw stores at every offset. The &raw mut escape hatch is unusable otherwise.
Root cause: not a miscompile — the repro's spelling; compiler is D22-conforming
Grounded in the emitted IR, the spec, and the D22 ruling:
1. No store is dropped. The optimized IR passes the callee the local's alloca address (call @show_b0(ptr %3)), and both raw stores land. The &raw mut escape, the offset stores, and call marshalling are all correct.
&target[0] is a safe Copy-element view (&u8), and per docs/d22-Eric-Ruling.md §6.2 — "An owned-value demand is established independently by … cast target" — the integer cast target materializes the pointee and widens it. chunk.ptr becomes the integer 65, and show_b0 faults dereferencing address 65. Not a stale pointer; not a dropped store; exactly the ruled materialization.
3. Both correct spellings work today (verified, print 3 / 65, exit 0):
*(sp as *mut u64) = (&raw const target[0]) as u64 // raw address-of
*(sp as *mut u64) = (&target[0] as *const u8) as u64 // blessed `&place as *T` spelling
The str-punning pattern over &raw mut is fully functional with either.
The std.http recv-loop crash that motivated this was the same spelling trap (already fixed at source by appending bytes directly).
The remaining question — a ruling for Eric
&place as <integer> on a Copy pointee silently yields the value. There is no legitimate use for the & in a value cast (target[0] as u64 says it directly), so the only plausible author intent is address-taking — which this doesn't do.
What the others do: Rust rejects &T as u64 outright (E0606: must go through as *const T as usize). C reads &x cast to integer as the address. Zig requires @intFromPtr.
What the spec says: D22 §6.2 rules the cast-target demand (current behavior conforms). §16.11 blesses integer address observation only for raw pointers (p as usize).
Mission fit: a diagnostic earns its place by catching a real mistake the compiler cannot otherwise resolve — this repro (and the http crash) are that mistake, and the rule costs no legitimate program: the value spelling is shorter without the &.
Proposal: make &place as <integer-type> a compile error with a fix-it offering both intents: &raw const place as u64 (address) or place as u64 (value). D22 §6.2 stays intact for every other cast target; this carves out only the integer-target-of-explicit-borrow case, where materialization can never be what the author meant.
BDFL prediction (80%): accept — it matches "the & is pure ceremony under the value reading" (no-ceremony forbids requiring it, and here it changes nothing except to mislead), and Rust's error at the same seam is the safety-bar precedent.
Addendum: Mojo (verified in .reference/mojo) + mission re-read
Mojo agrees with Rust/Zig, verified in the tree rather than from memory:
Address-taking is a named construction, never an operator: Pointer(to=x) / UnsafePointer(to=x) (test_pointer.mojo:63).
Pointer→integer is an explicit conversion on the pointer type: Int(ptr), UInt64(ptr) (test_pointer.mojo:233-242).
There is no path at all from a borrow to an integer — a Mojo ref can't become an Int; you must construct a Pointer first, then explicitly convert.
So the reference field is 3-to-1: Rust (E0606), Zig (@intFromPtr), and Mojo all require named address-of → explicit pointer-to-int, with no borrow-to-integer spelling. C is the lone divergent — and C reads &x cast to integer as the address, which is the worst possible interaction with our mission's own audience: a C migrant writing &x as u64 expects an address and With silently hands them the value.
Mission re-read — the letter of the law decides this more strongly than I first argued:
"Every unnecessary character is a compiler failure."
Under the value reading, the & in &x as u64is an unnecessary character (x as u64 is the value, one character shorter). By the mission's own sentence, that spelling is already a defect — the proposal isn't adding ceremony or a new rule, it's the compiler enforcing the no-ceremony law at a seam where the surplus character actively misleads. "Remove ceremony without removing guardrails": this guardrail removes zero legitimate programs.
One implementation note: the migrator is unaffected — the pcre2 re-migration already emits the correct raw spellings (&raw const …[0] as …, verified in the promoted zone), so the error would only ever fire on hand-written source.
Revised BDFL prediction (90%): accept — every reference except C requires the two-step spelling, C's divergence points the other way (address), and the mission's "every unnecessary character is a compiler failure" makes the value reading illegal by principle.
Canonized as D31 (commit 78d15891, battery green, reseeded):
spec §16.11: an explicit borrow cast to an integer type is a compile error — normative, next to raw integer address observation; D22 §6.2 carve-out scoped to the integer target only.
decisions.md D31: Eric's blessed ruling verbatim, with context, alternatives, and the in-tree reference survey (Rust E0606, Zig @intFromPtr, Mojo Pointer(to=x)+Int(ptr); C reads the address — the divergence that made the silent value reading burn C migrants).
Compiler: the error with both fix-it intents — `&raw const place as u64` for the address, `place as u64` for the value. &raw and the blessed &place as *T pointer spelling are unaffected.
Tests: behav_d31_borrow_int_cast (expect-check-fail) and behav_d31_borrow_int_cast_ok — both intents run, and the original str-punning-over-&raw mut pattern from this issue is proven end-to-end (raw stores at every offset honored across a call; prints 3/65).
Self-host migration: 34 address-intent sites across 8 rt/ platform files + ClangBridge rewritten to &raw const — each had only worked because a non-Copy-compatible pointee fell through to address semantics, the incoherence this ruling removes.
The originally-reported miscompile does not exist (see the root-cause comment above); the real defect — a spelling whose only plausible intent silently did something else — is now a compile error with a fix-it. Closing.
Activity
std/http: append recv bytes directly instead of punning a str over th…
ehartford commentedon Aug 30, 2026
Root cause: not a miscompile — the repro's spelling; compiler is D22-conforming
Grounded in the emitted IR, the spec, and the D22 ruling:
1. No store is dropped. The optimized IR passes the callee the local's alloca address (
call @show_b0(ptr %3)), and both raw stores land. The&raw mutescape, the offset stores, and call marshalling are all correct.2. The actual failure is this line of the repro:
The IR shows what it compiles to:
&target[0]is a safe Copy-element view (&u8), and per docs/d22-Eric-Ruling.md §6.2 — "An owned-value demand is established independently by … cast target" — the integer cast target materializes the pointee and widens it.chunk.ptrbecomes the integer 65, andshow_b0faults dereferencing address 65. Not a stale pointer; not a dropped store; exactly the ruled materialization.3. Both correct spellings work today (verified, print
3/65, exit 0):The str-punning pattern over
&raw mutis fully functional with either.The
std.httprecv-loop crash that motivated this was the same spelling trap (already fixed at source by appending bytes directly).The remaining question — a ruling for Eric
&place as <integer>on a Copy pointee silently yields the value. There is no legitimate use for the&in a value cast (target[0] as u64says it directly), so the only plausible author intent is address-taking — which this doesn't do.What the others do: Rust rejects
&T as u64outright (E0606: must go throughas *const T as usize). C reads&xcast to integer as the address. Zig requires@intFromPtr.What the spec says: D22 §6.2 rules the cast-target demand (current behavior conforms). §16.11 blesses integer address observation only for raw pointers (
p as usize).Mission fit: a diagnostic earns its place by catching a real mistake the compiler cannot otherwise resolve — this repro (and the http crash) are that mistake, and the rule costs no legitimate program: the value spelling is shorter without the
&.Proposal: make
&place as <integer-type>a compile error with a fix-it offering both intents:&raw const place as u64(address) orplace as u64(value). D22 §6.2 stays intact for every other cast target; this carves out only the integer-target-of-explicit-borrow case, where materialization can never be what the author meant.BDFL prediction (80%): accept — it matches "the
&is pure ceremony under the value reading" (no-ceremony forbids requiring it, and here it changes nothing except to mislead), and Rust's error at the same seam is the safety-bar precedent.ehartford commentedon Aug 30, 2026
Addendum: Mojo (verified in
.reference/mojo) + mission re-readMojo agrees with Rust/Zig, verified in the tree rather than from memory:
Pointer(to=x)/UnsafePointer(to=x)(test_pointer.mojo:63).Int(ptr),UInt64(ptr)(test_pointer.mojo:233-242).refcan't become anInt; you must construct a Pointer first, then explicitly convert.So the reference field is 3-to-1: Rust (E0606), Zig (
@intFromPtr), and Mojo all require named address-of → explicit pointer-to-int, with no borrow-to-integer spelling. C is the lone divergent — and C reads&xcast to integer as the address, which is the worst possible interaction with our mission's own audience: a C migrant writing&x as u64expects an address and With silently hands them the value.Mission re-read — the letter of the law decides this more strongly than I first argued:
Under the value reading, the
&in&x as u64is an unnecessary character (x as u64is the value, one character shorter). By the mission's own sentence, that spelling is already a defect — the proposal isn't adding ceremony or a new rule, it's the compiler enforcing the no-ceremony law at a seam where the surplus character actively misleads. "Remove ceremony without removing guardrails": this guardrail removes zero legitimate programs.One implementation note: the migrator is unaffected — the pcre2 re-migration already emits the correct raw spellings (
&raw const …[0] as …, verified in the promoted zone), so the error would only ever fire on hand-written source.Revised BDFL prediction (90%): accept — every reference except C requires the two-step spelling, C's divergence points the other way (address), and the mission's "every unnecessary character is a compiler failure" makes the value reading illegal by principle.
ehartford commentedon Aug 30, 2026
Canonized as D31 (commit
78d15891, battery green, reseeded):@intFromPtr, MojoPointer(to=x)+Int(ptr); C reads the address — the divergence that made the silent value reading burn C migrants).`&raw const place as u64`for the address,`place as u64`for the value.&rawand the blessed&place as *Tpointer spelling are unaffected.behav_d31_borrow_int_cast(expect-check-fail) andbehav_d31_borrow_int_cast_ok— both intents run, and the original str-punning-over-&raw mutpattern from this issue is proven end-to-end (raw stores at every offset honored across a call; prints 3/65).rt/platform files + ClangBridge rewritten to&raw const— each had only worked because a non-Copy-compatible pointee fell through to address semantics, the incoherence this ruling removes.The originally-reported miscompile does not exist (see the root-cause comment above); the real defect — a spelling whose only plausible intent silently did something else — is now a compile error with a fix-it. Closing.
D31: a borrow does not cast to an integer (#888)