Skip to content

Commit 1dc22f1

Browse files
morlutoSII-YDD
andauthored
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>
1 parent 833916b commit 1dc22f1

8 files changed

Lines changed: 322 additions & 42 deletions

‎src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,106 @@
11
import { expect, it } from "vitest";
22
import { graphForJavaScript } from "../../../tests/fixtures/javascriptSemanticGraph.js";
33

4+
it.each([
5+
"const { mode: selected, fallback = 0 } = config;",
6+
'const { mode: selected, ["fallback"]: later } = config;',
7+
"const { mode: selected, nested: { fallback = 0 } } = config;",
8+
"const { mode: selected, ...later } = config;",
9+
"const { mode: selected, accessor: later } = config;",
10+
"let selected; const target = {}; ({ mode: selected, fallback: target.value } = config);",
11+
])(
12+
"retains a proved read before a later unproven pattern operation",
13+
(read) => {
14+
const graph = graphForJavaScript(`
15+
const config = { mode: "strict", fallback: 0, nested: { fallback: 0 }, get accessor() { return 0; } };
16+
${read}
17+
config.mode = "loose";
18+
`);
19+
const selected = graph.nodes.find(
20+
({ kind, label }) => kind === "binding" && label === "selected",
21+
);
22+
expect(
23+
graph.relations.find(
24+
(edge) =>
25+
edge.relation === "destructures" &&
26+
edge.target_node_id === selected?.node_id,
27+
),
28+
).toMatchObject({ resolution: "resolved" });
29+
},
30+
);
31+
32+
it("leaves the next read uncertain after a source-mutating assignment target", () => {
33+
const graph = graphForJavaScript(`
34+
const config = { first: 1, mode: "strict" };
35+
let selected;
36+
const target = { set value(_) { delete config.mode; } };
37+
({ first: target.value, mode: selected } = config);
38+
config.mode = "loose";
39+
`);
40+
expect(
41+
graph.relations.find(({ relation }) => relation === "destructures"),
42+
).toMatchObject({ resolution: "candidate" });
43+
});
44+
45+
it.each([
46+
{ read: "const { mode: selected } = config;", relation: "destructures" },
47+
{
48+
read: "let selected; ({ mode: selected } = config);",
49+
relation: "destructures",
50+
},
51+
{ read: "const selected = config.mode;", relation: "reads-property" },
52+
] as const)(
53+
"keeps earlier $relation at its read position",
54+
({ read, relation }) => {
55+
const source = 'const config = { mode: "strict", other: 0 };';
56+
for (const [before, after, resolution] of [
57+
["", 'config.mode = "loose";', "resolved"],
58+
["", "config.other = 1;", "resolved"],
59+
["delete config.mode;", "", "candidate"],
60+
["mutate(config);", "", "candidate"],
61+
]) {
62+
const graph = graphForJavaScript(
63+
[source, before, read, after].join("\n"),
64+
);
65+
expect(
66+
graph.relations.find((edge) => edge.relation === relation),
67+
).toMatchObject({ resolution });
68+
}
69+
},
70+
);
71+
72+
it("keeps a direct nested data-slot dependency after a later write", () => {
73+
const graph = graphForJavaScript(`
74+
const config = { nested: { mode: "strict" } };
75+
const { mode: selected } = config.nested;
76+
config.nested.mode = "loose";
77+
`);
78+
expect(
79+
graph.relations.find(({ relation }) => relation === "destructures"),
80+
).toMatchObject({ resolution: "resolved" });
81+
});
82+
83+
it.each([
84+
`const config = { mode: "strict" }; const { first = (delete config.mode), mode: selected } = config;`,
85+
`const config = { get first() { delete this.mode; return {}; }, mode: "strict" };
86+
const { first: {}, mode: selected } = config;`,
87+
])(
88+
"leaves a dependency uncertain after an effectful earlier pattern lookup",
89+
(source) => {
90+
const graph = graphForJavaScript(source + 'config.mode = "loose";');
91+
const selected = graph.nodes.find(
92+
({ kind, label }) => kind === "binding" && label === "selected",
93+
);
94+
expect(
95+
graph.relations.find(
96+
(edge) =>
97+
edge.relation === "destructures" &&
98+
edge.target_node_id === selected?.node_id,
99+
),
100+
).toMatchObject({ resolution: "candidate" });
101+
},
102+
);
103+
4104
it("links declared and assigned destructuring bindings to their exact source slots", () => {
5105
const cases = [
6106
{ pattern: "{ a: selected }", source: "{ a: 1 }", path: ["a"] },

‎src/application/javascript/JavaScriptSemanticGraphObjectProjection.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ const objectTarget = (
120120
objectNode: slot,
121121
slot,
122122
slotApplicability: "resolved",
123-
presence: fact.presence,
123+
presence: operation.sourcePresence ?? fact.presence,
124124
receiverResolved:
125125
receiver.presence === "present" &&
126126
semanticContainer(receiver.value) !== null,

‎src/domain/javascript/javascriptSemanticEffectLog.ts‎

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,14 @@ export const semanticBindingEffectGeneration = (
484484
binding: JavaScriptSemanticBindingState,
485485
): number => binding.memberWrites.length + binding.escapedPaths.length;
486486

487+
/** A read may need to distinguish a captured reference from its merged value. */
488+
export const hasRecordedReferenceEffects = (
489+
binding: JavaScriptSemanticBindingState,
490+
state: JavaScriptSemanticAnalysisState,
491+
): boolean =>
492+
semanticBindingEffectGeneration(binding) > 0 ||
493+
state.opaqueEffects.byScope.size > 0;
494+
487495
/**
488496
* Recorded ambient writes to `global` paths, as intrinsic paths with
489497
* non-literal keys as null. A write recorded without a site is placed at the
@@ -967,6 +975,62 @@ const opaqueCodeMayReachBindingReference = (
967975
): boolean =>
968976
semanticBindingMayHaveOpaqueEffect(binding, state, capturePoint, "reference");
969977

978+
const observesCapturedWrite = (
979+
binding: JavaScriptSemanticBindingState,
980+
capture: t.Node | undefined,
981+
node: t.Node | undefined,
982+
parents: JavaScriptSemanticAnalysisState["parentsByNode"],
983+
): boolean =>
984+
capture === undefined ||
985+
node === undefined ||
986+
!semanticBindingEffectFollowsCapture(binding, capture, node, parents);
987+
988+
const observesCapturedEscape = (
989+
binding: JavaScriptSemanticBindingState,
990+
capture: t.Node | undefined,
991+
node: t.Node | undefined,
992+
parents: JavaScriptSemanticAnalysisState["parentsByNode"],
993+
): boolean =>
994+
capture === undefined ||
995+
!semanticEscapeFollowsCapture(binding, capture, node, parents);
996+
997+
/** Whether a captured own slot or its receiver may have changed before the read. */
998+
export const capturedBindingPathMayHaveChanged = (
999+
binding: JavaScriptSemanticBindingState,
1000+
path: readonly string[],
1001+
capture: t.Node,
1002+
state: JavaScriptSemanticAnalysisState,
1003+
): boolean => {
1004+
return (
1005+
opaqueCodeMayReachBindingReference(binding, state, capture) ||
1006+
semanticOpaqueGlobalMayHaveChanged(capture, state) ||
1007+
semanticSomeEffectPathCandidate(
1008+
binding.memberWrites,
1009+
path,
1010+
(effect) =>
1011+
semanticEffectPathMatches(effect.path, path) &&
1012+
observesCapturedWrite(
1013+
binding,
1014+
capture,
1015+
effect.node,
1016+
state.parentsByNode,
1017+
),
1018+
) ||
1019+
semanticSomeEffectPathCandidate(
1020+
binding.escapedPaths,
1021+
path,
1022+
(effect) =>
1023+
semanticEffectPathMatches(effect.path, path) &&
1024+
observesCapturedEscape(
1025+
binding,
1026+
capture,
1027+
effect.node,
1028+
state.parentsByNode,
1029+
),
1030+
)
1031+
);
1032+
};
1033+
9701034
/**
9711035
* The value of `binding`'s reference as a read captured at `capturePoint`
9721036
* observes it, after the recorded effects that may precede the capture:
@@ -983,25 +1047,16 @@ export const applySemanticBindingReferenceEffects = (
9831047
let projected = invalidateSemanticMutationPaths(
9841048
value,
9851049
binding.memberWrites
986-
.filter(
987-
({ node }) =>
988-
capturePoint === undefined ||
989-
node === undefined ||
990-
!semanticBindingEffectFollowsCapture(
991-
binding,
992-
capturePoint,
993-
node,
994-
state.parentsByNode,
995-
),
1050+
.filter(({ node }) =>
1051+
observesCapturedWrite(binding, capturePoint, node, state.parentsByNode),
9961052
)
9971053
.map(({ path }) => path),
9981054
);
9991055
for (const escape of binding.escapedPaths) {
10001056
// Escapes only invalidate object references; other lattice values survive.
10011057
if (projected.status !== "object" && projected.status !== "array") break;
10021058
if (
1003-
capturePoint !== undefined &&
1004-
semanticEscapeFollowsCapture(
1059+
!observesCapturedEscape(
10051060
binding,
10061061
capturePoint,
10071062
escape.node,

‎src/domain/javascript/javascriptSemanticIr.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,8 @@ export interface JavaScriptSemanticResourceOperation {
403403
export interface JavaScriptSemanticObjectOperation {
404404
readonly objectOperationId: string;
405405
readonly kind: "read" | "write" | "spread" | "destructure";
406+
/** Proven own data slot at this operation's read rather than in the merged value. */
407+
readonly sourcePresence?: "present";
406408
readonly location: JavaScriptSourceRange;
407409
readonly ownerCallableId: string | null;
408410
readonly objectBindingId: string | null;

‎src/domain/javascript/javascriptSemanticMutationInitializers.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,9 @@ const computeDirectInitializerPosition = (
558558
if (
559559
t.isAssignmentExpression(parent) &&
560560
parent.right === node &&
561-
t.isIdentifier(parent.left)
561+
(t.isIdentifier(parent.left) ||
562+
(parent.operator === "=" &&
563+
(t.isObjectPattern(parent.left) || t.isArrayPattern(parent.left))))
562564
) {
563565
return directMutationPosition(parent, parents);
564566
}

0 commit comments

Comments
 (0)