Skip to content

Calling class fields that hold functions: an arrow reading a later getter; own call results through a field - #746

Merged
ASDAlexander77 merged 2 commits into
mainfrom
arrow-field-getter
Oct 30, 2026
Merged

ASDAlexander77 merged 2 commits into
mainfrom
arrow-field-getter

Conversation

@ASDAlexander77

@ASDAlexander77 ASDAlexander77 commented Oct 30, 2026 •

Copy link
Copy Markdown
Owner

Two fixes for calling class fields that hold functions.

1. An arrow reading a get accessor declared later

An arrow with no return type that reads a get accessor declared later in the class failed in every memory model:

class A {
    n = 3;
    fn = () => this.prop;   // error: can't find return variable, seems your function type has 'void' return type
    get prop() { return this.n * 2; }
}

The same happened to const f = () => this.prop in a constructor or method declared before the accessor. In a method it failed with only "failed statement". It happened whether or not the accessor's type was given. A method call (() => this.m()) and a typed arrow ((): number => this.prop) were fine.

Cause. A class's members are generated in source order, and an accessor is registered only on its own turn. Before that, reading it in a discovery pass gives no value. A return statement then fails, so the member is retried after the others are declared. A concise arrow body (() => expr) instead took the missing value as "returns nothing", so discovery marked the arrow () => void for good.

Fix (mlirGenBody). In a discovery pass, a concise body whose property, element or name read has no value now fails, as a return statement does. A call body that returns nothing is unchanged.

Tests

  • 00class_arrow_getter (compile and JIT) covers:
    • a field's arrow;
    • an arrow in the constructor;
    • arrows in methods declared before the getter, one in parentheses;
    • a getter with its type given;
    • an arrow that calls a setter.

With spec 70 (#745) on main, the field case works under -mm=own too: its getter-reading arrow is a back-reference like any other.

2. -mm=own: a call through a field's function returns the caller's own value

const s = a.fn() with a field fn: () => string, or fn = () => this.s, failed under -mm=own --no-default-lib:

error: 'this value' borrows a field but is used here after it may be released or overwritten

Cause. walkBorrowed (OwnershipInferenceBorrowers.cpp) treated the call's result as something that might borrow the field's function value. So releasing the string after a looked like using the field after it was freed. With the default library the module isn't a "closed world", so the call's string wasn't marked owned, nothing released it after a, and the error never fired.

Fix. Being the function a call goes through (operand 0) no longer makes the call's result a borrow of it. A method's this argument still can, because a method can return a borrow of its this.

Test. own_field_function_result (own, plus own's no-counting check) covers results of calls through:

  • a typed field;
  • an arrow field reading this;
  • a function value read out of a field;
  • an object released before its result is used;
  • a temporary's field, in a loop.

Results

ctest 6419/6419. DefaultLib: gc 182/182, rc 181, none 181, own 171 (11 skipped by design).

🤖 Generated with Claude Code

ASDAlexander77 and others added 2 commits October 30, 2026 00:02
… returning nothing

`fn = () => this.prop` with `get prop()` failed in every model with "can't find return variable,
seems your function type has 'void' return type", and so did `const f = () => this.prop` in a
method or constructor declared before the accessor (a method's with only "failed statement").

A concise arrow body that reads something not resolved yet - the accessor isn't registered until
its own turn - had no value in the discovery pass, and was taken as a body returning nothing: the
arrow was typed `() => void` for good. Now, as a return statement's expression does, a property,
element or name read with no value in a discovery pass fails it, and the member is asked again
once the accessor is declared. A call can still return nothing.

00class_arrow_getter: a field's arrow, one in the constructor and in methods declared before the
getter, a getter with its type given, and an arrow calling a setter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…'s, not a borrow of the field

`const s = a.fn()` with a field `fn: () => string` (or `fn = () => this.s`) failed under
`-mm=own --no-default-lib` with "'this value' borrows a field but is used here after it may be
released or overwritten". With the default library the module is not a closed world, so the call's
string was not marked owned and nothing released it after `a`.

walkBorrowed followed the call's result as a value that might borrow the field's function, so the
string's release after `a` looked like a use of the field after it was freed. The function value a
call goes through gives nothing of itself to the result: being the callee (operand 0) no longer
walks into the call's results. A method's `this` still does, as a method can return a borrow of it.

own_field_function_result (own, plus own's no-counting check): results of calls through a typed
field, an arrow field reading `this` and a function read out of a field, one kept after its object
is released, and one of a temporary's field in a loop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77 ASDAlexander77 changed the title An arrow reading a get accessor declared later is typed by it, not as returning nothing Calling class fields that hold functions: an arrow reading a later getter; own call results through a field Oct 30, 2026
@ASDAlexander77
ASDAlexander77 merged commit a0ab109 into main Oct 30, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the arrow-field-getter branch October 30, 2026 00:53
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