fix(javascript): acquire nested iterators from stored receivers (#12542)

* fix(javascript): acquire nested iterators from stored receivers

* fix(javascript): preserve stored iterator branches and pattern timing
This commit is contained in:
morluto authored and GitHub committed 2026-11-19 22:22:46 +00:00
1 parent 4892615ecb
commit f5573a0064
8 files changed
+490 -65

No files matched your search

@@ -37,10 +37,15 @@ import {
evaluateSemanticBinding,
evaluateSemanticExpression,
evaluateSemanticExpressionProvenance,
semanticSelectedBranchNodesAt,
} from "./javascriptSemanticValues.js";
import { bindingHasMemberWrites } from "./javascriptSemanticEffectLog.js";
import {
bindingHasMemberWrites,
bindingProjectedContentsMayChangeAt,
} from "./javascriptSemanticEffectLog.js";
import { semanticContainer } from "./javascriptSemanticSlots.js";
import {
hasFreshProjectedReceiver,
semanticProjectedSourceFactsAtRead,
semanticCapturedSelectionsAfterPropertyRead,
semanticShiftCapturedSelections,
@@ -72,6 +77,7 @@ import {
semanticBindingAtRead,
semanticCaptureOrderingIdentity,
semanticArrayPatternCloseScope,
semanticArrayPatternAcquisitionEffect,
semanticScopeExitEffect,
} from "./javascriptSemanticMutationInitializers.js";
import {
@@ -105,6 +111,7 @@ import {
} from "./javascriptSemanticTraversal.js";
import {
semanticStaticPropertyKey,
semanticMemberChain,
semanticWriteTargets,
unwrapJavaScriptExpression,
} from "./javascriptAstValues.js";
@@ -2008,6 +2015,104 @@ export function* collectSemanticMemberMutationsSteps(
// A value thrown out of its function or the program reaches handlers the
// throw-to-catch index does not link to it.
for (const node of state.escapingThrows) markEscaped(node.argument, node);
const markAcquiredNestedIterator = (
reference: IterableReference,
): boolean => {
const { arrayPattern: pattern, mutation, node, projection } = reference;
if (pattern === undefined || projection.length === 0) return false;
const receiver = semanticMemberChain(node);
if (receiver === undefined || !t.isIdentifier(receiver.root)) return false;
const binding = resolveBinding(receiver.root, receiver.root.name);
if (binding === undefined || !bindingHasMemberWrites(binding)) return false;
const path = [...receiver.members, ...projection];
// The RHS captures the parent, while the nested pattern selects the
// iterable later, after any preceding defaults or assignments.
const receiverCaptures = semanticNormalizeCapturedSelections([
...(semanticShiftCapturedSelections(
reference.capturedSelections,
receiver.members.length,
) ?? []),
...(receiver.members.length === 0
? []
: [{ at: node, depth: receiver.members.length }]),
]);
const captured = semanticProjectedSourceFactsAtRead(
binding,
receiver.root,
path,
state,
true,
pattern,
receiverCaptures,
);
// A later-only write can invalidate the merged lattice without changing
// this acquisition. Leave the original capture on its established route.
if (captured === undefined || captured.storedSources.length === 0)
return false;
const contentsMayChange = bindingProjectedContentsMayChangeAt(
state,
binding,
path,
pattern,
);
for (const source of captured.sources) {
const pending = [source.node];
while (pending.length > 0) {
work.charge();
const alternative = unwrapJavaScriptExpression(pending.pop()!).node;
if (
t.isConditionalExpression(alternative) ||
t.isLogicalExpression(alternative)
) {
for (const branch of semanticSelectedBranchNodesAt(
alternative,
state,
source.node,
))
pending.push(branch);
continue;
}
const value = semanticSelectedReferenceSlot(
{ node: alternative, projection: source.path },
state,
source.node,
)?.value;
const native =
!contentsMayChange &&
semanticKnownArrayIteration(value, state, pattern) &&
hasFreshProjectedReceiver(alternative, source.path, state);
const selection = native
? semanticArrayPatternCloseSelection(pattern, mutation, value, state)
: undefined;
if (!native || selection !== undefined)
markValue(
alternative,
selection === undefined ? source.path : [...source.path, selection],
reference.bindings,
"escape",
native
? semanticScopeExitEffect(pattern, mutation)
: semanticArrayPatternAcquisitionEffect(pattern, mutation),
alternative,
undefined,
false,
undefined,
false,
{
memberReadAt: source.node,
capturedSelections: semanticNormalizeCapturedSelections([
...(semanticShiftCapturedSelections(
reference.capturedSelections,
source.path.length - projection.length,
) ?? []),
...(source.capturedSelections ?? []),
]),
},
);
}
}
return true;
};
// A later effect can make a previously defined destructuring source uncertain.
// Reconsider defaults and iterable origins until their effects are stable.
while (
@@ -2110,6 +2215,10 @@ export function* collectSemanticMemberMutationsSteps(
state,
capturesRootPattern ? node : undefined,
);
// A slot's value lattice loses identities invalidated by writes. Recover
// stored alternatives at the nested receiver's actual acquisition.
if (slot?.presence !== "present" && markAcquiredNestedIterator(reference))
continue;
// Custom iteration can yield the iterable itself as well as any child.
// Treat its root as escaped, including writes through yielded references.
const knownArray = semanticKnownArrayIteration(