Skip to content

Classes as values: typeof A, and new c(args) through it - #742

Merged
ASDAlexander77 merged 6 commits into
mainfrom
classes-as-values
Oct 29, 2026
Merged

ASDAlexander77 merged 6 commits into
mainfrom
classes-as-values

Conversation

@ASDAlexander77

@ASDAlexander77 ASDAlexander77 commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

function make(c: typeof A, n: number) { return new c(n); } make(B, 1) was "failed to legalize operation 'ts.ClassRef'": a class named only a class at compile time, and new c(n) built an A whatever c held.

The value

  • typeof A of a class is a ClassValueType: a tuple of four opaque pointers, the class's vtable, its .new, its constructor and its rtti. No class's vtable layout changes.
  • A class becomes a value where a typeof A is expected: a parameter, a typed variable or field, an array element, a return.
  • B is a value of typeof A when it extends A and its constructor takes A's parameters (fewer is fine). Otherwise it is an error.

Using it

  • new c(args) calls .new through the value (an owned, fresh result), then the constructor when there is one. Arguments are passed as in any call: spread, rest and optional all work. A value of an abstract class's type is an error.
  • == / != compare the rtti, so abstract classes compare too.
  • typeof c is "function".
  • A static method is called through the value's vtable, so a subclass's static of the same name is the one called. A static field is read through it too, and one a subclass declares again (same type) takes over the base's slot. An abstract class now has a vtable (its abstract and interface slots null), so a value holding the abstract class itself reads its statics. Any other member through a value is an error.

Local classes: a method, constructor or accessor of a class declared in a function, that reads one of the function's locals, is now an error naming the variable. Before, it was "Expected 1 arguments, but got 0".

Another module's classes are values too, a -shared library's included: .new, the constructor and the rtti through the importer's load-time pointers, the vtable looked up in the library by name. Tested by import_class_values + export_class_values (linked, -shared, -jit -shared under every memory model).

Not done: @value and @cpp classes; a field initializer that captures a local, in a class without a constructor; captures stored in the instance. docs/classes-as-values-design.md §5 has the details.

Tests

  • New: 00class_values (gc, rc, none, own) and two error tests.
  • newWithSpread.ts read c/g without setting them. That only worked while typeof B was B at compile time; they are initialized now.

ctest 6387/6387 on the merged tree. DefaultLib: gc 182/182, rc 181, none 181, own 171 (11 skipped by design).

🤖 Generated with Claude Code

ASDAlexander77 and others added 6 commits October 29, 2026 18:30
`function make(c: typeof A, n: number) { return new c(n); } make(B, 1)` was "failed to legalize
operation 'ts.ClassRef'": a class named only a class at compile time, and `new c(n)` built an `A`
whatever `c` held.

- `typeof A` of a class is a ClassValueType: a tuple of the class's vtable, its `.new`, its
  constructor and its rtti. No vtable layout changes.
- A class becomes a value where a `typeof A` is expected. B is a value of `typeof A` when it
  extends A and its constructor takes A's parameters.
- `new c(args)` calls `.new` through the value (an owned, fresh result) and then the constructor,
  when there is one. Too many arguments, or a value of an abstract class's type, is an error.
- `==`/`!=` compare the rtti, `typeof` is "function", and a static field is read through the
  value's vtable; any other member through a value is an error.
- A member of a class declared in a function that reads one of its locals is now an error naming
  the variable, instead of "Expected 1 arguments, but got 0".

docs/classes-as-values-design.md §5 describes it and its limits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nto classes-as-values

# Conflicts:
#	tslang/test/tester/CMakeLists.txt
…ps the error tests

- `new c(args)` through a class value now calls the constructor with mlirGenCallFunction, so a
  spread argument and a rest parameter are handled as in any call (newWithSpread.ts was "Expected 3
  arguments, but got 4").
- newWithSpread.ts read `c` and `g` without setting them: that only worked while `typeof B` was B at
  compile time. They are initialized now.
- verify-ownership.cmake skips class_value_constructor_error.ts and class_local_capture_error.ts,
  which are errors by design.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tatics

- `c.label()` through a `typeof Unit` value calls the static of the class `c` holds: a subclass's
  static method of the same name takes over the vtable slot (ClassMethodAccess). Before, any member
  but a static field was an error.
- An abstract class gets a vtable too, though no instance carries it: a value holding the abstract
  class itself read a static field (now a method too) through a null vtable and crashed. Its abstract
  methods' and interfaces' slots are null.
- 00class_values covers static methods (a subclass's, an inherited one, with arguments) and an
  abstract class's own statics; the design doc §5 says so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`k.count` through a `typeof Counter` value holding `Recount` read Counter's count: a subclass's
static field of the same name got a vtable slot of its own. Now one of the same type takes over the
base's slot, as a static method does, so it is the field of the class the value holds. The
compiler's own statics (`.rtti`, `.size`) keep a slot each.

00class_values covers it; the design doc §5 says so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`const c: typeof K.Shape = K.Square` with K from another module was "class 'K.Square' can't be used
as a value yet".

- A linked module's class: the check is lifted; its symbols are linked as any.
- A shared library's (-shared): `.new`, the constructor and the rtti are read through the pointers the
  importer resolves at load time, and the vtable is looked up in the library by name.
- A class without a constructor of its own whose base is a library's takes the base's constructor
  through that pointer, not as a function of that name (it crashed).
- A library class's static field read through a vtable is the field the slot points at, not the
  importer's pointer to it (it crashed).
- import_class_values + export_class_values: linked, -shared, -jit -shared, and -jit -shared under rc,
  none and own. The design doc §5 says so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 8ec221b into main Oct 29, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the classes-as-values branch October 29, 2026 20:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant