Optimizing Rustc's next trait solver
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
Optimizing Rustc’s next trait solver
Optimizing Rustc’s next trait solver Press for table of Contents The next rust trait solver, a large subsystem of the rust compiler that we’ve been trying to replace, will finally land in rust 1.102.0. In the last stages, before stabilizing it, its performance was a major blocker. But what is “a trait solver”? And why is its performance a problem? My goal for this blog post is to answer those questions in a way where you don’t need any prior knowledge. I think discussions of the trait solver can quickly become quite involved, and daunting. So today, we’ll start from the very basics!
优化 Rustc 的下一个 trait 解析器。目录:Rust 的下一个 trait 解析器是 Rust 编译器的一个大型子系统,我们一直试图对其进行替换,它最终将在 Rust 1.102.0 中落地。在稳定化之前的最后阶段,其性能是一个主要的阻碍。但什么是“trait 解析器”?为什么它的性能会成为一个问题?我写这篇博文的目标是以一种无需任何先验知识的方式回答这些问题。我认为关于 trait 解析器的讨论很快就会变得非常复杂且令人望而生畏。所以今天,我们将从最基础的内容开始!
This is a writeup of my eurorust 2026 talk of the same name, that I gave on October 15, 2026. And if you’d rather watch than read this talk, you can do so here on youtube: The new solver is the codebase I work on daily, and I’ve done so for about a year full-time. I’m not the main person driving its stabilization though, that would be lcnr , who’s worked on it for far longer than I’ve been around in the Rust project. My main focus has been optimizing the new solver’s performance. Many people have worked avery hard, for years, to make the new solver a reality. You can find some of them here, and here. My work here is only possible because of Hexcat and RustNL paying me and others to work on rust compiler development. Consider whether you or your company could sponsor Hexcat or the RustNL maintainer team.
这是我于 2026 年 10 月 15 日发表的同名 Eurorust 2026 演讲的文字稿。如果你更愿意观看演讲而不是阅读,可以在 YouTube 上观看:这个新的解析器是我每天都在使用的代码库,我已经全职从事这项工作大约一年了。不过,我并不是推动其稳定化的主要负责人,那是 lcnr,他在 Rust 项目中工作的时间比我长得多。我的主要工作重点是优化新解析器的性能。许多人多年来付出了巨大的努力,才使新的解析器成为现实。你可以在这里和这里找到他们中的一些人。我的工作之所以能够实现,是因为 Hexcat 和 RustNL 资助我和其他人进行 Rust 编译器开发。请考虑你或你的公司是否可以赞助 Hexcat 或 RustNL 维护团队。
History of the new solver
新解析器的历史
On the 15th of May, 2015, Rust reached a major milestone. The very first stable version of rust was released. Version 1.0.0. Just 72 days later, on the 24th of July, work started on a new trait solver. Or well, that’s when the Chalk project got its first commit. It seems the initial goal was to formalize the trait system so we could prove its correctness. One reason that didn’t work well is because the trait system wasn’t very correct at all. Chalk is, according to its github description, “An implementation and definition of the Rust trait system using a PROLOG-like logic solver”. The fact that it’s “PROLOG-like” we’ll come back to in a few paragraphs, it’s quite interesting! However, Chalk isn’t around anymore. Its README.md says it has been “sunset in favor of the newer next-generation trait solver”.
2015 年 5 月 15 日,Rust 达到了一个重要的里程碑。Rust 的第一个稳定版本 1.0.0 发布了。仅仅 72 天后,即 7 月 24 日,一项关于新 trait 解析器的工作就开始了。或者说,那是 Chalk 项目获得第一次提交的时间。最初的目标似乎是将 trait 系统形式化,以便我们能够证明其正确性。它进展不顺利的原因之一是当时的 trait 系统本身并不太正确。根据其 GitHub 描述,Chalk 是“使用类似 PROLOG 的逻辑解析器对 Rust trait 系统进行的实现和定义”。关于它“类似 PROLOG”这一点,我们稍后会详细讨论,这非常有趣!然而,Chalk 已经不再存在了。其 README.md 显示它已被“弃用,转而支持更新的下一代 trait 解析器”。
In October 2022, this project was officially started to turn the lessons learned from Chalk, into an actual new subsystem of the rust compiler, that, once finished, could replace the code of the old trait solver. The goal of the new trait solver is to make sure we wouldn’t have as many correctness bugs in the trait system. We now call this project “project zero”, referring to the number of soundness bugs we’d ideally like to have in our type system. There have been several very long standing bugs in Rust’s (old) trait solver. Like this one, that has been open since May 28, 2015 (just 13 days after Rust 1.0!). It abuses something called “implied bounds” to convince the Rust compiler to turn a short lifetime ‘a into a ‘static lifetime, allowing you to use a reference after its already freed. Some of these bugs were fundamentally impossible to fix with the way the old trait solver worked. Note: Chalk and the new trait solver aren’t the same thing. Chalk came first, and learned a lot of lessons incorporated in the new solver. But what is a trait system? And how does a trait solver solve it?
2022 年 10 月,该项目正式启动,旨在将从 Chalk 中汲取的经验转化为 Rust 编译器的一个实际的新子系统,一旦完成,它就可以取代旧 trait 解析器的代码。新 trait 解析器的目标是确保我们在 trait 系统中不会出现那么多的正确性错误。我们现在将此项目称为“零号项目”(project zero),指的是我们理想中类型系统应有的健全性错误数量。Rust 的(旧)trait 解析器中存在几个长期未解决的错误。比如这个错误,自 2015 年 5 月 28 日(仅在 Rust 1.0 发布 13 天后!)以来一直处于开启状态。它滥用了一种称为“隐含边界”(implied bounds)的机制,诱导 Rust 编译器将短生命周期 ‘a 转换为 ‘static 生命周期,从而允许你在引用被释放后继续使用它。其中一些错误在旧 trait 解析器的工作方式下是根本无法修复的。注意:Chalk 和新的 trait 解析器不是一回事。Chalk 出现得更早,并吸取了许多经验教训,这些经验被整合到了新的解析器中。那么,什么是 trait 系统?trait 解析器又是如何解决它的呢?
What is a trait solver?
什么是 trait 解析器?
The first place to look for documentation about the Rust compiler is the “rustc dev guide”. It says “The goal of the trait system is to check whether a given trait bound is satisfied. Most notably when typechecking the body of - potentially generic - functions.”. But I promised I’d start at absolute zero, so what’s a trait? Some types act in similar ways. They share certain properties. For example, a lot of different types can be cloned, or copared for equality. To codify these properties, we can use traits. In other words, a trait is a property of a type.
查找有关 Rust 编译器文档的第一个地方是“rustc 开发指南”。它写道:“trait 系统的目标是检查给定的 trait 边界是否满足。最显著的应用是在对(可能是泛型的)函数体进行类型检查时。”但我承诺过要从零开始,那么什么是 trait?有些类型以相似的方式运行。它们共享某些属性。例如,许多不同的类型可以被克隆,或者进行相等性比较。为了规范这些属性,我们可以使用 trait。换句话说,trait 是类型的一种属性。
// Declare that a property called “Clone” exists.
trait Clone {
// …
}
// Declares that the type u64 has this property
impl Clone for u64 {
// …
}
// 声明存在一个名为 “Clone” 的属性。
trait Clone {
// …
}
// 声明类型 u64 具有此属性
impl Clone for u64 {
// …
}
Notably, you can associate behaviors with these properties. The standard library’s definition of Clone also includes two methods that can be called to perform this cloning operation. To check whether the code you wrote is correct, the Rust compiler sometimes has to check that these properties hold. If you’re trying to clone a value, the compiler has to check whether that value can be cloned.
值得注意的是,你可以将行为与这些属性关联起来。标准库对 Clone 的定义还包括两个可以调用以执行此克隆操作的方法。为了检查你编写的代码是否正确,Rust 编译器有时必须检查这些属性是否成立。如果你试图克隆一个值,编译器必须检查该值是否可以被克隆。
Prolog
Prolog
Prolog is a cool programming language that works and looks very differently to what you may be used to. However, it is just as powerful (i.e. turing complete) as any other. The way it works has strong parallels to our trait system, let me show you. Here’s an example:
Prolog 是一种很酷的编程语言,它的工作方式和外观与你可能习惯的语言非常不同。然而,它和其他语言一样强大(即图灵完备)。它的工作方式与我们的 trait 系统有很强的相似性,让我演示一下。这是一个例子:
Copy(u64). Clone(String). Clone(x) :- Copy(x). ?- Clone(String) ?- Clone(u64)
Copy(u64). Clone(String). Clone(x) :- Copy(x). ?- Clone(String) ?- Clone(u64)
A Prolog program consists of a set of statements, called “facts”, as well as relationships between facts. In the above program, there are two facts: u64 has the property Copy, String has the property Clone, And there’s also a relationship between these two properties. If some variable x has the Copy property, it also has the Clone property. Finally, we can ask the prolog program some questions. For example, Does the type u64 have the Clone property? To answer that question, the runtime of a prolog program has to analyze all the known facts and relations. In this program we haven’t said that u64 has the Clone property. However, we have said that it has the Copy property. And by using the relationship between Copy and Clone, the solver can derive that both questions (starting with ?-) are true!
Prolog 程序由一组称为“事实”的语句以及事实之间的关系组成。在上面的程序中,有两个事实:u64 具有 Copy 属性,String 具有 Clone 属性。此外,这两个属性之间还存在一种关系:如果某个变量 x 具有 Copy 属性,那么它也具有 Clone 属性。最后,我们可以向 Prolog 程序提出一些问题。例如,类型 u64 是否具有 Clone 属性?为了回答这个问题,Prolog 程序的运行时必须分析所有已知的事实和关系。在这个程序中,我们没有直接说 u64 具有 Clone 属性。但是,我们说过它具有 Copy 属性。通过利用 Copy 和 Clone 之间的关系,解析器可以推导出这两个问题(以 ?- 开头)都是正确的!
The reason Chalk was using a “prolog-like” solver, is because as you can see, the trait system is extremely similar to a logic solver. Both “facts” and “relations” can be written using impl blocks. In fact, I can write the above program as a Rust program:
Chalk 使用“类似 Prolog”的解析器的原因在于,正如你所见,trait 系统与逻辑解析器极其相似。“事实”和“关系”都可以使用 impl 块来编写。事实上,我可以将上面的程序写成 Rust 程序:
struct u64; struct String; trait Copy {} trait Clone {} impl Copy for u64 {} impl Clone for String {}
struct u64; struct String; trait Copy {} trait Clone {} impl Copy for u64 {} impl Clone for String {}