Files
scriptc/tests/corpus/2393-loop-exit-guard-narrow.ts
Chris Tate b9662a34a1 Pin flow-narrowing state routing across merges, loop edges, and do-while (corpus 2390-2423)
- Establishment/containment: miss-first ternary polarity, else-if head exits, bare-block vs alias guards, break/continue exit guards, post-loop re-tests, while-condition guards, born-narrowed declarations (2390-2396)
- Kill routing: branch/switch-arm/reassignment-polarity kills at merges, and kills riding return/break/labeled-break/continue/throw edges (2397-2406)
- Terminality: infinite-loop sealing and re-opening, loop-body returns, stacked and trailing-empty case labels, defaultless exhaustive switches (2407-2411)
- do-while trailing tests under body guards, kills, continue, and break, plus spread-arm ternary lowering shapes, ?? null identity, float-lane bounds, IEEE remainder corners, and parameter null reassignment (2412-2423)
- Order-parity baselines re-recorded for the new entries (pure addition)
2026-07-22 21:40:11 -05:00

39 lines
1.2 KiB
TypeScript

// break and continue guards narrow the rest of the loop iteration exactly like an early return — null guards and discriminant guards both.
interface NumResult { readonly value: number; readonly next: number; }
function parseNumber(body: readonly number[], i: number): NumResult | null {
if (i >= body.length) return null;
return { value: body[i], next: i + 1 };
}
function collect(body: readonly number[]): number[] {
const out: number[] = [];
let i = 0;
while (i < body.length) {
const r = parseNumber(body, i);
if (r === null) break;
out.push(r.value);
i = r.next;
}
return out;
}
type Msg = { readonly kind: "num"; readonly value: number } | { readonly kind: "stop" };
function prefixTotal(msgs: readonly Msg[]): number {
let sum = 0;
for (const msg of msgs) {
if (msg.kind !== "num") break;
sum += msg.value;
}
return sum;
}
function numTotal(msgs: readonly Msg[]): number {
let sum = 0;
for (const msg of msgs) {
if (msg.kind !== "num") continue;
sum += msg.value;
}
return sum;
}
console.log(collect([5, 6, 7]).join(","));
const msgs: Msg[] = [{ kind: "num", value: 2 }, { kind: "stop" }, { kind: "num", value: 5 }];
console.log(prefixTotal(msgs));
console.log(numTotal(msgs));