Files
scriptc/packages/compiler/test/class-protocol-boundaries.test.ts
T
Malte UblandClaude Opus 5.5 83e26da25e Keep a literal's accessors live through a declared data member
An object literal whose get/set accessor satisfies a member the target type
declares as DATA was refused with SC1090: a record member is one storage
slot, so the accessor could only be snapshotted at construction. Node keeps
the accessor live, and the pi sites that hit this (agent.ts:90,
runner.ts:889, codemode/execution.ts:562) are all live getters — one throws
until the object observes later state — so a snapshot is disqualifying.

One static record layout cannot hold both member kinds at one name, so the
analysis marks the TARGET type instead of inventing a literal-only variant:
when a literal's member kind disagrees with the member the contextual type
declares at the same name, that type keeps checked identity, exactly as a
structural type that carries a class instance's identity does. The literal
then lowers through the checked-dynamic literal path, which defines accessor
members as live get/set properties and data members as plain properties, and
reads, writes, Object.keys, `in` and JSON.stringify on the declared type
dispatch dynamically — Node's own model.

The mirrored direction is closed too: a data property satisfying a declared
accessor member (`interface Live { get size(): number }`) used to be an
SC2002 missing-`%get:`-slot refusal; it now keeps the declared type's
identity as well.

Cost, the accepted trade: every value of a MARKED type becomes a checked
dynamic view — a property table and closure dispatch instead of a static
struct slot, so reads and writes on those types are slower (never wrong).
The cost is scoped to the marked type: a program with no such literal marks
nothing, and the frontend analysis itself adds only one array push per object
literal and one Set lookup per data member, with no checker query unless a
member name is an accessor somewhere in the program. IR is byte-identical
for 64 existing corpus programs compiled before and after. The whole-type
accessor-shape form (static reads, closure slots) remains available as the
faster representation if profiles ever show this matters; it needs the
accessor enumeration surfaces (keys, JSON, inspect, for-in, spread, rest)
that this route gets for free.

Only a single object type can be the literal's target: a multi-arm union
picks its arm from the value itself and an intersection merges members, so
neither is marked and both keep today's behavior. A value that reaches the
slot through a variable rather than a literal keeps today's refusal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 17:58:04 -07:00

88 lines
2.9 KiB
TypeScript

import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { expect, test } from "vitest";
import { compile } from "../src/index.js";
test("generic protocol dispatch without a concrete body calls the erased method", async () => {
const directory = mkdtempSync(join(tmpdir(), "scriptc-protocol-boundary-"));
try {
const entry = join(directory, "main.ts");
writeFileSync(
entry,
`interface Live { value(): number; identity<T>(value: T): T; }
class Value implements Live {
value(): number { return 1; }
identity<T>(value: T): T { return value; }
}
function read(value: Live): number { return value.identity(3); }
console.log(read(new Value()));`,
);
const result = await compile(entry, {
outDir: directory,
outPath: join(directory, "program.ir"),
outputKind: "ir",
});
if (!result.ok)
throw new Error(result.diagnostics.map((d) => `${d.code}: ${d.message}`).join("\n"));
expect(result.ok).toBe(true);
} finally {
rmSync(directory, { recursive: true, force: true });
}
});
test("an accessor satisfying a declared data member keeps the target live", async () => {
const directory = mkdtempSync(join(tmpdir(), "scriptc-accessor-data-member-"));
try {
const entry = join(directory, "main.ts");
writeFileSync(
entry,
`interface Counter { size: number; label: string; }
function makeCounter(): Counter {
let current = 1;
return {
get size(): number { return current; },
set size(next: number) { current = next; },
label: "counter",
};
}
const counter = makeCounter();
counter.size = 10;
console.log(counter.size, counter.label, Object.keys(counter).join(","));`,
);
const result = await compile(entry, {
outDir: directory,
outPath: join(directory, "program.ir"),
outputKind: "ir",
});
if (!result.ok)
throw new Error(result.diagnostics.map((d) => `${d.code}: ${d.message}`).join("\n"));
expect(result.ok).toBe(true);
} finally {
rmSync(directory, { recursive: true, force: true });
}
});
test("a data property satisfying a declared accessor keeps the target live", async () => {
const directory = mkdtempSync(join(tmpdir(), "scriptc-data-accessor-member-"));
try {
const entry = join(directory, "main.ts");
writeFileSync(
entry,
`interface Live { readonly label: string; get size(): number; }
function makeLive(): Live { return { label: "counter", size: 3 }; }
console.log(makeLive().size);`,
);
const result = await compile(entry, {
outDir: directory,
outPath: join(directory, "program.ir"),
outputKind: "ir",
});
if (!result.ok)
throw new Error(result.diagnostics.map((d) => `${d.code}: ${d.message}`).join("\n"));
expect(result.ok).toBe(true);
} finally {
rmSync(directory, { recursive: true, force: true });
}
});