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
//! The Intermediate Representation for components and modules.
use ;
use SliceIndex;
pub
/// An append-only vector with stable element addresses.
///
/// `AppendOnlyVec<T>` is a wrapper around `Vec<T>` that enforces a single
/// structural invariant: **elements may be appended, but never removed,
/// reordered, or replaced**.
///
/// This type exists to support IR instrumentation and encoding workflows
/// that rely on *stable identity* of nodes over time, typically via raw
/// pointers or addresses registered during parsing.
///
/// # Motivation
///
/// Many parts of the encoding pipeline associate metadata (such as scope
/// IDs or index spaces) with IR nodes using their memory address. If nodes
/// were allowed to be removed, swapped, or compacted, these addresses would
/// become invalid and lead to subtle, hard-to-debug encoding failures.
///
/// `AppendOnlyVec` provides a constrained mutation model that preserves:
///
/// - **Pointer stability** for all elements
/// - **Index stability** for previously appended elements
/// - **Monotonic growth**, which matches how Wasm sections are built
///
/// This allows instrumentation passes to safely mutate nodes *in place*
/// without invalidating previously registered scope or index information.
///
/// # Allowed Operations
///
/// - Appending new elements to the end of the vector
/// - Iterating over elements (immutably or mutably)
/// - Indexing into the vector
///
/// # Disallowed Operations
///
/// `AppendOnlyVec` intentionally does **not** expose APIs for:
///
/// - Removing elements
/// - Inserting elements at arbitrary positions
/// - Reordering or swapping elements
/// - Clearing the vector
///
/// These operations would invalidate assumptions made by the encoder.
///
/// # Relationship to Scopes
///
/// Nodes stored in an `AppendOnlyVec` may be registered in the scope registry
/// using their address. Because elements are never moved or removed, these
/// registrations remain valid for the entire lifetime of the component.
///
/// For nodes that *may* own scopes, this wrapper is commonly used together
/// with `Box<T>` (e.g. `AppendOnlyVec<Box<Node>>`) to ensure heap allocation
/// and stable pointers.
///
/// # Examples
///
/// ```rust
/// use wirm::ir::AppendOnlyVec;
///
/// let mut vec = AppendOnlyVec::from(vec![42, 100]);
/// for v in vec.iter_mut() {
/// *v += 1;
/// }
///
/// assert_eq!(vec[0], 43);
/// assert_eq!(vec[1], 101);
/// ```
///
/// # Design Notes
///
/// `AppendOnlyVec` is a *semantic* restriction, not a performance abstraction.
/// Internally it may still use a `Vec<T>`, but its API enforces invariants
/// required by the encoder.
///
/// If you need more flexibility (e.g. temporary collections during parsing),
/// use a plain `Vec<T>` instead and transfer elements into an `AppendOnlyVec`
/// once they become part of the component’s stable IR.
///
/// # Panics
///
/// This type does not panic on its own, but misuse of raw pointers or
/// assumptions about append-only behavior elsewhere in the system may
/// result in panics during encoding.
/// General iterator support.
/// Iterator support for references to the vector.