An object literal with methods assigned to a class is an error (#494) - #495
Merged
Merged
Conversation
`const c: C = { x: 3, f() { return 2; } }` compiled, but the instance
was wrong: an object literal with a method or an accessor is boxed (an
`!ts.object`), and its cast to the class was a plain ts.Cast of the
pointer. No instance was built, so `c.x` read the class's field slot
out of memory laid out as `{x, f}` (garbage such as 1.34e-311), and
`c.f()` called through a vtable that was not there (0xC0000005).
A class instance takes its methods from its class's vtable, so one
object cannot have a method of its own; TypeScript, where `c.f()` would
give 2, has no counterpart here. Such a literal is now a compile error,
"an object literal with methods or accessors cannot be assigned to
class 'C': a class instance takes its methods from its class; use
'new', or an interface type", wherever it meets a class type: a
declaration, a parameter, a return, `as C`. A literal with fields only
is still made into an instance (#487), and a literal with methods
assigned to an interface type is unchanged.
Closes #494
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.
const c: C = { x: 3, f() { return 2; } }compiled, but the instance was wrong. An object literal with a method or an accessor is boxed (an!ts.object), and its cast to the class was a plaints.Castof the pointer, so no instance was built:c.xread the class's field slot out of memory laid out as{x, f}, which gave garbage such as1.34e-311.c.f()called through a vtable that wasn't there, which crashed with 0xC0000005.A class instance takes its methods from its class's vtable, so one object cannot have a method of its own. In TypeScript
c.f()would give 2; tslang has no way to express that. As agreed, such a literal is now a compile error wherever it meets a class type (a declaration, a parameter, a return, oras C):A literal with fields only is still made into an instance (#487). A literal with methods assigned to an interface type is unchanged: it prints
3 2under gc, rc, none and own.New tests:
object-literal-to-class/{method,accessor,parameter,return}.tsexpect the error. Onmainthese files compile with no error.weakref_basicis gc-only, as onmain)Closes #494
🤖 Generated with Claude Code