Skip to content

Next-generation trait solver enabled on nightly #160895

Description

@lcnr

The next-solver is not yet enabled on nightly, but will be soon. This issue is currently still WIP to collect all the relevant changes and relevant information.

The the next-generation trait solver will be enabled on nightly by default. 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:

[target.x86_64-unknown-linux-gnu]
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 mitsuhiko/minijinja#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

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 rust-lang/trait-system-refactor-initiative#248 which we'll fix going forward.

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.

Compilation errors

crate cause known affected versions
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
minijinja higher-ranked associated type 0.23-0.341 ✔️ 2 ✔️
tera higher-ranked associated type 2 ✔️

Future compatibility warnings

crate cause known affected versions
generic-array recursion depth tracking 1
wgpu recursion depth tracking 25-30

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-typesRelevant to the types team, which will review and decide on the PR/issue.WG-trait-system-refactorThe Rustc Trait System Refactor Initiative (-Znext-solver)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions