Repository navigation
Classes as values: typeof A, and new c(args) through it - #742
Merged
Merged
Conversation
`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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, andnew c(n)built anAwhatevercheld.The value
typeof Aof a class is aClassValueType: a tuple of four opaque pointers, the class's vtable, its.new, its constructor and its rtti. No class's vtable layout changes.typeof Ais expected: a parameter, a typed variable or field, an array element, a return.Bis a value oftypeof Awhen it extendsAand its constructor takesA's parameters (fewer is fine). Otherwise it is an error.Using it
new c(args)calls.newthrough 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 cis"function".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
-sharedlibrary'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 byimport_class_values+export_class_values(linked,-shared,-jit -sharedunder every memory model).Not done:
@valueand@cppclasses; 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
00class_values(gc, rc, none, own) and two error tests.newWithSpread.tsreadc/gwithout setting them. That only worked whiletypeof BwasBat 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