Ante: New Way to Blend Borrow Checking and Reference Counting

Ante: New Way to Blend Borrow Checking and Reference Counting

Ante:融合借用检查与引用计数的新方法

Blending borrowing and reference counting without the crashes! 在不崩溃的前提下融合借用检查与引用计数!

June 28, 2026 — Evan Ovadia 2026年6月28日 — Evan Ovadia

Ante has taken the first step towards something we all thought was impossible: blending reference counting and borrow checking without run-time crashes. Ante 向着我们曾认为不可能的目标迈出了第一步:在不发生运行时崩溃的前提下,融合引用计数与借用检查。

This is very promising, because it means that someday, we can more easily use each approach when it makes sense without risking crashes. If there was a language like that, I could prototype a game in a flexible way with reference-counting, and gradually migrate it to faster borrow-checked code. 这非常有前景,因为它意味着未来我们可以在合适的场景下更轻松地使用这两种方法,而无需担心崩溃风险。如果存在这样一种语言,我就可以先用引用计数灵活地进行游戏原型开发,然后再逐步将其迁移到性能更高的借用检查代码中。

No mainstream language has figured out how to combine them seamlessly, even though many have tried. Rust tried, but when we try to use Rust’s reference-counting Rc type, it often requires RefCell (like Rc<RefCell>) which can crash at run-time if you hold it wrong. 尽管许多语言尝试过,但目前还没有主流语言能够无缝地结合这两者。Rust 尝试过,但当我们使用 Rust 的引用计数类型 Rc 时,往往需要配合 RefCell(例如 Rc<RefCell<Spaceship>>),如果使用不当,它可能会在运行时崩溃。

I wish rustc could check for proper Rc usage at compile-time! 我真希望 rustc 能够在编译时检查 Rc 的正确使用!

Swift too has tried, with its new borrowing system, but it has an expensive run-time check which crashes at run-time if you hold it wrong. It turns out, blending reference counting and borrow checking is hard, for reasons you’ll see below. So let’s talk about Ante! Swift 也尝试过,通过其新的借用系统,但它包含昂贵的运行时检查,如果使用不当同样会导致运行时崩溃。事实证明,融合引用计数和借用检查非常困难,原因你将在下文中看到。那么,让我们来聊聊 Ante!

Ante is a work-in-progress! Some parts of this are implemented, some are still theoretical, and the design is still in flux. This is being actively developed as we speak, so follow Ante’s progress on its site and discord! Ante 目前仍在开发中!部分功能已经实现,部分仍处于理论阶段,设计也在不断变动。它正在积极开发中,请通过其官网和 Discord 关注 Ante 的进展!


Ante

Ante

Ante aims to be a simpler Rust, a systems programming language with memory-safety and thread-safety. It has single ownership and borrow checking, so values are inline (on the stack, or in the containing struct/array). And when the user wants to prioritize simplicity, they can opt into reference counting with the shared keyword on their types. Ante 旨在成为一个更简单的 Rust,一种具备内存安全和线程安全的系统编程语言。它拥有单一所有权和借用检查机制,因此值是内联的(存储在栈上或包含它的结构体/数组中)。当用户希望优先考虑简洁性时,可以通过在类型上使用 shared 关键字来选择引用计数。

I normally favor C-style syntax, but even I have to admit, this is beautiful. And it’s concise too, as small as the Python equivalent, and smaller than the C++ equivalent and Rust equivalent. 我通常偏爱 C 风格的语法,但即便是我也不得不承认,这非常优雅。而且它很简洁,代码量与 Python 等价代码相当,且比 C++ 和 Rust 的等价代码更短。

But the most interesting thing for me is what Ante is doing in memory safety. Ante has shared mutability superpowers: if you want to mutably borrow reference-counted data, you don’t need to risk run-time errors. No mainstream language can do that, not even Rust or Swift. 但对我来说,最有趣的是 Ante 在内存安全方面所做的工作。Ante 拥有共享可变性的“超能力”:如果你想可变地借用引用计数数据,你无需承担运行时错误的风险。目前没有任何主流语言能做到这一点,即使是 Rust 或 Swift 也不行。


Shape-Stability

形状稳定性 (Shape-Stability)

Ante has a concept of shape-stability, which means “a reference to something of stable shape is always valid no matter what mutations are made elsewhere.” Because of this, Ante code can safely have multiple mutable borrow references to the same struct at the same time. Ante 引入了“形状稳定性”的概念,这意味着“对具有稳定形状的事物的引用,无论其他地方发生了什么变动,始终是有效的”。正因如此,Ante 代码可以安全地同时拥有对同一个结构体的多个可变借用引用。

In Ante, we can call heal with the same Entity for both parameters — for example, when an entity heals itself. Mutating healer can’t invalidate shared references to the Entity in any way, so the compiler accepts this code as valid. In other words, even though healer and target might point to the same Entity, this is memory safe: nothing here can destroy the Entity, so both references stay valid. 在 Ante 中,我们可以用同一个 Entity 作为两个参数来调用 heal 函数——例如,当一个实体自我治疗时。修改 healer 不会以任何方式使指向该 Entity 的共享引用失效,因此编译器认为此代码有效。换句话说,即使 healertarget 可能指向同一个 Entity,这也是内存安全的:这里没有任何操作可以销毁该 Entity,因此两个引用都保持有效。

Now let’s get a little more complicated. Ante code can actually have multiple mutable borrow references to the same struct, or any of its struct fields, or any of their struct fields, at the same time. 现在让我们深入一点。Ante 代码实际上可以同时拥有对同一个结构体、其任意字段,或字段的字段的多个可变借用引用。

Ante knows this is completely memory safe, because during this function, nobody can destroy ship, and therefore nobody can destroy its engine or its fuel. In other words, these references are to shape-stable data. Those familiar with Rust and Swift will be surprised by this: In Rust, we can’t have multiple &mut references pointing to the same data. Ante 知道这是完全内存安全的,因为在该函数执行期间,没有人能销毁 ship,因此也就没有人能销毁它的引擎或燃料。换句话说,这些引用指向的是形状稳定的数据。熟悉 Rust 和 Swift 的人会对此感到惊讶:在 Rust 中,我们不能拥有指向同一数据的多个 &mut 引用。