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,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure live-alias conflict kernel for Milestone 3b
|
|
3
|
+
* (`midspiral/LemmaScript#387`, `robertpenner/LemmaScript#50`).
|
|
4
|
+
*
|
|
5
|
+
* Kept strictly inside the LemmaScript subset so its core conflict-detection
|
|
6
|
+
* and alias-binding contracts are self-verified via
|
|
7
|
+
* `npx lsc check --backend=dafny tools/src/alias-check.ts`.
|
|
8
|
+
*/
|
|
9
|
+
//@ pure
|
|
10
|
+
export function lookupAliasGroup(aliases, name) {
|
|
11
|
+
const g = aliases.get(name);
|
|
12
|
+
return g !== undefined ? g : -1;
|
|
13
|
+
}
|
|
14
|
+
/** Bind `name` to a fresh alias group `groupId` (breaking any prior alias). */
|
|
15
|
+
//@ ensures \result.has(name)
|
|
16
|
+
//@ ensures \result.get(name) == groupId
|
|
17
|
+
export function bindFreshGroup(aliases, name, groupId) {
|
|
18
|
+
const next = new Map(aliases);
|
|
19
|
+
next.set(name, groupId);
|
|
20
|
+
return next;
|
|
21
|
+
}
|
|
22
|
+
/** Bind `dst` to share `src`'s alias group if `src` is tracked, else `fallbackId`. */
|
|
23
|
+
//@ ensures \result.has(dst)
|
|
24
|
+
export function bindCopyGroup(aliases, dst, src, fallbackId) {
|
|
25
|
+
const next = new Map(aliases);
|
|
26
|
+
const srcGroup = aliases.get(src);
|
|
27
|
+
if (srcGroup !== undefined) {
|
|
28
|
+
next.set(dst, srcGroup);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
next.set(dst, fallbackId);
|
|
32
|
+
}
|
|
33
|
+
return next;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Find the first candidate variable `other` that:
|
|
37
|
+
* 1. is distinct from `mutatedVar`,
|
|
38
|
+
* 2. is live after the mutation (`liveAfter.has(other)`), and
|
|
39
|
+
* 3. shares the same non-negative alias group ID as `mutatedVar`.
|
|
40
|
+
*
|
|
41
|
+
* Returns `""` when no live alias conflict exists (covering both read-only
|
|
42
|
+
* aliases when no mutation occurs and dead-source moves where the aliased
|
|
43
|
+
* variable is not in `liveAfter`).
|
|
44
|
+
*/
|
|
45
|
+
//@ ensures \result != "" ==> \result != mutatedVar && liveAfter.has(\result)
|
|
46
|
+
export function findLiveAliasConflict(mutatedVar, aliases, liveAfter, candidates) {
|
|
47
|
+
const mutGroup = aliases.get(mutatedVar);
|
|
48
|
+
if (mutGroup === undefined)
|
|
49
|
+
return "";
|
|
50
|
+
let i = 0;
|
|
51
|
+
while (i < candidates.length) {
|
|
52
|
+
//@ invariant 0 <= i && i <= candidates.length
|
|
53
|
+
const cand = candidates[i];
|
|
54
|
+
if (cand !== mutatedVar && liveAfter.has(cand)) {
|
|
55
|
+
const candGroup = aliases.get(cand);
|
|
56
|
+
if (candGroup !== undefined && candGroup === mutGroup) {
|
|
57
|
+
return cand;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
i = i + 1;
|
|
61
|
+
}
|
|
62
|
+
return "";
|
|
63
|
+
}
|
|
64
|
+
/** Boolean wrapper over `findLiveAliasConflict`. */
|
|
65
|
+
export function hasLiveAliasConflict(mutatedVar, aliases, liveAfter, candidates) {
|
|
66
|
+
return findLiveAliasConflict(mutatedVar, aliases, liveAfter, candidates) !== "";
|
|
67
|
+
}
|
package/tools/dist/autohavoc.js
CHANGED
|
@@ -82,6 +82,9 @@ function forEachChildExpr(e, f) {
|
|
|
82
82
|
case "unop":
|
|
83
83
|
f(e.expr);
|
|
84
84
|
return;
|
|
85
|
+
case "checkedUnwrap":
|
|
86
|
+
f(e.expr);
|
|
87
|
+
return;
|
|
85
88
|
case "call":
|
|
86
89
|
f(e.fn);
|
|
87
90
|
e.args.forEach(f);
|
|
@@ -354,6 +357,7 @@ function rewriteExpr(e, hoisted) {
|
|
|
354
357
|
case "havoc": return e;
|
|
355
358
|
case "binop": return { ...e, left: rewriteExpr(e.left, hoisted), right: rewriteExpr(e.right, hoisted) };
|
|
356
359
|
case "unop": return { ...e, expr: rewriteExpr(e.expr, hoisted) };
|
|
360
|
+
case "checkedUnwrap": return { ...e, expr: rewriteExpr(e.expr, hoisted) };
|
|
357
361
|
case "call": return { ...e, fn: rewriteExpr(e.fn, hoisted), args: e.args.map(a => rewriteExpr(a, hoisted)) };
|
|
358
362
|
case "index": return { ...e, obj: rewriteExpr(e.obj, hoisted), idx: rewriteExpr(e.idx, hoisted) };
|
|
359
363
|
case "field": return { ...e, obj: rewriteExpr(e.obj, hoisted) };
|
package/tools/dist/builtins.js
CHANGED
|
@@ -79,7 +79,7 @@ export const BUILTINS = {
|
|
|
79
79
|
}
|
|
80
80
|
return UNKNOWN;
|
|
81
81
|
} },
|
|
82
|
-
"array.with": { ret: () =>
|
|
82
|
+
"array.with": { ret: (objTy) => objTy, pure: true,
|
|
83
83
|
intArgPositions: [0] },
|
|
84
84
|
// ── string ────────────────────────────────────────────────
|
|
85
85
|
"string.trim": { ret: STRING, pure: true },
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Post-narrow checked-read insertion (`midspiral/LemmaScript#387`, Milestone 4 /
|
|
3
|
+
* `robertpenner/LemmaScript#52`).
|
|
4
|
+
*
|
|
5
|
+
* After `narrow.ts`'s `someMatch` / `tagMatch` drivers run, any access path
|
|
6
|
+
* that `resolve.ts` proved non-optional (via `&&`/`||` chains, `if ... else`,
|
|
7
|
+
* `while` presence guards, or definite assignment narrowing) whose underlying
|
|
8
|
+
* storage binding is still `Option<T>` is wrapped in an explicit
|
|
9
|
+
* `{ kind: "checkedUnwrap", expr, ty }`.
|
|
10
|
+
*
|
|
11
|
+
* Lowering `checkedUnwrap` emits a backend-checked projection (`.value` in
|
|
12
|
+
* Dafny, `.get (by grind)` in Lean) directly on the live storage variable,
|
|
13
|
+
* without introducing match-binder copies, sequential `if i_branch0` splits,
|
|
14
|
+
* or unnecessary `i_compareN` temporaries.
|
|
15
|
+
*/
|
|
16
|
+
import { parseTsType } from "./types.js";
|
|
17
|
+
const ARITH = new Set(["+", "-", "*", "/", "%", "**"]);
|
|
18
|
+
function pathKey(e) {
|
|
19
|
+
if (e.kind === "var")
|
|
20
|
+
return e.name;
|
|
21
|
+
if (e.kind === "checkedUnwrap")
|
|
22
|
+
return pathKey(e.expr);
|
|
23
|
+
if (e.kind === "field") {
|
|
24
|
+
const inner = pathKey(e.obj);
|
|
25
|
+
return inner === null ? null : `${inner}.${e.field}`;
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
function isNullable(ty) {
|
|
30
|
+
return ty !== null && (ty.kind === "optional" || ty.kind === "nullish");
|
|
31
|
+
}
|
|
32
|
+
function fieldInfoTy(f) {
|
|
33
|
+
if (!f)
|
|
34
|
+
return null;
|
|
35
|
+
const ty = f.type ?? parseTsType(f.tsType);
|
|
36
|
+
if (!f.optionalKey)
|
|
37
|
+
return ty;
|
|
38
|
+
if (ty.kind === "nullish")
|
|
39
|
+
return ty;
|
|
40
|
+
if (ty.kind === "optional") {
|
|
41
|
+
return ty.absent === "null" ? { kind: "nullish", inner: ty.inner } : ty;
|
|
42
|
+
}
|
|
43
|
+
return { kind: "optional", inner: ty };
|
|
44
|
+
}
|
|
45
|
+
function declField(decl, field, ofVariant) {
|
|
46
|
+
if (decl.kind === "record")
|
|
47
|
+
return decl.fields?.find(f => f.name === field) ?? null;
|
|
48
|
+
if (decl.kind !== "discriminated-union" || !decl.variants)
|
|
49
|
+
return null;
|
|
50
|
+
const variants = ofVariant ? decl.variants.filter(v => v.name === ofVariant) : decl.variants;
|
|
51
|
+
const found = variants.map(v => v.fields.find(f => f.name === field)).filter((f) => !!f);
|
|
52
|
+
if (found.length === 0)
|
|
53
|
+
return null;
|
|
54
|
+
const nullable = found.map(f => isNullable(fieldInfoTy(f)));
|
|
55
|
+
return nullable.every(o => o === nullable[0]) ? found[0] : null;
|
|
56
|
+
}
|
|
57
|
+
function fieldStorageTy(ty, field, ofVariant, mod, depth = 0) {
|
|
58
|
+
if (depth > 8 || ty.kind !== "user")
|
|
59
|
+
return null;
|
|
60
|
+
const cls = mod.classes.find(c => c.name === ty.name);
|
|
61
|
+
if (cls)
|
|
62
|
+
return cls.fields.find(f => f.name === field)?.ty ?? null;
|
|
63
|
+
const decl = mod.typeDecls.find(d => d.name === ty.name);
|
|
64
|
+
if (!decl || (decl.typeParams && decl.typeParams.length > 0))
|
|
65
|
+
return null;
|
|
66
|
+
if (decl.kind === "alias" && decl.aliasOfTy) {
|
|
67
|
+
return fieldStorageTy(decl.aliasOfTy, field, ofVariant, mod, depth + 1);
|
|
68
|
+
}
|
|
69
|
+
return fieldInfoTy(declField(decl, field, ofVariant));
|
|
70
|
+
}
|
|
71
|
+
function storageTyOf(e, scope, mod) {
|
|
72
|
+
if (e.kind === "checkedUnwrap")
|
|
73
|
+
return e.ty;
|
|
74
|
+
const key = pathKey(e);
|
|
75
|
+
if (key !== null) {
|
|
76
|
+
const bound = scope.types.get(key);
|
|
77
|
+
if (bound)
|
|
78
|
+
return bound;
|
|
79
|
+
}
|
|
80
|
+
if (e.kind !== "field")
|
|
81
|
+
return null;
|
|
82
|
+
const objTy = storageTyOf(e.obj, scope, mod);
|
|
83
|
+
if (!objTy || objTy.kind === "optional" || objTy.kind === "nullish")
|
|
84
|
+
return null;
|
|
85
|
+
return fieldStorageTy(objTy, e.field, e.ofVariant, mod);
|
|
86
|
+
}
|
|
87
|
+
function extendScope(scope, names) {
|
|
88
|
+
if (names.length === 0)
|
|
89
|
+
return scope;
|
|
90
|
+
const types = new Map(scope.types);
|
|
91
|
+
for (const n of names) {
|
|
92
|
+
types.set(n.name, n.ty);
|
|
93
|
+
for (const k of [...types.keys()])
|
|
94
|
+
if (k.startsWith(`${n.name}.`))
|
|
95
|
+
types.delete(k);
|
|
96
|
+
}
|
|
97
|
+
return { types };
|
|
98
|
+
}
|
|
99
|
+
function enterSomeMatch(scope, scrutinee, binder, binderTy) {
|
|
100
|
+
const types = new Map(scope.types);
|
|
101
|
+
types.set(binder, binderTy);
|
|
102
|
+
const key = pathKey(scrutinee);
|
|
103
|
+
if (key !== null)
|
|
104
|
+
types.set(key, binderTy);
|
|
105
|
+
return { types };
|
|
106
|
+
}
|
|
107
|
+
function isDefinitelyUnwrappedStamp(ty) {
|
|
108
|
+
return !isNullable(ty) && ty.kind !== "unknown" && ty.kind !== "void" && ty.kind !== "null";
|
|
109
|
+
}
|
|
110
|
+
/** Rewrite an expression that is the direct operand of `===`/`!==`/`==`/`!=`
|
|
111
|
+
* with `undefined` or `null`: never wrap the operand itself in `checkedUnwrap`
|
|
112
|
+
* (restore its declared `Option<T>` or `Nullish<T>` storage type so
|
|
113
|
+
* `transform.ts` emits the right presence check), while still rewriting any
|
|
114
|
+
* optional/nullish receiver prefix. */
|
|
115
|
+
function rewritePresenceOperand(e, scope, mod) {
|
|
116
|
+
const storage = storageTyOf(e, scope, mod);
|
|
117
|
+
if (e.kind === "var" && isNullable(storage)) {
|
|
118
|
+
return { ...e, ty: storage };
|
|
119
|
+
}
|
|
120
|
+
if (e.kind === "field") {
|
|
121
|
+
const obj = rewriteExpr(e.obj, scope, mod);
|
|
122
|
+
const updated = obj === e.obj ? e : { ...e, obj };
|
|
123
|
+
const fStorage = storageTyOf(updated, scope, mod);
|
|
124
|
+
if (isNullable(fStorage))
|
|
125
|
+
return { ...updated, ty: fStorage };
|
|
126
|
+
return updated;
|
|
127
|
+
}
|
|
128
|
+
return rewriteExpr(e, scope, mod);
|
|
129
|
+
}
|
|
130
|
+
function isNullishVar(e) {
|
|
131
|
+
return e.kind === "var" && (e.name === "undefined" || e.name === "null");
|
|
132
|
+
}
|
|
133
|
+
function rewriteExpr(e, scope, mod) {
|
|
134
|
+
switch (e.kind) {
|
|
135
|
+
case "var": {
|
|
136
|
+
const storage = storageTyOf(e, scope, mod);
|
|
137
|
+
if (isNullable(storage)) {
|
|
138
|
+
if (isDefinitelyUnwrappedStamp(e.ty)) {
|
|
139
|
+
return { kind: "checkedUnwrap", expr: { ...e, ty: storage }, ty: e.ty };
|
|
140
|
+
}
|
|
141
|
+
// Partially narrowed `fromNullish` value still lives in `Nullish<T>` storage.
|
|
142
|
+
if (storage.kind === "nullish" && e.ty.kind === "optional" && e.ty.fromNullish) {
|
|
143
|
+
return { ...e, ty: storage };
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return e;
|
|
147
|
+
}
|
|
148
|
+
case "num":
|
|
149
|
+
case "bigint":
|
|
150
|
+
case "str":
|
|
151
|
+
case "bool":
|
|
152
|
+
case "havoc":
|
|
153
|
+
return e;
|
|
154
|
+
case "checkedUnwrap":
|
|
155
|
+
return { ...e, expr: rewriteExpr(e.expr, scope, mod) };
|
|
156
|
+
case "field": {
|
|
157
|
+
// Rewrite receiver first (so an optional/nullish receiver `box` becomes `checkedUnwrap(box)`).
|
|
158
|
+
const obj = rewriteExpr(e.obj, scope, mod);
|
|
159
|
+
const updated = obj === e.obj ? e : { ...e, obj };
|
|
160
|
+
// Check if this field itself is optional/nullish by declaration (or bound in scope) while stamped non-nullable.
|
|
161
|
+
const storage = storageTyOf(updated, scope, mod);
|
|
162
|
+
if (isNullable(storage)) {
|
|
163
|
+
if (isDefinitelyUnwrappedStamp(updated.ty)) {
|
|
164
|
+
return { kind: "checkedUnwrap", expr: { ...updated, ty: storage }, ty: updated.ty };
|
|
165
|
+
}
|
|
166
|
+
if (storage.kind === "nullish" && updated.ty.kind === "optional" && updated.ty.fromNullish) {
|
|
167
|
+
return { ...updated, ty: storage };
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return updated;
|
|
171
|
+
}
|
|
172
|
+
case "index":
|
|
173
|
+
return { ...e, obj: rewriteExpr(e.obj, scope, mod), idx: rewriteExpr(e.idx, scope, mod) };
|
|
174
|
+
case "binop": {
|
|
175
|
+
let left;
|
|
176
|
+
let right;
|
|
177
|
+
if (["===", "!==", "==", "!="].includes(e.op) && (isNullishVar(e.left) || isNullishVar(e.right))) {
|
|
178
|
+
left = isNullishVar(e.right) ? rewritePresenceOperand(e.left, scope, mod) : rewriteExpr(e.left, scope, mod);
|
|
179
|
+
right = isNullishVar(e.left) ? rewritePresenceOperand(e.right, scope, mod) : rewriteExpr(e.right, scope, mod);
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
left = rewriteExpr(e.left, scope, mod);
|
|
183
|
+
right = rewriteExpr(e.right, scope, mod);
|
|
184
|
+
}
|
|
185
|
+
let ty = e.ty;
|
|
186
|
+
// Clear stale optional/nullish arithmetic stamp once operands are unwrapped.
|
|
187
|
+
if (ARITH.has(e.op) && isNullable(ty) && !isNullable(left.ty) && !isNullable(right.ty)) {
|
|
188
|
+
ty = left.ty;
|
|
189
|
+
}
|
|
190
|
+
return { ...e, left, right, ty };
|
|
191
|
+
}
|
|
192
|
+
case "unop": {
|
|
193
|
+
const expr = rewriteExpr(e.expr, scope, mod);
|
|
194
|
+
let ty = e.ty;
|
|
195
|
+
if (e.op === "-" && isNullable(ty) && !isNullable(expr.ty))
|
|
196
|
+
ty = expr.ty;
|
|
197
|
+
return { ...e, expr, ty };
|
|
198
|
+
}
|
|
199
|
+
case "call":
|
|
200
|
+
return {
|
|
201
|
+
...e,
|
|
202
|
+
fn: rewriteExpr(e.fn, scope, mod),
|
|
203
|
+
args: e.args.map(a => rewriteExpr(a, scope, mod)),
|
|
204
|
+
};
|
|
205
|
+
case "record":
|
|
206
|
+
return {
|
|
207
|
+
...e,
|
|
208
|
+
spread: e.spread ? rewriteExpr(e.spread, scope, mod) : null,
|
|
209
|
+
fields: e.fields.map(f => ({ ...f, value: rewriteExpr(f.value, scope, mod) })),
|
|
210
|
+
};
|
|
211
|
+
case "arrayLiteral":
|
|
212
|
+
return { ...e, elems: e.elems.map(el => rewriteExpr(el, scope, mod)) };
|
|
213
|
+
case "lambda": {
|
|
214
|
+
const inner = extendScope(scope, e.params.map(p => ({ name: p.name, ty: p.ty })));
|
|
215
|
+
return { ...e, body: rewriteStmts(e.body, inner, mod) };
|
|
216
|
+
}
|
|
217
|
+
case "conditional":
|
|
218
|
+
return {
|
|
219
|
+
...e,
|
|
220
|
+
cond: rewriteExpr(e.cond, scope, mod),
|
|
221
|
+
then: rewriteExpr(e.then, scope, mod),
|
|
222
|
+
else: rewriteExpr(e.else, scope, mod),
|
|
223
|
+
};
|
|
224
|
+
case "optChain":
|
|
225
|
+
return {
|
|
226
|
+
...e,
|
|
227
|
+
obj: rewriteExpr(e.obj, scope, mod),
|
|
228
|
+
chain: e.chain.map(st => st.kind === "call"
|
|
229
|
+
? { ...st, args: st.args.map(a => rewriteExpr(a, scope, mod)) }
|
|
230
|
+
: st.kind === "index"
|
|
231
|
+
? { ...st, idx: rewriteExpr(st.idx, scope, mod) }
|
|
232
|
+
: st),
|
|
233
|
+
};
|
|
234
|
+
case "nullish":
|
|
235
|
+
return { ...e, left: rewriteExpr(e.left, scope, mod), right: rewriteExpr(e.right, scope, mod) };
|
|
236
|
+
case "someMatch": {
|
|
237
|
+
// Do NOT unwrap the scrutinee itself (it is supposed to remain Option<T>),
|
|
238
|
+
// but DO rewrite subexpressions inside non-var scrutinees (e.g. `box.inner`
|
|
239
|
+
// or `(flag ? x : undefined)`).
|
|
240
|
+
const scrutinee = e.scrutinee.kind === "field"
|
|
241
|
+
? { ...e.scrutinee, obj: rewriteExpr(e.scrutinee.obj, scope, mod) }
|
|
242
|
+
: e.scrutinee.kind === "var"
|
|
243
|
+
? e.scrutinee
|
|
244
|
+
: rewriteExpr(e.scrutinee, scope, mod);
|
|
245
|
+
const someScope = enterSomeMatch(scope, e.scrutinee, e.binder, e.binderTy);
|
|
246
|
+
return {
|
|
247
|
+
...e,
|
|
248
|
+
scrutinee,
|
|
249
|
+
someBody: rewriteExpr(e.someBody, someScope, mod),
|
|
250
|
+
noneBody: rewriteExpr(e.noneBody, scope, mod),
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
case "tagMatch":
|
|
254
|
+
return {
|
|
255
|
+
...e,
|
|
256
|
+
scrutinee: rewriteExpr(e.scrutinee, scope, mod),
|
|
257
|
+
cases: e.cases.map(c => ({ ...c, body: rewriteExpr(c.body, scope, mod) })),
|
|
258
|
+
fallthrough: e.fallthrough ? rewriteExpr(e.fallthrough, scope, mod) : null,
|
|
259
|
+
};
|
|
260
|
+
case "forall":
|
|
261
|
+
case "exists": {
|
|
262
|
+
const inner = extendScope(scope, [{ name: e.var, ty: e.varTy }]);
|
|
263
|
+
return { ...e, body: rewriteExpr(e.body, inner, mod) };
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
function rewriteStmts(stmts, scope, mod) {
|
|
268
|
+
let cur = scope;
|
|
269
|
+
const out = [];
|
|
270
|
+
for (const s of stmts) {
|
|
271
|
+
switch (s.kind) {
|
|
272
|
+
case "let": {
|
|
273
|
+
const init = rewriteExpr(s.init, cur, mod);
|
|
274
|
+
out.push({ ...s, init });
|
|
275
|
+
cur = extendScope(cur, [{ name: s.name, ty: s.ty }]);
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
case "ghostLet": {
|
|
279
|
+
const init = rewriteExpr(s.init, cur, mod);
|
|
280
|
+
out.push({ ...s, init });
|
|
281
|
+
cur = extendScope(cur, [{ name: s.name, ty: s.ty }]);
|
|
282
|
+
break;
|
|
283
|
+
}
|
|
284
|
+
case "assign":
|
|
285
|
+
out.push({ ...s, value: rewriteExpr(s.value, cur, mod) });
|
|
286
|
+
break;
|
|
287
|
+
case "ghostAssign":
|
|
288
|
+
out.push({ ...s, value: rewriteExpr(s.value, cur, mod) });
|
|
289
|
+
break;
|
|
290
|
+
case "return":
|
|
291
|
+
out.push({ ...s, value: rewriteExpr(s.value, cur, mod) });
|
|
292
|
+
break;
|
|
293
|
+
case "expr":
|
|
294
|
+
out.push({ ...s, expr: rewriteExpr(s.expr, cur, mod) });
|
|
295
|
+
break;
|
|
296
|
+
case "assert":
|
|
297
|
+
out.push({ ...s, expr: rewriteExpr(s.expr, cur, mod) });
|
|
298
|
+
break;
|
|
299
|
+
case "break":
|
|
300
|
+
case "continue":
|
|
301
|
+
case "throw":
|
|
302
|
+
out.push(s);
|
|
303
|
+
break;
|
|
304
|
+
case "if":
|
|
305
|
+
out.push({
|
|
306
|
+
...s,
|
|
307
|
+
cond: rewriteExpr(s.cond, cur, mod),
|
|
308
|
+
then: rewriteStmts(s.then, cur, mod),
|
|
309
|
+
else: rewriteStmts(s.else, cur, mod),
|
|
310
|
+
});
|
|
311
|
+
break;
|
|
312
|
+
case "while":
|
|
313
|
+
out.push({
|
|
314
|
+
...s,
|
|
315
|
+
cond: rewriteExpr(s.cond, cur, mod),
|
|
316
|
+
invariants: s.invariants.map(inv => rewriteExpr(inv, cur, mod)),
|
|
317
|
+
decreases: s.decreases ? rewriteExpr(s.decreases, cur, mod) : null,
|
|
318
|
+
doneWith: s.doneWith ? rewriteExpr(s.doneWith, cur, mod) : null,
|
|
319
|
+
body: rewriteStmts(s.body, cur, mod),
|
|
320
|
+
});
|
|
321
|
+
break;
|
|
322
|
+
case "forof": {
|
|
323
|
+
const iterable = rewriteExpr(s.iterable, cur, mod);
|
|
324
|
+
const loopScope = extendScope(cur, [
|
|
325
|
+
...s.names.map((n, i) => ({ name: n, ty: s.nameTypes[i] ?? { kind: "unknown" } })),
|
|
326
|
+
{ name: s.idx, ty: { kind: "nat" } },
|
|
327
|
+
]);
|
|
328
|
+
out.push({
|
|
329
|
+
...s,
|
|
330
|
+
iterable,
|
|
331
|
+
invariants: s.invariants.map(inv => rewriteExpr(inv, loopScope, mod)),
|
|
332
|
+
doneWith: s.doneWith ? rewriteExpr(s.doneWith, loopScope, mod) : null,
|
|
333
|
+
body: rewriteStmts(s.body, loopScope, mod),
|
|
334
|
+
});
|
|
335
|
+
break;
|
|
336
|
+
}
|
|
337
|
+
case "switch":
|
|
338
|
+
out.push({
|
|
339
|
+
...s,
|
|
340
|
+
expr: rewriteExpr(s.expr, cur, mod),
|
|
341
|
+
cases: s.cases.map(c => ({ ...c, body: rewriteStmts(c.body, cur, mod) })),
|
|
342
|
+
defaultBody: rewriteStmts(s.defaultBody, cur, mod),
|
|
343
|
+
});
|
|
344
|
+
break;
|
|
345
|
+
case "someMatch": {
|
|
346
|
+
const scrutinee = s.scrutinee.kind === "field"
|
|
347
|
+
? { ...s.scrutinee, obj: rewriteExpr(s.scrutinee.obj, cur, mod) }
|
|
348
|
+
: s.scrutinee;
|
|
349
|
+
const someScope = enterSomeMatch(cur, s.scrutinee, s.binder, s.binderTy);
|
|
350
|
+
out.push({
|
|
351
|
+
...s,
|
|
352
|
+
scrutinee,
|
|
353
|
+
someBody: rewriteStmts(s.someBody, someScope, mod),
|
|
354
|
+
noneBody: rewriteStmts(s.noneBody, cur, mod),
|
|
355
|
+
});
|
|
356
|
+
break;
|
|
357
|
+
}
|
|
358
|
+
case "tagMatch":
|
|
359
|
+
out.push({
|
|
360
|
+
...s,
|
|
361
|
+
scrutinee: rewriteExpr(s.scrutinee, cur, mod),
|
|
362
|
+
cases: s.cases.map(c => ({ ...c, body: rewriteStmts(c.body, cur, mod) })),
|
|
363
|
+
fallthrough: rewriteStmts(s.fallthrough, cur, mod),
|
|
364
|
+
});
|
|
365
|
+
break;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return out;
|
|
369
|
+
}
|
|
370
|
+
function rewriteFunction(fn, className, globals, mod) {
|
|
371
|
+
const types = new Map(globals);
|
|
372
|
+
for (const p of fn.params)
|
|
373
|
+
types.set(p.name, p.ty);
|
|
374
|
+
if (className !== null)
|
|
375
|
+
types.set("this", { kind: "user", name: className });
|
|
376
|
+
const scope = { types };
|
|
377
|
+
const postTypes = new Map(types);
|
|
378
|
+
postTypes.set("\\result", fn.returnTy);
|
|
379
|
+
const postScope = { types: postTypes };
|
|
380
|
+
return {
|
|
381
|
+
...fn,
|
|
382
|
+
requires: fn.requires.map(r => rewriteExpr(r, scope, mod)),
|
|
383
|
+
decreases: fn.decreases ? rewriteExpr(fn.decreases, scope, mod) : null,
|
|
384
|
+
ensures: fn.ensures.map(e => rewriteExpr(e, postScope, mod)),
|
|
385
|
+
body: rewriteStmts(fn.body, scope, mod),
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
/** Reconcile any narrowed-but-not-someMatch-bound optional reads by inserting
|
|
389
|
+
* explicit `checkedUnwrap` nodes. */
|
|
390
|
+
export function insertCheckedUnwraps(mod) {
|
|
391
|
+
const globals = new Map();
|
|
392
|
+
for (const c of mod.constants)
|
|
393
|
+
globals.set(c.name, c.ty);
|
|
394
|
+
return {
|
|
395
|
+
...mod,
|
|
396
|
+
constants: mod.constants.map(c => ({
|
|
397
|
+
...c,
|
|
398
|
+
value: rewriteExpr(c.value, { types: new Map(globals) }, mod),
|
|
399
|
+
})),
|
|
400
|
+
functions: mod.functions.map(fn => rewriteFunction(fn, null, globals, mod)),
|
|
401
|
+
classes: mod.classes.map(cls => ({
|
|
402
|
+
...cls,
|
|
403
|
+
methods: cls.methods.map(m => rewriteFunction(m, cls.name, globals, mod)),
|
|
404
|
+
})),
|
|
405
|
+
};
|
|
406
|
+
}
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
* State is explicit (§6.1): detection that mints binders or consults type
|
|
20
20
|
* declarations takes a `CondCtx`; nothing module-level.
|
|
21
21
|
*/
|
|
22
|
+
import { isNullishStorageTy, optAbsent } from "./typedir.js";
|
|
22
23
|
import { freshName, freshNameWhere, isUserName } from "./names.js";
|
|
23
24
|
import { unionDeclOfTy, discriminantOf } from "./typedecls.js";
|
|
24
25
|
/** Allocate within this context, retaining a path's name on repeated
|
|
@@ -67,47 +68,84 @@ export function freshPathBinder(e, ctx) {
|
|
|
67
68
|
export function bindPresentFact(f, ctx) {
|
|
68
69
|
return { ...f, binder: freshPathBinder(f.scrutinee, ctx) };
|
|
69
70
|
}
|
|
70
|
-
/** Detect optional checks: `e !== undefined`, `e === undefined`,
|
|
71
|
-
*
|
|
72
|
-
* bare `e`
|
|
73
|
-
*
|
|
71
|
+
/** Detect optional/nullish checks: `e !== undefined`, `e === undefined`,
|
|
72
|
+
* `e !== null`, `e === null`, loose `e == null`/`e != null`/`e == undefined`/`e != undefined`,
|
|
73
|
+
* `!e`, or a bare nullable `e` — for a pure-access-path `Option<T>` or `Nullish<T>`.
|
|
74
|
+
* `!e` and bare `e` are truthiness forms. `!!c` is the same check as `c`.
|
|
75
|
+
* Following TS, only pure access paths narrow; complex scrutinees return null. */
|
|
74
76
|
export function presentFact(cond) {
|
|
75
77
|
if (cond.kind === "unop" && cond.op === "!" && cond.expr.kind === "unop" && cond.expr.op === "!")
|
|
76
78
|
return presentFact(cond.expr.expr);
|
|
77
|
-
// `!e` where e is optional — a truthiness form: false iff e is
|
|
78
|
-
//
|
|
79
|
-
if (cond.kind === "unop" && cond.op === "!" && cond.expr.ty.kind === "optional") {
|
|
79
|
+
// `!e` where e is optional or nullish — a truthiness form: false iff e is
|
|
80
|
+
// absent (None / Null / Undefined) OR its inner value is itself falsy.
|
|
81
|
+
if (cond.kind === "unop" && cond.op === "!" && (cond.expr.ty.kind === "optional" || cond.expr.ty.kind === "nullish")) {
|
|
80
82
|
const e = cond.expr;
|
|
81
83
|
const innerTy = cond.expr.ty.inner;
|
|
82
84
|
const hint = binderHintFor(e);
|
|
83
85
|
if (hint === null)
|
|
84
86
|
return null;
|
|
85
|
-
|
|
87
|
+
const isNullish = isNullishStorageTy(e.ty) ? true : undefined;
|
|
88
|
+
return { scrutinee: e, innerTy, negated: true, binder: freshName(hint), truthiness: true, ...(isNullish ? { isNullish } : {}) };
|
|
86
89
|
}
|
|
87
|
-
if (cond.kind !== "binop" ||
|
|
88
|
-
// Bare
|
|
89
|
-
|
|
90
|
-
if (cond.ty.kind === "optional") {
|
|
90
|
+
if (cond.kind !== "binop" || !["!==", "===", "!=", "=="].includes(cond.op)) {
|
|
91
|
+
// Bare nullable truthiness: `if (e)` where e: T | undefined | null.
|
|
92
|
+
if (cond.ty.kind === "optional" || cond.ty.kind === "nullish") {
|
|
91
93
|
const hint = binderHintFor(cond);
|
|
92
94
|
if (hint === null)
|
|
93
95
|
return null;
|
|
94
|
-
|
|
96
|
+
const isNullish = isNullishStorageTy(cond.ty) ? true : undefined;
|
|
97
|
+
return { scrutinee: cond, innerTy: cond.ty.inner, negated: false, binder: freshName(hint), truthiness: true, ...(isNullish ? { isNullish } : {}) };
|
|
95
98
|
}
|
|
96
99
|
return null;
|
|
97
100
|
}
|
|
98
|
-
// Explicit `e === undefined
|
|
99
|
-
// independent of the inner value (so NOT a truthiness form).
|
|
101
|
+
// Explicit sentinel comparison: `e === undefined`, `e === null`, `e == null`, etc.
|
|
100
102
|
let e = null;
|
|
101
|
-
|
|
103
|
+
let sentinel = null;
|
|
104
|
+
if (cond.right.kind === "var" && (cond.right.name === "undefined" || cond.right.name === "null")) {
|
|
102
105
|
e = cond.left;
|
|
103
|
-
|
|
106
|
+
sentinel = cond.right.name;
|
|
107
|
+
}
|
|
108
|
+
else if (cond.left.kind === "var" && (cond.left.name === "undefined" || cond.left.name === "null")) {
|
|
104
109
|
e = cond.right;
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
if (hint === null)
|
|
110
|
+
sentinel = cond.left.name;
|
|
111
|
+
}
|
|
112
|
+
if (!e || !sentinel)
|
|
109
113
|
return null;
|
|
110
|
-
|
|
114
|
+
const isLoose = cond.op === "==" || cond.op === "!=";
|
|
115
|
+
const negated = cond.op === "===" || cond.op === "==";
|
|
116
|
+
if (e.ty.kind === "nullish") {
|
|
117
|
+
const hint = binderHintFor(e);
|
|
118
|
+
if (hint === null)
|
|
119
|
+
return null;
|
|
120
|
+
// Loose `== null` / `== undefined` checks BOTH sentinels, narrowing directly to T.
|
|
121
|
+
// Strict `=== null` rules out null (leaving T | undefined); strict `=== undefined`
|
|
122
|
+
// rules out undefined (leaving T | null).
|
|
123
|
+
const innerTy = isLoose
|
|
124
|
+
? e.ty.inner
|
|
125
|
+
: sentinel === "null"
|
|
126
|
+
? { kind: "optional", inner: e.ty.inner, absent: "undefined", fromNullish: true }
|
|
127
|
+
: { kind: "optional", inner: e.ty.inner, absent: "null", fromNullish: true };
|
|
128
|
+
return { scrutinee: e, innerTy, negated, binder: freshName(hint), truthiness: false, isNullish: true };
|
|
129
|
+
}
|
|
130
|
+
if (e.ty.kind === "optional") {
|
|
131
|
+
// On single-sentinel Option<T>, strict check against the *other* sentinel
|
|
132
|
+
// never matches and does NOT narrow (`x: T | undefined; x !== null` leaves
|
|
133
|
+
// x as T | undefined). Loose `==`/`!=` against either sentinel checks None.
|
|
134
|
+
if (!isLoose && sentinel !== optAbsent(e.ty))
|
|
135
|
+
return null;
|
|
136
|
+
const hint = binderHintFor(e);
|
|
137
|
+
if (hint === null)
|
|
138
|
+
return null;
|
|
139
|
+
const isNullish = e.ty.fromNullish ? true : undefined;
|
|
140
|
+
return { scrutinee: e, innerTy: e.ty.inner, negated, binder: freshName(hint), truthiness: false, ...(isNullish ? { isNullish } : {}) };
|
|
141
|
+
}
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
/** Presence fact restricted to genuine `Option<T>` storage (excluding `Nullish<T>`
|
|
145
|
+
* and partially narrowed `fromNullish` values, which stay off `someMatch`). */
|
|
146
|
+
export function optionalPresentFact(cond) {
|
|
147
|
+
const f = presentFact(cond);
|
|
148
|
+
return f && !f.isNullish ? f : null;
|
|
111
149
|
}
|
|
112
150
|
/** Which types have falsy values in JS (`0`, `""`, `false`); everything else
|
|
113
151
|
* (array, user type, …) is always truthy. The single home of that set —
|
|
@@ -153,7 +191,7 @@ export function extractConjunct(cond, parse) {
|
|
|
153
191
|
* It becomes the outermost match, keeping later path checks in its arm. */
|
|
154
192
|
export function leadingPresent(cond) {
|
|
155
193
|
return extractConjunct(cond, e => {
|
|
156
|
-
const f =
|
|
194
|
+
const f = optionalPresentFact(e);
|
|
157
195
|
return f && !f.negated ? f : null;
|
|
158
196
|
});
|
|
159
197
|
}
|
|
@@ -168,7 +206,7 @@ export function noneDetector(leaf, ctx) {
|
|
|
168
206
|
// `x?.chain !== lit` — `undefined !== lit` is true when x is None.
|
|
169
207
|
if (leaf.kind === "binop" && leaf.op === "!==") {
|
|
170
208
|
const oc = leaf.left.kind === "optChain" ? leaf.left : leaf.right.kind === "optChain" ? leaf.right : null;
|
|
171
|
-
if (oc && oc.kind === "optChain" && oc.obj.ty.kind === "optional") {
|
|
209
|
+
if (oc && oc.kind === "optChain" && oc.obj.ty.kind === "optional" && !oc.obj.ty.fromNullish) {
|
|
172
210
|
const binder = freshPathBinder(oc.obj, ctx);
|
|
173
211
|
if (binder === null)
|
|
174
212
|
return null;
|
|
@@ -178,8 +216,8 @@ export function noneDetector(leaf, ctx) {
|
|
|
178
216
|
return { scrutinee: oc.obj, innerTy: oc.obj.ty.inner, binder, residual: { kind: "binop", op: "!==", left: unwrapped, right: lit, ty: { kind: "bool" } } };
|
|
179
217
|
}
|
|
180
218
|
}
|
|
181
|
-
// `!x` / `x === undefined
|
|
182
|
-
const detected =
|
|
219
|
+
// `!x` / `x === undefined` (genuine Option<T> only; Nullish<T> uses checked reads).
|
|
220
|
+
const detected = optionalPresentFact(leaf);
|
|
183
221
|
if (detected && detected.negated) {
|
|
184
222
|
const f = bindPresentFact(detected, ctx);
|
|
185
223
|
const residual = canBeFalsy(f)
|