Files
scriptc/tests/corpus/2394-loop-narrow-containment.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

29 lines
983 B
TypeScript

// Narrowing established by an in-loop break guard ends with the loop body — post-loop and post-branch reads must re-test the optional.
interface Sel { readonly value: number; }
interface Model { readonly sel: Sel | null; readonly count: number; }
function drain(model: Model): number {
let total = 0;
let i = 0;
while (i < model.count) {
if (model.sel === null) break;
total += model.sel.value;
i += 1;
}
return model.sel === null ? total : total + model.sel.value;
}
function pick(flag: boolean, model: Model): number {
let total = 0;
for (let i = 0; i < model.count; i += 1) {
if (flag) {
if (model.sel === null) break;
total += model.sel.value;
}
total += model.sel === null ? 0 : model.sel.value;
}
return total;
}
console.log(drain({ sel: { value: 4 }, count: 3 }));
console.log(drain({ sel: null, count: 3 }));
console.log(pick(true, { sel: { value: 2 }, count: 2 }));
console.log(pick(false, { sel: null, count: 2 }));