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/tools/dist/resolve.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Uses linked environments (Scheme-style) for lexical scoping.
|
|
5
5
|
* No mutation — each let extends the chain, lookup walks it.
|
|
6
6
|
*/
|
|
7
|
-
import { isBigInt, tyEqual, isTerminatorKind } from "./typedir.js";
|
|
7
|
+
import { isBigInt, optAbsent, tyEqual, isTerminatorKind } from "./typedir.js";
|
|
8
8
|
import { parseTsType, tyToCanonical } from "./types.js";
|
|
9
9
|
import { parseExpr } from "./specparser.js";
|
|
10
10
|
import { freshName } from "./names.js";
|
|
@@ -82,32 +82,40 @@ function resolveTsType(tsType, overrides, varName) {
|
|
|
82
82
|
return parseTsType(tsType);
|
|
83
83
|
}
|
|
84
84
|
/** Infer a conditional's result type after its branches have been resolved or
|
|
85
|
-
* contextually coerced.
|
|
85
|
+
* contextually coerced. `void` is `undefined`, `null` is `null`; mixing both
|
|
86
|
+
* sentinels yields `nullish<T>` so `null !== undefined` is preserved (#304). */
|
|
86
87
|
function conditionalResultTy(thenTy, elseTy) {
|
|
87
88
|
if ((thenTy.kind === "int" || thenTy.kind === "nat") &&
|
|
88
89
|
(elseTy.kind === "int" || elseTy.kind === "nat")) {
|
|
89
90
|
return { ...thenTy, kind: thenTy.kind === "int" || elseTy.kind === "int" ? "int" : "nat" };
|
|
90
91
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
92
|
+
const hasNull = (t) => t.kind === "null" || t.kind === "nullish" || (t.kind === "optional" && optAbsent(t) === "null");
|
|
93
|
+
const hasUndef = (t) => t.kind === "void" || t.kind === "nullish" || (t.kind === "optional" && optAbsent(t) === "undefined");
|
|
94
|
+
const payloadOf = (t) => t.kind === "void" || t.kind === "null" || t.kind === "unknown" ? null
|
|
95
|
+
: (t.kind === "optional" || t.kind === "nullish") ? t.inner : t;
|
|
96
|
+
const bothNull = hasNull(thenTy) || hasNull(elseTy);
|
|
97
|
+
const bothUndef = hasUndef(thenTy) || hasUndef(elseTy);
|
|
98
|
+
if (bothNull || bothUndef) {
|
|
99
|
+
const p1 = payloadOf(thenTy);
|
|
100
|
+
const p2 = payloadOf(elseTy);
|
|
101
|
+
const inner = p1 ?? p2;
|
|
102
|
+
if (inner) {
|
|
103
|
+
if (bothNull && bothUndef)
|
|
104
|
+
return { kind: "nullish", inner };
|
|
105
|
+
if (bothNull)
|
|
106
|
+
return { kind: "optional", inner, absent: "null" };
|
|
107
|
+
return { kind: "optional", inner };
|
|
108
|
+
}
|
|
109
|
+
if (bothNull && bothUndef)
|
|
110
|
+
return { kind: "nullish", inner: { kind: "void" } };
|
|
111
|
+
}
|
|
112
|
+
return thenTy.kind !== "unknown" ? thenTy : elseTy;
|
|
105
113
|
}
|
|
106
114
|
/** Contextually type string literals as string-union constructors. Ternary branches inherit
|
|
107
|
-
* the ternary's target, and an optional target contributes its payload type —
|
|
108
|
-
* the caller adds the Some wrapper only after the payload has been coerced. */
|
|
115
|
+
* the ternary's target, and an optional/nullish target contributes its payload type —
|
|
116
|
+
* the caller adds the Some/Value wrapper only after the payload has been coerced. */
|
|
109
117
|
function coerceStr(expr, targetTy, typeDecls) {
|
|
110
|
-
const payloadTy = targetTy.kind === "optional" ? targetTy.inner : targetTy;
|
|
118
|
+
const payloadTy = (targetTy.kind === "optional" || targetTy.kind === "nullish") ? targetTy.inner : targetTy;
|
|
111
119
|
if (expr.kind === "str" && payloadTy.kind === "user") {
|
|
112
120
|
const enumTy = expandAlias(payloadTy, typeDecls);
|
|
113
121
|
// A synthesized string/array union has a string payload, not a constructor
|
|
@@ -130,6 +138,13 @@ function wrapSome(value, optionalTy) {
|
|
|
130
138
|
args: [value], ty: optionalTy, callKind: "pure",
|
|
131
139
|
};
|
|
132
140
|
}
|
|
141
|
+
/** Wrap a resolved non-nullish value in Nullish.Value(...) for Nullish<T> coercion. */
|
|
142
|
+
function wrapNullishValue(value, nullishTy) {
|
|
143
|
+
return {
|
|
144
|
+
kind: "call", fn: { kind: "var", name: "NullishValue", ty: nullishTy },
|
|
145
|
+
args: [value], ty: nullishTy, callKind: "pure",
|
|
146
|
+
};
|
|
147
|
+
}
|
|
133
148
|
/** Find the synth array-union TypeDecl named `name` (discriminant `__isArray__`). */
|
|
134
149
|
function findSynthArrayUnion(name, typeDecls) {
|
|
135
150
|
const decl = declOf(typeDecls, name);
|
|
@@ -140,6 +155,8 @@ function findSynthArrayUnion(name, typeDecls) {
|
|
|
140
155
|
/** Coerce `value` to `targetTy` at an assignment-position. Contextually types
|
|
141
156
|
* real literals, then mirrors TS subtyping for the upcast shapes LS synthesizes:
|
|
142
157
|
* - `T` into `optional<T>` slot → wrap with `Some(...)`
|
|
158
|
+
* - `T` / `Option<T>` / `null` / `undefined` into `nullish<T>` slot → `Nullish<T>`
|
|
159
|
+
* - `fromNullish` into `optional<T>` slot → `__nullishToOption(...)`
|
|
143
160
|
* - `T[]` into a synth `T[] | U` slot → wrap with `ArrayBranch(...)`
|
|
144
161
|
* - `U` into a synth `T[] | U` slot → wrap with `NonArrayBranch(...)`
|
|
145
162
|
* Returns `value` unchanged if no coercion applies (types already match,
|
|
@@ -147,8 +164,8 @@ function findSynthArrayUnion(name, typeDecls) {
|
|
|
147
164
|
function coerceToTargetTy(value, targetTy, typeDecls) {
|
|
148
165
|
value = coerceStr(value, targetTy, typeDecls);
|
|
149
166
|
// Real targets also type integer literals, including negative literals and
|
|
150
|
-
// conditional arms. Coerce optional payloads before adding
|
|
151
|
-
const payloadTy = targetTy.kind === "optional" ? targetTy.inner : targetTy;
|
|
167
|
+
// conditional arms. Coerce optional/nullish payloads before adding wrappers.
|
|
168
|
+
const payloadTy = (targetTy.kind === "optional" || targetTy.kind === "nullish") ? targetTy.inner : targetTy;
|
|
152
169
|
if (payloadTy.kind === "real") {
|
|
153
170
|
if (value.kind === "num") {
|
|
154
171
|
value = { ...value, ty: payloadTy };
|
|
@@ -162,10 +179,53 @@ function coerceToTargetTy(value, targetTy, typeDecls) {
|
|
|
162
179
|
value = { ...value, then: then_, else: else_, ty: conditionalResultTy(then_.ty, else_.ty) };
|
|
163
180
|
}
|
|
164
181
|
}
|
|
165
|
-
if (
|
|
182
|
+
if (targetTy.kind === "nullish") {
|
|
183
|
+
if (value.ty.kind === "unknown")
|
|
184
|
+
return value;
|
|
185
|
+
if (value.kind === "var" && (value.name === "undefined" || value.name === "null")) {
|
|
186
|
+
return { ...value, ty: targetTy };
|
|
187
|
+
}
|
|
188
|
+
if (value.ty.kind === "void")
|
|
189
|
+
return { kind: "var", name: "undefined", ty: targetTy };
|
|
190
|
+
if (value.ty.kind === "null")
|
|
191
|
+
return { kind: "var", name: "null", ty: targetTy };
|
|
192
|
+
if (value.ty.kind === "nullish")
|
|
193
|
+
return value;
|
|
194
|
+
if (value.ty.kind === "optional") {
|
|
195
|
+
// Partially narrowed fromNullish already lives in Nullish<T> storage.
|
|
196
|
+
if (value.ty.fromNullish)
|
|
197
|
+
return { ...value, ty: targetTy };
|
|
198
|
+
const helper = optAbsent(value.ty) === "null" ? "__optNullToNullish" : "__optUndefToNullish";
|
|
199
|
+
return {
|
|
200
|
+
kind: "call",
|
|
201
|
+
fn: { kind: "var", name: helper, ty: targetTy },
|
|
202
|
+
args: [value],
|
|
203
|
+
ty: targetTy,
|
|
204
|
+
callKind: "pure",
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
if (value.kind === "conditional") {
|
|
208
|
+
const then_ = coerceToTargetTy(value.then, targetTy, typeDecls);
|
|
209
|
+
const else_ = coerceToTargetTy(value.else, targetTy, typeDecls);
|
|
210
|
+
return { ...value, then: then_, else: else_, ty: targetTy };
|
|
211
|
+
}
|
|
212
|
+
return wrapNullishValue(value, targetTy);
|
|
213
|
+
}
|
|
214
|
+
if (value.ty.kind === "unknown" || value.ty.kind === "void" || value.ty.kind === "null")
|
|
166
215
|
return value;
|
|
167
|
-
if (targetTy.kind === "optional"
|
|
168
|
-
|
|
216
|
+
if (targetTy.kind === "optional") {
|
|
217
|
+
if (value.ty.kind === "nullish" || (value.ty.kind === "optional" && value.ty.fromNullish)) {
|
|
218
|
+
return {
|
|
219
|
+
kind: "call",
|
|
220
|
+
fn: { kind: "var", name: "__nullishToOption", ty: targetTy },
|
|
221
|
+
args: [value],
|
|
222
|
+
ty: targetTy,
|
|
223
|
+
callKind: "pure",
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
if (value.ty.kind !== "optional") {
|
|
227
|
+
return wrapSome(value, targetTy);
|
|
228
|
+
}
|
|
169
229
|
}
|
|
170
230
|
if (targetTy.kind === "user") {
|
|
171
231
|
if (value.ty.kind === "user" && value.ty.name === targetTy.name)
|
|
@@ -410,14 +470,33 @@ function expandAlias(ty, typeDecls, seen = new Set()) {
|
|
|
410
470
|
if (decl.constEnumMembers)
|
|
411
471
|
return { kind: "int" };
|
|
412
472
|
const target = decl.aliasOfTy;
|
|
413
|
-
if (target.kind === "array" || target.kind === "map" || target.kind === "set" || target.kind === "optional" || target.kind === "user") {
|
|
473
|
+
if (target.kind === "array" || target.kind === "map" || target.kind === "set" || target.kind === "optional" || target.kind === "nullish" || target.kind === "user") {
|
|
414
474
|
return expandAlias(target, typeDecls, new Set([...seen, ty.name]));
|
|
415
475
|
}
|
|
416
476
|
}
|
|
417
477
|
return ty;
|
|
418
478
|
}
|
|
419
|
-
if (ty.kind === "optional")
|
|
420
|
-
|
|
479
|
+
if (ty.kind === "optional") {
|
|
480
|
+
const expanded = expandAlias(ty.inner, typeDecls, seen);
|
|
481
|
+
if (expanded.kind === "nullish")
|
|
482
|
+
return expanded;
|
|
483
|
+
if (expanded.kind === "optional") {
|
|
484
|
+
const mergedNull = optAbsent(ty) === "null" || optAbsent(expanded) === "null";
|
|
485
|
+
const mergedUndef = optAbsent(ty) === "undefined" || optAbsent(expanded) === "undefined";
|
|
486
|
+
if (mergedNull && mergedUndef)
|
|
487
|
+
return { kind: "nullish", inner: expanded.inner };
|
|
488
|
+
return { ...ty, inner: expanded.inner };
|
|
489
|
+
}
|
|
490
|
+
return { ...ty, inner: expanded };
|
|
491
|
+
}
|
|
492
|
+
if (ty.kind === "nullish") {
|
|
493
|
+
const expanded = expandAlias(ty.inner, typeDecls, seen);
|
|
494
|
+
if (expanded.kind === "nullish")
|
|
495
|
+
return expanded;
|
|
496
|
+
if (expanded.kind === "optional")
|
|
497
|
+
return { kind: "nullish", inner: expanded.inner };
|
|
498
|
+
return { kind: "nullish", inner: expanded };
|
|
499
|
+
}
|
|
421
500
|
if (ty.kind === "array")
|
|
422
501
|
return { kind: "array", elem: expandAlias(ty.elem, typeDecls, seen) };
|
|
423
502
|
if (ty.kind === "tuple")
|
|
@@ -429,11 +508,13 @@ function expandAlias(ty, typeDecls, seen = new Set()) {
|
|
|
429
508
|
return ty;
|
|
430
509
|
}
|
|
431
510
|
/** Numeric aliases keep their names in declarations but use their underlying
|
|
432
|
-
* types in expressions. Optional payloads need the same treatment for
|
|
433
|
-
* falsiness; collection elements are resolved when read. */
|
|
511
|
+
* types in expressions. Optional/nullish payloads need the same treatment for
|
|
512
|
+
* zero's falsiness; collection elements are resolved when read. */
|
|
434
513
|
function numericValueType(ty, typeDecls, seen = new Set()) {
|
|
435
514
|
if (ty.kind === "optional")
|
|
436
515
|
return { ...ty, inner: numericValueType(ty.inner, typeDecls, seen) };
|
|
516
|
+
if (ty.kind === "nullish")
|
|
517
|
+
return { kind: "nullish", inner: numericValueType(ty.inner, typeDecls, seen) };
|
|
437
518
|
if (ty.kind !== "user" || seen.has(ty.name))
|
|
438
519
|
return ty;
|
|
439
520
|
const decl = declOfDotted(typeDecls, ty.name);
|
|
@@ -487,7 +568,7 @@ function warnRefEq(op, l, r) {
|
|
|
487
568
|
function isUnmodeledTy(ty, typeDecls) {
|
|
488
569
|
if (ty.kind === "unknown")
|
|
489
570
|
return true;
|
|
490
|
-
if (ty.kind === "optional")
|
|
571
|
+
if (ty.kind === "optional" || ty.kind === "nullish")
|
|
491
572
|
return isUnmodeledTy(ty.inner, typeDecls);
|
|
492
573
|
if (ty.kind === "array")
|
|
493
574
|
return isUnmodeledTy(ty.elem, typeDecls);
|
|
@@ -683,8 +764,14 @@ let callbackCounter = 0;
|
|
|
683
764
|
function callbackDefaultState(ty, ctx, seen = new Set()) {
|
|
684
765
|
if (ty.kind === "void")
|
|
685
766
|
return "missing";
|
|
686
|
-
if (ty.kind === "
|
|
687
|
-
return
|
|
767
|
+
if (ty.kind === "null")
|
|
768
|
+
return "defined";
|
|
769
|
+
if (ty.kind === "optional")
|
|
770
|
+
return optAbsent(ty) === "null" ? "defined" : "optional";
|
|
771
|
+
if (ty.kind === "nullish")
|
|
772
|
+
return "optional";
|
|
773
|
+
if (ty.kind === "unknown")
|
|
774
|
+
return "unknown";
|
|
688
775
|
if (ty.kind !== "user" || ty.name === "null")
|
|
689
776
|
return "defined";
|
|
690
777
|
if (seen.has(ty.name))
|
|
@@ -849,20 +936,25 @@ function argumentPack(e, ctx) {
|
|
|
849
936
|
entry.onRead?.();
|
|
850
937
|
return pack;
|
|
851
938
|
}
|
|
852
|
-
/** A defined-check filter predicate: `(x) => x !== undefined`
|
|
853
|
-
*
|
|
854
|
-
|
|
939
|
+
/** A defined-check filter predicate: `(x) => x !== undefined`, `(x) => x !== null`,
|
|
940
|
+
* or loose `(x) => x != null` / `(x) => x != undefined`. Returns which sentinel
|
|
941
|
+
* the filter removes so `filterSome` only fires when it matches the array
|
|
942
|
+
* element's absent sentinel (`#304`). */
|
|
943
|
+
function definedCheckRawLambdaSentinel(raw) {
|
|
855
944
|
if (raw.kind !== "lambda" || raw.params.length !== 1)
|
|
856
|
-
return
|
|
945
|
+
return null;
|
|
857
946
|
const p = raw.params[0].name;
|
|
858
947
|
const body = Array.isArray(raw.body)
|
|
859
948
|
? (raw.body.length === 1 && raw.body[0].kind === "return" ? raw.body[0].value : null)
|
|
860
949
|
: raw.body;
|
|
861
|
-
if (!body || body.kind !== "binop" || body.op !== "!==")
|
|
862
|
-
return
|
|
950
|
+
if (!body || body.kind !== "binop" || (body.op !== "!==" && body.op !== "!="))
|
|
951
|
+
return null;
|
|
863
952
|
const isParam = (x) => x.kind === "var" && x.name === p;
|
|
864
|
-
const
|
|
865
|
-
|
|
953
|
+
const sentinelOf = (x) => x.kind === "var" && (x.name === "undefined" || x.name === "null") ? x.name : null;
|
|
954
|
+
const s = isParam(body.left) ? sentinelOf(body.right) : isParam(body.right) ? sentinelOf(body.left) : null;
|
|
955
|
+
if (!s)
|
|
956
|
+
return null;
|
|
957
|
+
return body.op === "!=" ? "both" : s;
|
|
866
958
|
}
|
|
867
959
|
/** Coerce call arguments to their declared parameter slots and pad missing optional args. */
|
|
868
960
|
function coerceCallArgs(args, fn, ctx) {
|
|
@@ -874,9 +966,9 @@ function coerceCallArgs(args, fn, ctx) {
|
|
|
874
966
|
return a;
|
|
875
967
|
return coerceToTargetTy(a, paramTys[i], ctx.typeDecls);
|
|
876
968
|
});
|
|
877
|
-
// Pad missing optional args with None
|
|
969
|
+
// Pad missing optional/nullish args with undefined (None / Nullish.Undefined)
|
|
878
970
|
for (let i = args.length; i < paramTys.length; i++) {
|
|
879
|
-
if (paramTys[i].kind === "optional") {
|
|
971
|
+
if (paramTys[i].kind === "optional" || paramTys[i].kind === "nullish") {
|
|
880
972
|
args.push({ kind: "var", name: "undefined", ty: paramTys[i] });
|
|
881
973
|
}
|
|
882
974
|
}
|
|
@@ -996,8 +1088,8 @@ let mergeBinder = 0;
|
|
|
996
1088
|
function resolveRecordMerge(base, override, ctx) {
|
|
997
1089
|
const tbase = resolveExpr(base, ctx);
|
|
998
1090
|
const tover = resolveExpr(override, ctx);
|
|
999
|
-
const overInner = tover.ty.kind === "optional" ? tover.ty.inner : tover.ty;
|
|
1000
|
-
// Result record type: prefer the override's (an optional override still merges
|
|
1091
|
+
const overInner = (tover.ty.kind === "optional" || tover.ty.kind === "nullish") ? tover.ty.inner : tover.ty;
|
|
1092
|
+
// Result record type: prefer the override's (an optional/nullish override still merges
|
|
1001
1093
|
// into its inner type), else the base's.
|
|
1002
1094
|
const rTy = overInner.kind === "user" ? overInner
|
|
1003
1095
|
: tbase.ty.kind === "user" ? tbase.ty : null;
|
|
@@ -1006,7 +1098,7 @@ function resolveRecordMerge(base, override, ctx) {
|
|
|
1006
1098
|
throw new Error(`object spread merge { ...a, ...b } needs a known record type for both operands ` +
|
|
1007
1099
|
`(base: ${tyToCanonical(tbase.ty)}, override: ${tyToCanonical(tover.ty)})`);
|
|
1008
1100
|
}
|
|
1009
|
-
if (tbase.ty.kind === "optional") {
|
|
1101
|
+
if (tbase.ty.kind === "optional" || tbase.ty.kind === "nullish") {
|
|
1010
1102
|
throw new Error(`object spread merge with an optional base operand is not supported (base: ${tyToCanonical(tbase.ty)})`);
|
|
1011
1103
|
}
|
|
1012
1104
|
const userTy = rTy;
|
|
@@ -1018,10 +1110,19 @@ function resolveRecordMerge(base, override, ctx) {
|
|
|
1018
1110
|
const ft = f.type;
|
|
1019
1111
|
const ovf = { kind: "field", obj: ov, field: f.name, ty: ft };
|
|
1020
1112
|
// A required key is always copied, even when its value is null/undefined.
|
|
1021
|
-
if (ft.kind !== "optional"
|
|
1113
|
+
if (!f.optionalKey || (ft.kind !== "optional" && ft.kind !== "nullish"))
|
|
1022
1114
|
return { name: f.name, value: ovf };
|
|
1023
|
-
// `f?:` key: override field wins iff present, else base's field
|
|
1024
1115
|
const bvf = { kind: "field", obj: bv, field: f.name, ty: ft };
|
|
1116
|
+
if (ft.kind === "nullish") {
|
|
1117
|
+
// `f?: T | null`: override wins unless `ov.f === undefined` (null still overrides!).
|
|
1118
|
+
const isUndef = {
|
|
1119
|
+
kind: "binop", op: "===",
|
|
1120
|
+
left: ovf, right: { kind: "var", name: "undefined", ty: { kind: "void" } },
|
|
1121
|
+
ty: { kind: "bool" },
|
|
1122
|
+
};
|
|
1123
|
+
return { name: f.name, value: { kind: "conditional", cond: isUndef, then: bvf, else: ovf, ty: ft } };
|
|
1124
|
+
}
|
|
1125
|
+
// `f?:` optional key: override field wins iff present, else base's field
|
|
1025
1126
|
const binder = freshName(`_m${mergeBinder++}`);
|
|
1026
1127
|
// someBody is the unwrapped present value; transform re-wraps each arm in
|
|
1027
1128
|
// the backend's Some constructor (Dafny `Some`, Lean `Option.some`).
|
|
@@ -1031,6 +1132,16 @@ function resolveRecordMerge(base, override, ctx) {
|
|
|
1031
1132
|
} };
|
|
1032
1133
|
}),
|
|
1033
1134
|
});
|
|
1135
|
+
if (tover.ty.kind === "nullish" || (tover.ty.kind === "optional" && tover.ty.fromNullish)) {
|
|
1136
|
+
// Nullish override: undefined/null spreads nothing → base unchanged; Value(v) merges.
|
|
1137
|
+
const isPresent = {
|
|
1138
|
+
kind: "binop", op: "!=",
|
|
1139
|
+
left: tover, right: { kind: "var", name: "null", ty: { kind: "null" } },
|
|
1140
|
+
ty: { kind: "bool" },
|
|
1141
|
+
};
|
|
1142
|
+
const unwrappedOver = { kind: "checkedUnwrap", expr: tover, ty: userTy };
|
|
1143
|
+
return { kind: "conditional", cond: isPresent, then: merged(tbase, unwrappedOver), else: tbase, ty: userTy };
|
|
1144
|
+
}
|
|
1034
1145
|
if (tover.ty.kind === "optional") {
|
|
1035
1146
|
// override may be absent (undefined spreads nothing) → base unchanged
|
|
1036
1147
|
const binder = freshName(`_mo${mergeBinder++}`);
|
|
@@ -1112,6 +1223,8 @@ function resolveExprInner(e, ctx) {
|
|
|
1112
1223
|
case "var": {
|
|
1113
1224
|
if (e.name === "undefined")
|
|
1114
1225
|
return { kind: "var", name: "undefined", ty: { kind: "void" } };
|
|
1226
|
+
if (e.name === "null")
|
|
1227
|
+
return { kind: "var", name: "null", ty: { kind: "null" } };
|
|
1115
1228
|
const entry = binding(ctx.env, e.name);
|
|
1116
1229
|
if (entry?.argumentPack) {
|
|
1117
1230
|
throw new Error(`cannot materialize callback rest parameter '${e.name}'; use its length or a constant indexed read`);
|
|
@@ -1138,12 +1251,12 @@ function resolveExprInner(e, ctx) {
|
|
|
1138
1251
|
expr.fn.obj.ty.kind === "map" && expr.fn.field === "get") {
|
|
1139
1252
|
return { ...expr, ty: expr.fn.obj.ty.value };
|
|
1140
1253
|
}
|
|
1141
|
-
const ty = expr.ty.kind === "optional" ? expr.ty.inner : expr.ty;
|
|
1254
|
+
const ty = (expr.ty.kind === "optional" || expr.ty.kind === "nullish") ? expr.ty.inner : expr.ty;
|
|
1142
1255
|
return { ...expr, ty };
|
|
1143
1256
|
}
|
|
1144
1257
|
case "binop": {
|
|
1145
1258
|
let left = resolveExpr(e.left, ctx);
|
|
1146
|
-
// && and ==> narrowing: left-side optional checks narrow the right side.
|
|
1259
|
+
// && and ==> narrowing: left-side optional/nullish checks narrow the right side.
|
|
1147
1260
|
// (For ==>, the premise is assumed in the conclusion — same principle.)
|
|
1148
1261
|
let rightCtx = ctx;
|
|
1149
1262
|
let rawRight = e.right;
|
|
@@ -1154,18 +1267,17 @@ function resolveExprInner(e, ctx) {
|
|
|
1154
1267
|
// Preserve direct single-optional var comparisons (`opt === null || x < opt`,
|
|
1155
1268
|
// `excludeTag === undefined || getTagId(t) !== excludeTag`) for transform's
|
|
1156
1269
|
// native mirrored match lowering, as long as the non-optional side doesn't
|
|
1157
|
-
// also depend on a variable narrowed by `left
|
|
1158
|
-
//
|
|
1159
|
-
// comparisons) via checked reads.
|
|
1270
|
+
// also depend on a variable narrowed by `left` and neither side is `Nullish<T>`;
|
|
1271
|
+
// narrow all other RHS shapes via checked reads.
|
|
1160
1272
|
const orCtx = collectOrChainNarrowings(left, ctx);
|
|
1161
1273
|
const narrowedVars = new Set();
|
|
1162
1274
|
for (let cur = orCtx.env; cur && cur !== ctx.env; cur = cur.parent) {
|
|
1163
1275
|
narrowedVars.add(cur.name);
|
|
1164
1276
|
}
|
|
1165
|
-
const
|
|
1166
|
-
|
|
1167
|
-
const
|
|
1168
|
-
|
|
1277
|
+
const leftTy = rawRight.kind === "binop" && rawRight.left.kind === "var" ? lookup(ctx.env, rawRight.left.name) : undefined;
|
|
1278
|
+
const rightTy = rawRight.kind === "binop" && rawRight.right.kind === "var" ? lookup(ctx.env, rawRight.right.name) : undefined;
|
|
1279
|
+
const leftIsOptVar = leftTy?.kind === "optional" && !leftTy.fromNullish;
|
|
1280
|
+
const rightIsOptVar = rightTy?.kind === "optional" && !rightTy.fromNullish;
|
|
1169
1281
|
const isDirectSingleOptCmp = rawRight.kind === "binop" &&
|
|
1170
1282
|
["===", "!==", "<", "<=", ">", ">="].includes(rawRight.op) &&
|
|
1171
1283
|
(leftIsOptVar !== rightIsOptVar) &&
|
|
@@ -1175,7 +1287,7 @@ function resolveExprInner(e, ctx) {
|
|
|
1175
1287
|
}
|
|
1176
1288
|
}
|
|
1177
1289
|
let right = resolveExpr(rawRight, rightCtx);
|
|
1178
|
-
if (e.op === "===" || e.op === "!==") {
|
|
1290
|
+
if (e.op === "===" || e.op === "!==" || e.op === "==" || e.op === "!=") {
|
|
1179
1291
|
left = coerceStr(left, right.ty, ctx.typeDecls);
|
|
1180
1292
|
right = coerceStr(right, left.ty, ctx.typeDecls);
|
|
1181
1293
|
// Spec (`//@`) comparisons are proof-only, so they can't diverge at
|
|
@@ -1187,13 +1299,27 @@ function resolveExprInner(e, ctx) {
|
|
|
1187
1299
|
let ty = { kind: "unknown" };
|
|
1188
1300
|
// <==> is bool like the comparisons; unlike ==>, neither side narrows
|
|
1189
1301
|
// the other (no premise to assume).
|
|
1190
|
-
if (["===", "!==", ">=", "<=", ">", "<", "in", "<==>"].includes(e.op))
|
|
1302
|
+
if (["===", "!==", "==", "!=", ">=", "<=", ">", "<", "in", "<==>"].includes(e.op))
|
|
1191
1303
|
ty = { kind: "bool" };
|
|
1192
1304
|
else if (e.op === "&&")
|
|
1193
1305
|
ty = right.ty;
|
|
1194
|
-
else if (e.op === "||" && left.ty.kind === "optional") {
|
|
1195
|
-
// || undefined
|
|
1196
|
-
|
|
1306
|
+
else if (e.op === "||" && (left.ty.kind === "optional" || left.ty.kind === "nullish")) {
|
|
1307
|
+
// || undefined / || null / || nullable preserves the RHS nullability;
|
|
1308
|
+
// otherwise yields inner type.
|
|
1309
|
+
if (e.right.kind === "var" && e.right.name === "undefined") {
|
|
1310
|
+
ty = left.ty.kind === "optional" && optAbsent(left.ty) === "undefined"
|
|
1311
|
+
? left.ty
|
|
1312
|
+
: { kind: "optional", inner: left.ty.inner };
|
|
1313
|
+
}
|
|
1314
|
+
else if (e.right.kind === "var" && e.right.name === "null") {
|
|
1315
|
+
ty = { kind: "optional", inner: left.ty.inner, absent: "null" };
|
|
1316
|
+
}
|
|
1317
|
+
else if (right.ty.kind === "optional" || right.ty.kind === "nullish") {
|
|
1318
|
+
ty = right.ty;
|
|
1319
|
+
}
|
|
1320
|
+
else {
|
|
1321
|
+
ty = left.ty.inner;
|
|
1322
|
+
}
|
|
1197
1323
|
}
|
|
1198
1324
|
else if (e.op === "||")
|
|
1199
1325
|
ty = right.ty;
|
|
@@ -1290,7 +1416,7 @@ function resolveExprInner(e, ctx) {
|
|
|
1290
1416
|
let args = coerceCallArgs(rawArgs.map((a, i) => {
|
|
1291
1417
|
let aCtx = argCtx;
|
|
1292
1418
|
if (paramTypes && i < paramTypes.length &&
|
|
1293
|
-
(paramTypes[i].kind === "user" || paramTypes[i].kind === "array" || paramTypes[i].kind === "optional")) {
|
|
1419
|
+
(paramTypes[i].kind === "user" || paramTypes[i].kind === "array" || paramTypes[i].kind === "optional" || paramTypes[i].kind === "nullish")) {
|
|
1294
1420
|
aCtx = { ...aCtx, returnTy: paramTypes[i] };
|
|
1295
1421
|
}
|
|
1296
1422
|
if (i === 0 && a.kind === "lambda" && callback)
|
|
@@ -1322,9 +1448,11 @@ function resolveExprInner(e, ctx) {
|
|
|
1322
1448
|
// seq<T>. Rewrite to a synthetic `filterSome` call lowered to the proven
|
|
1323
1449
|
// SeqFilterSome preamble (a plain `Map(.value, Filter(.Some?))` wouldn't
|
|
1324
1450
|
// verify — `.value` is partial).
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1451
|
+
const filterSentinel = e.fn.kind === "field" && e.fn.field === "filter" && e.args.length === 1
|
|
1452
|
+
? definedCheckRawLambdaSentinel(e.args[0]) : null;
|
|
1453
|
+
if (filterSentinel !== null
|
|
1454
|
+
&& fn.kind === "field" && fn.obj.ty.kind === "array" && fn.obj.ty.elem.kind === "optional"
|
|
1455
|
+
&& (filterSentinel === "both" || filterSentinel === optAbsent(fn.obj.ty.elem))) {
|
|
1328
1456
|
return { kind: "call", fn: { ...fn, field: "filterSome" }, args: [], ty: { kind: "array", elem: fn.obj.ty.elem.inner }, callKind: "method" };
|
|
1329
1457
|
}
|
|
1330
1458
|
const builtinId = fn.kind === "field" ? recognizeBuiltin(fn.obj.ty, fn.field) : null;
|
|
@@ -1395,9 +1523,12 @@ function resolveExprInner(e, ctx) {
|
|
|
1395
1523
|
if (ctx.narrowedPaths.length > 0) {
|
|
1396
1524
|
const myPath = asRawAccessPath(e);
|
|
1397
1525
|
if (myPath) {
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1526
|
+
for (let i = ctx.narrowedPaths.length - 1; i >= 0; i--) {
|
|
1527
|
+
if (accessPathsEqual(ctx.narrowedPaths[i].path, myPath)) {
|
|
1528
|
+
ty = ctx.narrowedPaths[i].narrowedTy;
|
|
1529
|
+
break;
|
|
1530
|
+
}
|
|
1531
|
+
}
|
|
1401
1532
|
}
|
|
1402
1533
|
}
|
|
1403
1534
|
// A variant narrowing on the object picks the field's type from that
|
|
@@ -1405,7 +1536,16 @@ function resolveExprInner(e, ctx) {
|
|
|
1405
1536
|
let ofVariant;
|
|
1406
1537
|
if (ty.kind === "unknown" && ctx.narrowedPaths.length > 0 && obj.ty.kind === "user") {
|
|
1407
1538
|
const objPath = asRawAccessPath(e.obj);
|
|
1408
|
-
|
|
1539
|
+
let np;
|
|
1540
|
+
if (objPath) {
|
|
1541
|
+
for (let i = ctx.narrowedPaths.length - 1; i >= 0; i--) {
|
|
1542
|
+
const cand = ctx.narrowedPaths[i];
|
|
1543
|
+
if (cand.variant && accessPathsEqual(cand.path, objPath)) {
|
|
1544
|
+
np = cand;
|
|
1545
|
+
break;
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1409
1549
|
if (np?.variant) {
|
|
1410
1550
|
const decl = findDecl(ctx, tyBaseName(obj.ty.name));
|
|
1411
1551
|
const f = decl?.variants?.find(v => v.name === np.variant)?.fields.find(f => f.name === e.field);
|
|
@@ -1423,37 +1563,45 @@ function resolveExprInner(e, ctx) {
|
|
|
1423
1563
|
return { kind: "field", obj, field: e.field, ty, isDiscriminant, ofVariant };
|
|
1424
1564
|
}
|
|
1425
1565
|
case "nullish": {
|
|
1426
|
-
// left ?? right — result type is left's inner (when left is optional)
|
|
1566
|
+
// left ?? right — result type is left's inner (when left is optional/nullish)
|
|
1427
1567
|
// or just left's type, unified with right's type.
|
|
1428
1568
|
const left = resolveExpr(e.left, ctx);
|
|
1429
|
-
const
|
|
1569
|
+
const isLeftNullishLit = (left.kind === "var" && (left.name === "undefined" || left.name === "null")) ||
|
|
1570
|
+
left.ty.kind === "void" || left.ty.kind === "null";
|
|
1430
1571
|
const isArrayIndex = left.kind === "index" && left.obj.ty.kind === "array";
|
|
1431
|
-
const
|
|
1572
|
+
const isLeftNullable = left.ty.kind === "optional" || left.ty.kind === "nullish";
|
|
1573
|
+
const inner = (left.ty.kind === "optional" || left.ty.kind === "nullish") ? left.ty.inner : left.ty;
|
|
1432
1574
|
// The default shares the result type, so coerce a string literal to a
|
|
1433
1575
|
// string-union enum (e.g. `availableLevels[0] ?? "off"`).
|
|
1434
|
-
const right = coerceToTargetTy(resolveExpr(e.right, ctx),
|
|
1435
|
-
// When `left` is non-
|
|
1436
|
-
// `undefined ?? right` has `right`'s type, and `nonOpt ?? right`
|
|
1437
|
-
// to `left` directly (narrow.ts strips the dead fallback).
|
|
1438
|
-
if (
|
|
1439
|
-
return { kind: "nullish", left, right, ty:
|
|
1440
|
-
}
|
|
1441
|
-
// `??` is only total when its default is: with a nullable
|
|
1442
|
-
// (
|
|
1443
|
-
//
|
|
1444
|
-
|
|
1445
|
-
|
|
1576
|
+
const right = coerceToTargetTy(resolveExpr(e.right, ctx), isLeftNullishLit ? { kind: "unknown" } : inner, ctx.typeDecls);
|
|
1577
|
+
// When `left` is non-nullable and not an unchecked array index (#352):
|
|
1578
|
+
// `undefined ?? right` / `null ?? right` has `right`'s type, and `nonOpt ?? right`
|
|
1579
|
+
// evaluates to `left` directly (narrow.ts strips the dead fallback).
|
|
1580
|
+
if (!isLeftNullable && !isArrayIndex) {
|
|
1581
|
+
return { kind: "nullish", left, right, ty: isLeftNullishLit ? right.ty : left.ty };
|
|
1582
|
+
}
|
|
1583
|
+
// `??` is only total when its default is: with a nullable/null/undefined
|
|
1584
|
+
// right operand (`x ?? undefined`, `ruleA(e) ?? ruleB(e) ?? null`), the
|
|
1585
|
+
// result keeps the right operand's nullability.
|
|
1586
|
+
let ty = inner;
|
|
1587
|
+
if (right.ty.kind === "optional" || right.ty.kind === "nullish")
|
|
1588
|
+
ty = right.ty;
|
|
1589
|
+
else if (right.ty.kind === "void" || (right.kind === "var" && right.name === "undefined"))
|
|
1590
|
+
ty = { kind: "optional", inner };
|
|
1591
|
+
else if (right.ty.kind === "null" || (right.kind === "var" && right.name === "null"))
|
|
1592
|
+
ty = { kind: "optional", inner, absent: "null" };
|
|
1446
1593
|
return { kind: "nullish", left, right, ty };
|
|
1447
1594
|
}
|
|
1448
1595
|
case "optChain": {
|
|
1449
|
-
// obj?.<chain> — obj has type Option<T>; we walk the chain
|
|
1450
|
-
// through types from T.
|
|
1451
|
-
// (
|
|
1452
|
-
// Narrow rewrites this to a someMatch with the chain applied to the binder.
|
|
1596
|
+
// obj?.<chain> — obj has type Option<T> or Nullish<T>; we walk the chain
|
|
1597
|
+
// stepping through types from T. Short-circuiting always yields `undefined`
|
|
1598
|
+
// in JS (so a `T | null` chain step becomes `Nullish<T>`).
|
|
1453
1599
|
const obj = resolveExpr(e.obj, ctx);
|
|
1454
|
-
const
|
|
1600
|
+
const isObjNullishLit = (obj.kind === "var" && (obj.name === "undefined" || obj.name === "null")) ||
|
|
1601
|
+
obj.ty.kind === "void" || obj.ty.kind === "null";
|
|
1455
1602
|
const isObjArrayIndex = obj.kind === "index" && obj.obj.ty.kind === "array";
|
|
1456
|
-
|
|
1603
|
+
const isObjNullable = obj.ty.kind === "optional" || obj.ty.kind === "nullish";
|
|
1604
|
+
let stepInTy = (obj.ty.kind === "optional" || obj.ty.kind === "nullish") ? obj.ty.inner : obj.ty;
|
|
1457
1605
|
const chain = [];
|
|
1458
1606
|
for (const step of e.chain) {
|
|
1459
1607
|
if (step.kind === "field") {
|
|
@@ -1483,7 +1631,7 @@ function resolveExprInner(e, ctx) {
|
|
|
1483
1631
|
let rawArgs = step.args;
|
|
1484
1632
|
if (lastField) {
|
|
1485
1633
|
const priorInTy = chain.length >= 2 ? chain[chain.length - 2].ty
|
|
1486
|
-
: (obj.ty.kind === "optional" ? obj.ty.inner : obj.ty);
|
|
1634
|
+
: ((obj.ty.kind === "optional" || obj.ty.kind === "nullish") ? obj.ty.inner : obj.ty);
|
|
1487
1635
|
const fakeObj = { kind: "var", name: "_chain_recv", ty: priorInTy };
|
|
1488
1636
|
const fakeFn = { kind: "field", obj: fakeObj, field: lastField.name, ty: lastField.ty };
|
|
1489
1637
|
rawArgs = inferLambdaParamTypes(fakeFn, rawArgs);
|
|
@@ -1508,9 +1656,17 @@ function resolveExprInner(e, ctx) {
|
|
|
1508
1656
|
const finalTy = stepInTy;
|
|
1509
1657
|
// Non-optional receiver (excluding unchecked array index) never produces
|
|
1510
1658
|
// None from the `?.` step itself (#352); narrow.ts lowers it directly.
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1659
|
+
// When the receiver IS nullable, short-circuiting adds `| undefined`:
|
|
1660
|
+
// - `T | null` + `| undefined` → `Nullish<T>`
|
|
1661
|
+
// - `Nullish<T>` → `Nullish<T>`
|
|
1662
|
+
// - `T | undefined` → `T | undefined`
|
|
1663
|
+
// - `T` → `T | undefined`
|
|
1664
|
+
const ty = isObjNullishLit ? { kind: "void" }
|
|
1665
|
+
: (!isObjNullable && !isObjArrayIndex) ? finalTy
|
|
1666
|
+
: finalTy.kind === "nullish" ? finalTy
|
|
1667
|
+
: finalTy.kind === "optional"
|
|
1668
|
+
? (optAbsent(finalTy) === "null" ? { kind: "nullish", inner: finalTy.inner } : finalTy)
|
|
1669
|
+
: { kind: "optional", inner: finalTy };
|
|
1514
1670
|
return { kind: "optChain", obj, chain, ty };
|
|
1515
1671
|
}
|
|
1516
1672
|
case "record": {
|
|
@@ -1528,9 +1684,9 @@ function resolveExprInner(e, ctx) {
|
|
|
1528
1684
|
return { kind: "record", spread: null, fields, ty: mapTy };
|
|
1529
1685
|
}
|
|
1530
1686
|
// Infer record type: from spread, or from return type context. Unwrap
|
|
1531
|
-
// an outer Optional when looking at returnTy — `return {...} : null`
|
|
1532
|
-
// has ctx.returnTy = Option<T>, but the record literal's natural type is T.
|
|
1533
|
-
const returnTyUnwrapped = ctx.returnTy.kind === "optional" ? ctx.returnTy.inner : ctx.returnTy;
|
|
1687
|
+
// an outer Optional/Nullish when looking at returnTy — `return {...} : null`
|
|
1688
|
+
// has ctx.returnTy = Option<T>/Nullish<T>, but the record literal's natural type is T.
|
|
1689
|
+
const returnTyUnwrapped = (ctx.returnTy.kind === "optional" || ctx.returnTy.kind === "nullish") ? ctx.returnTy.inner : ctx.returnTy;
|
|
1534
1690
|
const recordTy = ty.kind === "user" ? ty : returnTyUnwrapped.kind === "user" ? returnTyUnwrapped : null;
|
|
1535
1691
|
const decl = recordTy ? declOfKind(ctx.typeDecls, recordTy.name, "record") : undefined;
|
|
1536
1692
|
// Union-variant literal in union-typed context (a constructed IR node,
|
|
@@ -1556,7 +1712,7 @@ function resolveExprInner(e, ctx) {
|
|
|
1556
1712
|
// by coerceToTargetTy below); array fields propagate whole, so the
|
|
1557
1713
|
// arrayLiteral case can thread the element type.
|
|
1558
1714
|
const fdTy = fieldDecl?.type;
|
|
1559
|
-
const fdCtxTy = fdTy?.kind === "optional" ? fdTy.inner : fdTy;
|
|
1715
|
+
const fdCtxTy = (fdTy?.kind === "optional" || fdTy?.kind === "nullish") ? fdTy.inner : fdTy;
|
|
1560
1716
|
const valueCtx = fdCtxTy && (fdCtxTy.kind === "user" || fdCtxTy.kind === "array")
|
|
1561
1717
|
? { ...fieldCtx, returnTy: fdCtxTy }
|
|
1562
1718
|
: fieldCtx;
|
|
@@ -1615,7 +1771,7 @@ function resolveExprInner(e, ctx) {
|
|
|
1615
1771
|
// get their type via ctx.returnTy). Only narrow when the context type is an
|
|
1616
1772
|
// array (unwrapping one optional level — `TStmt[] | null` return positions);
|
|
1617
1773
|
// otherwise leave ctx untouched.
|
|
1618
|
-
const rtUnwrapped = ctx.returnTy.kind === "optional" ? ctx.returnTy.inner : ctx.returnTy;
|
|
1774
|
+
const rtUnwrapped = (ctx.returnTy.kind === "optional" || ctx.returnTy.kind === "nullish") ? ctx.returnTy.inner : ctx.returnTy;
|
|
1619
1775
|
const expectedElem = rtUnwrapped.kind === "array" ? rtUnwrapped.elem : null;
|
|
1620
1776
|
const elemCtx = expectedElem ? { ...ctx, returnTy: expectedElem } : ctx;
|
|
1621
1777
|
const elems = e.elems.map(el => {
|
|
@@ -1670,20 +1826,24 @@ function resolveExprInner(e, ctx) {
|
|
|
1670
1826
|
// checks are in scope when resolving later ones.
|
|
1671
1827
|
let thenCtx = collectAndChainNarrowings(cond, ctx);
|
|
1672
1828
|
let elseCtx = ctx;
|
|
1673
|
-
// Truthiness — cond itself is optional (`opt ? a : b`), only for simple vars.
|
|
1674
|
-
if (cond.ty.kind === "optional" && cond.kind === "var") {
|
|
1829
|
+
// Truthiness — cond itself is optional/nullish (`opt ? a : b`), only for simple vars.
|
|
1830
|
+
if ((cond.ty.kind === "optional" || cond.ty.kind === "nullish") && cond.kind === "var") {
|
|
1675
1831
|
thenCtx = withEnv(thenCtx, refine(thenCtx.env, cond.name, cond.ty.inner));
|
|
1676
1832
|
}
|
|
1677
|
-
//
|
|
1833
|
+
// Negated presence check (single or `||`-chain, var or field path) narrows else-branch.
|
|
1678
1834
|
const single = negatedVarPresence(cond);
|
|
1679
1835
|
if (single) {
|
|
1680
1836
|
elseCtx = withEnv(elseCtx, refine(elseCtx.env, single.varName, single.innerTy));
|
|
1681
1837
|
}
|
|
1682
|
-
if (
|
|
1838
|
+
else if (cond.kind === "binop" && cond.op === "||") {
|
|
1683
1839
|
for (const n of collectEarlyReturnNarrowings(cond)) {
|
|
1684
1840
|
elseCtx = withEnv(elseCtx, refine(elseCtx.env, n.varName, n.innerTy));
|
|
1685
1841
|
}
|
|
1686
1842
|
}
|
|
1843
|
+
const negPaths = collectEarlyReturnPathNarrowings(cond);
|
|
1844
|
+
if (negPaths.length > 0) {
|
|
1845
|
+
elseCtx = { ...elseCtx, narrowedPaths: [...elseCtx.narrowedPaths, ...negPaths] };
|
|
1846
|
+
}
|
|
1687
1847
|
// Map-index narrowing: `k in m` in the cond narrows the then-branch; `!(k in m)`
|
|
1688
1848
|
// narrows the else-branch. Parallel to the if-statement hook in resolveStmt.
|
|
1689
1849
|
thenCtx = withInAtoms(thenCtx, extractInAtoms(cond));
|
|
@@ -1787,14 +1947,14 @@ function invalidateNarrowings(env, narrowedPaths, roots) {
|
|
|
1787
1947
|
for (const root of roots) {
|
|
1788
1948
|
const cur = lookup(nextEnv, root);
|
|
1789
1949
|
const storage = storageLookup(nextEnv, root);
|
|
1790
|
-
if (storage?.kind === "optional" && cur && cur
|
|
1950
|
+
if ((storage?.kind === "optional" || storage?.kind === "nullish") && cur && !tyEqual(cur, storage)) {
|
|
1791
1951
|
nextEnv = refine(nextEnv, root, storage);
|
|
1792
1952
|
}
|
|
1793
1953
|
}
|
|
1794
1954
|
const nextPaths = narrowedPaths.filter(np => !roots.has(np.path.rootVar));
|
|
1795
1955
|
return { env: nextEnv, narrowedPaths: nextPaths };
|
|
1796
1956
|
}
|
|
1797
|
-
/** True if `stmts` definitely assigns a non-optional
|
|
1957
|
+
/** True if `stmts` definitely assigns a non-optional/non-nullish value
|
|
1798
1958
|
* to `varName` (without subsequently clearing or shadowing it) on every
|
|
1799
1959
|
* non-terminating exit path. Used for `#340` assignment narrowing:
|
|
1800
1960
|
* `if (v === undefined) { v = 6; } return v + 3;`. */
|
|
@@ -1814,13 +1974,14 @@ function definitelyAssignsNonOptional(stmts, varName, ctx) {
|
|
|
1814
1974
|
continue;
|
|
1815
1975
|
}
|
|
1816
1976
|
if (st.kind === "assign" && st.target === varName) {
|
|
1817
|
-
if (st.value.kind === "var" && st.value.name === "undefined") {
|
|
1977
|
+
if (st.value.kind === "var" && (st.value.name === "undefined" || st.value.name === "null")) {
|
|
1818
1978
|
assigned = false;
|
|
1819
1979
|
}
|
|
1820
1980
|
else {
|
|
1821
1981
|
const rhsCtx = { ...ctx, env, returnTy: targetStorage ?? ctx.returnTy };
|
|
1822
1982
|
const rhsTy = resolveExpr(st.value, rhsCtx).ty;
|
|
1823
|
-
assigned = rhsTy.kind !== "optional" && rhsTy.kind !== "
|
|
1983
|
+
assigned = rhsTy.kind !== "optional" && rhsTy.kind !== "nullish" &&
|
|
1984
|
+
rhsTy.kind !== "void" && rhsTy.kind !== "null" && rhsTy.kind !== "unknown";
|
|
1824
1985
|
}
|
|
1825
1986
|
}
|
|
1826
1987
|
else if (st.kind === "if") {
|
|
@@ -1872,11 +2033,13 @@ function resolveBlock(stmts, ctx) {
|
|
|
1872
2033
|
narrowedPaths = narrowedPaths.filter(np => np.path.rootVar !== root);
|
|
1873
2034
|
if (!s.target.includes(".")) {
|
|
1874
2035
|
const storage = storageLookup(env, s.target);
|
|
1875
|
-
if (storage?.kind === "optional") {
|
|
1876
|
-
const
|
|
2036
|
+
if (storage?.kind === "optional" || storage?.kind === "nullish") {
|
|
2037
|
+
const isNullLit = s.value.kind === "var" && s.value.name === "null";
|
|
2038
|
+
const isUndefLit = s.value.kind === "var" && s.value.name === "undefined";
|
|
1877
2039
|
const rhsCtx = { ...currentCtx, returnTy: storage };
|
|
1878
|
-
const rawTy =
|
|
1879
|
-
if (!
|
|
2040
|
+
const rawTy = isUndefLit ? { kind: "void" } : isNullLit ? { kind: "null" } : resolveExpr(s.value, rhsCtx).ty;
|
|
2041
|
+
if (!isUndefLit && !isNullLit && rawTy.kind !== "optional" && rawTy.kind !== "nullish" &&
|
|
2042
|
+
rawTy.kind !== "void" && rawTy.kind !== "null" && rawTy.kind !== "unknown") {
|
|
1880
2043
|
env = refine(env, s.target, storage.inner);
|
|
1881
2044
|
}
|
|
1882
2045
|
else {
|
|
@@ -1964,7 +2127,7 @@ function resolveStmt(s, ctx) {
|
|
|
1964
2127
|
// type into the array literal (see the arrayLiteral case). Optionals too
|
|
1965
2128
|
// (`const r: TExpr | null = cond ? {…} : null`) — the record case unwraps
|
|
1966
2129
|
// one optional level when consulting returnTy.
|
|
1967
|
-
const initCtx = (declTy.kind === "user" || declTy.kind === "array" || declTy.kind === "optional")
|
|
2130
|
+
const initCtx = (declTy.kind === "user" || declTy.kind === "array" || declTy.kind === "optional" || declTy.kind === "nullish")
|
|
1968
2131
|
? { ...ctx, returnTy: declTy } : ctx;
|
|
1969
2132
|
let init = coerceStr(resolveExpr(s.init, initCtx), declTy, ctx.typeDecls);
|
|
1970
2133
|
// Under noUncheckedIndexedAccess, TS gives `const e = arr[i]` type T | undefined
|
|
@@ -1982,9 +2145,11 @@ function resolveStmt(s, ctx) {
|
|
|
1982
2145
|
// by inlining an alias we shadow via declare-type, or any-laden), but
|
|
1983
2146
|
// LS resolved the initializer to something concrete. Take the structure
|
|
1984
2147
|
// from `init.ty`, keeping only the optionality ts-morph reported.
|
|
1985
|
-
ty = declTy.kind === "
|
|
1986
|
-
? { kind: "
|
|
1987
|
-
: init.ty
|
|
2148
|
+
ty = declTy.kind === "nullish" && init.ty.kind !== "nullish"
|
|
2149
|
+
? { kind: "nullish", inner: init.ty.kind === "optional" ? init.ty.inner : init.ty }
|
|
2150
|
+
: declTy.kind === "optional" && init.ty.kind !== "optional"
|
|
2151
|
+
? { ...declTy, inner: init.ty }
|
|
2152
|
+
: init.ty;
|
|
1988
2153
|
}
|
|
1989
2154
|
else if (isWidenedStringUnionTy(declTy, init.ty, ctx.typeDecls) && !ctx.overrides.has(s.name)) {
|
|
1990
2155
|
// ts-morph widened a string-union to `string`; keep the initializer's
|
|
@@ -1999,7 +2164,7 @@ function resolveStmt(s, ctx) {
|
|
|
1999
2164
|
}
|
|
2000
2165
|
else {
|
|
2001
2166
|
// Map indexing: TS says T, but access can fail → use Optional<T> from init
|
|
2002
|
-
ty = (declTy.kind !== "optional" && init.ty.kind === "optional") ? init.ty : declTy;
|
|
2167
|
+
ty = (declTy.kind !== "optional" && declTy.kind !== "nullish" && init.ty.kind === "optional") ? init.ty : declTy;
|
|
2003
2168
|
}
|
|
2004
2169
|
// const collections are mutable in value-semantics world (TS mutates in place, Dafny/Lean reassign)
|
|
2005
2170
|
const mutable = s.mutable || isRefMutableInTS(ty);
|
|
@@ -2018,7 +2183,7 @@ function resolveStmt(s, ctx) {
|
|
|
2018
2183
|
// Propagate the target's type as returnTy, mirroring the annotated-let
|
|
2019
2184
|
// case, so record/union literals and array literals in the RHS resolve
|
|
2020
2185
|
// to their named datatypes.
|
|
2021
|
-
const valueCtx = (targetTy.kind === "user" || targetTy.kind === "array" || targetTy.kind === "optional")
|
|
2186
|
+
const valueCtx = (targetTy.kind === "user" || targetTy.kind === "array" || targetTy.kind === "optional" || targetTy.kind === "nullish")
|
|
2022
2187
|
? { ...ctx, returnTy: targetTy } : ctx;
|
|
2023
2188
|
const value = coerceToTargetTy(resolveExpr(s.value, valueCtx), targetTy, ctx.typeDecls);
|
|
2024
2189
|
return [{ kind: "assign", target: entry?.emittedName ?? s.target, value }, ctx.env];
|
|
@@ -2045,6 +2210,15 @@ function resolveStmt(s, ctx) {
|
|
|
2045
2210
|
if (single) {
|
|
2046
2211
|
elseCtx = withEnv(ctx, refine(ctx.env, single.varName, single.innerTy));
|
|
2047
2212
|
}
|
|
2213
|
+
else if (resolvedCond.kind === "binop" && resolvedCond.op === "||") {
|
|
2214
|
+
for (const n of collectEarlyReturnNarrowings(resolvedCond)) {
|
|
2215
|
+
elseCtx = withEnv(elseCtx, refine(elseCtx.env, n.varName, n.innerTy));
|
|
2216
|
+
}
|
|
2217
|
+
}
|
|
2218
|
+
const negPaths = collectEarlyReturnPathNarrowings(resolvedCond);
|
|
2219
|
+
if (negPaths.length > 0) {
|
|
2220
|
+
elseCtx = { ...elseCtx, narrowedPaths: [...elseCtx.narrowedPaths, ...negPaths] };
|
|
2221
|
+
}
|
|
2048
2222
|
// Narrow map index access across `k in m` / `!(k in m)` in the cond:
|
|
2049
2223
|
// positive atoms (from `k in m` or &&-chains containing it) → then-branch;
|
|
2050
2224
|
// negated atoms (from `!(k in m)`) → else-branch.
|