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!
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.
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". 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?
What is a trait solver?
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.
// Declare that a property called "Clone" exists.
trait Clone {
// ...
}
// Declares that the type `u64` has this property
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.
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:
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!
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:
struct u64;
struct String;
trait Copy {}
trait Clone {}
impl Copy for u64 {}
impl Clone for String {}
impl<T> Clone for T where T: Copy {}
// To ask direct questions to the solver,
// we have to get a tiny bit creative:
fn foo() where String: Clone, u64: Clone {}
Neither the new solver or the old solver state as plainly as chalk does that they're using a "prolog-like solver". However, given the similarities, both the old solver and the new solver really aren't so different from how you'd implement a prolog runtime.
A note about type inference
Trait solving is not the same as type inference. This is a common misconception. Inside the Rust compiler, the two are completely different subsystems. Type inference is the process of replacing blanks with types.
Throughout this text I'll refer to some the following example code (with slight variations):
fn weird_add<T>(a: T, b: T) -> T where T: Add {
a + b + b
}
fn main() {
let a = 4;
let b = 5
println!("{}", weird_add(a, b))
}
We use type inference to determine that the type of the variable a is an integer. The same for b.
Having said that, sometimes during type inference we do have to ask things about traits, and will call into the trait solver. Furthermore, the trait solver sometimes "unifies type variables", which means that while Rust makes a query about traits, we might make some progress on figuring out what the types of some variables are.
Trait solving
In the example code, one place where the trait solver comes into effect is when we call weird_add. On the function there's a where bound. A where bound is a requirement that the Rust compiler must check before allowing a call to that function. In this case, Rust has to make sure that i32 (a type we found through type inference) implements the Add trait.
To do that, the solver can use one of a couple of strategies, and we'll look at each. In this case, we'll be looking for an impl block that mentions the i32 type, and we'll find one of those in the standard library!
impl Add for i32 {
type Output = i32;
// ...
}
Once the trait solver finds this impl block, it acts as an answer to the question "does i32 implement Add?". We somtimes refer to this answer as a a proof that the statement holds.
The example code contains several more calls to the trait solver. One you may not have thought about is that all local variables in Rust have to implement the Sized trait to be allowed on the stack. The Sized trait codifies that the compiler knows how many bytes of memory the value takes up, so it can be allocated on the stack.
So, there will also be a query to the trait solver asking "does i32 implement Sized?". The proof for this statement doesn't come from an impl block though. I did say that the compiler has several stratagies to derive proofs. In this case, the fact that some primitive types implement Sized is built-in to the compiler. i32 is inherently Sized, and there is no impl block anywhere that says so.
Answering Sized queries in general turns out to be quite hard because of the shear volume at which it occurs. To compile Rust's standard library, we'll ask "does type implement Sized?" about 800 000 times!
Additionally, answering it isn't always this simple. Let's take a slightly more involved example:
struct Kitten {
name: String,
cuteness: f64,
}
To prove Kitten: Sized, a recursive process takes place. Both fields have to implement Sized too. So in the process of answering this question, the trait solver will produce more questions to be answered. Kitten: Sized leads to String: Sized & f64: Sized. f64 luckily is trivial again, and built into the compiler. String isn't though, and we go one more layer deeper to Vec: Sized, etcetera.
Generic functions
In Rust, generic functions can be type checked standalone. The example using weird_add doesn't need a main function to be checked, and may emit its own type errors.
fn weird_add<T>(a: T, b: T) -> T where T: Add {
a + b + b
}
This is where the trait solver actually does the most work. For example, to type check weird_add, we have to prove that a + b is valid. a + b desugars to a.add(b), and when finding the method add the trait solver gets invoked, since the add method comes from the Add trait.
Previously, proof candidates came either from impl blocks or they were built into the compiler. Not here though! A new proof strategy is used here instead. When calling weird_add we saw that a where bound acts as a requirement that has to be checked before calling, but that means that inside weird_add we can assume that where bound holds and thus it acts as a proof strategy.
Concretely, we know a has the type T, and to answer the question "does T implement Add" we find a where bound that says so.
Add isn't the only trait bound that has to be proven inside weird_add. This is another place where we have to prove that T: Sized. Again we have to look for candidates, and there's no built in one for generic types T, nor is there an impl block, nor a where bound that mentions Sized. So how does that work?
Well, it turns out the where bound is implicit. T: Add really means T: Add + Sized, and so proving Sized works the same as proving Add.
fn weird_add<T>(a: T, b: T) -> T where T: Add /* Rust automatically adds */ + Sized {
a + b + b
}
On lobsters I got asked about whether the Add bound is required to add the Sized bound. The T: Sized implicit bound gets added for any generic. Even if there was not already an Add bound, T would still be Sized.
Currently, you can opt out of that implicit bound using T: ?Sized (or in the example, T: Add + ?Sized). In the future we may change the syntax for that to T: MetaSized. Look for the keyword "Sized Hierarchy" if you want to learn more. This document should also give you a primer.
Finally, this code will actually give a type error, because there's one more trait to prove. Because b gets mentioned twice, we have to clone or copy values of type T. The function signature really should have been:
fn weird_add<T>(a: T, b: T) -> T where T: Add + Copy /* Rust automatically adds */ + Sized {
a + b + b
}
To summarize, trait solving is the process of answering questions about relations between types and traits. These questions are submitted to the trait solver, and the trait solver looks for candidates in the form of:
implblocks,- built-in candiates,
- or where bounds.
When it finds one of these, we say we've found a proof, and if we cannot find one, a type error is emitted.
Multi-step trait solving
Sofar, all trait solving came down to finding a single candidate, but that's not always how it works. Let's look at the definition of the From and the Into traits:
impl<U> From<U> for u64 {
fn from(t: u32) -> u64 {
t as u64
}
}
impl<T, U> Into<U> for T where U: From<T>, {
fn into(self) -> U {
U::from(self)
}
}
If the trait solver gets a question like u32: Into (does u32 implement the Into trait), it will find the impl block above. However, the impl block is generic, and has a where bound that says "T implements Into U IF U implements From T". Luckily there's another impl block that gives that implementation and so the initial statement can be proven.
What this all means, is that while answering a question, the trait solver emits more questions. These subquestions get added to an internal queue in the trait solver, and this queue of questions will be proven one after the other. Like this, some questions can turn into several subquestions, which can even have their own subquestions again.
Also, in the example from above, the second question actually has two proofs! There are two impl blocks that apply, since there's also this blanket implementation for identity conversion:
impl<T> From<T> for T {
fn from(t: T) -> T {
t
}
}
Some preference takes place here. Since this is a blanket implementation, the other more specific one takes precedence. We can think about the trait solving about answering a tree of candidates that may apply. In this case, the single candidate for u32: Into turns into two, u64: From and T: From.
Something we'll not cover today, but this tree of candidates in reality is not a tree, it can have cy
Comments
No comments yet. Start the discussion.