Skip to content

Commit 6b1473d

Browse files
committed
fix: correct three strong-effect analysis bugs
Bug 1 (AND-await treated as always waiting): For &&, the short-circuit value is falsy and never thenable, so awaitYields now returns false and maySettle returns true when the left operand is unknown. Bug 2 (Switch breaks drop unsigned paths): A break inside an if within a switch case had its flow discarded by joinBranchFlows, so only the end-of-case flow was tracked. Added a switchBreakFlows collector that joinBranchFlows pushes break-exit flows to, which the switch handler merges into exits. Bug 3 (Abort proof ignores static operands): requestControllers intersected both arms of conditional/logical expressions without checking selected(), so a literal dead arm dropped the live request's controller. Now consults selected() first.
1 parent 9ea692e commit 6b1473d

2 files changed

Lines changed: 37 additions & 13 deletions

File tree

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ export function createStrongEffectPolicy({ ast, analysis, callNames, report }) {
265265
return maySettle(node.expressions?.at(-1), depth);
266266
case 'ConditionalExpression':
267267
case 'LogicalExpression':
268+
// The && short-circuit value is falsy and never thenable.
269+
if (node.operator === '&&' && !selected(node)) return true;
268270
return someResult(node, maySettle, depth);
269271
case 'CallExpression':
270272
return settledPromise(node);
@@ -450,15 +452,19 @@ export function createStrongEffectPolicy({ ast, analysis, callNames, report }) {
450452
case 'SequenceExpression':
451453
return requestControllers(node.expressions?.at(-1), depth + 1);
452454
case 'ConditionalExpression':
453-
return sharedControllers(
454-
requestControllers(node.consequent, depth + 1),
455-
requestControllers(node.alternate, depth + 1),
456-
);
457-
case 'LogicalExpression':
458-
return sharedControllers(
459-
requestControllers(node.left, depth + 1),
460-
requestControllers(node.right, depth + 1),
461-
);
455+
case 'LogicalExpression': {
456+
const only = selected(node);
457+
if (only !== null) return requestControllers(only, depth + 1);
458+
return node.type === 'ConditionalExpression'
459+
? sharedControllers(
460+
requestControllers(node.consequent, depth + 1),
461+
requestControllers(node.alternate, depth + 1),
462+
)
463+
: sharedControllers(
464+
requestControllers(node.left, depth + 1),
465+
requestControllers(node.right, depth + 1),
466+
);
467+
}
462468
case 'Identifier': {
463469
const init = stableInitOf(node);
464470
return init === null ? null : requestControllers(init, depth + 1);

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,7 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
14361436
const stateOwners = [];
14371437
let returnCollector = null;
14381438
let renderOwner = null;
1439+
let switchBreakFlows = null;
14391440
let currentEffect = null;
14401441
let currentFunction = null;
14411442
let collectEffectReads = true;
@@ -1955,9 +1956,12 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
19551956
if (node?.type === 'ConditionalExpression' || node?.type === 'LogicalExpression') {
19561957
const only = effectPolicy.selected(node);
19571958
if (only !== null) return awaitYields(only);
1958-
return node.type === 'ConditionalExpression'
1959-
? awaitYields(node.consequent) && awaitYields(node.alternate)
1960-
: awaitYields(node.left) && awaitYields(node.right);
1959+
if (node.type === 'ConditionalExpression') {
1960+
return awaitYields(node.consequent) && awaitYields(node.alternate);
1961+
}
1962+
// The && short-circuit value is falsy and never thenable.
1963+
if (node.operator === '&&') return false;
1964+
return awaitYields(node.left) && awaitYields(node.right);
19611965
}
19621966
if (node?.type === 'SequenceExpression') return awaitYields(node.expressions?.at(-1));
19631967
return !effectPolicy.zeroDelayAwait(node);
@@ -3875,7 +3879,15 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
38753879
if (node.type === 'IfStatement') {
38763880
const consequentExits = branchAlwaysExits(node.consequent);
38773881
const alternateExits = node.alternate != null && branchAlwaysExits(node.alternate);
3878-
if (consequentExits !== alternateExits) return consequentExits ? alternate : consequent;
3882+
if (consequentExits !== alternateExits) {
3883+
if (switchBreakFlows !== null) {
3884+
const exitBranch = consequentExits ? node.consequent : node.alternate;
3885+
if (statementCompletions(exitBranch).has('break:')) {
3886+
switchBreakFlows.push(consequentExits ? consequent : alternate);
3887+
}
3888+
}
3889+
return consequentExits ? alternate : consequent;
3890+
}
38793891
}
38803892
return effectPolicy.joinFlow(consequent, alternate);
38813893
}
@@ -4768,6 +4780,8 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
47684780
// Each case starts from the switch, or also from the case falling into it.
47694781
const before = currentEffect === null ? undefined : effectPolicy.saveFlow();
47704782
const exits = [];
4783+
const savedSwitchBreakFlows = switchBreakFlows;
4784+
switchBreakFlows = before !== undefined ? [] : null;
47714785
for (const branch of branches) {
47724786
if (before !== undefined) {
47734787
effectPolicy.restoreFlow(
@@ -4805,6 +4819,10 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
48054819
exits.push(effectPolicy.saveFlow());
48064820
}
48074821
}
4822+
if (switchBreakFlows !== null) {
4823+
for (const flow of switchBreakFlows) exits.push(flow);
4824+
}
4825+
switchBreakFlows = savedSwitchBreakFlows;
48084826
if (before !== undefined) {
48094827
if (!branches.some((branch) => branch.test == null)) exits.push(before);
48104828
if (exits.length !== 0) effectPolicy.restoreFlow(exits.reduce(effectPolicy.joinFlow));

0 commit comments

Comments
 (0)