Skip to content

An object literal with methods assigned to a class is an error (#494) - #495

Merged
ASDAlexander77 merged 1 commit into
mainfrom
fix-object-literal-to-class
Oct 4, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
fix-object-literal-to-class

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

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, so no instance was built:

  • Fields: c.x read the class's field slot out of memory laid out as {x, f}, which gave garbage such as 1.34e-311.
  • Methods: 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, or as C):

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

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 2 under gc, rc, none and own.

New tests: object-literal-to-class/{method,accessor,parameter,return}.ts expect the error. On main these files compile with no error.

Gate Result
Windows full suite (Release) 3804/3804
Linux (WSL) full suite 3789/3789
DefaultLib suite, release + debug, compile + JIT gc 159/159; rc, none 158/159 (weakref_basic is gc-only, as on main)
own corpus no flips (459/616)

Closes #494

🤖 Generated with Claude Code

`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>
@ASDAlexander77
ASDAlexander77 merged commit 7e88c71 into main Oct 4, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the fix-object-literal-to-class branch October 4, 2026 20:24
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.

An object literal with a method assigned to a class type reads garbage fields and crashes on the method call

1 participant