Skip to content

-mm=own: copy a string read out of the parameter the result borrows into a place - #702

Merged
ASDAlexander77 merged 1 commit into
mainfrom
own-array-element-moves
Oct 27, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
own-array-element-moves

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Summary

Under -mm=own, a function that returns this (copyWithin, reverse) gets a result that borrows this (__own_result_borrows). Spec §22.4 then kept every string read out of this from being copied, so two shapes failed with "borrows an element":

  • this[t] = this[s]
  • a swap's let v = this[i]

That fact is about what is returned. copyForRetain now makes a copy in two cases:

  • A store into an element or a field: a read of the parameter the result borrows, stored through a ts.ElementRef or a ts.PropertyRef.
  • A let that cannot be returned: a let initialised from such a read starts from a copy when mayBeReturned finds that no load of it, or of a local it is stored into, reaches a ts.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.md lists the remaining limit: sort and reverse over 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 returns this
    • a swap through a let in one
    • a read returned directly
    • a read returned through a local

    AOT 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/sort change (ASDAlexander77/TypeScriptCompilerDefaultLib, PR to follow):

    • rc, gc and none pass every test.
    • own goes from 145 to 150/170 (array_copyWithin, owning_copies, array_reverse, array_sort, string_localeCompare), with no new failures.

🤖 Generated with Claude Code

…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>
@ASDAlexander77
ASDAlexander77 merged commit 61f9f74 into main Oct 27, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the own-array-element-moves branch October 27, 2026 18:27
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