Make the density proof conservative about holes

This commit is contained in:
Chris Tate
2026-12-05 07:56:54 -06:00
committed by GitHub
parent d9c7f15e45
commit 5ad18b3063
6 changed files with 140 additions and 2 deletions
@@ -1227,6 +1227,7 @@ class SourceAnalysis {
if (ARR_COPIES.has(e.method) || e.method === "splice" || e.method === "spliceInsert")
return [this.site(fn, e)];
return [];
case "arrayNewLen":
case "arrayLit":
case "setNew":
case "mapNew":
@@ -1894,7 +1895,8 @@ class SourceAnalysis {
* planned field's read is dense: its constructor arguments were checked
* the same way. */
private dense(fn: IrFunction, e: IrExpr): boolean {
if (e.kind === "fieldGet" && this.isFieldRead(e)) return true;
if (e.kind === "fieldGet" && this.isFieldRead(e))
return !this.sparse.has(this.fieldSlot(e.className, e.field));
return this.slotsOf(fn, e).every((slot) => !this.sparse.has(slot));
}
@@ -1920,7 +1922,12 @@ class SourceAnalysis {
// through one makes the array of every alias sparse, so seeding a write
// also reaches the slots the receiver may come from.
const aliasSlot = (slot: string): boolean =>
slot.startsWith("l:") || slot.startsWith("r:") || slot.startsWith("g:");
slot.startsWith("l:") ||
slot.startsWith("r:") ||
slot.startsWith("g:") ||
slot.startsWith("f:") ||
slot.startsWith("s:") ||
slot.startsWith("v:");
const reverse = new Map<string, Set<string>>();
for (const [from, tos] of this.edges) {
if (!aliasSlot(from)) continue;
@@ -1939,6 +1946,11 @@ class SourceAnalysis {
if (seen.has(slot)) continue;
seen.add(slot);
seed(slot);
if (slot.startsWith("v:")) {
const at = this.seeds.get(slot);
const owner = at === undefined ? undefined : this.origins.get(at);
if (owner !== undefined) queue.push(this.fieldSlot(owner.declaringClass, owner.field));
}
for (const from of reverse.get(slot) ?? NO_SLOTS) queue.push(from);
}
};
@@ -2033,6 +2045,14 @@ class SourceAnalysis {
};
const scanExpr = (fn: IrFunction, e: IrExpr, bounds: ReadonlyMap<string, Bound>): void => {
if (e.kind === "arrayNewLen") seed(this.site(fn, e));
// A value that reaches dynamic or unknown code may be mutated there:
// its density cannot be proved.
if (e.kind === "dynFrom" || e.kind === "caughtToDyn") seedWrite(this.slotsOf(fn, e.value));
if (e.kind === "callValue") for (const arg of e.args) seedWrite(this.slotsOf(fn, arg));
else if (e.kind === "libCall" && !readsArguments(e))
for (const arg of e.args) seedWrite(this.slotsOf(fn, arg));
else if ((e.kind === "call" || e.kind === "virtualCall") && this.callees(e).length === 0)
for (const arg of e.args) seedWrite(this.slotsOf(fn, arg));
if (e.kind === "arrIntrinsic" && ARR_COPIES.has(e.method)) {
copies.push({
sources: this.slotsOf(fn, e.receiver),
@@ -236,6 +236,55 @@ console.log(list.items.length, list.items[1]!.id);
expect(isInline(llvm)).toBe(false);
});
test.runIf(lowRegion)("a mapper-less Array.from copy is not inferred", async () => {
const { ok, llvm, messages } = await build(`
class Item implements ProgramLifetime {
readonly id: number;
constructor(id: number) {
this.id = id | 0;
}
}
class List implements ProgramLifetime {
readonly items: readonly Item[];
constructor(items: readonly Item[]) {
this.items = items;
}
}
const sparse = Array.from<Item>({ length: 3 });
const list = new List(sparse.slice());
console.log(list.items.length, 0 in list.items);
`);
expect(ok, messages.join("\n")).toBe(true);
expect(isInline(llvm)).toBe(false);
});
test.runIf(lowRegion)("an element write through a field alias is not inferred", async () => {
const { ok, llvm, messages } = await build(`
class Item implements ProgramLifetime {
readonly id: number;
constructor(id: number) {
this.id = id | 0;
}
}
class List implements ProgramLifetime {
readonly items: readonly Item[];
constructor(items: readonly Item[]) {
this.items = items;
}
}
class Holder {
items: Item[] = [new Item(1), new Item(2)];
}
const holder = new Holder();
const alias = holder.items;
alias[5] = new Item(3);
const list = new List(holder.items.slice());
console.log(list.items.length, 5 in list.items, list.items[5]!.id);
`);
expect(ok, messages.join("\n")).toBe(true);
expect(isInline(llvm)).toBe(false);
});
test("an annotation without a ProgramLifetime class still reports", async () => {
const { ok, codes, messages } = await build(
`
@@ -0,0 +1,6 @@
{
"order": [
"<repo>/tests/corpus/inline-array-inference-field-alias.ts"
],
"diags": []
}
@@ -0,0 +1,6 @@
{
"order": [
"<repo>/tests/corpus/inline-array-inference-from-length.ts"
],
"diags": []
}
@@ -0,0 +1,34 @@
// An element write beyond the length of an array held by an object field,
// through a local alias of that field, leaves holes in the field's array.
// A copy of the field keeps the holes, so an inferred field would trap when
// the constructor copies it; it stays an ordinary array instead.
import type { ProgramLifetime } from "@scriptc/types";
class Item implements ProgramLifetime {
readonly id: number;
constructor(id: number) {
this.id = id | 0;
}
}
class List implements ProgramLifetime {
readonly items: readonly Item[];
constructor(items: readonly Item[]) {
this.items = items;
}
}
class Holder {
items: Item[] = [new Item(1), new Item(2)];
}
const holder = new Holder();
const alias = holder.items;
alias[5] = new Item(3);
const list = new List(holder.items.slice());
console.log(
list.items.length,
2 in list.items,
5 in list.items,
list.items[5]!.id,
);
@@ -0,0 +1,23 @@
// A mapper-less Array.from({ length: n }) builds an array whose slots are
// absent. A copy of it keeps them absent, so a field inferred from the copy
// would trap when the constructor copies the elements whole. The field stays
// an ordinary array, and lengths and presence read as under Node.
import type { ProgramLifetime } from "@scriptc/types";
class Item implements ProgramLifetime {
readonly id: number;
constructor(id: number) {
this.id = id | 0;
}
}
class List implements ProgramLifetime {
readonly items: readonly Item[];
constructor(items: readonly Item[]) {
this.items = items;
}
}
const sparse = Array.from<Item>({ length: 3 });
const list = new List(sparse.slice());
console.log(list.items.length, 0 in list.items, 2 in list.items);