1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//! Phase 4b Round 5c-2-α jit-ref-param-chain-stamp regression tests.
//!
//! Pins the JIT-side ref-param dereference fix per ADR-006 §2.7.13
//! ref-chain stamp + §2.7.5 producer-side stamp + supervisor ratify
//! 2026-05-19 (v0.3-gating soundness).
//!
//! ## Background
//!
//! The bytecode compiler at `compiler/expressions/identifiers.rs:219-221`
//! emits `OpCode::DerefLoad` for identifier reads of reference parameters
//! and `OpCode::DerefStore` for writes (`compiler/statements.rs` /
//! `helpers_reference.rs:81`). The W14.2-G4-derefstore-drift close
//! (`compiler/functions.rs:1331-1390`, commit `005b5170`, 2026-05-18)
//! fixed the BYTECODE-side producer kind-stamping race for unannotated
//! ref-params that drove a `DerefStore` kind drift.
//!
//! The JIT MIR-lowering at `crates/shape-vm/src/mir/lowering/expr.rs:24-25`
//! returns `Place::Local(slot)` for `Expr::Identifier` lookups WITHOUT
//! emitting `Place::Deref` projections for reference parameters — the
//! slot is treated as if it held the referenced value directly. The
//! JIT-MIR consumer was never updated to honor reference semantics:
//! the `Borrow` site in the caller allocates a stack cell holding the
//! callee's borrowed-value snapshot, the callee reads/writes that slot
//! as a raw value, and the caller's binding never sees the mutation.
//!
//! Empirical W15.2-F SURFACE at HEAD `989b18d6` documented this as a
//! v0.3-gating soundness bug. The fix at
//! `mir_compiler/places.rs::read_place` / `write_place` /
//! `null_place` + `mir_compiler/rvalues.rs::Rvalue::Borrow`
//! short-circuit dispatches ref-param slot accesses through the cell
//! indirection (load/store at the referent address), and re-borrows
//! (`&x` from within `outer(&x) { inner(&x) }`) forward the existing
//! pointer instead of allocating a fresh per-function stack cell.
//!
//! W14.2-G4 was VM-only by composition: the `tools/shape-test` harness's
//! `BytecodeExecutor` (`tools/shape-test/src/shape_test.rs:237`) runs every
//! assertion via the bytecode interpreter, so JIT divergence is not
//! surfaced by `expect_number` assertions. These tests execute through
//! `JITExecutor::execute_program` so VM=JIT regression coverage is
//! direct.
//!
//! Sister-class to:
//! - `closure_dispatch_regression_tests` (Phase 4b Round 4 Surface-1a:
//! `__call__` MethodCall MIR producer routing for IIFE/value-call JIT)
//! - LANG-9-spin-1-identity / W14.2-G4-derefstore-drift
//! (`compile_function_body` producer-side type-tracker setup)
use crateJITExecutor;
use ;
use initialize_shared_runtime;
use WireValue;
/// Primary regression gate: `fn bump(&x: int) { x = x + 1 }; bump(&a)`
/// must mutate the caller's `a` binding through the borrowed cell.
///
/// Pre-fix at HEAD `7eb82205`: JIT returned `5` (caller's `a` un-mutated)
/// while VM returned `6` post-W14.2-G4 close. The JIT-MIR pipeline
/// treated the param slot as if it held the int value directly,
/// computed `x + 1` against the slot's pointer-bits, and wrote the
/// result back into the local slot — never touching the caller's cell.
/// W14.2-G4 sister-test (unannotated form). The W14.2-G4-derefstore-drift
/// fix at `compiler/functions.rs:1339` re-stamps unannotated ref-params
/// to `int` when the body's literal-pairing heuristic recovers the
/// integer-family signal; this regression pin verifies the JIT-MIR
/// auto-deref fix lands the same VM-correct semantics for the
/// unannotated shape.
/// W14.2-G4 sister-test (2-fn chain unannotated). Exercises the
/// `Rvalue::Borrow` re-borrow short-circuit in `rvalues.rs`: when
/// `double_inc(&x)` forwards its own ref-param `x` via `inc(&x)`,
/// the inner call must receive the SAME cell pointer the caller's
/// `double_inc(&a)` carried — not a fresh per-function stack cell
/// that decouples the inner mutation from the outer chain.
/// W14.2-G4 sister-test (3-fn chain unannotated). Pins the re-borrow
/// short-circuit at every level of the chain — the outer
/// `add_four(&a)` borrows the caller's `a`; `add_two(&x)` forwards;
/// `add_one(&x)` forwards. Pre-fix the deepest leaf saw a fresh
/// per-function stack cell carrying a snapshot, decoupling all
/// mutations from the caller's binding.
/// Multi-call regression: three sequential calls through the same
/// ref-param must compose into a +3 net mutation visible to the
/// caller. Pre-fix the JIT silently dropped every call's mutation;
/// post-fix the cell-indirection path threads each `DerefStore`
/// equivalent to the caller's `let mut a` binding.
/// Negative test: writing the ref-param does NOT corrupt the slot's
/// own pointer bits — repeated mutations through `&a` keep the cell
/// pointer alive across calls. Pre-fix the un-deref'd write into
/// the slot WOULD have corrupted the pointer (if any non-trivial
/// computation were performed against it), but the JIT happened to
/// short-circuit out of the dynamic-arith path; the post-fix
/// guarantee is stronger: the slot's pointer is never overwritten,
/// only the cell at the referent address.