Skip to content

-mm=own: @lends, and a string parameter the body assigns owns what it holds - #623

Merged
ASDAlexander77 merged 1 commit into
mainfrom
own-lends-string-params
Oct 19, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
own-lends-string-params

Conversation

@ASDAlexander77

@ASDAlexander77 ASDAlexander77 commented Oct 19, 2026 •

Copy link
Copy Markdown
Owner

Closes the two follow-ups left by #622.

Corrupted regexp strings in an own DefaultLib. This turned out not to be a borrowed temporary. resize assigns its this parameter (this.length = n). Under own, a string parameter's slot did not own what was assigned to it, which caused two problems:

  • The resized string leaked.
  • return this returned a ts.StringCopy of it. The copy is read up to the first NUL, so it came back one byte long, and the caller ("".clone().resize(len) in MatchResults.get, padStart, repeat, ...) then wrote len bytes into it.

Now, under own, a string parameter that the body assigns (including p.length = n) starts a local of its own, made from a copy (mlirGenFunctionOwnAssignedParams), as rc already did. Assigned class parameters are still rejected, as before. Test: own_string_param_assigned.ts, run under own, rc and gc. It fails without the fix.

@lends("p"). A new marker says that a function's result is a borrow of the argument p (__own_result_borrows). The caller owns nothing of it, and it may not outlive p. Until now an exported function like that was rejected, because callers in other modules couldn't know.

  • It names exactly one parameter. It can be combined with @keeps but not with @borrows.
  • It is printed into __decls and copied onto dynamic-import globals.
  • It is checked against the body, and family overrides must say the same.
  • A body that returns a borrow under @keeps alone is told to add @lends; a body that keeps an argument under @lends alone is told to add @keeps.
  • The error for an open function now says that @lends or @keeps would put the fact in its signature.

Tests:

  • own_lends.ts runs under own, rc and gc.
  • Six own_err_lends_* cases cover the errors.
  • Across modules, import_own_markers (under gc, rc and own, compiled and JIT) and import_own_markers_lent_outlives.

Full suite: 4827/4827 passed (Release, Windows), with DefaultLib rebuilt for gc, rc, none and own. matchAll, padStart and repeat through the own DefaultLib now give correct results.

🤖 Generated with Claude Code

… holds

- `@lends("p")` says a function's result is a borrow of argument p: the
  caller owns nothing of it and it may not outlive p. It crosses modules
  through __decls, is checked against the body and family overrides, and
  names one parameter; it cannot be combined with @borrows.
- Under own a string parameter the body assigns, including `p.length = n`,
  starts a local of its own made from a copy, as rc already did. Left as the
  parameter's slot, the assigned string leaked and `return this` returned a
  copy read up to its first NUL: the default library's
  `"".clone().resize(n)` returned one byte that its caller filled with n,
  which corrupted the own DefaultLib's regexp results.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 0763641 into main Oct 19, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the own-lends-string-params branch October 19, 2026 23:45
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