Skip to content

Commit fa4122d

Browse files
committed
fix(compiler): stop treating continue as a loop exit in signed-test loops
When a while or C-style for loop has a test that always awaits (e.g., `while (await fetch(url, { signal }))`), a continue statement re-runs the test rather than exiting the loop. The jump-target machinery was recording continue flows as exit edges, which intersected away the signed controller and produced false OCTANE_STRONG_EFFECT_DATA_FETCH diagnostics. Two changes: 1. enterJumpTarget gains a continueExits flag. When false, recordJump skips recording continue flows. While/for/do-while pass false when alwaysAwaits(node.test) is true. For-in/for-of keep the default (true) because continue goes to the iterator, not a signed test. 2. After visiting the body of a while or C-style for whose test always awaits, re-apply the test's continuation to restore the controllers the test establishes. This fixes joinFlow(before, afterBody) losing the signed controller when the body contains an unsigned await.
1 parent 12285b0 commit fa4122d

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3927,9 +3927,9 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
39273927
}
39283928

39293929
// Only paths that reach the next statement share its flow.
3930-
function enterJumpTarget(loop) {
3930+
function enterJumpTarget(loop, continueExits = true) {
39313931
if (currentEffect === null) return null;
3932-
jumpTargets = { loop, flows: [], next: jumpTargets };
3932+
jumpTargets = { loop, continueExits, flows: [], next: jumpTargets };
39333933
return jumpTargets;
39343934
}
39353935

@@ -3949,6 +3949,10 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
39493949
const flow = effectPolicy.saveFlow();
39503950
for (let target = jumpTargets; target !== null; target = target.next) {
39513951
if (node.type === 'ContinueStatement' && !target.loop) continue;
3952+
if (node.type === 'ContinueStatement' && !target.continueExits) {
3953+
if (node.label == null) return;
3954+
continue;
3955+
}
39523956
target.flows.push(flow);
39533957
// A labeled jump may leave any enclosing target.
39543958
if (node.label == null) return;
@@ -5389,14 +5393,19 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
53895393
}
53905394
const before = currentEffect === null ? undefined : effectPolicy.saveFlow();
53915395
// A test that always yields runs right before every iteration.
5392-
if (before !== undefined && !alwaysAwaits(node.test)) {
5396+
const testAlwaysAwaits = before !== undefined && alwaysAwaits(node.test);
5397+
if (before !== undefined && !testAlwaysAwaits) {
53935398
effectPolicy.enterLoopBody([node.body, node.update]);
53945399
}
5395-
const forTarget = enterJumpTarget(true);
5400+
const forTarget = enterJumpTarget(true, !testAlwaysAwaits);
53965401
visit(node.body, loop, executionPhase);
53975402
visit(node.update, loop, executionPhase);
53985403
// The body may not run.
53995404
if (before !== undefined) {
5405+
if (testAlwaysAwaits) {
5406+
const testNode = unwrap(node.test);
5407+
if (testNode?.type === 'AwaitExpression') effectPolicy.continuation(testNode.argument);
5408+
}
54005409
effectPolicy.restoreFlow(
54015410
exitJumpTarget(forTarget, effectPolicy.joinFlow(before, effectPolicy.saveFlow())),
54025411
);
@@ -5500,9 +5509,14 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
55005509
: phase;
55015510
const before = currentEffect === null ? undefined : effectPolicy.saveFlow();
55025511
if (before !== undefined && !alwaysAwaits(node.test)) effectPolicy.enterLoopBody(node.body);
5503-
const whileTarget = enterJumpTarget(true);
5512+
const testAlwaysAwaits = before !== undefined && alwaysAwaits(node.test);
5513+
const whileTarget = enterJumpTarget(true, !testAlwaysAwaits);
55045514
visit(node.body, scope, executionPhase);
55055515
if (before !== undefined) {
5516+
if (testAlwaysAwaits) {
5517+
const testNode = unwrap(node.test);
5518+
if (testNode?.type === 'AwaitExpression') effectPolicy.continuation(testNode.argument);
5519+
}
55065520
effectPolicy.restoreFlow(
55075521
exitJumpTarget(whileTarget, effectPolicy.joinFlow(before, effectPolicy.saveFlow())),
55085522
);
@@ -5511,7 +5525,7 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
55115525
}
55125526
case 'DoWhileStatement': {
55135527
if (currentEffect !== null) effectPolicy.enterLoopBody([node.body, node.test]);
5514-
const doTarget = enterJumpTarget(true);
5528+
const doTarget = enterJumpTarget(true, !alwaysAwaits(node.test));
55155529
visit(node.body, scope, phase);
55165530
const executionPhase =
55175531
currentFunctionIsAsync &&

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,14 @@ describe('Strong asynchronous effect updates', () => {
298298
`await api.warm(); try { await fetch('/a', { signal }); } catch { return; } finally { api.log(); } setData(1);`,
299299
],
300300
['a for test', `for (; await fetch('/a', { signal }); ) { setData(1); }`],
301+
[
302+
'a while test with continue after an unsigned request',
303+
`while (await fetch('/a', { signal })) { await api.get(props.id); if (props.skip) continue; } setData(1);`,
304+
],
305+
[
306+
'a for test with continue after an unsigned request',
307+
`for (; await fetch('/a', { signal }); ) { await api.get(props.id); if (props.skip) continue; } setData(1);`,
308+
],
301309
[
302310
'a switch case that breaks inside a branch after a signed request',
303311
`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)