fix(rust): change `ts_set_allocator` to accept one optional `Allocator` object rather than 4 separate optional function pointers. by WillLillis · Pull Request #5657 · tree-sitter/tree-sitter · GitHub
Skip to content

fix(rust): change ts_set_allocator to accept one optional Allocator object rather than 4 separate optional function pointers.#5657

Open
WillLillis wants to merge 1 commit into
tree-sitter:masterfrom
WillLillis:alloc_safety
Open

fix(rust): change ts_set_allocator to accept one optional Allocator object rather than 4 separate optional function pointers.#5657
WillLillis wants to merge 1 commit into
tree-sitter:masterfrom
WillLillis:alloc_safety

Conversation

@WillLillis

Copy link
Copy Markdown
Member

In addition to the API change, this PR also adds some safety documentation to ts_set_allocator, indicating which invariants must be upheld. Finally, instead of caching the currently set free function in a static global, we can just resolve what ts_current_free is at use time. This should give more intuitive behavior (rather than silently using the libc free function.

AI Policy

  • I have read the AI Policy and this PR complies with it.
  • If AI tools were used: I have disclosed the tool and extent of usage below.

Used Claude Opus 4.7 to look for additional safety pitfalls after an initial scan myself. This was particularly helpful for the alignment issue.

object rather than 4 separate optional function pointers.

Helps prevent misuse via mixing different allocators. Also update the
doc comment with relevant safety information.
@WillLillis WillLillis requested a review from amaanq June 2, 2026 21:17
@WillLillis WillLillis added the rust label Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Better safety documentation for tree_sitter::set_allocator in Rust SDK

1 participant