Repository navigation
A class field fn = () => this.prop is a function: callable, and assignable later - #744
Merged
Merged
Conversation
…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>
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.
class A { prop = "x"; fn = () => this.prop; }thennew A().fn()was "not a function to call". The field was typedany, 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
thisnamed the class andthis.proplooked 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:
this.xisn't evaluated untilthisis the object.mlirGenClassRetypeFieldstypes it withthisdeclared as one, at two points in a discovery pass:this.xit reads is a field already typed (readsOnlyTypedFields). This is so the constructor can call another arrow field, as intwice = () => this.fn() + this.fn();fieldTypesFromMembers).fn: () => stringis stored. Soa.fn = () => "z"andthis.fn = ...in a method compile.Tests:
00class_arrow_fields(gc, rc, none) covers readingthis, 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:
fn = () => this.propwithget prop()) is still an error. Annotating the field avoids it.-mm=own, a field holding a closure overthis, orthis.m, is rejected. It is a back-reference to its own object, and supporting it needs a new ownership rule.🤖 Generated with Claude Code