Skip to content

Class values for @cpp classes implemented here; local class field initializer reading a local - #743

Merged
ASDAlexander77 merged 1 commit into
mainfrom
cpp-class-values
Oct 29, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
cpp-class-values

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Two follow-ups to classes as values (#742).

@cpp classes implemented here as values

  • A @cpp class whose methods have bodies (it implements C++ methods) can be a typeof value. new k(n) through the value makes an object whose vtable has C++'s layout, as new of the class does.
  • @cpp classes have no rtti, so == compares the vtable, which each such class has of its own.
  • A @cpp class whose objects C++ makes has no .new or constructor here, so it is now an error: "its objects are made by C++".
  • @cpp classes have no statics, so only new and == go through such a value.

A local class's field initializer reading a local

  • function f(start: number) { class C { v = start; } } was "'ts.Load' op using value defined outside the region". It now reports "a member of class 'C' reads 'start', a local of the function the class is declared in", as a method that reads a local already does.
  • The constructor's discovery pass is given the field initializers along with its body, but only for a class declared in a function. For a top-level class it broke fn = () => this.prop.

Tests

  • New: 00cpp_class_values (gc, rc, none, own), cpp_class_value_error and class_field_capture_error. The ownership verifier skips the two error tests.
  • ctest 6394/6394. DefaultLib: gc 182/182, rc 181, none 181, own 171 (11 skipped by design).

Found on the way, not fixed (both on main too)

  • new A().fn() with fn = () => this.prop is "not a function to call".
  • Under -mm=own, an arrow field that reads this is an escaping-closure error.

🤖 Generated with Claude Code

… initializer reading a local

- A @cpp class implemented here (methods with bodies) is a value: `new k(n)` through it makes an
  object with C++'s vtable, as `new` of the class does. It has no rtti, so the value's identity is its
  vtable, which each such class has of its own. A @cpp class whose objects C++ makes has no `.new` or
  constructor here: "the @cpp class 'X' can't be used as a value: its objects are made by C++".
- `function f(start) { class C { v = start; } }` was "'ts.Load' op using value defined outside the
  region". The discovery pass of a constructor of a class declared in a function is given its field
  initializers with its body, so the read is found and reported as a member's is: "a member of class
  'C' reads 'start', a local of the function the class is declared in". A top-level class's are not
  given to it (its `fn = () => this.prop` stopped resolving `this`).
- Tests: 00cpp_class_values (gc, rc, none, own), cpp_class_value_error, class_field_capture_error;
  the verifier skips the two errors. The design doc §5 says so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 55009d4 into main Oct 29, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the cpp-class-values branch October 29, 2026 21:22
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