lemmascript 0.6.12 → 0.6.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/tools/dist/alias-check-pass.js +1049 -0
- package/tools/dist/alias-check.js +67 -0
- package/tools/dist/autohavoc.js +4 -0
- package/tools/dist/builtins.js +1 -1
- package/tools/dist/checked-reads.js +406 -0
- package/tools/dist/condition-facts.js +64 -26
- package/tools/dist/dafny-emit.js +42 -5
- package/tools/dist/extract.js +52 -20
- package/tools/dist/ir.js +2 -1
- package/tools/dist/lean-emit.js +132 -12
- package/tools/dist/lsc.js +2 -1
- package/tools/dist/narrow-checks.js +421 -0
- package/tools/dist/narrow.js +106 -12
- package/tools/dist/peephole.js +1 -0
- package/tools/dist/place-lowering.js +219 -0
- package/tools/dist/places.js +123 -0
- package/tools/dist/resolve.js +591 -103
- package/tools/dist/specparser.js +10 -6
- package/tools/dist/transform.js +922 -113
- package/tools/dist/typedir.js +19 -1
- package/tools/dist/types.js +52 -15
|
@@ -0,0 +1,1049 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Intra-procedural live-alias mutation checker (`midspiral/LemmaScript#387`,
|
|
3
|
+
* Milestone 3b / `robertpenner/LemmaScript#50`).
|
|
4
|
+
*
|
|
5
|
+
* Mirrors `NoLiveAliasAtTarget` in `formal/narrowing-model.dfy`:
|
|
6
|
+
* - Read-only aliases (`const b = a; return a.length + b.length`) are ALLOWED.
|
|
7
|
+
* - Dead-source moves (`const b = a; b.push(1); return b.length`) are ALLOWED.
|
|
8
|
+
* - Live-alias mutations (`const b = a; b.push(1); return a.length`) are
|
|
9
|
+
* REJECTED with a diagnostic naming both bindings.
|
|
10
|
+
*
|
|
11
|
+
* Uses the self-verified alias conflict kernel from `alias-check.ts`.
|
|
12
|
+
*/
|
|
13
|
+
import { declOfTy } from "./typedecls.js";
|
|
14
|
+
import { isMutatingCollectionMethod } from "./place-lowering.js";
|
|
15
|
+
import { bindCopyGroup, bindFreshGroup, findLiveAliasConflict } from "./alias-check.js";
|
|
16
|
+
function cloneAliasState(s) {
|
|
17
|
+
const sp = new Map();
|
|
18
|
+
for (const [k, v] of s.shallowParents.entries())
|
|
19
|
+
sp.set(k, new Set(v));
|
|
20
|
+
return { map: new Map(s.map), shallowParents: sp, nextId: s.nextId };
|
|
21
|
+
}
|
|
22
|
+
/** True for JS types whose values have mutable reference identity in JS
|
|
23
|
+
* (arrays, maps, sets, tuples, records, discriminated unions, and optionals
|
|
24
|
+
* wrapping them). Primitives and string-unions are immutable values. */
|
|
25
|
+
export function isMutableRefTy(ty, typeDecls) {
|
|
26
|
+
switch (ty.kind) {
|
|
27
|
+
case "array":
|
|
28
|
+
case "map":
|
|
29
|
+
case "set":
|
|
30
|
+
case "tuple":
|
|
31
|
+
return true;
|
|
32
|
+
case "optional":
|
|
33
|
+
case "nullish":
|
|
34
|
+
return isMutableRefTy(ty.inner, typeDecls);
|
|
35
|
+
case "user": {
|
|
36
|
+
const decl = declOfTy(typeDecls, ty);
|
|
37
|
+
if (!decl)
|
|
38
|
+
return false;
|
|
39
|
+
if (decl.kind === "string-union" || decl.kind === "opaque")
|
|
40
|
+
return false;
|
|
41
|
+
if (decl.kind === "alias")
|
|
42
|
+
return decl.aliasOfTy ? isMutableRefTy(decl.aliasOfTy, typeDecls) : false;
|
|
43
|
+
return true; // record or discriminated-union
|
|
44
|
+
}
|
|
45
|
+
default:
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/** True if values of `ty` contain nested mutable references (so a shallow copy
|
|
50
|
+
* such as `[...a]`, `new Map(m)`, or `{ ...obj }` still shares inner mutable
|
|
51
|
+
* objects with the source). */
|
|
52
|
+
function hasNestedMutableRef(ty, typeDecls, seen = new Set()) {
|
|
53
|
+
switch (ty.kind) {
|
|
54
|
+
case "array":
|
|
55
|
+
case "set":
|
|
56
|
+
return isMutableRefTy(ty.elem, typeDecls);
|
|
57
|
+
case "map":
|
|
58
|
+
return isMutableRefTy(ty.value, typeDecls);
|
|
59
|
+
case "tuple":
|
|
60
|
+
return ty.elems.some(e => isMutableRefTy(e, typeDecls));
|
|
61
|
+
case "optional":
|
|
62
|
+
case "nullish":
|
|
63
|
+
return hasNestedMutableRef(ty.inner, typeDecls, seen);
|
|
64
|
+
case "user": {
|
|
65
|
+
const decl = declOfTy(typeDecls, ty);
|
|
66
|
+
if (!decl || seen.has(decl.name))
|
|
67
|
+
return false;
|
|
68
|
+
seen.add(decl.name);
|
|
69
|
+
if (decl.kind === "record" && decl.fields) {
|
|
70
|
+
return decl.fields.some(f => f.type ? isMutableRefTy(f.type, typeDecls) : false);
|
|
71
|
+
}
|
|
72
|
+
if (decl.kind === "discriminated-union" && decl.variants) {
|
|
73
|
+
return decl.variants.some(v => v.fields.some(f => f.type ? isMutableRefTy(f.type, typeDecls) : false));
|
|
74
|
+
}
|
|
75
|
+
if (decl.kind === "alias" && decl.aliasOfTy) {
|
|
76
|
+
return hasNestedMutableRef(decl.aliasOfTy, typeDecls, seen);
|
|
77
|
+
}
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
default:
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/** Root variable of a pure access path `x`, `x.a`, `x.a.b` (or null). */
|
|
85
|
+
function accessPathRoot(e) {
|
|
86
|
+
if (e.kind === "var")
|
|
87
|
+
return e.name;
|
|
88
|
+
if (e.kind === "field")
|
|
89
|
+
return accessPathRoot(e.obj);
|
|
90
|
+
if (e.kind === "checkedUnwrap")
|
|
91
|
+
return accessPathRoot(e.expr);
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
function mergeSourceInfo(parts) {
|
|
95
|
+
const direct = new Set();
|
|
96
|
+
const shallow = new Set();
|
|
97
|
+
const derefBases = new Set();
|
|
98
|
+
for (const p of parts) {
|
|
99
|
+
for (const v of p.direct)
|
|
100
|
+
direct.add(v);
|
|
101
|
+
for (const v of p.shallow)
|
|
102
|
+
shallow.add(v);
|
|
103
|
+
for (const v of p.derefBases)
|
|
104
|
+
derefBases.add(v);
|
|
105
|
+
}
|
|
106
|
+
return { direct: [...direct], shallow: [...shallow], derefBases: [...derefBases] };
|
|
107
|
+
}
|
|
108
|
+
/** Collect all direct and shallow-copy alias sources of `e` (handling
|
|
109
|
+
* conditionals, `??`, `?.`, `||`/`&&`, `record`, `arrayLiteral`, `m.get`,
|
|
110
|
+
* `m.values()`, `m.entries()`, and `freshClone` shallow clones). */
|
|
111
|
+
function aliasSourceInfo(e, typeDecls, allowNonMutableWrap = false) {
|
|
112
|
+
const empty = { direct: [], shallow: [], derefBases: [] };
|
|
113
|
+
if (!allowNonMutableWrap && !isMutableRefTy(e.ty, typeDecls))
|
|
114
|
+
return empty;
|
|
115
|
+
const isClone = "freshClone" in e && !!e.freshClone;
|
|
116
|
+
if (isClone) {
|
|
117
|
+
// Explicit JS shallow clone (`[...a]`, `new Map(m)`, `new Set(s)`, `{ ...a }`):
|
|
118
|
+
// Top-level object is fresh (no direct alias!), but if the cloned container
|
|
119
|
+
// holds nested mutable references, record its underlying roots as `shallow`.
|
|
120
|
+
if (!hasNestedMutableRef(e.ty, typeDecls))
|
|
121
|
+
return empty;
|
|
122
|
+
const inner = aliasSourceInfo({ ...e, freshClone: undefined }, typeDecls, true);
|
|
123
|
+
return {
|
|
124
|
+
direct: [],
|
|
125
|
+
shallow: [...new Set([...inner.direct, ...inner.shallow, ...inner.derefBases])],
|
|
126
|
+
derefBases: [],
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
switch (e.kind) {
|
|
130
|
+
case "var":
|
|
131
|
+
return e.name === "undefined" || e.name === "null"
|
|
132
|
+
? empty
|
|
133
|
+
: { direct: [e.name], shallow: [], derefBases: [] };
|
|
134
|
+
case "field": {
|
|
135
|
+
const base = aliasSourceInfo(e.obj, typeDecls, true);
|
|
136
|
+
const root = accessPathRoot(e.obj);
|
|
137
|
+
const derefs = root ? [root, ...base.derefBases] : base.derefBases;
|
|
138
|
+
return { direct: [...base.direct], shallow: [...base.shallow], derefBases: [...new Set(derefs)] };
|
|
139
|
+
}
|
|
140
|
+
case "index": {
|
|
141
|
+
const base = aliasSourceInfo(e.obj, typeDecls, true);
|
|
142
|
+
const root = accessPathRoot(e.obj);
|
|
143
|
+
const derefs = root ? [root, ...base.derefBases] : base.derefBases;
|
|
144
|
+
return { direct: [...base.direct], shallow: [...base.shallow], derefBases: [...new Set(derefs)] };
|
|
145
|
+
}
|
|
146
|
+
case "call":
|
|
147
|
+
if (e.fn.kind === "var" && e.args.length === 1 && (e.fn.name === "Some" ||
|
|
148
|
+
e.fn.name === "NullishValue" ||
|
|
149
|
+
e.fn.name === "__optNullToNullish" ||
|
|
150
|
+
e.fn.name === "__optUndefToNullish" ||
|
|
151
|
+
e.fn.name === "__nullishToOption" ||
|
|
152
|
+
e.fn.name === "ArrayBranch" ||
|
|
153
|
+
e.fn.name === "NonArrayBranch")) {
|
|
154
|
+
return aliasSourceInfo(e.args[0], typeDecls, true);
|
|
155
|
+
}
|
|
156
|
+
if (e.fn.kind === "field") {
|
|
157
|
+
const m = e.fn.field;
|
|
158
|
+
const objTy = e.fn.obj.ty.kind;
|
|
159
|
+
if ((objTy === "map" && (m === "get" || m === "values" || m === "entries")) ||
|
|
160
|
+
((objTy === "set" || objTy === "array") && (m === "values" || m === "entries"))) {
|
|
161
|
+
const base = aliasSourceInfo(e.fn.obj, typeDecls, true);
|
|
162
|
+
const root = accessPathRoot(e.fn.obj);
|
|
163
|
+
const derefs = root ? [root, ...base.derefBases] : base.derefBases;
|
|
164
|
+
return { direct: [...base.direct], shallow: [...base.shallow], derefBases: [...new Set(derefs)] };
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return empty;
|
|
168
|
+
case "conditional":
|
|
169
|
+
return mergeSourceInfo([
|
|
170
|
+
aliasSourceInfo(e.then, typeDecls),
|
|
171
|
+
aliasSourceInfo(e.else, typeDecls),
|
|
172
|
+
]);
|
|
173
|
+
case "nullish":
|
|
174
|
+
return mergeSourceInfo([
|
|
175
|
+
aliasSourceInfo(e.left, typeDecls),
|
|
176
|
+
aliasSourceInfo(e.right, typeDecls),
|
|
177
|
+
]);
|
|
178
|
+
case "optChain": {
|
|
179
|
+
const base = aliasSourceInfo(e.obj, typeDecls, true);
|
|
180
|
+
const root = accessPathRoot(e.obj);
|
|
181
|
+
const derefs = root ? [root, ...base.derefBases] : base.derefBases;
|
|
182
|
+
return { direct: [...base.direct], shallow: [...base.shallow], derefBases: [...new Set(derefs)] };
|
|
183
|
+
}
|
|
184
|
+
case "binop":
|
|
185
|
+
if (e.op === "||" || e.op === "&&") {
|
|
186
|
+
return mergeSourceInfo([
|
|
187
|
+
aliasSourceInfo(e.left, typeDecls),
|
|
188
|
+
aliasSourceInfo(e.right, typeDecls),
|
|
189
|
+
]);
|
|
190
|
+
}
|
|
191
|
+
return empty;
|
|
192
|
+
case "record": {
|
|
193
|
+
// A record literal / spread `{ ...a, count: 2 }` or `{ items: a.items }` allocates a
|
|
194
|
+
// fresh outer object, so it has no `direct` alias, but its spread and mutable-ref field
|
|
195
|
+
// values share nested references -> record them in `shallow`.
|
|
196
|
+
const shallow = new Set();
|
|
197
|
+
if (e.spread && hasNestedMutableRef(e.ty, typeDecls)) {
|
|
198
|
+
const sp = aliasSourceInfo(e.spread, typeDecls, true);
|
|
199
|
+
for (const v of [...sp.direct, ...sp.shallow, ...sp.derefBases])
|
|
200
|
+
shallow.add(v);
|
|
201
|
+
}
|
|
202
|
+
for (const f of e.fields) {
|
|
203
|
+
if (isMutableRefTy(f.value.ty, typeDecls)) {
|
|
204
|
+
const fi = aliasSourceInfo(f.value, typeDecls, true);
|
|
205
|
+
for (const v of [...fi.direct, ...fi.shallow, ...fi.derefBases])
|
|
206
|
+
shallow.add(v);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return { direct: [], shallow: [...shallow], derefBases: [] };
|
|
210
|
+
}
|
|
211
|
+
case "arrayLiteral": {
|
|
212
|
+
// `[a, b]` allocates a fresh outer array; any mutable-ref elements are shared -> `shallow`.
|
|
213
|
+
const shallow = new Set();
|
|
214
|
+
for (const el of e.elems) {
|
|
215
|
+
if (isMutableRefTy(el.ty, typeDecls)) {
|
|
216
|
+
const ei = aliasSourceInfo(el, typeDecls, true);
|
|
217
|
+
for (const v of [...ei.direct, ...ei.shallow, ...ei.derefBases])
|
|
218
|
+
shallow.add(v);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return { direct: [], shallow: [...shallow], derefBases: [] };
|
|
222
|
+
}
|
|
223
|
+
case "someMatch":
|
|
224
|
+
return mergeSourceInfo([
|
|
225
|
+
aliasSourceInfo(e.someBody, typeDecls),
|
|
226
|
+
aliasSourceInfo(e.noneBody, typeDecls),
|
|
227
|
+
]);
|
|
228
|
+
case "checkedUnwrap":
|
|
229
|
+
return aliasSourceInfo(e.expr, typeDecls, true);
|
|
230
|
+
default:
|
|
231
|
+
return empty;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
// ── Free variable collection ────────────────────────────────
|
|
235
|
+
function collectFreeVarsExpr(e, out) {
|
|
236
|
+
switch (e.kind) {
|
|
237
|
+
case "var":
|
|
238
|
+
if (e.name !== "undefined" && e.name !== "null" && e.name !== "\\result")
|
|
239
|
+
out.add(e.name);
|
|
240
|
+
return;
|
|
241
|
+
case "num":
|
|
242
|
+
case "bigint":
|
|
243
|
+
case "str":
|
|
244
|
+
case "bool":
|
|
245
|
+
case "havoc":
|
|
246
|
+
return;
|
|
247
|
+
case "binop":
|
|
248
|
+
collectFreeVarsExpr(e.left, out);
|
|
249
|
+
collectFreeVarsExpr(e.right, out);
|
|
250
|
+
return;
|
|
251
|
+
case "unop":
|
|
252
|
+
collectFreeVarsExpr(e.expr, out);
|
|
253
|
+
return;
|
|
254
|
+
case "checkedUnwrap":
|
|
255
|
+
collectFreeVarsExpr(e.expr, out);
|
|
256
|
+
return;
|
|
257
|
+
case "call":
|
|
258
|
+
collectFreeVarsExpr(e.fn, out);
|
|
259
|
+
for (const a of e.args)
|
|
260
|
+
collectFreeVarsExpr(a, out);
|
|
261
|
+
return;
|
|
262
|
+
case "index":
|
|
263
|
+
collectFreeVarsExpr(e.obj, out);
|
|
264
|
+
collectFreeVarsExpr(e.idx, out);
|
|
265
|
+
return;
|
|
266
|
+
case "field":
|
|
267
|
+
collectFreeVarsExpr(e.obj, out);
|
|
268
|
+
return;
|
|
269
|
+
case "record":
|
|
270
|
+
if (e.spread)
|
|
271
|
+
collectFreeVarsExpr(e.spread, out);
|
|
272
|
+
for (const f of e.fields)
|
|
273
|
+
collectFreeVarsExpr(f.value, out);
|
|
274
|
+
return;
|
|
275
|
+
case "arrayLiteral":
|
|
276
|
+
for (const el of e.elems)
|
|
277
|
+
collectFreeVarsExpr(el, out);
|
|
278
|
+
return;
|
|
279
|
+
case "lambda": {
|
|
280
|
+
const inner = liveBeforeStmts(e.body, new Set(), new Set(), new Set(), new Set());
|
|
281
|
+
for (const p of e.params)
|
|
282
|
+
inner.delete(p.name);
|
|
283
|
+
for (const v of inner)
|
|
284
|
+
out.add(v);
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
case "conditional":
|
|
288
|
+
collectFreeVarsExpr(e.cond, out);
|
|
289
|
+
collectFreeVarsExpr(e.then, out);
|
|
290
|
+
collectFreeVarsExpr(e.else, out);
|
|
291
|
+
return;
|
|
292
|
+
case "optChain":
|
|
293
|
+
collectFreeVarsExpr(e.obj, out);
|
|
294
|
+
for (const step of e.chain) {
|
|
295
|
+
if (step.kind === "call")
|
|
296
|
+
for (const a of step.args)
|
|
297
|
+
collectFreeVarsExpr(a, out);
|
|
298
|
+
else if (step.kind === "index")
|
|
299
|
+
collectFreeVarsExpr(step.idx, out);
|
|
300
|
+
}
|
|
301
|
+
return;
|
|
302
|
+
case "nullish":
|
|
303
|
+
collectFreeVarsExpr(e.left, out);
|
|
304
|
+
collectFreeVarsExpr(e.right, out);
|
|
305
|
+
return;
|
|
306
|
+
case "someMatch":
|
|
307
|
+
collectFreeVarsExpr(e.scrutinee, out);
|
|
308
|
+
{
|
|
309
|
+
const sb = new Set();
|
|
310
|
+
collectFreeVarsExpr(e.someBody, sb);
|
|
311
|
+
sb.delete(e.binder);
|
|
312
|
+
for (const v of sb)
|
|
313
|
+
out.add(v);
|
|
314
|
+
}
|
|
315
|
+
collectFreeVarsExpr(e.noneBody, out);
|
|
316
|
+
return;
|
|
317
|
+
case "tagMatch":
|
|
318
|
+
collectFreeVarsExpr(e.scrutinee, out);
|
|
319
|
+
for (const c of e.cases)
|
|
320
|
+
collectFreeVarsExpr(c.body, out);
|
|
321
|
+
if (e.fallthrough)
|
|
322
|
+
collectFreeVarsExpr(e.fallthrough, out);
|
|
323
|
+
return;
|
|
324
|
+
case "forall":
|
|
325
|
+
case "exists": {
|
|
326
|
+
const inner = new Set();
|
|
327
|
+
collectFreeVarsExpr(e.body, inner);
|
|
328
|
+
inner.delete(e.var);
|
|
329
|
+
for (const v of inner)
|
|
330
|
+
out.add(v);
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
function freeVarsExpr(e) {
|
|
336
|
+
const s = new Set();
|
|
337
|
+
collectFreeVarsExpr(e, s);
|
|
338
|
+
return s;
|
|
339
|
+
}
|
|
340
|
+
function unionSets(...sets) {
|
|
341
|
+
const out = new Set();
|
|
342
|
+
for (const s of sets)
|
|
343
|
+
for (const v of s)
|
|
344
|
+
out.add(v);
|
|
345
|
+
return out;
|
|
346
|
+
}
|
|
347
|
+
function setsEqual(a, b) {
|
|
348
|
+
if (a.size !== b.size)
|
|
349
|
+
return false;
|
|
350
|
+
for (const v of a)
|
|
351
|
+
if (!b.has(v))
|
|
352
|
+
return false;
|
|
353
|
+
return true;
|
|
354
|
+
}
|
|
355
|
+
/** If statement `s` mutates a storage variable in place (rather than merely
|
|
356
|
+
* rebinding a local variable to a fresh object), return `{ root, nested }`. */
|
|
357
|
+
function mutationInfoOfStmt(s) {
|
|
358
|
+
if (s.kind === "expr") {
|
|
359
|
+
const e = s.expr;
|
|
360
|
+
if (e.kind === "call" && e.fn.kind === "field" && (isMutatingCollectionMethod(e.fn.obj.ty, e.fn.field) ||
|
|
361
|
+
(e.fn.obj.ty.kind === "unknown" && ["push", "unshift", "sort", "set", "add", "delete"].includes(e.fn.field)))) {
|
|
362
|
+
const root = accessPathRoot(e.fn.obj);
|
|
363
|
+
if (!root)
|
|
364
|
+
return null;
|
|
365
|
+
return { root, nested: e.fn.obj.kind === "field" };
|
|
366
|
+
}
|
|
367
|
+
// Optional chaining mutation before narrow: `m.get(k)?.push(v)` or `b?.items?.push(v)` or `arr?.push(v)`
|
|
368
|
+
if (e.kind === "optChain" && e.chain.length >= 2) {
|
|
369
|
+
const last = e.chain[e.chain.length - 1];
|
|
370
|
+
const prev = e.chain[e.chain.length - 2];
|
|
371
|
+
if (last.kind === "call" && prev.kind === "field") {
|
|
372
|
+
const method = prev.name;
|
|
373
|
+
if (["push", "unshift", "sort", "set", "add", "delete"].includes(method)) {
|
|
374
|
+
if (e.obj.kind === "call" && e.obj.fn.kind === "field" && e.obj.fn.field === "get") {
|
|
375
|
+
const root = accessPathRoot(e.obj.fn.obj);
|
|
376
|
+
return root ? { root, nested: true } : null;
|
|
377
|
+
}
|
|
378
|
+
const root = accessPathRoot(e.obj);
|
|
379
|
+
const nested = e.obj.kind === "field" || e.chain.length > 2;
|
|
380
|
+
return root ? { root, nested } : null;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
// Post-narrow `m.get(k)?.push(v)` lowered to expression `someMatch`
|
|
385
|
+
if (e.kind === "someMatch" &&
|
|
386
|
+
e.scrutinee.kind === "call" && e.scrutinee.fn.kind === "field" &&
|
|
387
|
+
e.scrutinee.fn.field === "get" && e.scrutinee.fn.obj.ty.kind === "map" &&
|
|
388
|
+
e.someBody.kind === "call" && e.someBody.fn.kind === "field" &&
|
|
389
|
+
["push", "unshift", "sort", "set", "add", "delete"].includes(e.someBody.fn.field)) {
|
|
390
|
+
const root = accessPathRoot(e.scrutinee.fn.obj);
|
|
391
|
+
return root ? { root, nested: true } : null;
|
|
392
|
+
}
|
|
393
|
+
return null;
|
|
394
|
+
}
|
|
395
|
+
if (s.kind === "let" && s.init.kind === "call" && s.init.fn.kind === "field" &&
|
|
396
|
+
(s.init.fn.field === "pop" || s.init.fn.field === "shift") && s.init.fn.obj.ty.kind === "array") {
|
|
397
|
+
const root = accessPathRoot(s.init.fn.obj);
|
|
398
|
+
return root ? { root, nested: s.init.fn.obj.kind === "field" } : null;
|
|
399
|
+
}
|
|
400
|
+
if (s.kind === "assign") {
|
|
401
|
+
if (s.value.kind === "call" && s.value.fn.kind === "field" &&
|
|
402
|
+
(s.value.fn.field === "pop" || s.value.fn.field === "shift") && s.value.fn.obj.ty.kind === "array") {
|
|
403
|
+
const root = accessPathRoot(s.value.fn.obj);
|
|
404
|
+
return root ? { root, nested: s.value.fn.obj.kind === "field" } : null;
|
|
405
|
+
}
|
|
406
|
+
// Dotted field assignment (`b.count = ...` -> top-level field of `b`; `b.a.x = ...` -> nested).
|
|
407
|
+
if (s.target.includes(".")) {
|
|
408
|
+
const parts = s.target.split(".");
|
|
409
|
+
const root = parts[0];
|
|
410
|
+
return { root, nested: parts.length > 2 };
|
|
411
|
+
}
|
|
412
|
+
// Array index assignment (`arr[i] = v` desugared to `arr = arr.with(i, v)`): mutates `arr` in place.
|
|
413
|
+
if (s.value.kind === "call" && s.value.fn.kind === "field" && s.value.fn.field === "with" &&
|
|
414
|
+
s.value.fn.obj.kind === "var" && s.value.fn.obj.name === s.target) {
|
|
415
|
+
return { root: s.target, nested: false };
|
|
416
|
+
}
|
|
417
|
+
return null;
|
|
418
|
+
}
|
|
419
|
+
return null;
|
|
420
|
+
}
|
|
421
|
+
// ── Backward liveness analysis (with explicit break/continue targets) ──
|
|
422
|
+
function liveBeforeStmt(s, liveAfter, ensuresVars, breakLive, continueLive) {
|
|
423
|
+
switch (s.kind) {
|
|
424
|
+
case "return":
|
|
425
|
+
return unionSets(freeVarsExpr(s.value), ensuresVars);
|
|
426
|
+
case "throw":
|
|
427
|
+
return new Set();
|
|
428
|
+
case "break":
|
|
429
|
+
return new Set(breakLive);
|
|
430
|
+
case "continue":
|
|
431
|
+
return new Set(continueLive);
|
|
432
|
+
case "let": {
|
|
433
|
+
const next = new Set(liveAfter);
|
|
434
|
+
next.delete(s.name);
|
|
435
|
+
return unionSets(next, freeVarsExpr(s.init));
|
|
436
|
+
}
|
|
437
|
+
case "ghostLet": {
|
|
438
|
+
const next = new Set(liveAfter);
|
|
439
|
+
next.delete(s.name);
|
|
440
|
+
return unionSets(next, freeVarsExpr(s.init));
|
|
441
|
+
}
|
|
442
|
+
case "assign": {
|
|
443
|
+
const mut = mutationInfoOfStmt(s);
|
|
444
|
+
const next = new Set(liveAfter);
|
|
445
|
+
if (mut) {
|
|
446
|
+
next.add(mut.root);
|
|
447
|
+
}
|
|
448
|
+
else if (!s.target.includes(".")) {
|
|
449
|
+
next.delete(s.target);
|
|
450
|
+
}
|
|
451
|
+
return unionSets(next, freeVarsExpr(s.value));
|
|
452
|
+
}
|
|
453
|
+
case "ghostAssign": {
|
|
454
|
+
const next = new Set(liveAfter);
|
|
455
|
+
next.delete(s.target);
|
|
456
|
+
return unionSets(next, freeVarsExpr(s.value));
|
|
457
|
+
}
|
|
458
|
+
case "expr":
|
|
459
|
+
return unionSets(liveAfter, freeVarsExpr(s.expr));
|
|
460
|
+
case "assert":
|
|
461
|
+
return unionSets(liveAfter, freeVarsExpr(s.expr));
|
|
462
|
+
case "if":
|
|
463
|
+
return unionSets(freeVarsExpr(s.cond), liveBeforeStmts(s.then, liveAfter, ensuresVars, breakLive, continueLive), liveBeforeStmts(s.else, liveAfter, ensuresVars, breakLive, continueLive));
|
|
464
|
+
case "someMatch": {
|
|
465
|
+
const someLive = liveBeforeStmts(s.someBody, liveAfter, ensuresVars, breakLive, continueLive);
|
|
466
|
+
someLive.delete(s.binder);
|
|
467
|
+
return unionSets(freeVarsExpr(s.scrutinee), someLive, liveBeforeStmts(s.noneBody, liveAfter, ensuresVars, breakLive, continueLive));
|
|
468
|
+
}
|
|
469
|
+
case "tagMatch": {
|
|
470
|
+
const parts = [
|
|
471
|
+
freeVarsExpr(s.scrutinee),
|
|
472
|
+
liveBeforeStmts(s.fallthrough, liveAfter, ensuresVars, breakLive, continueLive),
|
|
473
|
+
];
|
|
474
|
+
for (const c of s.cases)
|
|
475
|
+
parts.push(liveBeforeStmts(c.body, liveAfter, ensuresVars, breakLive, continueLive));
|
|
476
|
+
return unionSets(...parts);
|
|
477
|
+
}
|
|
478
|
+
case "switch": {
|
|
479
|
+
const parts = [
|
|
480
|
+
freeVarsExpr(s.expr),
|
|
481
|
+
liveBeforeStmts(s.defaultBody, liveAfter, ensuresVars, liveAfter, continueLive),
|
|
482
|
+
];
|
|
483
|
+
for (const c of s.cases)
|
|
484
|
+
parts.push(liveBeforeStmts(c.body, liveAfter, ensuresVars, liveAfter, continueLive));
|
|
485
|
+
return unionSets(...parts);
|
|
486
|
+
}
|
|
487
|
+
case "while": {
|
|
488
|
+
const headVars = unionSets(freeVarsExpr(s.cond), ...s.invariants.map(freeVarsExpr), ...(s.decreases ? [freeVarsExpr(s.decreases)] : []), ...(s.doneWith ? [freeVarsExpr(s.doneWith)] : []));
|
|
489
|
+
let loopLive = unionSets(liveAfter, headVars);
|
|
490
|
+
for (let iter = 0; iter < 32; iter++) {
|
|
491
|
+
const next = unionSets(loopLive, liveBeforeStmts(s.body, loopLive, ensuresVars, liveAfter, loopLive));
|
|
492
|
+
if (setsEqual(next, loopLive))
|
|
493
|
+
break;
|
|
494
|
+
loopLive = next;
|
|
495
|
+
}
|
|
496
|
+
return loopLive;
|
|
497
|
+
}
|
|
498
|
+
case "forof": {
|
|
499
|
+
const headVars = unionSets(freeVarsExpr(s.iterable), ...s.invariants.map(freeVarsExpr), ...(s.doneWith ? [freeVarsExpr(s.doneWith)] : []));
|
|
500
|
+
let loopLive = unionSets(liveAfter, headVars);
|
|
501
|
+
for (let iter = 0; iter < 32; iter++) {
|
|
502
|
+
const bodyLive = liveBeforeStmts(s.body, loopLive, ensuresVars, liveAfter, loopLive);
|
|
503
|
+
for (const n of s.names)
|
|
504
|
+
bodyLive.delete(n);
|
|
505
|
+
bodyLive.delete(s.idx);
|
|
506
|
+
const next = unionSets(loopLive, bodyLive);
|
|
507
|
+
if (setsEqual(next, loopLive))
|
|
508
|
+
break;
|
|
509
|
+
loopLive = next;
|
|
510
|
+
}
|
|
511
|
+
return loopLive;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
function liveBeforeStmts(stmts, liveAfter, ensuresVars, breakLive, continueLive) {
|
|
516
|
+
let cur = new Set(liveAfter);
|
|
517
|
+
for (let i = stmts.length - 1; i >= 0; i--) {
|
|
518
|
+
cur = liveBeforeStmt(stmts[i], cur, ensuresVars, breakLive, continueLive);
|
|
519
|
+
}
|
|
520
|
+
return cur;
|
|
521
|
+
}
|
|
522
|
+
/** Precompute `liveAfter[i]` for every statement `stmts[i]`. */
|
|
523
|
+
function computeStmtLiveAfter(stmts, liveAfterBlock, ensuresVars, breakLive, continueLive) {
|
|
524
|
+
const result = new Array(stmts.length);
|
|
525
|
+
let cur = new Set(liveAfterBlock);
|
|
526
|
+
for (let i = stmts.length - 1; i >= 0; i--) {
|
|
527
|
+
result[i] = cur;
|
|
528
|
+
cur = liveBeforeStmt(stmts[i], cur, ensuresVars, breakLive, continueLive);
|
|
529
|
+
}
|
|
530
|
+
return result;
|
|
531
|
+
}
|
|
532
|
+
// ── Conservative alias-map + shallowParents join ────────────
|
|
533
|
+
function uniteGroups(map, vars) {
|
|
534
|
+
const present = vars.filter(v => map.has(v));
|
|
535
|
+
if (present.length < 2)
|
|
536
|
+
return map;
|
|
537
|
+
const targetGroup = map.get(present[0]);
|
|
538
|
+
const groupsToMerge = new Set(present.map(v => map.get(v)));
|
|
539
|
+
const next = new Map(map);
|
|
540
|
+
for (const [k, g] of next.entries()) {
|
|
541
|
+
if (groupsToMerge.has(g))
|
|
542
|
+
next.set(k, targetGroup);
|
|
543
|
+
}
|
|
544
|
+
return next;
|
|
545
|
+
}
|
|
546
|
+
function joinAliasStates(states) {
|
|
547
|
+
if (states.length === 0)
|
|
548
|
+
return { map: new Map(), shallowParents: new Map(), nextId: 0 };
|
|
549
|
+
if (states.length === 1)
|
|
550
|
+
return cloneAliasState(states[0]);
|
|
551
|
+
const allVars = new Set();
|
|
552
|
+
let nextId = 0;
|
|
553
|
+
for (const st of states) {
|
|
554
|
+
if (st.nextId > nextId)
|
|
555
|
+
nextId = st.nextId;
|
|
556
|
+
for (const k of st.map.keys())
|
|
557
|
+
allVars.add(k);
|
|
558
|
+
}
|
|
559
|
+
const parent = new Map();
|
|
560
|
+
for (const v of allVars)
|
|
561
|
+
parent.set(v, v);
|
|
562
|
+
const find = (x) => {
|
|
563
|
+
let r = x;
|
|
564
|
+
while (parent.get(r) !== r)
|
|
565
|
+
r = parent.get(r);
|
|
566
|
+
let cur = x;
|
|
567
|
+
while (cur !== r) {
|
|
568
|
+
const p = parent.get(cur);
|
|
569
|
+
parent.set(cur, r);
|
|
570
|
+
cur = p;
|
|
571
|
+
}
|
|
572
|
+
return r;
|
|
573
|
+
};
|
|
574
|
+
const unite = (a, b) => {
|
|
575
|
+
const ra = find(a);
|
|
576
|
+
const rb = find(b);
|
|
577
|
+
if (ra !== rb)
|
|
578
|
+
parent.set(ra, rb);
|
|
579
|
+
};
|
|
580
|
+
for (const st of states) {
|
|
581
|
+
const byGroup = new Map();
|
|
582
|
+
for (const [v, g] of st.map.entries()) {
|
|
583
|
+
const rep = byGroup.get(g);
|
|
584
|
+
if (rep !== undefined)
|
|
585
|
+
unite(rep, v);
|
|
586
|
+
else
|
|
587
|
+
byGroup.set(g, v);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
const rootToId = new Map();
|
|
591
|
+
const joined = new Map();
|
|
592
|
+
for (const v of allVars) {
|
|
593
|
+
const r = find(v);
|
|
594
|
+
let id = rootToId.get(r);
|
|
595
|
+
if (id === undefined) {
|
|
596
|
+
id = nextId++;
|
|
597
|
+
rootToId.set(r, id);
|
|
598
|
+
}
|
|
599
|
+
joined.set(v, id);
|
|
600
|
+
}
|
|
601
|
+
const shallowParents = new Map();
|
|
602
|
+
for (const st of states) {
|
|
603
|
+
for (const [k, ps] of st.shallowParents.entries()) {
|
|
604
|
+
let dst = shallowParents.get(k);
|
|
605
|
+
if (!dst) {
|
|
606
|
+
dst = new Set();
|
|
607
|
+
shallowParents.set(k, dst);
|
|
608
|
+
}
|
|
609
|
+
for (const p of ps)
|
|
610
|
+
dst.add(p);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
return { map: joined, shallowParents, nextId };
|
|
614
|
+
}
|
|
615
|
+
function aliasStatesEqual(a, b) {
|
|
616
|
+
if (a.map.size !== b.map.size)
|
|
617
|
+
return false;
|
|
618
|
+
for (const [k1, g1] of a.map.entries()) {
|
|
619
|
+
if (!b.map.has(k1))
|
|
620
|
+
return false;
|
|
621
|
+
const b1 = b.map.get(k1);
|
|
622
|
+
for (const [k2, g2] of a.map.entries()) {
|
|
623
|
+
if ((g1 === g2) !== (b1 === b.map.get(k2)))
|
|
624
|
+
return false;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
if (a.shallowParents.size !== b.shallowParents.size)
|
|
628
|
+
return false;
|
|
629
|
+
for (const [k, s1] of a.shallowParents.entries()) {
|
|
630
|
+
const s2 = b.shallowParents.get(k);
|
|
631
|
+
if (!s2 || !setsEqual(s1, s2))
|
|
632
|
+
return false;
|
|
633
|
+
}
|
|
634
|
+
return true;
|
|
635
|
+
}
|
|
636
|
+
/** Apply binding `dst = initExpr` to `state`, updating both direct alias
|
|
637
|
+
* equivalence classes and `shallowParents` (including promoting `shallowParents`
|
|
638
|
+
* of dereferenced bases into direct aliases when extracting nested mutable members). */
|
|
639
|
+
function bindAliasFromExpr(state, dst, srcInfo) {
|
|
640
|
+
const next = cloneAliasState(state);
|
|
641
|
+
// Start `dst` in a fresh group first (clears prior direct alias on reassignment).
|
|
642
|
+
next.map = bindFreshGroup(next.map, dst, next.nextId);
|
|
643
|
+
next.nextId += 1;
|
|
644
|
+
next.shallowParents.delete(dst);
|
|
645
|
+
// Collect all direct alias partners: explicit `direct` roots PLUS `shallowParents`
|
|
646
|
+
// of any dereferenced base (e.g. `const row = b[0]` where `b` is a shallow copy of `a`
|
|
647
|
+
// -> `row` directly aliases `a`'s inner element!).
|
|
648
|
+
const directPartners = new Set();
|
|
649
|
+
for (const d of srcInfo.direct) {
|
|
650
|
+
if (d !== dst && next.map.has(d))
|
|
651
|
+
directPartners.add(d);
|
|
652
|
+
}
|
|
653
|
+
for (const base of srcInfo.derefBases) {
|
|
654
|
+
const sp = state.shallowParents.get(base);
|
|
655
|
+
if (sp) {
|
|
656
|
+
for (const p of sp) {
|
|
657
|
+
if (p !== dst && next.map.has(p))
|
|
658
|
+
directPartners.add(p);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
if (directPartners.size > 0) {
|
|
663
|
+
const first = [...directPartners][0];
|
|
664
|
+
next.map = bindCopyGroup(next.map, dst, first, next.nextId);
|
|
665
|
+
next.nextId += 1;
|
|
666
|
+
if (directPartners.size > 1) {
|
|
667
|
+
next.map = uniteGroups(next.map, [dst, ...directPartners]);
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
// Record shallow parents (including transitive shallow parents of any direct/shallow source).
|
|
671
|
+
const newShallow = new Set();
|
|
672
|
+
for (const s of srcInfo.shallow) {
|
|
673
|
+
if (s !== dst && next.map.has(s))
|
|
674
|
+
newShallow.add(s);
|
|
675
|
+
const parentSp = state.shallowParents.get(s);
|
|
676
|
+
if (parentSp)
|
|
677
|
+
for (const p of parentSp)
|
|
678
|
+
if (p !== dst && next.map.has(p))
|
|
679
|
+
newShallow.add(p);
|
|
680
|
+
}
|
|
681
|
+
for (const d of directPartners) {
|
|
682
|
+
const parentSp = state.shallowParents.get(d);
|
|
683
|
+
if (parentSp)
|
|
684
|
+
for (const p of parentSp)
|
|
685
|
+
if (p !== dst && next.map.has(p))
|
|
686
|
+
newShallow.add(p);
|
|
687
|
+
}
|
|
688
|
+
if (newShallow.size > 0)
|
|
689
|
+
next.shallowParents.set(dst, newShallow);
|
|
690
|
+
return next;
|
|
691
|
+
}
|
|
692
|
+
/** Find a live conflicting alias for `mutated` (checking direct alias group, and
|
|
693
|
+
* for nested mutations also checking `shallowParents` and any other variable
|
|
694
|
+
* whose `shallowParents` includes `mutated` or shares its direct group). */
|
|
695
|
+
function findConflictForMutation(mut, state, liveAfter) {
|
|
696
|
+
const candidates = [...state.map.keys()];
|
|
697
|
+
const direct = findLiveAliasConflict(mut.root, state.map, new Set(liveAfter), candidates);
|
|
698
|
+
if (direct !== "")
|
|
699
|
+
return direct;
|
|
700
|
+
if (!mut.nested)
|
|
701
|
+
return "";
|
|
702
|
+
const mutGroup = state.map.get(mut.root);
|
|
703
|
+
// Check shallow parents of `mut.root` (and anyone sharing their direct group).
|
|
704
|
+
const sp = state.shallowParents.get(mut.root);
|
|
705
|
+
if (sp) {
|
|
706
|
+
for (const p of sp) {
|
|
707
|
+
if (p !== mut.root && liveAfter.has(p))
|
|
708
|
+
return p;
|
|
709
|
+
const pg = state.map.get(p);
|
|
710
|
+
if (pg !== undefined) {
|
|
711
|
+
for (const c of candidates) {
|
|
712
|
+
if (c !== mut.root && liveAfter.has(c) && state.map.get(c) === pg)
|
|
713
|
+
return c;
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
// Symmetric check: another live variable `c` is a shallow copy of `mut.root` (or its group).
|
|
719
|
+
for (const [c, cParents] of state.shallowParents.entries()) {
|
|
720
|
+
if (c === mut.root || !liveAfter.has(c))
|
|
721
|
+
continue;
|
|
722
|
+
for (const p of cParents) {
|
|
723
|
+
if (p === mut.root || (mutGroup !== undefined && state.map.get(p) === mutGroup)) {
|
|
724
|
+
return c;
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
return "";
|
|
729
|
+
}
|
|
730
|
+
function checkLambdasInExpr(e, state, liveAfter, ensuresVars, typeDecls, fnName, out) {
|
|
731
|
+
switch (e.kind) {
|
|
732
|
+
case "lambda": {
|
|
733
|
+
let lamState = cloneAliasState(state);
|
|
734
|
+
for (const p of e.params) {
|
|
735
|
+
if (isMutableRefTy(p.ty, typeDecls)) {
|
|
736
|
+
lamState.map = bindFreshGroup(lamState.map, p.name, lamState.nextId);
|
|
737
|
+
lamState.nextId += 1;
|
|
738
|
+
lamState.shallowParents.delete(p.name);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
// Captured variables remain observed by the enclosing scope after the callback returns.
|
|
742
|
+
const lamLiveOut = unionSets(liveAfter, ensuresVars);
|
|
743
|
+
for (const p of e.params)
|
|
744
|
+
lamLiveOut.delete(p.name);
|
|
745
|
+
checkStmts(e.body, lamState, lamLiveOut, lamLiveOut, new Set(), new Set(), typeDecls, fnName, out);
|
|
746
|
+
return;
|
|
747
|
+
}
|
|
748
|
+
case "binop":
|
|
749
|
+
checkLambdasInExpr(e.left, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
750
|
+
checkLambdasInExpr(e.right, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
751
|
+
return;
|
|
752
|
+
case "unop":
|
|
753
|
+
checkLambdasInExpr(e.expr, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
754
|
+
return;
|
|
755
|
+
case "checkedUnwrap":
|
|
756
|
+
checkLambdasInExpr(e.expr, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
757
|
+
return;
|
|
758
|
+
case "call":
|
|
759
|
+
checkLambdasInExpr(e.fn, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
760
|
+
for (const a of e.args)
|
|
761
|
+
checkLambdasInExpr(a, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
762
|
+
return;
|
|
763
|
+
case "index":
|
|
764
|
+
checkLambdasInExpr(e.obj, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
765
|
+
checkLambdasInExpr(e.idx, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
766
|
+
return;
|
|
767
|
+
case "field":
|
|
768
|
+
checkLambdasInExpr(e.obj, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
769
|
+
return;
|
|
770
|
+
case "record":
|
|
771
|
+
if (e.spread)
|
|
772
|
+
checkLambdasInExpr(e.spread, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
773
|
+
for (const f of e.fields)
|
|
774
|
+
checkLambdasInExpr(f.value, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
775
|
+
return;
|
|
776
|
+
case "arrayLiteral":
|
|
777
|
+
for (const el of e.elems)
|
|
778
|
+
checkLambdasInExpr(el, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
779
|
+
return;
|
|
780
|
+
case "conditional":
|
|
781
|
+
checkLambdasInExpr(e.cond, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
782
|
+
checkLambdasInExpr(e.then, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
783
|
+
checkLambdasInExpr(e.else, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
784
|
+
return;
|
|
785
|
+
case "optChain":
|
|
786
|
+
checkLambdasInExpr(e.obj, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
787
|
+
for (const st of e.chain) {
|
|
788
|
+
if (st.kind === "call")
|
|
789
|
+
for (const a of st.args)
|
|
790
|
+
checkLambdasInExpr(a, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
791
|
+
else if (st.kind === "index")
|
|
792
|
+
checkLambdasInExpr(st.idx, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
793
|
+
}
|
|
794
|
+
return;
|
|
795
|
+
case "nullish":
|
|
796
|
+
checkLambdasInExpr(e.left, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
797
|
+
checkLambdasInExpr(e.right, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
798
|
+
return;
|
|
799
|
+
case "someMatch":
|
|
800
|
+
checkLambdasInExpr(e.scrutinee, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
801
|
+
checkLambdasInExpr(e.someBody, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
802
|
+
checkLambdasInExpr(e.noneBody, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
803
|
+
return;
|
|
804
|
+
case "tagMatch":
|
|
805
|
+
checkLambdasInExpr(e.scrutinee, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
806
|
+
for (const c of e.cases)
|
|
807
|
+
checkLambdasInExpr(c.body, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
808
|
+
if (e.fallthrough)
|
|
809
|
+
checkLambdasInExpr(e.fallthrough, state, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
810
|
+
return;
|
|
811
|
+
default:
|
|
812
|
+
return;
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
function checkStmts(stmts, state, liveAfterBlock, ensuresVars, breakLive, continueLive, typeDecls, fnName, out) {
|
|
816
|
+
const liveAfterList = computeStmtLiveAfter(stmts, liveAfterBlock, ensuresVars, breakLive, continueLive);
|
|
817
|
+
let cur = cloneAliasState(state);
|
|
818
|
+
const breakStates = [];
|
|
819
|
+
const continueStates = [];
|
|
820
|
+
for (let i = 0; i < stmts.length; i++) {
|
|
821
|
+
if (!cur)
|
|
822
|
+
break; // unreachable after return/throw/break/continue
|
|
823
|
+
const s = stmts[i];
|
|
824
|
+
const liveAfter = liveAfterList[i];
|
|
825
|
+
// 1. Check any lambdas embedded in expressions of this statement.
|
|
826
|
+
if (s.kind === "let" || s.kind === "ghostLet") {
|
|
827
|
+
checkLambdasInExpr(s.init, cur, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
828
|
+
}
|
|
829
|
+
else if (s.kind === "assign" || s.kind === "ghostAssign" || s.kind === "return") {
|
|
830
|
+
checkLambdasInExpr(s.value, cur, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
831
|
+
}
|
|
832
|
+
else if (s.kind === "expr" || s.kind === "assert") {
|
|
833
|
+
checkLambdasInExpr(s.expr, cur, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
834
|
+
}
|
|
835
|
+
else if (s.kind === "if") {
|
|
836
|
+
checkLambdasInExpr(s.cond, cur, liveAfter, ensuresVars, typeDecls, fnName, out);
|
|
837
|
+
}
|
|
838
|
+
// 2. Check for live-alias conflict if this statement mutates a root in place.
|
|
839
|
+
const mut = mutationInfoOfStmt(s);
|
|
840
|
+
if (mut && cur.map.has(mut.root)) {
|
|
841
|
+
const conflict = findConflictForMutation(mut, cur, liveAfter);
|
|
842
|
+
if (conflict !== "") {
|
|
843
|
+
out.push({
|
|
844
|
+
fn: fnName,
|
|
845
|
+
mutatedVar: mut.root,
|
|
846
|
+
liveAlias: conflict,
|
|
847
|
+
detail: `Live-alias mutation in \`${fnName}\`: mutating \`${mut.root}\` in place while alias ` +
|
|
848
|
+
`\`${conflict}\` is still observed afterward. LemmaScript models arrays, maps, sets, ` +
|
|
849
|
+
`and records as immutable values, so mutating \`${mut.root}\` would not update \`${conflict}\` ` +
|
|
850
|
+
`(clone explicitly or mutate through a single live binding).`,
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
// 3. Advance alias state according to statement kind.
|
|
855
|
+
switch (s.kind) {
|
|
856
|
+
case "return":
|
|
857
|
+
case "throw":
|
|
858
|
+
cur = null;
|
|
859
|
+
break;
|
|
860
|
+
case "break":
|
|
861
|
+
breakStates.push(cloneAliasState(cur));
|
|
862
|
+
cur = null;
|
|
863
|
+
break;
|
|
864
|
+
case "continue":
|
|
865
|
+
continueStates.push(cloneAliasState(cur));
|
|
866
|
+
cur = null;
|
|
867
|
+
break;
|
|
868
|
+
case "let": {
|
|
869
|
+
if (isMutableRefTy(s.ty, typeDecls)) {
|
|
870
|
+
cur = bindAliasFromExpr(cur, s.name, aliasSourceInfo(s.init, typeDecls));
|
|
871
|
+
}
|
|
872
|
+
break;
|
|
873
|
+
}
|
|
874
|
+
case "assign": {
|
|
875
|
+
if (!mut && !s.target.includes(".")) {
|
|
876
|
+
if (isMutableRefTy(s.value.ty, typeDecls)) {
|
|
877
|
+
cur = bindAliasFromExpr(cur, s.target, aliasSourceInfo(s.value, typeDecls));
|
|
878
|
+
}
|
|
879
|
+
else if (cur.map.has(s.target)) {
|
|
880
|
+
const next = cloneAliasState(cur);
|
|
881
|
+
next.map = bindFreshGroup(next.map, s.target, next.nextId);
|
|
882
|
+
next.nextId += 1;
|
|
883
|
+
next.shallowParents.delete(s.target);
|
|
884
|
+
cur = next;
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
break;
|
|
888
|
+
}
|
|
889
|
+
case "if": {
|
|
890
|
+
const thenFlow = checkStmts(s.then, cur, liveAfter, ensuresVars, breakLive, continueLive, typeDecls, fnName, out);
|
|
891
|
+
const elseFlow = checkStmts(s.else, cur, liveAfter, ensuresVars, breakLive, continueLive, typeDecls, fnName, out);
|
|
892
|
+
breakStates.push(...thenFlow.breakStates, ...elseFlow.breakStates);
|
|
893
|
+
continueStates.push(...thenFlow.continueStates, ...elseFlow.continueStates);
|
|
894
|
+
const exits = [thenFlow.out, elseFlow.out].filter((x) => x !== null);
|
|
895
|
+
cur = exits.length > 0 ? joinAliasStates(exits) : null;
|
|
896
|
+
break;
|
|
897
|
+
}
|
|
898
|
+
case "someMatch": {
|
|
899
|
+
const someFlow = checkStmts(s.someBody, cur, liveAfter, ensuresVars, breakLive, continueLive, typeDecls, fnName, out);
|
|
900
|
+
const noneFlow = checkStmts(s.noneBody, cur, liveAfter, ensuresVars, breakLive, continueLive, typeDecls, fnName, out);
|
|
901
|
+
breakStates.push(...someFlow.breakStates, ...noneFlow.breakStates);
|
|
902
|
+
continueStates.push(...someFlow.continueStates, ...noneFlow.continueStates);
|
|
903
|
+
const exits = [someFlow.out, noneFlow.out].filter((x) => x !== null);
|
|
904
|
+
cur = exits.length > 0 ? joinAliasStates(exits) : null;
|
|
905
|
+
break;
|
|
906
|
+
}
|
|
907
|
+
case "tagMatch": {
|
|
908
|
+
const exits = [];
|
|
909
|
+
for (const c of s.cases) {
|
|
910
|
+
const cf = checkStmts(c.body, cur, liveAfter, ensuresVars, breakLive, continueLive, typeDecls, fnName, out);
|
|
911
|
+
if (cf.out)
|
|
912
|
+
exits.push(cf.out);
|
|
913
|
+
breakStates.push(...cf.breakStates);
|
|
914
|
+
continueStates.push(...cf.continueStates);
|
|
915
|
+
}
|
|
916
|
+
const ff = checkStmts(s.fallthrough, cur, liveAfter, ensuresVars, breakLive, continueLive, typeDecls, fnName, out);
|
|
917
|
+
if (ff.out)
|
|
918
|
+
exits.push(ff.out);
|
|
919
|
+
breakStates.push(...ff.breakStates);
|
|
920
|
+
continueStates.push(...ff.continueStates);
|
|
921
|
+
cur = exits.length > 0 ? joinAliasStates(exits) : null;
|
|
922
|
+
break;
|
|
923
|
+
}
|
|
924
|
+
case "switch": {
|
|
925
|
+
const exits = [];
|
|
926
|
+
for (const c of s.cases) {
|
|
927
|
+
const cf = checkStmts(c.body, cur, liveAfter, ensuresVars, liveAfter, continueLive, typeDecls, fnName, out);
|
|
928
|
+
if (cf.out)
|
|
929
|
+
exits.push(cf.out);
|
|
930
|
+
exits.push(...cf.breakStates); // switch catches `break`
|
|
931
|
+
continueStates.push(...cf.continueStates);
|
|
932
|
+
}
|
|
933
|
+
const df = checkStmts(s.defaultBody, cur, liveAfter, ensuresVars, liveAfter, continueLive, typeDecls, fnName, out);
|
|
934
|
+
if (df.out)
|
|
935
|
+
exits.push(df.out);
|
|
936
|
+
exits.push(...df.breakStates);
|
|
937
|
+
continueStates.push(...df.continueStates);
|
|
938
|
+
cur = exits.length > 0 ? joinAliasStates(exits) : null;
|
|
939
|
+
break;
|
|
940
|
+
}
|
|
941
|
+
case "while": {
|
|
942
|
+
const loopLiveAfter = liveBeforeStmt(s, liveAfter, ensuresVars, breakLive, continueLive);
|
|
943
|
+
let loopIn = cur;
|
|
944
|
+
for (let iter = 0; iter < 16; iter++) {
|
|
945
|
+
const dummyOut = [];
|
|
946
|
+
const bf = checkStmts(s.body, loopIn, loopLiveAfter, ensuresVars, liveAfter, loopLiveAfter, typeDecls, fnName, dummyOut);
|
|
947
|
+
const backEdges = [loopIn, ...bf.continueStates];
|
|
948
|
+
if (bf.out)
|
|
949
|
+
backEdges.push(bf.out);
|
|
950
|
+
const merged = joinAliasStates(backEdges);
|
|
951
|
+
if (aliasStatesEqual(merged, loopIn))
|
|
952
|
+
break;
|
|
953
|
+
loopIn = merged;
|
|
954
|
+
}
|
|
955
|
+
const finalFlow = checkStmts(s.body, loopIn, loopLiveAfter, ensuresVars, liveAfter, loopLiveAfter, typeDecls, fnName, out);
|
|
956
|
+
cur = joinAliasStates([loopIn, ...finalFlow.breakStates]);
|
|
957
|
+
break;
|
|
958
|
+
}
|
|
959
|
+
case "forof": {
|
|
960
|
+
const loopLiveAfter = liveBeforeStmt(s, liveAfter, ensuresVars, breakLive, continueLive);
|
|
961
|
+
const iterInfo = aliasSourceInfo(s.iterable, typeDecls, true);
|
|
962
|
+
const iterRoot = accessPathRoot(s.iterable);
|
|
963
|
+
const elemInfo = {
|
|
964
|
+
direct: iterInfo.direct,
|
|
965
|
+
shallow: iterInfo.shallow,
|
|
966
|
+
derefBases: [...new Set([...iterInfo.derefBases, ...(iterRoot ? [iterRoot] : [])])],
|
|
967
|
+
};
|
|
968
|
+
const iterContainerTy = s.iterable.kind === "call" && s.iterable.fn.kind === "field" &&
|
|
969
|
+
(s.iterable.fn.field === "values" || s.iterable.fn.field === "entries")
|
|
970
|
+
? s.iterable.fn.obj.ty
|
|
971
|
+
: s.iterable.ty;
|
|
972
|
+
const iterYieldsMutable = iterContainerTy !== null && hasNestedMutableRef(iterContainerTy, typeDecls);
|
|
973
|
+
let loopIn = cloneAliasState(cur);
|
|
974
|
+
for (let idx = 0; idx < s.names.length; idx++) {
|
|
975
|
+
const n = s.names[idx];
|
|
976
|
+
const ty = s.nameTypes[idx];
|
|
977
|
+
if ((ty && isMutableRefTy(ty, typeDecls)) || ((!ty || ty.kind === "unknown") && iterYieldsMutable)) {
|
|
978
|
+
loopIn = bindAliasFromExpr(loopIn, n, elemInfo);
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
for (let iter = 0; iter < 16; iter++) {
|
|
982
|
+
const dummyOut = [];
|
|
983
|
+
const bf = checkStmts(s.body, loopIn, loopLiveAfter, ensuresVars, liveAfter, loopLiveAfter, typeDecls, fnName, dummyOut);
|
|
984
|
+
const backEdges = [loopIn, ...bf.continueStates];
|
|
985
|
+
if (bf.out)
|
|
986
|
+
backEdges.push(bf.out);
|
|
987
|
+
const merged = joinAliasStates(backEdges);
|
|
988
|
+
if (aliasStatesEqual(merged, loopIn))
|
|
989
|
+
break;
|
|
990
|
+
loopIn = merged;
|
|
991
|
+
}
|
|
992
|
+
const finalFlow = checkStmts(s.body, loopIn, loopLiveAfter, ensuresVars, liveAfter, loopLiveAfter, typeDecls, fnName, out);
|
|
993
|
+
cur = joinAliasStates([loopIn, ...finalFlow.breakStates]);
|
|
994
|
+
break;
|
|
995
|
+
}
|
|
996
|
+
default:
|
|
997
|
+
break;
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
return { out: cur, breakStates, continueStates };
|
|
1001
|
+
}
|
|
1002
|
+
function checkFunctionAliases(fn, fnLabel, typeDecls, out, isClassMethod = false) {
|
|
1003
|
+
let state = { map: new Map(), shallowParents: new Map(), nextId: 1 };
|
|
1004
|
+
const paramNames = new Set();
|
|
1005
|
+
if (isClassMethod) {
|
|
1006
|
+
state.map = bindFreshGroup(state.map, "this", state.nextId);
|
|
1007
|
+
state.nextId += 1;
|
|
1008
|
+
}
|
|
1009
|
+
for (const p of fn.params) {
|
|
1010
|
+
paramNames.add(p.name);
|
|
1011
|
+
if (isMutableRefTy(p.ty, typeDecls)) {
|
|
1012
|
+
state.map = bindFreshGroup(state.map, p.name, state.nextId);
|
|
1013
|
+
state.nextId += 1;
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
// In Dafny/Lean, parameter mentions in `//@ ensures` refer to the entry
|
|
1017
|
+
// (pre-state) parameter value (e.g., `swap.ts`: `\result[i] === arr[j]`),
|
|
1018
|
+
// not a post-mutation executable read of `arr`. Keep non-parameter variables
|
|
1019
|
+
// (and `this` for class methods, where `this.field` in `ensures` is post-state).
|
|
1020
|
+
const ensuresVars = new Set();
|
|
1021
|
+
for (const e of fn.ensures) {
|
|
1022
|
+
for (const v of freeVarsExpr(e)) {
|
|
1023
|
+
if (!paramNames.has(v))
|
|
1024
|
+
ensuresVars.add(v);
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
checkStmts(fn.body, state, ensuresVars, ensuresVars, new Set(), new Set(), typeDecls, fnLabel, out);
|
|
1028
|
+
}
|
|
1029
|
+
/** Check all functions and class methods in `mod` for live-alias mutations. */
|
|
1030
|
+
export function checkModuleLiveAliases(mod) {
|
|
1031
|
+
const out = [];
|
|
1032
|
+
for (const fn of mod.functions) {
|
|
1033
|
+
checkFunctionAliases(fn, fn.name, mod.typeDecls, out, false);
|
|
1034
|
+
}
|
|
1035
|
+
for (const cls of mod.classes ?? []) {
|
|
1036
|
+
for (const m of cls.methods) {
|
|
1037
|
+
checkFunctionAliases(m, `${cls.name}.${m.name}`, mod.typeDecls, out, true);
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
return out;
|
|
1041
|
+
}
|
|
1042
|
+
/** Reject a module if any function mutates a mutable-ref binding while an alias is still live. */
|
|
1043
|
+
export function assertNoLiveAliasMutations(mod) {
|
|
1044
|
+
const conflicts = checkModuleLiveAliases(mod);
|
|
1045
|
+
if (conflicts.length > 0) {
|
|
1046
|
+
throw new Error(conflicts[0].detail);
|
|
1047
|
+
}
|
|
1048
|
+
return mod;
|
|
1049
|
+
}
|