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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//! Mutation-writeback support for `&mut self` method dispatch (ADR-006
//! §2.7.27 / Item 4 ruling, W17-mutation-writeback, 2026-05-12).
//!
//! ## Background
//!
//! The Item 4 ruling adopts Rust-style `&mut self` opt-in for COW
//! container mutating methods. When the compiler sees `let mut s =
//! HashSet(); s.add("a")`, the `add` handler does `Arc::make_mut(&mut
//! hs)` on the receiver Arc and returns the (possibly-cloned) new Arc.
//! Pre-ruling, that new Arc was pushed onto the stack and immediately
//! discarded (the call statement had no consumer), so the binding
//! slot `s` still held the OLD Arc and the mutation was silently lost.
//!
//! The fix is **write-back at the call site**: after `CallMethod`, the
//! compiler emits `Dup; StoreLocal recv` (or `Dup;
//! StoreModuleBinding recv` for top-level bindings) so the binding
//! slot receives the new Arc identity. The result still sits on top of
//! the stack as the expression value of `s.add("a")`.
//!
//! ## Scope of `is_mut_self`
//!
//! Only COW container handlers opt in. Specifically: HashSet, HashMap,
//! Array, Deque, PriorityQueue, TypedArray. Their handlers all share
//! the shape `let mut arc = clone_receiver(); Arc::make_mut(&mut arc);
//! ...; Ok(KindedSlot::from_*(arc))`.
//!
//! Interior-mutability primitives (`Mutex`, `Atomic`, `Lazy`,
//! `Channel`) do **not** opt in. Their mutating methods (`Mutex.set`,
//! `Atomic.store`, etc.) preserve the receiver Arc's identity — the
//! mutation reaches the shared interior via `Cell` / `AtomicI64` /
//! channel-buffer. No write-back is required, and `let m = Mutex(0);
//! m.set(5)` stays valid (the binding is immutable; the shared
//! interior changes).
//!
//! ## Compile-time vs runtime
//!
//! Write-back is a **compile-time concern**. The runtime dispatch shell
//! `op_call_method` pops the receiver before invoking the handler, so
//! the source-binding identity is lost by the time the handler returns.
//! The compiler is the only side that knows "this `s.add("a")` came
//! from a local-slot binding"; it emits `Dup; StoreLocal` accordingly.
//!
//! ## Receiver-kind narrowing
//!
//! Method names like `add` overlap between mutating and non-mutating
//! types (`HashSet.add` mutates; `DateTime.add` is the operator-trait
//! backing for `+` and is pure). The compiler narrows by tracking
//! per-local container kind: `let s = HashSet()` populates
//! `mut_self_container_locals[s_local_idx] = ContainerKind::HashSet`;
//! only then does `s.add(x)` emit the write-back. Unknown receivers
//! fall through to the old functional path (the dispatch text's "silent
//! drop" decision-call for r-value receivers like `compute_set().add(x)`
//! is the same shape).
use ;
/// Container-kind classifier for `&mut self` opt-in routing at compile
/// time.
///
/// Populated at let-binding time when the initializer is a recognized
/// container constructor (`Set()`, `HashMap()`, `Deque()`,
/// `PriorityQueue()`, `[…]`); consumed by `compile_expr_method_call`'s
/// write-back gate.
/// Reserved future surface: the place a writeback target can name. Used
/// by the runtime dispatch path when a future hardening pass moves the
/// write-back into `op_call_method` itself.
/// Compile-time write-back target resolved at the method-call site by
/// `resolve_mut_self_writeback_target`. Consumed by the post-CallMethod
/// emit step to lay down `Dup; StoreLocal` (or `Dup;
/// StoreModuleBinding`) so the binding slot receives the new (possibly
/// Arc-cloned) receiver Arc.
/// Write-back ABI mode resolved at the method-call site.
///
/// ADR-006 §2.7.27 base (W17-mutation-writeback): self-returning
/// `&mut self` methods publish the new container Arc as the call's
/// expression value; the compiler emits `Dup; Store*(receiver)` to
/// write back, leaving the new Arc on the stack as the expression
/// value too.
///
/// ADR-006 §2.7.27 amendment (W17-pop-mutation): pop-shaped tuple-
/// return methods follow the conceptual signature
/// `(&mut self) -> (Option<T>, Self)`. The handler returns `Option<T>`
/// (the popped element) via the standard `MethodFnV2` ABI and
/// side-channel-publishes the new container Arc to the VM stack
/// before returning. Post-call stack:
/// `[..., NewContainer, popped_element]`. The compiler emits
/// `Swap; Store*(receiver)` (writes back NewContainer, leaves
/// popped_element on the stack as the call's expression value) or
/// `Swap; Pop` (silent drop for r-value receivers — mirror of the
/// §2.7.27 self-returning r-value silent-drop rule).
/// Reserved future surface — runtime helper for write-back through a
/// `WriteBackTarget` after the handler returns. Currently unused; the
/// compile-time emission path covers Commit 1. Kept as a stub so the
/// follow-up hardening can wire it in without churning the module
/// shape.