lemmascript 0.6.13 → 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 +11 -0
- package/tools/dist/builtins.js +1 -1
- package/tools/dist/checked-reads.js +51 -29
- package/tools/dist/condition-facts.js +64 -26
- package/tools/dist/dafny-emit.js +34 -3
- package/tools/dist/extract.js +34 -13
- package/tools/dist/lean-emit.js +101 -13
- package/tools/dist/narrow-checks.js +9 -3
- package/tools/dist/narrow.js +24 -16
- package/tools/dist/resolve.js +291 -117
- package/tools/dist/specparser.js +10 -6
- package/tools/dist/transform.js +519 -74
- package/tools/dist/typedir.js +19 -1
- package/tools/dist/types.js +52 -15
package/package.json
CHANGED
|
@@ -30,6 +30,7 @@ export function isMutableRefTy(ty, typeDecls) {
|
|
|
30
30
|
case "tuple":
|
|
31
31
|
return true;
|
|
32
32
|
case "optional":
|
|
33
|
+
case "nullish":
|
|
33
34
|
return isMutableRefTy(ty.inner, typeDecls);
|
|
34
35
|
case "user": {
|
|
35
36
|
const decl = declOfTy(typeDecls, ty);
|
|
@@ -58,6 +59,7 @@ function hasNestedMutableRef(ty, typeDecls, seen = new Set()) {
|
|
|
58
59
|
case "tuple":
|
|
59
60
|
return ty.elems.some(e => isMutableRefTy(e, typeDecls));
|
|
60
61
|
case "optional":
|
|
62
|
+
case "nullish":
|
|
61
63
|
return hasNestedMutableRef(ty.inner, typeDecls, seen);
|
|
62
64
|
case "user": {
|
|
63
65
|
const decl = declOfTy(typeDecls, ty);
|
|
@@ -142,6 +144,15 @@ function aliasSourceInfo(e, typeDecls, allowNonMutableWrap = false) {
|
|
|
142
144
|
return { direct: [...base.direct], shallow: [...base.shallow], derefBases: [...new Set(derefs)] };
|
|
143
145
|
}
|
|
144
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
|
+
}
|
|
145
156
|
if (e.fn.kind === "field") {
|
|
146
157
|
const m = e.fn.field;
|
|
147
158
|
const objTy = e.fn.obj.ty.kind;
|
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 },
|
|
@@ -26,14 +26,21 @@ function pathKey(e) {
|
|
|
26
26
|
}
|
|
27
27
|
return null;
|
|
28
28
|
}
|
|
29
|
-
function
|
|
30
|
-
return ty !== null && ty.kind === "optional";
|
|
29
|
+
function isNullable(ty) {
|
|
30
|
+
return ty !== null && (ty.kind === "optional" || ty.kind === "nullish");
|
|
31
31
|
}
|
|
32
32
|
function fieldInfoTy(f) {
|
|
33
33
|
if (!f)
|
|
34
34
|
return null;
|
|
35
35
|
const ty = f.type ?? parseTsType(f.tsType);
|
|
36
|
-
|
|
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 };
|
|
37
44
|
}
|
|
38
45
|
function declField(decl, field, ofVariant) {
|
|
39
46
|
if (decl.kind === "record")
|
|
@@ -44,8 +51,8 @@ function declField(decl, field, ofVariant) {
|
|
|
44
51
|
const found = variants.map(v => v.fields.find(f => f.name === field)).filter((f) => !!f);
|
|
45
52
|
if (found.length === 0)
|
|
46
53
|
return null;
|
|
47
|
-
const
|
|
48
|
-
return
|
|
54
|
+
const nullable = found.map(f => isNullable(fieldInfoTy(f)));
|
|
55
|
+
return nullable.every(o => o === nullable[0]) ? found[0] : null;
|
|
49
56
|
}
|
|
50
57
|
function fieldStorageTy(ty, field, ofVariant, mod, depth = 0) {
|
|
51
58
|
if (depth > 8 || ty.kind !== "user")
|
|
@@ -73,7 +80,7 @@ function storageTyOf(e, scope, mod) {
|
|
|
73
80
|
if (e.kind !== "field")
|
|
74
81
|
return null;
|
|
75
82
|
const objTy = storageTyOf(e.obj, scope, mod);
|
|
76
|
-
if (!objTy || objTy.kind === "optional")
|
|
83
|
+
if (!objTy || objTy.kind === "optional" || objTy.kind === "nullish")
|
|
77
84
|
return null;
|
|
78
85
|
return fieldStorageTy(objTy, e.field, e.ofVariant, mod);
|
|
79
86
|
}
|
|
@@ -98,36 +105,43 @@ function enterSomeMatch(scope, scrutinee, binder, binderTy) {
|
|
|
98
105
|
return { types };
|
|
99
106
|
}
|
|
100
107
|
function isDefinitelyUnwrappedStamp(ty) {
|
|
101
|
-
return !
|
|
108
|
+
return !isNullable(ty) && ty.kind !== "unknown" && ty.kind !== "void" && ty.kind !== "null";
|
|
102
109
|
}
|
|
103
|
-
/** Rewrite an expression that is the direct operand of
|
|
104
|
-
*
|
|
105
|
-
* its declared
|
|
106
|
-
* presence check), while still rewriting any
|
|
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. */
|
|
107
115
|
function rewritePresenceOperand(e, scope, mod) {
|
|
108
116
|
const storage = storageTyOf(e, scope, mod);
|
|
109
|
-
if (e.kind === "var" &&
|
|
117
|
+
if (e.kind === "var" && isNullable(storage)) {
|
|
110
118
|
return { ...e, ty: storage };
|
|
111
119
|
}
|
|
112
120
|
if (e.kind === "field") {
|
|
113
121
|
const obj = rewriteExpr(e.obj, scope, mod);
|
|
114
122
|
const updated = obj === e.obj ? e : { ...e, obj };
|
|
115
123
|
const fStorage = storageTyOf(updated, scope, mod);
|
|
116
|
-
if (
|
|
124
|
+
if (isNullable(fStorage))
|
|
117
125
|
return { ...updated, ty: fStorage };
|
|
118
126
|
return updated;
|
|
119
127
|
}
|
|
120
128
|
return rewriteExpr(e, scope, mod);
|
|
121
129
|
}
|
|
122
|
-
function
|
|
123
|
-
return e.kind === "var" && e.name === "undefined";
|
|
130
|
+
function isNullishVar(e) {
|
|
131
|
+
return e.kind === "var" && (e.name === "undefined" || e.name === "null");
|
|
124
132
|
}
|
|
125
133
|
function rewriteExpr(e, scope, mod) {
|
|
126
134
|
switch (e.kind) {
|
|
127
135
|
case "var": {
|
|
128
136
|
const storage = storageTyOf(e, scope, mod);
|
|
129
|
-
if (
|
|
130
|
-
|
|
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
|
+
}
|
|
131
145
|
}
|
|
132
146
|
return e;
|
|
133
147
|
}
|
|
@@ -140,13 +154,18 @@ function rewriteExpr(e, scope, mod) {
|
|
|
140
154
|
case "checkedUnwrap":
|
|
141
155
|
return { ...e, expr: rewriteExpr(e.expr, scope, mod) };
|
|
142
156
|
case "field": {
|
|
143
|
-
// Rewrite receiver first (so an optional receiver `box` becomes `checkedUnwrap(box)`).
|
|
157
|
+
// Rewrite receiver first (so an optional/nullish receiver `box` becomes `checkedUnwrap(box)`).
|
|
144
158
|
const obj = rewriteExpr(e.obj, scope, mod);
|
|
145
159
|
const updated = obj === e.obj ? e : { ...e, obj };
|
|
146
|
-
// Check if this field itself is optional by declaration (or bound in scope) while stamped non-
|
|
160
|
+
// Check if this field itself is optional/nullish by declaration (or bound in scope) while stamped non-nullable.
|
|
147
161
|
const storage = storageTyOf(updated, scope, mod);
|
|
148
|
-
if (
|
|
149
|
-
|
|
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
|
+
}
|
|
150
169
|
}
|
|
151
170
|
return updated;
|
|
152
171
|
}
|
|
@@ -155,17 +174,17 @@ function rewriteExpr(e, scope, mod) {
|
|
|
155
174
|
case "binop": {
|
|
156
175
|
let left;
|
|
157
176
|
let right;
|
|
158
|
-
if (
|
|
159
|
-
left =
|
|
160
|
-
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);
|
|
161
180
|
}
|
|
162
181
|
else {
|
|
163
182
|
left = rewriteExpr(e.left, scope, mod);
|
|
164
183
|
right = rewriteExpr(e.right, scope, mod);
|
|
165
184
|
}
|
|
166
185
|
let ty = e.ty;
|
|
167
|
-
// Clear stale optional arithmetic stamp once operands are unwrapped.
|
|
168
|
-
if (ARITH.has(e.op) &&
|
|
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)) {
|
|
169
188
|
ty = left.ty;
|
|
170
189
|
}
|
|
171
190
|
return { ...e, left, right, ty };
|
|
@@ -173,7 +192,7 @@ function rewriteExpr(e, scope, mod) {
|
|
|
173
192
|
case "unop": {
|
|
174
193
|
const expr = rewriteExpr(e.expr, scope, mod);
|
|
175
194
|
let ty = e.ty;
|
|
176
|
-
if (e.op === "-" &&
|
|
195
|
+
if (e.op === "-" && isNullable(ty) && !isNullable(expr.ty))
|
|
177
196
|
ty = expr.ty;
|
|
178
197
|
return { ...e, expr, ty };
|
|
179
198
|
}
|
|
@@ -216,10 +235,13 @@ function rewriteExpr(e, scope, mod) {
|
|
|
216
235
|
return { ...e, left: rewriteExpr(e.left, scope, mod), right: rewriteExpr(e.right, scope, mod) };
|
|
217
236
|
case "someMatch": {
|
|
218
237
|
// Do NOT unwrap the scrutinee itself (it is supposed to remain Option<T>),
|
|
219
|
-
// but DO rewrite
|
|
238
|
+
// but DO rewrite subexpressions inside non-var scrutinees (e.g. `box.inner`
|
|
239
|
+
// or `(flag ? x : undefined)`).
|
|
220
240
|
const scrutinee = e.scrutinee.kind === "field"
|
|
221
241
|
? { ...e.scrutinee, obj: rewriteExpr(e.scrutinee.obj, scope, mod) }
|
|
222
|
-
: e.scrutinee
|
|
242
|
+
: e.scrutinee.kind === "var"
|
|
243
|
+
? e.scrutinee
|
|
244
|
+
: rewriteExpr(e.scrutinee, scope, mod);
|
|
223
245
|
const someScope = enterSomeMatch(scope, e.scrutinee, e.binder, e.binderTy);
|
|
224
246
|
return {
|
|
225
247
|
...e,
|
|
@@ -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)
|
package/tools/dist/dafny-emit.js
CHANGED
|
@@ -31,6 +31,7 @@ function tyToDafny(ty) {
|
|
|
31
31
|
case "bool": return "bool";
|
|
32
32
|
case "string": return "string";
|
|
33
33
|
case "void": return "()";
|
|
34
|
+
case "null": return "()";
|
|
34
35
|
case "array": return `seq<${tyToDafny(ty.elem)}>`;
|
|
35
36
|
case "tuple": return `(${ty.elems.map(tyToDafny).join(", ")})`;
|
|
36
37
|
case "map": return `map<${tyToDafny(ty.key)}, ${tyToDafny(ty.value)}>`;
|
|
@@ -39,6 +40,10 @@ function tyToDafny(ty) {
|
|
|
39
40
|
needPreamble("OptionType");
|
|
40
41
|
return `Option<${tyToDafny(ty.inner)}>`;
|
|
41
42
|
}
|
|
43
|
+
case "nullish": {
|
|
44
|
+
needPreamble("NullishType");
|
|
45
|
+
return `Nullish<${tyToDafny(ty.inner)}>`;
|
|
46
|
+
}
|
|
42
47
|
case "user": return escapeName(ty.name);
|
|
43
48
|
case "fn": return `(${ty.params.map(tyToDafny).join(", ")}) -> ${tyToDafny(ty.result)}`;
|
|
44
49
|
// Out-of-subset (`any`/`unknown`); opaque so real ops on it fail loudly
|
|
@@ -296,6 +301,8 @@ function emitExpr(e) {
|
|
|
296
301
|
// signature, so request the preamble here.
|
|
297
302
|
if (e.type === "Option")
|
|
298
303
|
needPreamble("OptionType");
|
|
304
|
+
if (e.type === "Nullish")
|
|
305
|
+
needPreamble("NullishType");
|
|
299
306
|
const head = qualifyCtor(e.name, e.type);
|
|
300
307
|
if (!e.args || e.args.length === 0)
|
|
301
308
|
return head;
|
|
@@ -583,6 +590,17 @@ function emitExpr(e) {
|
|
|
583
590
|
return `StringRepeat(${obj}, ${args[0]})`;
|
|
584
591
|
}
|
|
585
592
|
}
|
|
593
|
+
// Nullish<T> discriminators (#304)
|
|
594
|
+
if (ty === "nullish") {
|
|
595
|
+
needPreamble("NullishType");
|
|
596
|
+
const recv = e.obj.kind === "var" || e.obj.kind === "field" ? obj : `(${obj})`;
|
|
597
|
+
if (e.method === "isNull")
|
|
598
|
+
return `${recv}.Null?`;
|
|
599
|
+
if (e.method === "isUndefined")
|
|
600
|
+
return `${recv}.Undefined?`;
|
|
601
|
+
if (e.method === "isValue")
|
|
602
|
+
return `${recv}.Value?`;
|
|
603
|
+
}
|
|
586
604
|
// Map methods
|
|
587
605
|
if (ty === "map") {
|
|
588
606
|
if (e.method === "getDirect")
|
|
@@ -841,7 +859,7 @@ function emitExpr(e) {
|
|
|
841
859
|
const fieldNames = new Set(e.fields.map(f => f.name));
|
|
842
860
|
const matches = [];
|
|
843
861
|
for (const [name, fields] of _structureDecls) {
|
|
844
|
-
if (fields.length >= e.fields.length && fields.every(f => fieldNames.has(f.name) || f.type.kind === "optional")) {
|
|
862
|
+
if (fields.length >= e.fields.length && fields.every(f => fieldNames.has(f.name) || f.type.kind === "optional" || f.type.kind === "nullish")) {
|
|
845
863
|
matches.push(name);
|
|
846
864
|
}
|
|
847
865
|
}
|
|
@@ -869,6 +887,10 @@ function emitExpr(e) {
|
|
|
869
887
|
needPreamble("OptionType");
|
|
870
888
|
return "None";
|
|
871
889
|
}
|
|
890
|
+
if (sf.type.kind === "nullish") {
|
|
891
|
+
needPreamble("NullishType");
|
|
892
|
+
return "Nullish.Undefined";
|
|
893
|
+
}
|
|
872
894
|
return `/* missing: ${sf.name} */`;
|
|
873
895
|
});
|
|
874
896
|
return `${ctorName}(${vals.join(", ")})`;
|
|
@@ -1740,6 +1762,7 @@ const SET_TO_SEQ = `method SetToSeq<T>(s: set<T>) returns (res: seq<T>)
|
|
|
1740
1762
|
/** Preamble code keyed by name. Emitted in this order when needed. */
|
|
1741
1763
|
const PREAMBLE_CODE = [
|
|
1742
1764
|
["OptionType", "datatype Option<T> = None | Some(value: T)"],
|
|
1765
|
+
["NullishType", "datatype Nullish<T> = Undefined | Null | Value(value: T)"],
|
|
1743
1766
|
// Opaque carrier for `unknown`-typed values. `(==)` for compare/map-key/match;
|
|
1744
1767
|
// `(0)` (auto-init ⇒ nonempty) so `havoc` (`:= *`) is well-formed.
|
|
1745
1768
|
["UnknownType", "type Unknown(==, 0)"],
|
|
@@ -1882,7 +1905,9 @@ function resolveTy(ty) {
|
|
|
1882
1905
|
if (ty.kind === "user" && !_declaredTypes.has(ty.name))
|
|
1883
1906
|
return { kind: "string" };
|
|
1884
1907
|
if (ty.kind === "optional")
|
|
1885
|
-
return {
|
|
1908
|
+
return { ...ty, inner: resolveTy(ty.inner) };
|
|
1909
|
+
if (ty.kind === "nullish")
|
|
1910
|
+
return { kind: "nullish", inner: resolveTy(ty.inner) };
|
|
1886
1911
|
if (ty.kind === "array")
|
|
1887
1912
|
return { kind: "array", elem: resolveTy(ty.elem) };
|
|
1888
1913
|
if (ty.kind === "tuple")
|
|
@@ -1916,7 +1941,13 @@ function qualifyCtor(name, type) {
|
|
|
1916
1941
|
* literal value → quoted Dafny string
|
|
1917
1942
|
* "_" → "_"
|
|
1918
1943
|
*/
|
|
1919
|
-
const CTOR_MAP = {
|
|
1944
|
+
const CTOR_MAP = {
|
|
1945
|
+
"some": "Some",
|
|
1946
|
+
"none": "None",
|
|
1947
|
+
"nullishUndefined": "Undefined",
|
|
1948
|
+
"nullishNull": "Null",
|
|
1949
|
+
"nullishValue": "Value",
|
|
1950
|
+
};
|
|
1920
1951
|
function translatePattern(p) {
|
|
1921
1952
|
if (p.kind === "wild")
|
|
1922
1953
|
return "_";
|
package/tools/dist/extract.js
CHANGED
|
@@ -781,9 +781,10 @@ function extractExpr(node) {
|
|
|
781
781
|
};
|
|
782
782
|
}
|
|
783
783
|
}
|
|
784
|
-
// null
|
|
784
|
+
// Preserve `null` distinctly from `undefined` so `T | null | undefined`
|
|
785
|
+
// (`Nullish<T>`) and single-sentinel `T | null` / `T | undefined` stay sound (#304).
|
|
785
786
|
if (Node.isNullLiteral(node)) {
|
|
786
|
-
return { kind: "var", name: "
|
|
787
|
+
return { kind: "var", name: "null" };
|
|
787
788
|
}
|
|
788
789
|
// `typeof X` — only meaningful in a `typeof X === "string"` discriminator over
|
|
789
790
|
// a synth `U | T[]` union (see narrow's parseTypeofStringCheck); any other use
|
|
@@ -1264,6 +1265,8 @@ function typeToString(type) {
|
|
|
1264
1265
|
return "number";
|
|
1265
1266
|
if (type.isUndefined())
|
|
1266
1267
|
return "undefined";
|
|
1268
|
+
if (type.isNull())
|
|
1269
|
+
return "null";
|
|
1267
1270
|
if (type.isNumber() || type.isNumberLiteral())
|
|
1268
1271
|
return "number";
|
|
1269
1272
|
if (type.isBigInt() || type.isBigIntLiteral())
|
|
@@ -1331,9 +1334,17 @@ function typeToString(type) {
|
|
|
1331
1334
|
if (real.length <= 1)
|
|
1332
1335
|
return parts.join(" | ");
|
|
1333
1336
|
// A genuine multi-member union with no tagged-union shape LS can model →
|
|
1334
|
-
// a single opaque type. (See _synthOpaque.) Preserve
|
|
1337
|
+
// a single opaque type. (See _synthOpaque.) Preserve outer null/undefined.
|
|
1335
1338
|
const opaque = _synthOpaque(real);
|
|
1336
|
-
|
|
1339
|
+
const hasNull = parts.includes("null");
|
|
1340
|
+
const hasUndef = parts.includes("undefined");
|
|
1341
|
+
if (hasNull && hasUndef)
|
|
1342
|
+
return `${opaque} | null | undefined`;
|
|
1343
|
+
if (hasNull)
|
|
1344
|
+
return `${opaque} | null`;
|
|
1345
|
+
if (hasUndef)
|
|
1346
|
+
return `${opaque} | undefined`;
|
|
1347
|
+
return opaque;
|
|
1337
1348
|
}
|
|
1338
1349
|
if (type.isTuple()) {
|
|
1339
1350
|
return `[${type.getTupleElements().map(t => typeToString(t)).join(", ")}]`;
|
|
@@ -3059,16 +3070,16 @@ export function extractModule(sourceFile, options = DEFAULT_OPTIONS, validateDep
|
|
|
3059
3070
|
// already-extracted `fn.returnType` string for nullish suffixes to detect
|
|
3060
3071
|
// the wrap-in-Optional case.
|
|
3061
3072
|
let innerType = null;
|
|
3062
|
-
let wrapOptional = false;
|
|
3063
3073
|
const sourceReturnText = fn.returnType ?? "";
|
|
3064
|
-
const
|
|
3065
|
-
|
|
3066
|
-
|
|
3074
|
+
const sourceHasNull = /(^|\|\s*)null(\s*\||$)/.test(sourceReturnText);
|
|
3075
|
+
const sourceHasUndef = /(^|\|\s*)undefined(\s*\||$)/.test(sourceReturnText);
|
|
3076
|
+
let wrapNull = false;
|
|
3077
|
+
let wrapUndef = false;
|
|
3067
3078
|
const sym = retType.getSymbol();
|
|
3068
3079
|
if ((sym?.getName() === "__type" || sym?.getName() === "__object") && retType.isObject() && !retType.isArray()) {
|
|
3069
3080
|
innerType = retType;
|
|
3070
|
-
|
|
3071
|
-
|
|
3081
|
+
wrapNull = sourceHasNull;
|
|
3082
|
+
wrapUndef = sourceHasUndef;
|
|
3072
3083
|
}
|
|
3073
3084
|
else if (retType.isUnion()) {
|
|
3074
3085
|
const arms = retType.getUnionTypes();
|
|
@@ -3079,15 +3090,25 @@ export function extractModule(sourceFile, options = DEFAULT_OPTIONS, validateDep
|
|
|
3079
3090
|
const otherSym = onlyOther.getSymbol();
|
|
3080
3091
|
if ((otherSym?.getName() === "__type" || otherSym?.getName() === "__object") && onlyOther.isObject() && !onlyOther.isArray()) {
|
|
3081
3092
|
innerType = onlyOther;
|
|
3082
|
-
|
|
3093
|
+
wrapNull = nullish.some(t => t.isNull());
|
|
3094
|
+
wrapUndef = nullish.some(t => t.isUndefined());
|
|
3083
3095
|
}
|
|
3084
3096
|
}
|
|
3085
3097
|
}
|
|
3098
|
+
const formatReturnNullability = (base) => {
|
|
3099
|
+
if (wrapNull && wrapUndef)
|
|
3100
|
+
return `${base} | null | undefined`;
|
|
3101
|
+
if (wrapNull)
|
|
3102
|
+
return `${base} | null`;
|
|
3103
|
+
if (wrapUndef)
|
|
3104
|
+
return `${base} | undefined`;
|
|
3105
|
+
return base;
|
|
3106
|
+
};
|
|
3086
3107
|
if (innerType) {
|
|
3087
3108
|
// Try typeToString first — it resolves collapsed single-variant unions
|
|
3088
3109
|
const resolved = typeToString(innerType);
|
|
3089
3110
|
if (resolved !== "__type" && !resolved.includes("__type") && knownTypes.has(resolved)) {
|
|
3090
|
-
fn.returnType =
|
|
3111
|
+
fn.returnType = formatReturnNullability(resolved);
|
|
3091
3112
|
continue;
|
|
3092
3113
|
}
|
|
3093
3114
|
const synName = f.owner + fn.name.charAt(0).toUpperCase() + fn.name.slice(1) + "Result";
|
|
@@ -3100,7 +3121,7 @@ export function extractModule(sourceFile, options = DEFAULT_OPTIONS, validateDep
|
|
|
3100
3121
|
knownTypes.add(synName);
|
|
3101
3122
|
}
|
|
3102
3123
|
}
|
|
3103
|
-
fn.returnType =
|
|
3124
|
+
fn.returnType = formatReturnNullability(synName);
|
|
3104
3125
|
// Also resolve imported types referenced in the return type's fields
|
|
3105
3126
|
for (const prop of innerType.getProperties()) {
|
|
3106
3127
|
resolveType(prop.getTypeAtLocation(f.node), f.node);
|