fix(javascript): retain proven property presence at reads (#12477)

* fix(javascript): retain proven property presence at reads

The read-time IR and projection boundary builds on the destructuring work in #7716. This change adds an indexed own-slot proof and uses it for ordinary member reads as well.

Co-authored-by: SII-YDD <38822679+SII-YDD@users.noreply.github.com>

* fix(javascript): retain destructuring proof in read order

---------

Co-authored-by: SII-YDD <38822679+SII-YDD@users.noreply.github.com>
This commit is contained in:
morlutoandSII-YDD authored and GitHub committed 2026-11-19 19:17:32 +00:00
1 parent 833916bb4a
commit 1dc22f18cd
8 files changed
+322 -42

No files matched your search

@@ -18,6 +18,7 @@ import {
} from "./javascriptSemanticState.js";
import { traverseJavaScriptValueAst } from "./javascriptSemanticTraversal.js";
import { range } from "./javascriptStaticAnalysisHelpers.js";
import { createSemanticReadPresenceQuery } from "./javascriptSemanticValues.js";
/** Recover bounded static property reads, writes, spreads, and destructuring. */
export const collectJavaScriptSemanticObjects = (
@@ -32,6 +33,7 @@ export const collectJavaScriptSemanticObjects = (
state,
output,
writeTargets: new Map(),
readPresence: createSemanticReadPresenceQuery(state),
};
traverseJavaScriptValueAst(program, state.erasedSyntaxNodes, {
enter: (node, parent) => {
@@ -65,24 +67,12 @@ export const collectJavaScriptSemanticObjects = (
t.isVariableDeclarator(node) &&
(t.isObjectPattern(node.id) || t.isArrayPattern(node.id))
)
collectDestructuring(
node.id,
node.init,
ownerCallableId,
state,
output,
);
collectDestructuring(node.id, node.init, ownerCallableId, context);
else if (
t.isAssignmentExpression(node, { operator: "=" }) &&
(t.isObjectPattern(node.left) || t.isArrayPattern(node.left))
)
collectDestructuring(
node.left,
node.right,
ownerCallableId,
state,
output,
);
collectDestructuring(node.left, node.right, ownerCallableId, context);
},
exit: (node) => callableStack.exit(node),
});
@@ -93,6 +83,7 @@ interface ObjectCollectionContext {
readonly state: JavaScriptSemanticAnalysisState;
readonly output: JavaScriptSemanticObjectOperation[];
readonly writeTargets: Map<t.Node, t.Node>;
readonly readPresence: ReturnType<typeof createSemanticReadPresenceQuery>;
}
const collectMember = (
@@ -132,6 +123,11 @@ const collectMember = (
{
node,
kind: write ? "write" : "read",
provenOwnSlot:
!write &&
identity.path !== null &&
context.readPresence(identity.bindingId, node)?.(identity.path) ===
true,
ownerCallableId,
objectBindingId: identity.bindingId,
targetBindingId: null,
@@ -146,32 +142,64 @@ const collectDestructuring = (
root: t.Node,
initializer: t.Node | null | undefined,
ownerCallableId: string | null,
state: JavaScriptSemanticAnalysisState,
output: JavaScriptSemanticObjectOperation[],
{ state, output, readPresence }: ObjectCollectionContext,
): void => {
const source = expressionObjectIdentity(initializer, state);
const pending: {
let ownSlot =
initializer == null || source.path === null
? undefined
: readPresence(source.bindingId, initializer);
// An unsupported property still occupies its place in the pattern's
// evaluation order, even when it cannot produce a graph operation.
const pending: ({
readonly pattern: t.Node;
readonly path: readonly string[];
}[] = [{ pattern: root, path: [] }];
readonly lookup?: "own" | "unproven";
} | null)[] = [{ pattern: root, path: [] }];
while (pending.length > 0) {
const next = pending.pop();
if (next === undefined) break;
if (next === null) {
ownSlot = undefined;
continue;
}
const { path } = next;
const propertyPath =
source.path === null ? null : [...source.path, ...path];
if (
next.lookup === "unproven" ||
(next.lookup === "own" &&
ownSlot !== undefined &&
propertyPath !== null &&
!ownSlot(propertyPath))
)
ownSlot = undefined;
const occurrence = unwrapJavaScriptExpression(next.pattern).node;
if (t.isAssignmentPattern(occurrence)) ownSlot = undefined;
const pattern = t.isAssignmentPattern(occurrence)
? unwrapJavaScriptExpression(occurrence.left).node
: occurrence;
if (t.isObjectPattern(pattern)) {
for (const property of pattern.properties.toReversed()) {
if (!t.isObjectProperty(property)) continue;
if (!t.isObjectProperty(property)) {
pending.push(null);
continue;
}
const name = semanticStaticPropertyKey(property.key, property.computed);
if (name !== null)
pending.push({ pattern: property.value, path: [...path, name] });
if (name === null) {
pending.push(null);
continue;
}
pending.push({
pattern: property.value,
path: [...path, name],
lookup: property.computed ? "unproven" : "own",
});
}
continue;
}
if (t.isArrayPattern(pattern)) {
ownSlot = undefined;
for (let index = pattern.elements.length - 1; index >= 0; index -= 1) {
const element = pattern.elements[index];
if (
@@ -183,17 +211,25 @@ const collectDestructuring = (
}
continue;
}
if (!t.isIdentifier(pattern)) continue;
if (!t.isIdentifier(pattern)) {
ownSlot = undefined;
continue;
}
const targetBinding = resolveSemanticBindingState(
state,
pattern,
pattern.name,
);
if (targetBinding === undefined) ownSlot = undefined;
addObjectOperation(
{
node: occurrence,
kind: "destructure",
ownerCallableId,
objectBindingId: source.bindingId,
targetBindingId:
resolveSemanticBindingState(state, pattern, pattern.name)
?.bindingId ?? null,
propertyPath: source.path === null ? null : [...source.path, ...path],
targetBindingId: targetBinding?.bindingId ?? null,
propertyPath,
provenOwnSlot: ownSlot !== undefined,
},
state,
output,
@@ -208,6 +244,7 @@ interface AddObjectOperationInput {
readonly objectBindingId: string | null;
readonly targetBindingId: string | null;
readonly propertyPath: readonly string[] | null;
readonly provenOwnSlot?: boolean;
}
const addObjectOperation = (
@@ -224,6 +261,9 @@ const addObjectOperation = (
String(input.node.end ?? -1),
].join(":"),
kind: input.kind,
...(input.provenOwnSlot === true
? { sourcePresence: "present" as const }
: {}),
location: range(input.node),
ownerCallableId: input.ownerCallableId,
objectBindingId: input.objectBindingId,