Skip to content

Commit 9d9bb07

Browse files
committed
fix(compiler): separate continue flows from break flows in Strong abort proofs
A `continue` in a classic `for` loop still executes the update expression, and in a `do-while` loop it still evaluates the test, before the loop can exit. The jump tracking recorded both `break` and `continue` in the same flow list, then intersected them at the loop exit, stripping abort proofs that the update or test would have provided on the continue path. Separate unlabeled `continue` flows into their own list and merge them into the current flow before visiting the update (`for`) or test (`do-while`), so those expressions' proofs apply to the continue path. For `while` and `for-of/in`, the merge happens right before the exit join, preserving the existing behavior (intersection is associative).
1 parent dba4555 commit 9d9bb07

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎packages/octane/src/compiler/strong-mode.js‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3925,7 +3925,7 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
39253925
// Only paths that reach the next statement share its flow.
39263926
function enterJumpTarget(loop) {
39273927
if (currentEffect === null) return null;
3928-
jumpTargets = { loop, flows: [], next: jumpTargets };
3928+
jumpTargets = { loop, flows: [], continueFlows: null, next: jumpTargets };
39293929
return jumpTargets;
39303930
}
39313931

@@ -3940,11 +3940,25 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
39403940
: target.flows.reduce(effectPolicy.joinFlow, flow);
39413941
}
39423942

3943+
// A `continue` in a `for` still runs the update, and in a `do-while` it
3944+
// still runs the test. Merge continue flows into the current flow before
3945+
// visiting that post-body code so its proofs apply to the continue path.
3946+
function flushContinueFlows(target) {
3947+
if (target === null || target.continueFlows === null) return;
3948+
effectPolicy.restoreFlow(
3949+
target.continueFlows.reduce(effectPolicy.joinFlow, effectPolicy.saveFlow()),
3950+
);
3951+
}
3952+
39433953
function recordJump(node) {
39443954
if (currentEffect === null) return;
39453955
const flow = effectPolicy.saveFlow();
39463956
for (let target = jumpTargets; target !== null; target = target.next) {
39473957
if (node.type === 'ContinueStatement' && !target.loop) continue;
3958+
if (node.type === 'ContinueStatement' && node.label == null) {
3959+
(target.continueFlows ??= []).push(flow);
3960+
return;
3961+
}
39483962
target.flows.push(flow);
39493963
// A labeled jump may leave any enclosing target.
39503964
if (node.label == null) return;
@@ -5383,6 +5397,7 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
53835397
}
53845398
const forTarget = enterJumpTarget(true);
53855399
visit(node.body, loop, executionPhase);
5400+
flushContinueFlows(forTarget);
53865401
visit(node.update, loop, executionPhase);
53875402
// The body may not run.
53885403
if (before !== undefined) {
@@ -5477,6 +5492,7 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
54775492
} else {
54785493
effectPolicy.restoreFlow(effectPolicy.joinFlow(before, effectPolicy.saveFlow()));
54795494
}
5495+
flushContinueFlows(iterationTarget);
54805496
effectPolicy.restoreFlow(exitJumpTarget(iterationTarget, effectPolicy.saveFlow()));
54815497
}
54825498
return;
@@ -5492,6 +5508,7 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
54925508
const whileTarget = enterJumpTarget(true);
54935509
visit(node.body, scope, executionPhase);
54945510
if (before !== undefined) {
5511+
flushContinueFlows(whileTarget);
54955512
effectPolicy.restoreFlow(
54965513
exitJumpTarget(whileTarget, effectPolicy.joinFlow(before, effectPolicy.saveFlow())),
54975514
);
@@ -5502,6 +5519,7 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
55025519
if (currentEffect !== null) effectPolicy.enterLoopBody([node.body, node.test]);
55035520
const doTarget = enterJumpTarget(true);
55045521
visit(node.body, scope, phase);
5522+
flushContinueFlows(doTarget);
55055523
const executionPhase =
55065524
currentFunctionIsAsync &&
55075525
phase !== 'deferred' &&

‎packages/octane/tests/compiler/strong-effects.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,10 @@ describe('Strong asynchronous effect updates', () => {
297297
`await api.warm(); try { await fetch('/a', { signal }); } catch { return; } finally { api.log(); } setData(1);`,
298298
],
299299
['a for test', `for (; await fetch('/a', { signal }); ) { setData(1); }`],
300+
[
301+
'a do-while test after continue',
302+
`await api.warm(); do { if (props.skip) continue; } while (await fetch('/a', { signal })); setData(1);`,
303+
],
300304
[
301305
'a switch case that breaks inside a branch after a signed request',
302306
`switch (props.mode) { case 'a': await fetch('/a', { signal }); if (props.fast) break; await fetch('/b', { signal }); break; default: await fetch('/c', { signal }); } setData(1);`,

0 commit comments

Comments
 (0)