fix(javascript): retain array-pattern iterator close effects (#12534)

* fix(javascript): retain array-pattern iterator close effects

* fix(javascript): bound array-pattern close exposure

* fix(javascript): follow stored array elements at iterator close

* fix(javascript): retain iterator closes before array rest
This commit is contained in:
morluto authored and GitHub committed 2026-11-19 21:57:50 +00:00
1 parent 756f772430
commit 8ef5a34ff5
9 files changed
+601 -49

No files matched your search

@@ -38,6 +38,7 @@ import {
evaluateSemanticExpression,
evaluateSemanticExpressionProvenance,
} from "./javascriptSemanticValues.js";
import { bindingHasMemberWrites } from "./javascriptSemanticEffectLog.js";
import { semanticContainer } from "./javascriptSemanticSlots.js";
import {
semanticProjectedSourceFactsAtRead,
@@ -70,6 +71,7 @@ import {
semanticMutationInitializers,
semanticBindingAtRead,
semanticCaptureOrderingIdentity,
semanticArrayPatternCloseScope,
semanticScopeExitEffect,
} from "./javascriptSemanticMutationInitializers.js";
import {
@@ -79,6 +81,7 @@ import {
type JavaScriptExecution,
} from "./javascriptSemanticExecutionPoints.js";
import {
semanticArrayPatternCloseSelection,
semanticKnownArrayIteration,
semanticReferenceSourcesInactive,
semanticSelectedReferenceSlot,
@@ -872,6 +875,7 @@ export function* collectSemanticMemberMutationsSteps(
reference.memberReadAt?.end,
capturedSelectionKey(reference.capturedSelections),
reference.iterationOnly,
reference.arrayPattern?.start,
reference.widenedGetterResult === true,
reference.definiteReceiver === true,
reference.assignedSources?.[0]?.target?.start,
@@ -932,6 +936,7 @@ export function* collectSemanticMemberMutationsSteps(
readonly value: JoinedProjection;
};
};
const patternClose = semanticArrayPatternCloseScope(mutation);
const pending: PendingValue[] = [];
const schedule = (value: PendingValue): void => {
work.charge();
@@ -1046,7 +1051,8 @@ export function* collectSemanticMemberMutationsSteps(
bindings,
originAt,
definiteReceiver,
memberReadAt: selection?.memberReadAt ?? mutation ?? originAt,
memberReadAt:
selection?.memberReadAt ?? patternClose ?? mutation ?? originAt,
capturedSelections: selection?.capturedSelections,
...(ownerInitializer === undefined ? {} : { ownerInitializer }),
});
@@ -1369,7 +1375,7 @@ export function* collectSemanticMemberMutationsSteps(
...origins.initializers,
...origins.referenceInitializers,
];
const receiver =
const storedReceiverPath =
effect === "write" &&
current.path.length > 1 &&
binding.memberWrites.some(
@@ -1377,16 +1383,24 @@ export function* collectSemanticMemberMutationsSteps(
write.assignedSources !== undefined &&
write.path.length < current.path.length,
)
? semanticProjectedSourceFactsAtRead(
? current.path.slice(0, -1)
: patternClose !== undefined &&
current.path.length > 0 &&
bindingHasMemberWrites(binding)
? current.path
: undefined;
const receiver =
storedReceiverPath === undefined
? undefined
: semanticProjectedSourceFactsAtRead(
binding,
current.node,
current.path.slice(0, -1),
storedReceiverPath,
state,
false,
current.memberReadAt ?? current.originAt ?? current.node,
current.capturedSelections,
)
: undefined;
);
// Stored receiver projections preserve their raw capture authority.
// Initializer origins retain the normalized mutation traversal below.
retainsInitializers = receiver?.retainsOrigin !== false;
@@ -1420,15 +1434,21 @@ export function* collectSemanticMemberMutationsSteps(
for (const source of receiver.storedSources)
enqueue({
node: source.node,
path: [...source.path, current.path.at(-1)!],
path:
patternClose === undefined
? [...source.path, current.path.at(-1)!]
: source.path,
bindings: nested,
definiteReceiver: false,
originAt: source.node,
memberReadAt: current.memberReadAt,
memberReadAt:
patternClose === undefined
? current.memberReadAt
: source.node,
capturedSelections: semanticNormalizeCapturedSelections([
...(semanticShiftCapturedSelections(
current.capturedSelections,
source.path.length - (current.path.length - 1),
source.path.length - storedReceiverPath!.length,
) ?? []),
...(source.capturedSelections ?? []),
]),
@@ -1898,6 +1918,9 @@ export function* collectSemanticMemberMutationsSteps(
effect: "escape",
originAt: source.mutation,
iterationOnly: source.kind === "iteration",
...(source.arrayPattern === undefined
? {}
: { arrayPattern: source.arrayPattern }),
});
}
for (const source of semanticPropertyReadSources(node, parent, state)) {
@@ -2094,7 +2117,28 @@ export function* collectSemanticMemberMutationsSteps(
state,
reference.node,
);
if (knownArray && reference.iterationOnly) continue;
if (knownArray && reference.iterationOnly) {
const pattern = reference.arrayPattern;
const selection =
pattern === undefined
? undefined
: semanticArrayPatternCloseSelection(
pattern,
mutation,
slot.value,
state,
);
if (pattern !== undefined && selection !== undefined)
markValue(
node,
[...projection, selection],
reference.bindings,
"escape",
semanticScopeExitEffect(pattern, mutation),
reference.originAt,
);
continue;
}
markValue(
reference.node,
knownArray ? reference.path : reference.fallbackPath,
@@ -2143,6 +2187,7 @@ interface IterableReference extends ReferencedValue, MutationSelectionContext {
readonly mutation?: t.Node;
readonly originAt: t.Node | undefined;
readonly iterationOnly?: boolean;
readonly arrayPattern?: t.ArrayPattern;
readonly definiteReceiver?: boolean;
readonly assignedSources?: readonly SemanticIterationSource[];
readonly requiredSources?: NonNullable<