rust-lang / rust-lang/rust

Next-generation trait solver enabled on nightly

Open
#160,895 8 comments 58 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-tracking-issue T-types WG-trait-system-refactor
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

The next-generation trait solver is currently enabled on nightly by default. See the corresponding blog post for more details. This is the largest single change to the Rust compiler since its initial stable release. It completely replaces the existing type system components responsible for proving where-clauses, normalizing associated types, and much more.

It is an internal component of rustc and the main benefits will come in the future as the removal of the old implementation will unblock features such a Type Alias Impl Trait and Return Type Notation, adding new implicit default trait bounds, e.g. Move and Forget, large future compile-time performance improvements and will enable us to fix the remaining type system unsoundnesses. Even so, there are already a lot of intended behavior changes. When developing on nightly, you may rely on subtle type inference or method and trait resolution changes which cause your project to not compile on the previous stable version.

This issue collects intended changes and their impact, both intended and any remaining bugs and issues. If you are affected by this change, please tell us about it. Either here, in a separate GitHub issue, or on zulip.


This is only being enabled on nightly. To disable the next-generation trait solver, you can pass -Znext-solver=coherence to rustc, use RUSTFLAGS=-Znext-solver=coherence, or change your project's .cargo/config.toml configuration file:

[build]
rustflags = ["-Znext-solver=coherence"]

We want to not only know about what changes due to the new implementation, but also how widespread its impact is. Please tell us if you're working on a project which is not on this list, even if the underlying issue is already known.

Known impact and breakage

Avoiding incorrect type inference when relating higher-ranked associated types

When relating higher-ranked types containing associated types, the old implementation sometimes incorrectly guided inference. See https://github.com/mitsuhiko/minijinja/pull/787 for an example of this intended breakage and how to fix it.

Correctly tracking the required recursion depth

The old implementation did not properly track the required recursion depth when using cache entries, causing it to accept programs whose required depth exceeded the recursion_limit. The next-generation trait solver tracks the depth required for cache entries and therefore causes some crates to now hit the recursion limit which previously did not. We have weakened these errors to a Future Compatibility Warning for now.

Trait solver cycles were sometimes treated as errors

Proving where-clauses can sometimes result in cycles. The way they are handled is quite involved depending on which bounds participate in a cycle. The old implementation incorrectly treats some cycles as an error even though these cycles should result in ambiguity. This can then be used to avoid ambiguity in method selection.

Handling of Return Position impl Trait (RPIT)

We have significantly changed the way Return Position impl Trait - but also the unstable Type Alias Impl Trait and Return Type Notation - are handled. This results in a lot of minor type inference changes and fixes, but also breaks some crates.

Most changes here are intended, one exception is that type inference involving associated types of a not-yet-defined RPIT is weaker than with the old solver. This is tracked and https://github.com/rust-lang/trait-system-refactor-initiative/issues/248 we'll fix it going forward.

#[feature(generic_const_exprs)]

This feature is not supported with the next-generation trait solver and is intended to be superseded by feature(generic_const_args) in the near future. For now, we're falling back to the currently stable -Znext-solver=coherence for crates which enable generic_const_exprs while emitting a warning. @rust-lang/project-const-generics will publish a separate blog post explaining its removal and replacement when we remove support for this feature.

Affected crates

When tracking the affected versions, we're using ❌ for versions which are not patched yet while ✔️ indicates that there is a minor version in which this breakage has been fixed. We're also using 🛠️ for breakage which we intend fix in the compiler itself. The required crate does not need any changes in this case.

Compilation errors
crate cause known affected versions
amaru-pure-stage overflow opaque type auto-trait checking #161658 main ✔️
bevy_ecs higher-ranked associated type 0.15 ✔️
commit_verify trait solver cycle handling 0.10-0.11
diskann-wide higher-ranked associated type 0.55
git-url-parse non dyn-compatible trait object 0.6
gizmo-core higher-ranked associated type 0.1-0.8
glaredb_core higher-ranked associated type 25
loess Assoc type of RPIT inference 0.2 🛠️
minijinja higher-ranked associated type 0.23-0.341 ✔️ 2 ✔️
sparsey higher-ranked associated type 0.13 ✔️
tera higher-ranked associated type 2 ✔️
wasmtime-wasi-http higher-ranked associated type 48.0.1 ✔️
Hangs
crate cause known affected versions
varisat exponential number of unconstrained regions 0.2.2 ✔️
Future compatibility warnings
crate cause known affected versions
generic-array recursion depth tracking 1 ✔️
wgpu recursion depth tracking 25-30
Unintended breakage

The following breakage was found since enabling the next-generation trait solver and should get fixed in the compiler itself:

The following have been fixed:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source files, tests, or entry points are named. Start by reading the known impact and affected-crates sections, then inspect the linked trait-system issues and reproduce an unlisted behavior change on nightly. Done means documenting a confirmed impact or filing a focused follow-up with a minimal reproduction.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.