From 17ada289acefc39eaa8554925411657a0115a7f1 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 20:20:59 -0500 Subject: [PATCH] fix(javascript): retain original nested iterator acquisitions (#12645) --- ...vaScriptSemanticGraphDestructuring.test.ts | 86 +++++++++++++++++++ .../javascriptSemanticMemberMutations.ts | 20 ++++- .../javascriptDestructuringEvidence.test.ts | 2 +- 3 files changed, 104 insertions(+), 4 deletions(-) diff --git a/src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts b/src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts index d7810b58f2..ac8454007e 100644 --- a/src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts +++ b/src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts @@ -2125,6 +2125,54 @@ it.each([ ["nested", "pairs", "0", "0"], "resolved", ], + [ + "a projected array before its whole receiver is replaced", + 'const box={pairs:[["one"]]};const {0:[selected]}=box.pairs;box.pairs=[];', + ["pairs", "0", "0"], + "resolved", + ], + [ + "a projected array before a later custom receiver", + 'const box={pairs:[["one"]]};const {0:[selected]}=box.pairs;box.pairs={*[Symbol.iterator](){yield "later";}};', + ["pairs", "0", "0"], + "resolved", + ], + [ + "a deeper projected array before a later parent replacement", + 'const box={nested:{pairs:[["one"]]}};const {0:[selected]}=box.nested.pairs;box.nested={pairs:[]};', + ["nested", "pairs", "0", "0"], + "resolved", + ], + [ + "an earlier custom inner receiver before the whole array is replaced", + 'const box={pairs:[["one"]]};box.pairs[0]={*[Symbol.iterator](){yield "earlier";}};const {0:[selected]}=box.pairs;box.pairs=[];', + ["pairs", "0", "0"], + "candidate", + ], + [ + "an earlier default replacing the next own numeric receiver", + 'const box={pairs:[["one"],["two"]]};const {none=(box.pairs[1]={*[Symbol.iterator](){yield "earlier";}}),1:[selected]}=box.pairs;box.pairs=[];', + ["pairs", "1", "0"], + "candidate", + ], + [ + "a projected array before later receiver and iterator replacements", + 'const box={pairs:[["one"]]};const {0:[selected]}=box.pairs;Object.getPrototypeOf([].values()).next=()=>({done:true});box.pairs=[];', + ["pairs", "0", "0"], + "resolved", + ], + [ + "an earlier opaque escape from the projected receiver", + 'const box={pairs:[["one"]]};consume(box.pairs);const {0:[selected]}=box.pairs;box.pairs=[];', + ["pairs", "0", "0"], + "candidate", + ], + [ + "an earlier local iterator override on the selected inner array", + 'const box={pairs:[["one"]]};box.pairs[0][Symbol.iterator]=function*(){};const {0:[selected]}=box.pairs;box.pairs=[];', + ["pairs", "0", "0"], + "candidate", + ], [ "a numeric slot selected through an accessor", 'const box={get pairs(){return [["one"]];}};const {0:[selected]}=box.pairs;box.pairs[0][0]="later";', @@ -2427,3 +2475,41 @@ it.each([ ).toEqual(path); }, ); + +it("keeps a retained alias unknown after a possible close on a captured receiver", () => { + const graph = graphForJavaScript(` + const box = { pairs: [[0, { mode: "old" }]] }; + const kept = box.pairs[0][1]; + Object.getPrototypeOf([].values()).return = function () { + const value = this.next().value; + if (value) delete value.mode; + return {}; + }; + const { 0: [used] } = box.pairs; + const { mode: selected } = kept; + box.pairs = []; + `); + const target = graph.nodes.find( + (node) => node.kind === "binding" && node.label === "selected", + ); + expect(target).toBeDefined(); + const relation = graph.relations.find( + (edge) => + edge.relation === "destructures" && + edge.target_node_id === target?.node_id, + ); + expect(relation).toMatchObject({ resolution: "candidate" }); + const literalIds = new Set( + graph.nodes + .filter((node) => node.kind === "literal") + .map((node) => node.node_id), + ); + expect( + graph.relations.some( + (edge) => + edge.relation === "defines" && + edge.target_node_id === target?.node_id && + literalIds.has(edge.source_node_id), + ), + ).toBe(false); +}); diff --git a/src/domain/javascript/javascriptSemanticMemberMutations.ts b/src/domain/javascript/javascriptSemanticMemberMutations.ts index 880f522667..654e167baa 100644 --- a/src/domain/javascript/javascriptSemanticMemberMutations.ts +++ b/src/domain/javascript/javascriptSemanticMemberMutations.ts @@ -2036,9 +2036,20 @@ export function* collectSemanticMemberMutationsSteps( 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) + if (captured === undefined || captured.sources.length === 0) return false; + const originalOnly = captured.storedSources.length === 0; + // A later receiver write can hide the original slot in the merged value. + // Reuse the captured own-data source when that original receiver is proved. + if ( + originalOnly && + (receiver.members.length === 0 || + captured.sources.length !== 1 || + !semanticBindingAtRead(binding, receiver.root, state) + .hasDefiniteInitializer || + !t.isObjectExpression( + unwrapJavaScriptExpression(captured.sources[0]!.node).node, + )) + ) return false; const contentsMayChange = bindingProjectedContentsMayChangeAt( state, @@ -2075,6 +2086,9 @@ export function* collectSemanticMemberMutationsSteps( const selection = native ? semanticArrayPatternCloseSelection(pattern, mutation, value, state) : undefined; + // The original-source path can discharge acquisition only when it + // adds no effects. Keep the ordinary escape for a possible close. + if (originalOnly && (!native || selection !== undefined)) return false; if (!native || selection !== undefined) markValue( alternative, diff --git a/tests/boundary/cli/javascriptDestructuringEvidence.test.ts b/tests/boundary/cli/javascriptDestructuringEvidence.test.ts index 216af0dc80..905e2b32c5 100644 --- a/tests/boundary/cli/javascriptDestructuringEvidence.test.ts +++ b/tests/boundary/cli/javascriptDestructuringEvidence.test.ts @@ -141,7 +141,7 @@ cliTest( nestedSource.sibling[0] = "later"; const numericSource = { pairs: [["read-time"]] }; const { 0: [projectedNumericInnerElement] } = numericSource.pairs; - numericSource.pairs[0][0] = "later"; + numericSource.pairs = []; const nestedPairs = [["read-time"], ["sibling"]]; const [[innerElement] = ["fallback"], [siblingInnerElement]] = nestedPairs; nestedPairs[0][0] = "later";