Files
scriptc/tests/diagnostics/identity-collection-keys.ts
Chris Tate d3ecb0fc9d feat(compiler): advance self-hosting with identity collections (#500)
* feat(compiler): advance self-hosting with identity collections

- Support reference identity in Maps and Sets, including unions and cycle cleanup.
- Run production integer-range analysis natively with typed IR traversal.
- Add differential coverage for native analyses and collection behavior.

* chore: ignore generated output directories

- Exclude generated output directories from version control.
2026-09-27 20:02:13 -05:00

41 lines
1.4 KiB
TypeScript

interface Key { id: number }
interface Wide { id: number; extra: string }
const wide: Wide = { id: 1, extra: "kept" };
const narrow = new Map<Key, number>();
// Copying an existing wider record would lose JavaScript key identity.
narrow.set(wide, 1);
narrow.get(wide);
narrow.has(wide);
narrow.delete(wide);
const seeded = new Map<Key, number>([[wide, 1]]);
console.log(seeded);
const members = new Set<Key>();
members.add(wide);
members.has(wide);
const seededMembers = new Set<Key>([wide]);
console.log(seededMembers);
// An identity-key union must consist entirely of identity-bearing arms.
const mixed = new Map<Key | string, number>();
const nullable = new Set<Key | undefined>();
console.log(mixed, nullable);
// Casting a checked-dynamic snapshot to a key must not hide the copy.
const unknownKey: unknown = wide;
narrow.get(unknownKey as Key);
// Array key covariance also allocates a new container in the static tier.
const narrowArrays = new Map<Key[], number>();
const wideArray: Wide[] = [wide];
narrowArrays.set(wideArray, 1);
// An explicit record assertion must not hide the same structural copy.
narrow.get(wide as Key);
members.delete((wide as Key)!);
new Set<Key>([wide as Key]);
// Dynamic array/tuple seeds would copy their reference elements too.
const unknownKeys: unknown = [{ id: 1 }];
new Set<Key>(unknownKeys as Key[]);
new Set<Key>(unknownKeys as [Key]);