Repository navigation
own: a record literal assigned to a class type is the instance (#492) - #504
Merged
Merged
Conversation
Under -mm=own, `const p: P = { x: i, items: [i] }` - and since #487
also the constant `{ x: 1, items: [1, 2] }` - was "'this value' borrows
a field and cannot be stored, returned or captured". The literal was
built in a temporary slot, and castTupleToClass read each field back
out of the slot to store it into a new P: own takes such a read for a
borrow of the slot, which cannot be stored.
A literal whose receiver is a class (a declaration, a parameter, a
result) is now built as the instance itself
(mlirGenObjectLiteralAsClassInstance): each field's value is stored into
the new P as it is - a value made at run time, a number, string or
boolean constant, or a constant array copied to a heap array of its own
(#479). Own sees fresh values moved into a new object. Only for a
literal whose fields are exactly the class's; a class with a base, a
literal missing a field or holding an extra one, or another kind of
constant still goes through the slot and the cast, as before.
Under rc and own, 1M iterations of a record-to-class (AOT): flat at
4.3 MB.
The record-variable case of 00record_to_class.ts (`let r = {...};
const q: P = r;`) moves into 00record_variable_to_class.ts: it shares
r's array, which own reports as an error by design, and
00record_to_class.ts now passes under own.
Closes #492
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.
Under
-mm=own, assigning a record literal to a class type was a borrow error ("'this value' borrows a field and cannot be stored, returned or captured"). That coveredconst p: P = { x: i, items: [i] }and, since #487, the constant{ x: 1, items: [1, 2] }too. The literal was built in a temporary slot, andcastTupleToClassread each field back out of the slot to store it into a newP. Own treats such a read as a borrow of the slot, and a borrow cannot be stored.A literal whose receiver is a class (a declaration, a parameter or a result) is now built as the instance itself, by
mlirGenObjectLiteralAsClassInstance. Each field's value is stored into the newPas it is: a value made at run time, a number, string or boolean constant, or a constant array copied to a heap array of its own (#479). Own sees fresh values moved into a new object.This only applies when the literal's fields are exactly the class's. These cases still go through the slot and the cast, unchanged:
I checked by hand: a base class and a missing required field give the same error as before, and a missing optional field and an extra field work as before. A nested record field still hits own's existing limit on moving an object literal.
The record-variable case of
00record_to_class.ts(let r = {...}; const q: P = r;) moves into a new00record_variable_to_class.ts. That case sharesr's array, which own reports as an error by design. With it moved,00record_to_class.tsnow passes under all four models.tests.ps1 -Model, release + debug, compile + JITweakref_basicskipped (gc only); all "All tests passed"--opt00record_to_class.tserr → ok; no other flipsCloses #492
🤖 Generated with Claude Code