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/lean-emit.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* opaque-tainted types, discriminator/destructor lowering, method dispatch,
|
|
5
5
|
* and support-import selection.
|
|
6
6
|
*/
|
|
7
|
-
import { anyExpr, decimalNumberLiteral, usesNameInDecl, patternBinders } from "./ir.js";
|
|
7
|
+
import { anyExpr, anyExprInStmts, decimalNumberLiteral, usesNameInDecl, patternBinders } from "./ir.js";
|
|
8
8
|
import { freshName, freshNameWhere } from "./names.js";
|
|
9
9
|
// ── Ty → Lean type string ──────────────────────────────────
|
|
10
10
|
function tyToLean(ty) {
|
|
@@ -30,6 +30,7 @@ function tyToLean(ty) {
|
|
|
30
30
|
case "bool": return "Bool";
|
|
31
31
|
case "string": return "String";
|
|
32
32
|
case "void": return "Unit";
|
|
33
|
+
case "null": return "Unit";
|
|
33
34
|
case "array": {
|
|
34
35
|
const elem = tyToLean(ty.elem);
|
|
35
36
|
return elem.includes(" ") ? `Array (${elem})` : `Array ${elem}`;
|
|
@@ -56,6 +57,11 @@ function tyToLean(ty) {
|
|
|
56
57
|
const inner = tyToLean(ty.inner);
|
|
57
58
|
return inner.includes(" ") ? `Option (${inner})` : `Option ${inner}`;
|
|
58
59
|
}
|
|
60
|
+
case "nullish": {
|
|
61
|
+
_needsNullish = true;
|
|
62
|
+
const inner = tyToLean(ty.inner);
|
|
63
|
+
return inner.includes(" ") ? `Nullish (${inner})` : `Nullish ${inner}`;
|
|
64
|
+
}
|
|
59
65
|
case "user": return ty.name;
|
|
60
66
|
case "fn": {
|
|
61
67
|
const params = ty.params.map(p => {
|
|
@@ -183,8 +189,10 @@ function emitDeriving(name, deriving) {
|
|
|
183
189
|
return der.length > 0 ? `\nderiving ${der.join(", ")}` : "";
|
|
184
190
|
}
|
|
185
191
|
let _needsJSString = false;
|
|
192
|
+
let _needsNullish = false;
|
|
186
193
|
let _needsUnknown = false;
|
|
187
194
|
let _unknownEmitted = false; // across files in one run — the def file imports the types file
|
|
195
|
+
let _depIfCounter = 0;
|
|
188
196
|
// Bool-vs-Prop context. Lean keeps `Bool` and `Prop` distinct; the IR uses the
|
|
189
197
|
// Prop connectives (∧/∨/¬) uniformly. In a computational position the connectives
|
|
190
198
|
// may need to be the Bool ones (&&/||/!). They are only *required* when a connective
|
|
@@ -192,10 +200,19 @@ let _unknownEmitted = false; // across files in one run — the def file imports
|
|
|
192
200
|
// built only from decidable atoms (comparisons, Bool-returning calls) coerces fine
|
|
193
201
|
// and stays in the more proof-friendly Prop form.
|
|
194
202
|
let _boolCtx = false;
|
|
203
|
+
const LEAN_CTOR_MAP = {
|
|
204
|
+
"nullishUndefined": "undefined",
|
|
205
|
+
"nullishNull": "null",
|
|
206
|
+
"nullishValue": "value",
|
|
207
|
+
};
|
|
195
208
|
/** Constructor names come from source strings (string-union values,
|
|
196
209
|
* discriminated-union tags) and may contain non-identifier characters
|
|
197
210
|
* ("spec-pure"); guillemet-quote those — exact and collision-free. */
|
|
198
211
|
function leanCtorName(name) {
|
|
212
|
+
if (Object.hasOwn(LEAN_CTOR_MAP, name)) {
|
|
213
|
+
_needsNullish = true;
|
|
214
|
+
return LEAN_CTOR_MAP[name];
|
|
215
|
+
}
|
|
199
216
|
return /^[A-Za-z_][A-Za-z0-9_'!?]*$/.test(name) ? name : `«${name}»`;
|
|
200
217
|
}
|
|
201
218
|
/** Render a match pattern to Lean syntax: `_`, a quoted literal, or `.ctor args`. */
|
|
@@ -235,9 +252,18 @@ function connectiveHasNonCoercibleOperand(e) {
|
|
|
235
252
|
// so a body with no such operand stays Prop — this keeps arithmetic predicates like
|
|
236
253
|
// `x ≥ 0 ∧ x < n` in `∧` form rather than forcing `&&`.)
|
|
237
254
|
function needsBoolConnectives(e) {
|
|
238
|
-
return anyExpr(e, connectiveHasNonCoercibleOperand)
|
|
255
|
+
return anyExpr(e, x => connectiveHasNonCoercibleOperand(x) ||
|
|
256
|
+
((x.kind === "binop" && (x.op === "∧" || x.op === "∨") && anyExpr(x.right, y => y.kind === "checkedUnwrap")) ||
|
|
257
|
+
(x.kind === "if" && (anyExpr(x.then, y => y.kind === "checkedUnwrap") || anyExpr(x.else, y => y.kind === "checkedUnwrap")))));
|
|
239
258
|
}
|
|
240
259
|
function emitMethodCall(tyKind, method, monadic, obj, args) {
|
|
260
|
+
// Nullish<T> discriminators (#304)
|
|
261
|
+
if (tyKind === "nullish") {
|
|
262
|
+
_needsNullish = true;
|
|
263
|
+
if (method === "isNull" || method === "isUndefined" || method === "isValue") {
|
|
264
|
+
return `(${obj}).${method}`;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
241
267
|
// Array methods
|
|
242
268
|
if (tyKind === "array") {
|
|
243
269
|
if (method === "map")
|
|
@@ -333,6 +359,8 @@ function emitExpr(e, parentPrec) {
|
|
|
333
359
|
case "bool": return e.value ? "true" : "false";
|
|
334
360
|
case "str": return `"${e.value.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, '\\n')}"`;
|
|
335
361
|
case "constructor": {
|
|
362
|
+
if (e.type === "Nullish")
|
|
363
|
+
_needsNullish = true;
|
|
336
364
|
// With type: emit `Type.name` (unambiguous; needed in expression positions
|
|
337
365
|
// like `match ... | .none => Type.some x` where elaboration can't infer).
|
|
338
366
|
// Without type: emit `.name` (dotted form; works in pattern positions
|
|
@@ -440,12 +468,13 @@ function emitExpr(e, parentPrec) {
|
|
|
440
468
|
const l = emitExpr(e.left);
|
|
441
469
|
const r = emitExpr(e.right);
|
|
442
470
|
const condProp = isPropValued(e.left) ? l : `(${l}) = true`;
|
|
471
|
+
const h = `_h${_depIfCounter++}`;
|
|
443
472
|
const tVal = _boolCtx ? "true" : "True";
|
|
444
473
|
const fVal = _boolCtx ? "false" : "False";
|
|
445
474
|
const rExpr = _boolCtx && isPropValued(e.right) ? `decide (${r})` : r;
|
|
446
475
|
return e.op === "∧"
|
|
447
|
-
? `(if
|
|
448
|
-
: `(if
|
|
476
|
+
? `(if ${h} : ${condProp} then (${rExpr}) else ${fVal})`
|
|
477
|
+
: `(if ${h} : ${condProp} then ${tVal} else (${rExpr}))`;
|
|
449
478
|
}
|
|
450
479
|
// Comparisons and ↔ do not chain in Lean. In particular, a Boolean
|
|
451
480
|
// postcondition `res = (s = t)` must keep its inner equality grouped.
|
|
@@ -463,10 +492,26 @@ function emitExpr(e, parentPrec) {
|
|
|
463
492
|
// parens (→ is right-associative, so `(a → b) → c` ≠ `a → b → c`), and
|
|
464
493
|
// ↔ binds looser than → in Lean. The conclusion is the right-assoc tail,
|
|
465
494
|
// where a nested implication is safe bare — only ↔ needs parens there.
|
|
495
|
+
// When any later premise or the conclusion contains `checkedUnwrap`
|
|
496
|
+
// (`(x).get (by grind)`), bind earlier premises dependently via
|
|
497
|
+
// `∀ (_hN : p), ...` so Lean puts each premise in local proof context.
|
|
466
498
|
const wrapIff = (x) => x.kind === "binop" && x.op === "↔" ? `(${emitExpr(x)})` : undefined;
|
|
467
|
-
const
|
|
468
|
-
|
|
469
|
-
|
|
499
|
+
const tailHasCU = (idx) => anyExpr(e.conclusion, x => x.kind === "checkedUnwrap") ||
|
|
500
|
+
e.premises.slice(idx + 1).some(p => anyExpr(p, x => x.kind === "checkedUnwrap"));
|
|
501
|
+
let acc = wrapIff(e.conclusion) ?? emitExpr(e.conclusion);
|
|
502
|
+
for (let i = e.premises.length - 1; i >= 0; i--) {
|
|
503
|
+
const p = e.premises[i];
|
|
504
|
+
if (tailHasCU(i)) {
|
|
505
|
+
const pStr = emitExpr(p);
|
|
506
|
+
const pProp = isPropValued(p) ? pStr : `(${pStr}) = true`;
|
|
507
|
+
const h = `_h${_depIfCounter++}`;
|
|
508
|
+
acc = `∀ (${h} : ${pProp}), ${acc}`;
|
|
509
|
+
}
|
|
510
|
+
else {
|
|
511
|
+
acc = `${wrapOperand(p, prec("→"))} → ${acc}`;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
return parentPrec !== undefined ? `(${acc})` : acc;
|
|
470
515
|
}
|
|
471
516
|
case "app": {
|
|
472
517
|
// A bare negative literal argument (`f -7 2`) parses as subtraction in Lean.
|
|
@@ -566,11 +611,12 @@ function emitExpr(e, parentPrec) {
|
|
|
566
611
|
if (hasCU) {
|
|
567
612
|
const c = emitExpr(e.cond);
|
|
568
613
|
const condProp = isPropValued(e.cond) ? c : `(${c}) = true`;
|
|
614
|
+
const h = `_h${_depIfCounter++}`;
|
|
569
615
|
const t = emitExpr(e.then);
|
|
570
616
|
const f = emitExpr(e.else);
|
|
571
617
|
const tExpr = _boolCtx && isPropValued(e.then) ? `decide (${t})` : t;
|
|
572
618
|
const fExpr = _boolCtx && isPropValued(e.else) ? `decide (${f})` : f;
|
|
573
|
-
return `(if
|
|
619
|
+
return `(if ${h} : ${condProp} then (${tExpr}) else (${fExpr}))`;
|
|
574
620
|
}
|
|
575
621
|
return `if ${emitExpr(e.cond)} then ${emitExpr(e.then)} else ${emitExpr(e.else)}`;
|
|
576
622
|
}
|
|
@@ -612,7 +658,26 @@ function isPropValued(e) {
|
|
|
612
658
|
}
|
|
613
659
|
// ── Statement emission ──────────────────────────────────────
|
|
614
660
|
function emitStmts(stmts, indent) {
|
|
615
|
-
|
|
661
|
+
const pad = " ".repeat(indent);
|
|
662
|
+
const out = [];
|
|
663
|
+
for (let i = 0; i < stmts.length; i++) {
|
|
664
|
+
const s = stmts[i];
|
|
665
|
+
const rest = stmts.slice(i + 1);
|
|
666
|
+
// Early-exit `if` followed by statements containing `checkedUnwrap`:
|
|
667
|
+
// nest the continuation in the `else` branch of a dependent `if _hN : ...`
|
|
668
|
+
// so Lean keeps the negated guard in local proof context for `grind`.
|
|
669
|
+
if (s.kind === "if" && s.else.length === 0 && rest.length > 0 && s.then.length > 0 &&
|
|
670
|
+
["return", "break", "continue"].includes(s.then[s.then.length - 1].kind) &&
|
|
671
|
+
anyExprInStmts(rest, x => x.kind === "checkedUnwrap")) {
|
|
672
|
+
const c = emitExpr(s.cond);
|
|
673
|
+
const condProp = isPropValued(s.cond) ? c : `(${c}) = true`;
|
|
674
|
+
const h = `_h${_depIfCounter++}`;
|
|
675
|
+
out.push(`${pad}if ${h} : ${condProp} then\n${emitStmts(s.then, indent + 1)}\n${pad}else\n${emitStmts(rest, indent + 1)}`);
|
|
676
|
+
break;
|
|
677
|
+
}
|
|
678
|
+
out.push(emitStmt(s, indent));
|
|
679
|
+
}
|
|
680
|
+
return out.join("\n");
|
|
616
681
|
}
|
|
617
682
|
function emitStmt(s, indent) {
|
|
618
683
|
const pad = " ".repeat(indent);
|
|
@@ -655,9 +720,18 @@ function emitStmt(s, indent) {
|
|
|
655
720
|
case "break": return `${pad}break`;
|
|
656
721
|
case "continue": return `${pad}continue`;
|
|
657
722
|
case "if": {
|
|
658
|
-
|
|
723
|
+
const hasCU = anyExprInStmts(s.then, x => x.kind === "checkedUnwrap") ||
|
|
724
|
+
anyExprInStmts(s.else, x => x.kind === "checkedUnwrap");
|
|
725
|
+
const condText = hasCU
|
|
726
|
+
? (() => {
|
|
727
|
+
const c = emitExpr(s.cond);
|
|
728
|
+
const condProp = isPropValued(s.cond) ? c : `(${c}) = true`;
|
|
729
|
+
return `_h${_depIfCounter++} : ${condProp}`;
|
|
730
|
+
})()
|
|
731
|
+
: emitExpr(s.cond);
|
|
732
|
+
let out = `${pad}if ${condText} then\n${emitStmts(s.then, indent + 1)}`;
|
|
659
733
|
if (s.else.length > 0) {
|
|
660
|
-
if (s.else.length === 1 && s.else[0].kind === "if") {
|
|
734
|
+
if (!hasCU && s.else.length === 1 && s.else[0].kind === "if") {
|
|
661
735
|
const ei = s.else[0];
|
|
662
736
|
out += `\n${pad}else if ${emitExpr(ei.cond)} then\n${emitStmts(ei.then, indent + 1)}`;
|
|
663
737
|
if (ei.else.length > 0)
|
|
@@ -920,8 +994,16 @@ function emitDecl(d) {
|
|
|
920
994
|
function emitPureExpr(e, indent) {
|
|
921
995
|
const pad = " ".repeat(indent);
|
|
922
996
|
switch (e.kind) {
|
|
923
|
-
case "if":
|
|
997
|
+
case "if": {
|
|
998
|
+
const hasCU = anyExpr(e.then, x => x.kind === "checkedUnwrap") || anyExpr(e.else, x => x.kind === "checkedUnwrap");
|
|
999
|
+
if (hasCU) {
|
|
1000
|
+
const c = emitExpr(e.cond);
|
|
1001
|
+
const condProp = isPropValued(e.cond) ? c : `(${c}) = true`;
|
|
1002
|
+
const h = `_h${_depIfCounter++}`;
|
|
1003
|
+
return `${pad}if ${h} : ${condProp} then\n${emitPureExpr(e.then, indent + 1)}\n${pad}else\n${emitPureExpr(e.else, indent + 1)}`;
|
|
1004
|
+
}
|
|
924
1005
|
return `${pad}if ${emitExpr(e.cond)} then\n${emitPureExpr(e.then, indent + 1)}\n${pad}else\n${emitPureExpr(e.else, indent + 1)}`;
|
|
1006
|
+
}
|
|
925
1007
|
case "match": {
|
|
926
1008
|
const lines = [`${pad}match ${emitExpr(e.scrutinee)} with`];
|
|
927
1009
|
for (const arm of e.arms) {
|
|
@@ -947,15 +1029,19 @@ export function resetLeanModule() {
|
|
|
947
1029
|
_typeRefs.clear();
|
|
948
1030
|
_taintedTypes.clear();
|
|
949
1031
|
_needsJSString = false;
|
|
1032
|
+
_needsNullish = false;
|
|
950
1033
|
_needsUnknown = false;
|
|
951
1034
|
_unknownEmitted = false;
|
|
1035
|
+
_depIfCounter = 0;
|
|
952
1036
|
_boolCtx = false;
|
|
953
1037
|
}
|
|
954
1038
|
export function emitLeanFile(file) {
|
|
955
1039
|
_needsJSString = false;
|
|
1040
|
+
_needsNullish = false;
|
|
956
1041
|
_needsUnknown = false;
|
|
1042
|
+
_depIfCounter = 0;
|
|
957
1043
|
registerInductives(file.decls);
|
|
958
|
-
// Emit declarations first so _needsJSString / _needsUnknown are set
|
|
1044
|
+
// Emit declarations first so _needsJSString / _needsNullish / _needsUnknown are set
|
|
959
1045
|
const declLines = [];
|
|
960
1046
|
for (const decl of file.decls) {
|
|
961
1047
|
declLines.push("");
|
|
@@ -976,6 +1062,8 @@ export function emitLeanFile(file) {
|
|
|
976
1062
|
lines.push(`import ${imp}`);
|
|
977
1063
|
if (_needsJSString && !file.imports.includes("LemmaScript.JSString"))
|
|
978
1064
|
lines.push("import LemmaScript.JSString");
|
|
1065
|
+
if (_needsNullish && !file.imports.includes("LemmaScript.Nullish"))
|
|
1066
|
+
lines.push("import LemmaScript.Nullish");
|
|
979
1067
|
if (file.options.length > 0)
|
|
980
1068
|
lines.push("");
|
|
981
1069
|
for (const opt of file.options)
|
|
@@ -113,7 +113,7 @@ function declaredTy(e, scope, ctx) {
|
|
|
113
113
|
if (e.kind !== "field")
|
|
114
114
|
return null;
|
|
115
115
|
const objTy = declaredTy(e.obj, scope, ctx);
|
|
116
|
-
if (!objTy || objTy.kind === "optional")
|
|
116
|
+
if (!objTy || objTy.kind === "optional" || objTy.kind === "nullish")
|
|
117
117
|
return null;
|
|
118
118
|
return fieldTy(objTy, e.field, e.ofVariant, ctx);
|
|
119
119
|
}
|
|
@@ -147,9 +147,15 @@ function fieldInfoTy(f) {
|
|
|
147
147
|
if (!f)
|
|
148
148
|
return null;
|
|
149
149
|
const ty = f.type ?? parseTsType(f.tsType);
|
|
150
|
-
|
|
150
|
+
if (!f.optionalKey)
|
|
151
|
+
return ty;
|
|
152
|
+
if (ty.kind === "nullish")
|
|
153
|
+
return ty;
|
|
154
|
+
if (ty.kind === "optional")
|
|
155
|
+
return ty.absent === "null" ? { kind: "nullish", inner: ty.inner } : ty;
|
|
156
|
+
return { kind: "optional", inner: ty };
|
|
151
157
|
}
|
|
152
|
-
const isOptional = (ty) => ty?.kind === "optional";
|
|
158
|
+
const isOptional = (ty) => ty?.kind === "optional" || ty?.kind === "nullish";
|
|
153
159
|
/** The type to judge a value by: declared for an access path, otherwise the stamp. */
|
|
154
160
|
function valueTy(e, scope, ctx) {
|
|
155
161
|
return declaredTy(e, scope, ctx) ?? exprTy(e);
|
package/tools/dist/narrow.js
CHANGED
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
*/
|
|
36
36
|
import { isTerminatorKind } from "./typedir.js";
|
|
37
37
|
import { builtinSpec } from "./builtins.js";
|
|
38
|
-
import {
|
|
38
|
+
import { optionalPresentFact, leadingPresent, leadingIsArray, flattenOr, noneDetector, bindPresentFact, freshPathBinder, binderHintForMapAccess, freshOcBinder, applyChain, restoreDiscriminantFlag, arrayBoundsCond, exprEqual, isArrayFact, typeofStringFact, variantFact, negVariantFact, presentMatchStmts, presentMatchExpr, } from "./condition-facts.js";
|
|
39
39
|
import { insertCheckedUnwraps } from "./checked-reads.js";
|
|
40
40
|
// ── Walkers ──────────────────────────────────────────────────
|
|
41
41
|
function walkExpr(e, ctx) {
|
|
@@ -177,10 +177,11 @@ function stmtsMayClearPath(stmts, pathStr) {
|
|
|
177
177
|
for (const s of stmts) {
|
|
178
178
|
if (s.kind === "assign") {
|
|
179
179
|
if (s.target === pathStr) {
|
|
180
|
-
const
|
|
181
|
-
s.value.fn.name === "Some" && s.value.args.length === 1;
|
|
182
|
-
if (!
|
|
183
|
-
|
|
180
|
+
const isPresentWrap = s.value.kind === "call" && s.value.fn.kind === "var" &&
|
|
181
|
+
(s.value.fn.name === "Some" || s.value.fn.name === "NullishValue") && s.value.args.length === 1;
|
|
182
|
+
if (!isPresentWrap && (s.value.ty.kind === "optional" || s.value.ty.kind === "nullish" ||
|
|
183
|
+
s.value.ty.kind === "void" || s.value.ty.kind === "null" ||
|
|
184
|
+
(s.value.kind === "var" && (s.value.name === "undefined" || s.value.name === "null")))) {
|
|
184
185
|
return true;
|
|
185
186
|
}
|
|
186
187
|
}
|
|
@@ -224,7 +225,7 @@ function scrutineeMayBeCleared(scrutinee, stmts) {
|
|
|
224
225
|
function ruleIfOptionalSimple(s, ctx) {
|
|
225
226
|
if (s.kind !== "if")
|
|
226
227
|
return null;
|
|
227
|
-
const check =
|
|
228
|
+
const check = optionalPresentFact(s.cond);
|
|
228
229
|
if (!check)
|
|
229
230
|
return null;
|
|
230
231
|
const someBody = check.negated ? s.else : s.then;
|
|
@@ -243,7 +244,7 @@ function ruleEarlyReturnConsume(s, rest, ctx) {
|
|
|
243
244
|
return null;
|
|
244
245
|
if (rest.length === 0)
|
|
245
246
|
return null;
|
|
246
|
-
const check =
|
|
247
|
+
const check = optionalPresentFact(s.cond);
|
|
247
248
|
if (!check)
|
|
248
249
|
return null;
|
|
249
250
|
const someBranch = check.negated ? s.else : s.then;
|
|
@@ -323,7 +324,7 @@ function ruleEarlyReturnOptChainCompare(s, rest, ctx) {
|
|
|
323
324
|
if (c.kind !== "binop" || c.op !== "!==")
|
|
324
325
|
return null;
|
|
325
326
|
const oc = c.left.kind === "optChain" ? c.left : c.right.kind === "optChain" ? c.right : null;
|
|
326
|
-
if (!oc || oc.kind !== "optChain" || oc.obj.ty.kind !== "optional")
|
|
327
|
+
if (!oc || oc.kind !== "optChain" || oc.obj.ty.kind !== "optional" || oc.obj.ty.fromNullish)
|
|
327
328
|
return null;
|
|
328
329
|
if (scrutineeMayBeCleared(oc.obj, rest))
|
|
329
330
|
return null;
|
|
@@ -348,7 +349,7 @@ function ruleEarlyReturnOptChainCompare(s, rest, ctx) {
|
|
|
348
349
|
function ruleConditionalOptionalSimple(e, ctx) {
|
|
349
350
|
if (e.kind !== "conditional")
|
|
350
351
|
return null;
|
|
351
|
-
const check =
|
|
352
|
+
const check = optionalPresentFact(e.cond);
|
|
352
353
|
if (!check)
|
|
353
354
|
return null;
|
|
354
355
|
const someBody = check.negated ? e.else : e.then;
|
|
@@ -373,7 +374,7 @@ function ruleImplOptional(e, ctx) {
|
|
|
373
374
|
restCond = extracted.restCond;
|
|
374
375
|
}
|
|
375
376
|
else {
|
|
376
|
-
const c =
|
|
377
|
+
const c = optionalPresentFact(e.left);
|
|
377
378
|
if (!c || c.negated)
|
|
378
379
|
return null;
|
|
379
380
|
check = c;
|
|
@@ -398,11 +399,14 @@ function ruleImplOptional(e, ctx) {
|
|
|
398
399
|
function ruleNullish(e, ctx) {
|
|
399
400
|
if (e.kind !== "nullish")
|
|
400
401
|
return null;
|
|
401
|
-
//
|
|
402
|
+
// Nullish<T> (and partially narrowed fromNullish) lowers via a 3-arm match in transform.ts.
|
|
403
|
+
if (e.left.ty.kind === "nullish" || (e.left.ty.kind === "optional" && e.left.ty.fromNullish))
|
|
404
|
+
return null;
|
|
405
|
+
// Non-optional left operand (#352): `undefined ?? r` / `null ?? r` -> `r`; plain `x ?? r` -> `x`.
|
|
402
406
|
if (e.left.ty.kind !== "optional") {
|
|
403
|
-
if (e.left.kind === "var" && e.left.name === "undefined")
|
|
407
|
+
if (e.left.kind === "var" && (e.left.name === "undefined" || e.left.name === "null"))
|
|
404
408
|
return e.right;
|
|
405
|
-
if (e.left.ty.kind === "void")
|
|
409
|
+
if (e.left.ty.kind === "void" || e.left.ty.kind === "null")
|
|
406
410
|
return e.right;
|
|
407
411
|
return e.left;
|
|
408
412
|
}
|
|
@@ -481,9 +485,13 @@ function ruleOptionalIndexBinding(s) {
|
|
|
481
485
|
function ruleOptChain(e, ctx) {
|
|
482
486
|
if (e.kind !== "optChain")
|
|
483
487
|
return null;
|
|
484
|
-
//
|
|
488
|
+
// Nullish<T> (and partially narrowed fromNullish) lowers via a 3-arm match in transform.ts.
|
|
489
|
+
if (e.obj.ty.kind === "nullish" || (e.obj.ty.kind === "optional" && e.obj.ty.fromNullish))
|
|
490
|
+
return null;
|
|
491
|
+
// Non-optional receiver (#352): `undefined?.foo` / `null?.foo` -> `undefined`; plain `s?.length` -> `s.length`.
|
|
485
492
|
if (e.obj.ty.kind !== "optional") {
|
|
486
|
-
if ((e.obj.kind === "var" && e.obj.name === "undefined"
|
|
493
|
+
if ((e.obj.kind === "var" && (e.obj.name === "undefined" || e.obj.name === "null")) ||
|
|
494
|
+
e.obj.ty.kind === "void" || e.obj.ty.kind === "null") {
|
|
487
495
|
return { kind: "var", name: "undefined", ty: { kind: "void" } };
|
|
488
496
|
}
|
|
489
497
|
return applyChain(e.obj, e.chain);
|
|
@@ -541,7 +549,7 @@ function ruleConditionalInMap(e, ctx) {
|
|
|
541
549
|
function ruleConditionalOptionalTruthy(e, ctx) {
|
|
542
550
|
if (e.kind !== "conditional")
|
|
543
551
|
return null;
|
|
544
|
-
if (e.cond.ty.kind !== "optional")
|
|
552
|
+
if (e.cond.ty.kind !== "optional" || e.cond.ty.fromNullish)
|
|
545
553
|
return null;
|
|
546
554
|
const binder = freshPathBinder(e.cond, ctx);
|
|
547
555
|
if (binder === null)
|