diff --git a/packages/compiler/src/ir/inline-array-sources.ts b/packages/compiler/src/ir/inline-array-sources.ts index 1fdb99a145..af7b5a3981 100644 --- a/packages/compiler/src/ir/inline-array-sources.ts +++ b/packages/compiler/src/ir/inline-array-sources.ts @@ -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>(); 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): 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), diff --git a/packages/compiler/test/inline-array-inferred.test.ts b/packages/compiler/test/inline-array-inferred.test.ts index ebddda417c..22d9e1b442 100644 --- a/packages/compiler/test/inline-array-inferred.test.ts +++ b/packages/compiler/test/inline-array-inferred.test.ts @@ -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({ 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( ` diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/inline-array-inference-field-alias.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/inline-array-inference-field-alias.ts.json new file mode 100644 index 0000000000..f1c598f0b4 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/inline-array-inference-field-alias.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/inline-array-inference-field-alias.ts" + ], + "diags": [] +} diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/inline-array-inference-from-length.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/inline-array-inference-from-length.ts.json new file mode 100644 index 0000000000..b868631987 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/inline-array-inference-from-length.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/inline-array-inference-from-length.ts" + ], + "diags": [] +} diff --git a/tests/corpus/inline-array-inference-field-alias.ts b/tests/corpus/inline-array-inference-field-alias.ts new file mode 100644 index 0000000000..64a94fe583 --- /dev/null +++ b/tests/corpus/inline-array-inference-field-alias.ts @@ -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, +); diff --git a/tests/corpus/inline-array-inference-from-length.ts b/tests/corpus/inline-array-inference-from-length.ts new file mode 100644 index 0000000000..0fd1fb7949 --- /dev/null +++ b/tests/corpus/inline-array-inference-from-length.ts @@ -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({ length: 3 }); +const list = new List(sparse.slice()); +console.log(list.items.length, 0 in list.items, 2 in list.items);