fix(javascript): bound loop capture and rest-copy expansion

This commit is contained in:
morluto committed 2026-10-24 19:36:20 +08:00
1 parent be2811e414
commit cd1878f3fe
5 files changed
+251 -45

No files matched your search

@@ -26,7 +26,7 @@ interface StatementPosition {
interface BindingReadPosition {
readonly position: StatementPosition | null;
readonly repeating: boolean;
readonly repeatingLoop: t.Loop | undefined;
}
interface ExpressionSiblings {
@@ -545,7 +545,7 @@ export const semanticDefiniteEffectPrecedesReference = (
): boolean => {
const before = directMutationPosition(effect, parents);
const after = bindingReadPosition(reference, parents);
if (before === null || after.position === null || after.repeating)
if (before === null || after.position === null || after.repeatingLoop)
return false;
if (positionPrecedes(before, after.position)) return true;
return (
@@ -701,7 +701,7 @@ const bindingReadPosition = (
path.push(current);
const parent = parents.get(current);
if (t.isLoop(parent)) {
result = { position: null, repeating: true };
result = { position: null, repeatingLoop: parent };
break;
}
if (
@@ -709,7 +709,7 @@ const bindingReadPosition = (
(t.isFunction(parent) && !semanticComputedMethodKey(parent, current)) ||
delayedInstanceFieldValue(parent, current)
) {
result = { position: null, repeating: false };
result = { position: null, repeatingLoop: undefined };
break;
}
current = parent;
@@ -736,6 +736,33 @@ const bindingReadPosition = (
return result;
};
/** Repeated member reads share unknown ordering, while retaining their source sites. */
export const semanticCaptureOrderingIdentity = (
capture: t.Node,
parents: WeakMap<t.Node, t.Node>,
): t.Node => {
// Calls can have a distinct invocation effect at their own selection site.
if (!t.isMemberExpression(capture) && !t.isOptionalMemberExpression(capture))
return capture;
const loop = bindingReadPosition(capture, parents).repeatingLoop;
if (loop === undefined) return capture;
let child: t.Node = capture;
for (
let parent = parents.get(child);
parent !== undefined && parent !== loop;
parent = parents.get(child)
)
child = parent;
// These inputs run once, before the repeating portion of the loop.
if (
(t.isForStatement(loop) && child === loop.init) ||
((t.isForInStatement(loop) || t.isForOfStatement(loop)) &&
child === loop.right)
)
return capture;
return loop;
};
/** Select a local alias at its read, before any later straight-line reassignment. */
type SemanticBindingRead = {
readonly status: "ordered" | "unresolved";
@@ -811,7 +838,7 @@ export const semanticBindingAtRead = (
? null
: initializerPosition(reference, parents, true)
: initializerPosition(initializer, parents);
const { position, repeating } = bindingReadPosition(read, parents);
const { position, repeatingLoop } = bindingReadPosition(read, parents);
// Coverage and latest-value selection are independent. A preceding write
// can cover entry even when another write's order remains unknown.
const hasDefiniteInitializer =
@@ -832,7 +859,7 @@ export const semanticBindingAtRead = (
);
});
if (
!repeating &&
!repeatingLoop &&
origin !== undefined &&
binding.initializers.length + binding.referenceInitializers.length === 1 &&
sourcePosition !== null &&
@@ -845,7 +872,7 @@ export const semanticBindingAtRead = (
hasDefiniteInitializer,
};
const uncertainOrder =
repeating ||
repeatingLoop ||
sourcePosition === null ||
position === null ||
sourcePosition.body !== position.body;