Skip to content

A class field fn = () => this.prop is a function: callable, and assignable later - #744

Merged
ASDAlexander77 merged 1 commit into
mainfrom
arrow-fields
Oct 29, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
arrow-fields

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

class A { prop = "x"; fn = () => this.prop; } then new A().fn() was "not a function to call". The field was typed any, with the warning "type for field 'fn' is any".

Why: the initializer was typed while the fields were being laid out. At that point no object is in scope, so this named the class and this.prop looked for a static. A discovery pass did type the field later, once the members were declared, but the full pass never used that type.

Fix:

  • A field with no annotation whose initializer reads this.x isn't evaluated until this is the object. mlirGenClassRetypeFields types it with this declared as one, at two points in a discovery pass:
    • right after the fields are laid out, when every this.x it reads is a field already typed (readsOnlyTypedFields). This is so the constructor can call another arrow field, as in twice = () => this.fn() + this.fn();
    • again once the members are declared. This covers an initializer that reads an abstract property's accessor.
  • The full pass uses the types the discovery pass found (fieldTypesFromMembers).
  • A capturing arrow's type is a bound function. The field now stores it as a hybrid of the function without its capture, the same way an annotated fn: () => string is stored. So a.fn = () => "z" and this.fn = ... in a method compile.

Tests: 00class_arrow_fields (gc, rc, none) covers reading this, a parameter, one arrow field calling another, reassignment from outside and in a method, and a subclass. ctest 6396/6396. DefaultLib: gc 182/182, rc 181, none 181, own 171 (11 skipped by design).

Not covered, unchanged from main:

  • An arrow field that reads a getter (fn = () => this.prop with get prop()) is still an error. Annotating the field avoids it.
  • Under -mm=own, a field holding a closure over this, or this.m, is rejected. It is a back-reference to its own object, and supporting it needs a new ownership rule.

🤖 Generated with Claude Code

…ignable later

`class A { prop = "x"; fn = () => this.prop; }` then `new A().fn()` was "not a function to call":
the field was `any` ("type for field 'fn' is any"). Its initializer was typed while the fields were
laid out, with no `this` but the class (`this.prop` a static), and the full pass never took the type
a discovery pass found for it later.

- An initializer reading `this.x`, with no type given, is not evaluated before `this` is the object.
  mlirGenClassRetypeFields types it with `this` declared as one: in a discovery pass right after the
  fields are laid out when every `this.x` it reads is a field typed by then (readsOnlyTypedFields;
  `twice = () => this.fn() + this.fn()`, for the constructor to read), and again once the members are
  declared (an abstract property's accessor). The full pass takes those types (fieldTypesFromMembers).
- A capturing arrow's type (a bound function) is stored as a hybrid of the function without its
  capture, as `fn: () => string` is: `a.fn = () => "z"` and `this.fn = ...` in a method compile.
- 00class_arrow_fields (gc, rc, none).

Not covered: an arrow field reading a getter (`fn = () => this.prop` with `get prop()`) is still an
error, as on main; and -mm=own rejects a field holding a closure over `this` (or `this.m`), a
back-reference to its own object, as on main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 4ccae84 into main Oct 29, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the arrow-fields branch October 29, 2026 22:56
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