Repository navigation
Calling class fields that hold functions: an arrow reading a later getter; own call results through a field - #746
Merged
Merged
Conversation
… 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>
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.
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:
The same happened to
const f = () => this.propin 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
returnstatement 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() => voidfor good.Fix (
mlirGenBody). In a discovery pass, a concise body whose property, element or name read has no value now fails, as areturnstatement does. A call body that returns nothing is unchanged.Tests
00class_arrow_getter(compile and JIT) covers:With spec 70 (#745) on main, the field case works under
-mm=owntoo: 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 valueconst s = a.fn()with a fieldfn: () => string, orfn = () => this.s, failed under-mm=own --no-default-lib:Cause.
walkBorrowed(OwnershipInferenceBorrowers.cpp) treated the call's result as something that might borrow the field's function value. So releasing the string afteralooked 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 aftera, 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
thisargument still can, because a method can return a borrow of itsthis.Test.
own_field_function_result(own, plus own's no-counting check) covers results of calls through:this;Results
ctest 6419/6419. DefaultLib: gc 182/182, rc 181, none 181, own 171 (11 skipped by design).
🤖 Generated with Claude Code