Repository navigation
-mm=own: copy a string read out of the parameter the result borrows into a place - #702
Merged
Merged
Conversation
…nto a place A function that returns `this` has a result that borrows `this`, and spec 22.4 kept every string read out of it from being copied - so `this[t] = this[s]` (copyWithin) and a swap's `let v = this[i]` (reverse) were "borrows an element". The fact is about what is returned: `copyForRetain` now copies such a read stored into an element or a field, and starts a `let` from a copy when the local cannot be returned (`mayBeReturned`). A read returned, directly or through a local, stays the borrow. Spec section 41. 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.
Summary
Under
-mm=own, a function that returnsthis(copyWithin,reverse) gets a result that borrowsthis(__own_result_borrows). Spec §22.4 then kept every string read out ofthisfrom being copied, so two shapes failed with "borrows an element":this[t] = this[s]let v = this[i]That fact is about what is returned.
copyForRetainnow makes a copy in two cases:ts.ElementRefor ats.PropertyRef.letthat cannot be returned: aletinitialised from such a read starts from a copy whenmayBeReturnedfinds that no load of it, or of a local it is stored into, reaches ats.ReturnInternal.A read that is returned, directly or through a local, stays the borrow. A parameter the function keeps is unchanged.
Spec §41 is new and §22.4 points to it.
docs/own-memory-model.mdlists the remaining limit:sortandreverseover class instances, where a swap's local would be a second owner.Tests
New
own_copy_within_returned_param, over strings built at run time with churn between steps, under own, rc, gc and none:this[t] = this[s]in a function that returnsthisletin oneAOT under own: 1000, 200000 and 400000 iterations peak at 4.0, 4.0 and 4.4 MB, so a returned read copied by mistake would show as a leak.
Full
ctest -C Release: 5901 passed, 0 failed.DefaultLib suites, with the companion
reverse/sortchange (ASDAlexander77/TypeScriptCompilerDefaultLib, PR to follow):array_copyWithin,owning_copies,array_reverse,array_sort,string_localeCompare), with no new failures.🤖 Generated with Claude Code