From c751013621e822c0eb2f68a1ca4ae5246825b169 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:33:39 -0500 Subject: [PATCH] fix(javascript): preserve read-time evidence for nested native arrays (#12551) --- ...vaScriptSemanticGraphDestructuring.test.ts | 128 ++++++++++++++++++ .../javascript/javascriptSemanticObjects.ts | 26 +++- .../javascriptSemanticReadPresence.ts | 80 +++++++---- .../javascriptDestructuringEvidence.test.ts | 5 + 4 files changed, 205 insertions(+), 34 deletions(-) diff --git a/src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts b/src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts index 07e19dcdcf..63f23e08b8 100644 --- a/src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts +++ b/src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts @@ -1650,3 +1650,131 @@ it.each([ ).toEqual(["parent", "next", "mode"]); }, ); + +it.each([ + [ + "direct nested array", + 'const box={items:["one"]};const {items:[selected]}=box;box.items[0]="later";', + ["items", "0"], + "resolved", + ], + [ + "nested array through a const alias", + 'const box={items:["one"]};const alias=box;const {items:[selected]}=alias;box.items[0]="later";', + ["items", "0"], + "resolved", + ], + [ + "nested array assignment", + 'const box={items:["one"]};let selected;({items:[selected]}=box);box.items[0]="later";', + ["items", "0"], + "resolved", + ], + [ + "nested object prefix", + 'const box={prefix:{items:["one"]}};const {prefix:{items:[selected]}}=box;box.prefix.items[0]="later";', + ["prefix", "items", "0"], + "resolved", + ], + [ + "projected object RHS", + 'const box={prefix:{items:["one"]}};const {items:[selected]}=box.prefix;box.prefix.items[0]="later";', + ["prefix", "items", "0"], + "resolved", + ], + [ + "earlier own read", + 'const box={label:0,items:["one"]};const {label,items:[selected]}=box;box.items[0]="later";', + ["items", "0"], + "resolved", + ], + [ + "native nested elision", + 'const box={items:[0,"one"]};const {items:[,selected]}=box;box.items[1]="later";', + ["items", "1"], + "resolved", + ], + [ + "earlier write through alias", + 'const box={items:["one"]};const alias=box;alias.items[0]="earlier";const {items:[selected]}=box;', + ["items", "0"], + "candidate", + ], + [ + "earlier default", + 'const box={items:["one"]};const {missing=(box.items[0]="earlier"),items:[selected]}=box;', + ["items", "0"], + "candidate", + ], + [ + "earlier getter", + 'const box={items:["one"],get label(){box.items[0]="earlier";return 0;}};const {label,items:[selected]}=box;', + ["items", "0"], + "candidate", + ], + [ + "inherited array receiver", + 'const box={__proto__:{items:["one"]}};const {items:[selected]}=box;', + ["items", "0"], + "candidate", + ], + [ + "aliased array receiver", + 'const items=["one"];const box={items};const {items:[selected]}=box;items[0]="later";', + ["items", "0"], + "candidate", + ], + [ + "custom own iterator", + 'const box={items:["one"]};box.items[Symbol.iterator]=function*(){yield "other";};const {items:[selected]}=box;', + ["items", "0"], + "candidate", + ], + [ + "shared next changed before capture", + 'const box={items:["one"]};Object.getPrototypeOf([].values()).next=()=>({value:"other",done:false});const {items:[selected]}=box;box.items[0]="later";', + ["items", "0"], + "candidate", + ], + [ + "shared next changed after capture", + 'const box={items:["one"]};const {items:[selected]}=box;Object.getPrototypeOf([].values()).next=()=>({value:"other",done:false});box.items[0]="later";', + ["items", "0"], + "resolved", + ], + [ + "earlier iterator close reaching this array", + 'const box={first:[0],items:["one"]};box.first[1]=box.items;Object.getPrototypeOf([].values()).return=function(){const value=this.next().value;if(value)delete value[0];return {};};const {first:[used],items:[selected]}=box;', + ["items", "0"], + "candidate", + ], + [ + "earlier member target in the iterator", + 'const box={items:["one","two"]};const target={set value(_){box.items[1]="earlier";}};let selected;({items:[target.value,selected]}=box);', + ["items", "1"], + "candidate", + ], + [ + "second nested iterator comes after first close", + 'const box={first:[0],items:["one"]};const {first:[used],items:[selected]}=box;box.items[0]="later";', + ["items", "0"], + "candidate", + ], +] as const)( + "owns a single native iterator below an object for %s", + (_name, code, path, resolution) => { + const graph = graphForJavaScript(code); + const selected = graph.nodes.find( + (n) => n.kind === "binding" && n.label === "selected", + ); + const relation = graph.relations.find( + (r) => + r.relation === "destructures" && r.target_node_id === selected?.node_id, + ); + expect(relation).toMatchObject({ resolution }); + expect( + graph.nodes.find((n) => n.node_id === relation?.source_node_id) + ?.properties.property_path, + ).toEqual(path); + }, +); diff --git a/src/domain/javascript/javascriptSemanticObjects.ts b/src/domain/javascript/javascriptSemanticObjects.ts index 60ee58cc51..09f1923c3a 100644 --- a/src/domain/javascript/javascriptSemanticObjects.ts +++ b/src/domain/javascript/javascriptSemanticObjects.ts @@ -126,7 +126,7 @@ const collectMember = ( provenOwnSlot: !write && identity.path !== null && - context.readPresence(identity.bindingId, node)?.(identity.path) !== + context.readPresence(identity.bindingId, node)?.read(identity.path) !== undefined, ownerCallableId, objectBindingId: identity.bindingId, @@ -154,7 +154,7 @@ const collectDestructuring = ( rootArray ? source.path : undefined, ) : undefined; - let ownSlot = sourcePresence; + let ownSlot = sourcePresence?.read; // An unsupported property still occupies its place in the pattern's // evaluation order, even when it cannot produce a graph operation. const pending: ({ @@ -194,7 +194,11 @@ const collectDestructuring = ( : occurrence; if (t.isObjectPattern(pattern)) { // A nested pattern needs a proved object even if it has no children. - if (rootArray && !t.isObjectExpression(selected)) ownSlot = undefined; + if ( + (rootArray && !t.isObjectExpression(selected)) || + (!rootArray && ownSlot !== sourcePresence?.read) + ) + ownSlot = undefined; for (const property of pattern.properties.toReversed()) { if (!t.isObjectProperty(property)) { pending.push(null); @@ -215,7 +219,19 @@ const collectDestructuring = ( continue; } if (t.isArrayPattern(pattern)) { - if (pattern !== root) ownSlot = undefined; + if (pattern !== root) { + ownSlot = + !rootArray && + ownSlot !== undefined && + ownSlot === sourcePresence?.read && + t.isArrayExpression(selected) && + propertyPath !== null + ? sourcePresence?.readNestedArray?.(propertyPath, path, pattern) + : undefined; + // IteratorClose can run after these elements and before the next + // sibling. Reaching this boundary ends the current presence proof. + if (ownSlot !== undefined) pending.push(null); + } for (let index = pattern.elements.length - 1; index >= 0; index -= 1) { const element = pattern.elements[index]; if (element === undefined || t.isRestElement(element)) @@ -252,7 +268,7 @@ const collectDestructuring = ( (plainObjectPath && targetBinding !== undefined && propertyPath !== null && - sourcePresence?.(propertyPath, occurrence) !== undefined), + sourcePresence?.read(propertyPath, occurrence) !== undefined), }, state, output, diff --git a/src/domain/javascript/javascriptSemanticReadPresence.ts b/src/domain/javascript/javascriptSemanticReadPresence.ts index c1f5898824..d88325ca2d 100644 --- a/src/domain/javascript/javascriptSemanticReadPresence.ts +++ b/src/domain/javascript/javascriptSemanticReadPresence.ts @@ -12,6 +12,7 @@ import { import { semanticBindingPrecedesCapture } from "./javascriptSemanticMutationInitializers.js"; import { semanticArrayIndex } from "./javascriptSemanticPropertyPaths.js"; import { semanticKnownArrayIteration } from "./javascriptSemanticReferenceSources.js"; +import { semanticSlotAtPath } from "./javascriptSemanticSlots.js"; import { evaluateSemanticExpression } from "./javascriptSemanticValues.js"; import { resolveSemanticBindingState, @@ -25,6 +26,20 @@ interface ReadPresenceOrigin { readonly value: t.Node; } +type ReadOwnData = ( + path: readonly string[], + beforeIterator?: t.Node, +) => t.Node | undefined; + +interface ReadPresence { + readonly read: ReadOwnData; + readonly readNestedArray?: ( + path: readonly string[], + fromRhs: readonly string[], + acquisition: t.ArrayPattern, + ) => ReadOwnData | undefined; +} + /** Prove a direct data slot in its source at the time of the read. */ export const createSemanticReadPresenceQuery = ( state: JavaScriptSemanticAnalysisState, @@ -104,7 +119,7 @@ export const createSemanticReadPresenceQuery = ( bindingId: string | null, capture: t.Node, iterationPath?: readonly string[], - ) => { + ): ReadPresence | undefined => { const binding = bindingId === null ? undefined : state.bindingsById.get(bindingId); const initializer = binding?.initializers[0]; @@ -135,37 +150,30 @@ export const createSemanticReadPresenceQuery = ( ) return undefined; const { value } = origin; - // An identifier RHS in an object pattern can select numeric own data - // without starting an iterator, even when a later read closes one. - const arrayPath = - iterationPath ?? - (t.isArrayExpression(value) && - t.isIdentifier(unwrapJavaScriptExpression(capture).node) - ? [] - : undefined); - if (arrayPath !== undefined) { - // The RHS must select an own data value before numeric array slots can - // explain the binding. Temporal checks still use the full origin path. + const arraySlots = ( + arrayPath: readonly string[], + fromRhs: readonly string[], + acquisition: t.Node | undefined, + ): ReadOwnData | undefined => { const literal = ownDataAtPath(value, arrayPath); if (!t.isArrayExpression(literal)) return undefined; - const array = evaluateSemanticExpression( - capture, - state, - capture, - "query", - ); + const base = evaluateSemanticExpression(capture, state, capture, "query"); + const selectedArray = semanticSlotAtPath(base, fromRhs); + const array = selectedArray.value; if ( + selectedArray.presence !== "present" || array.status !== "array" || array.unknownItems || - (iterationPath !== undefined && - !semanticKnownArrayIteration(array, state, capture)) + (acquisition !== undefined && + !semanticKnownArrayIteration(array, state, acquisition)) ) return undefined; - return ( - path: readonly string[], - beforeIterator?: t.Node, - ): t.Node | undefined => { - if (beforeIterator !== undefined) return undefined; + return (path, beforeIterator) => { + if ( + beforeIterator !== undefined || + (fromRhs.length > 0 && path.length !== arrayPath.length + 1) + ) + return undefined; const key = path[arrayPath.length]; if (key === undefined) return undefined; const index = semanticArrayIndex(key); @@ -193,12 +201,25 @@ export const createSemanticReadPresenceQuery = ( ? undefined : selected; }; + }; + // An identifier RHS in an object pattern can select numeric own data + // without starting an iterator, even when a later read closes one. + const arrayPath = + iterationPath ?? + (t.isArrayExpression(value) && + t.isIdentifier(unwrapJavaScriptExpression(capture).node) + ? [] + : undefined); + if (arrayPath !== undefined) { + const read = arraySlots( + arrayPath, + [], + iterationPath === undefined ? undefined : capture, + ); + return read === undefined ? undefined : { read }; } if (!t.isObjectExpression(value)) return undefined; - return ( - path: readonly string[], - beforeIterator?: t.Node, - ): t.Node | undefined => { + const read: ReadOwnData = (path, beforeIterator) => { if (path.length === 0) return undefined; const selected = ownDataAtPath(value, path); if (selected === undefined) return undefined; @@ -218,5 +239,6 @@ export const createSemanticReadPresenceQuery = ( ); return stillPresent ? selected : undefined; }; + return { read, readNestedArray: arraySlots }; }; }; diff --git a/tests/boundary/cli/javascriptDestructuringEvidence.test.ts b/tests/boundary/cli/javascriptDestructuringEvidence.test.ts index 5aed6fb692..b1f5a308c0 100644 --- a/tests/boundary/cli/javascriptDestructuringEvidence.test.ts +++ b/tests/boundary/cli/javascriptDestructuringEvidence.test.ts @@ -37,6 +37,7 @@ const expectedDependencies = [ ["elided", ["1"]], ["projected", ["items", "0"]], ["arrayObject", ["0", "mode"]], + ["nestedElement", ["items", "0"]], ["selected", ["0"], "candidate"], ["beforeClose", ["label"]], ["consumed", ["items", "0", "mode"]], @@ -89,6 +90,9 @@ cliTest( const nestedArray = [{ mode: "read-time" }]; const [{ mode: arrayObject }] = nestedArray; nestedArray[0].mode = "later"; + const nestedSource = { items: ["nested"] }; + const { items: [nestedElement] } = nestedSource; + nestedSource.items[0] = "later"; const box = { items: [] }; const iterator = box.items.values(); const prototype = Reflect.getPrototypeOf(iterator); @@ -145,6 +149,7 @@ cliTest( "elided", "projected", "arrayObject", + "nestedElement", "selected", "beforeClose", "consumed",