rust-lang / rust-lang/rust

-Znext-solver overflow evaluating the requirement

Open
#137,973 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I tried this code:

use std::ops::Add;

impl<S, const LEN: usize> MyTrait for [S; LEN]
where
    N<LEN>: ToNum,
    S: ToNum<Num: Add<Helper<LEN>, Output: Num>>,
{
}

type Helper<const LEN: usize> = <N<LEN> as ToNum>::Num;

trait MyTrait {}

struct N<const N: usize>;

trait ToNum {
    type Num: Num;
}

trait Num: ToNum<Num = Self> {}

I expected to see this happen: On the current trait solver the above code compiles so it would be expected that the new trait solver also does.

Instead, this happened: The new trait solver reports an overflow attempting to evaluate the trait bound.

error[E0275]: overflow evaluating the requirement `[S; LEN]: MyTrait`
 --> <source>:3:39
  |
3 | impl<S, const LEN: usize> MyTrait for [S; LEN]
  |                                       ^^^^^^^^

error[E0275]: overflow evaluating the requirement `<S as ToNum>::Num: Add<<N<LEN> as ToNum>::Num>`
 --> <source>:6:19
  |
6 |     S: ToNum<Num: Add<Helper<LEN>, Output: Num>>,
  |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0275]: overflow evaluating the requirement `<<S as ToNum>::Num as Add<<N<LEN> as ToNum>::Num>>::Output: Num`
 --> <source>:6:44
  |
6 |     S: ToNum<Num: Add<Helper<LEN>, Output: Num>>,
  |                                            ^^^

error[E0275]: overflow evaluating the requirement `<N<LEN> as ToNum>::Num: Sized`
 --> <source>:6:19
  |
6 |     S: ToNum<Num: Add<Helper<LEN>, Output: Num>>,
  |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
note: required by an implicit `Sized` bound in `Add`
 --> /rustc/f4a216d28ee635afce685b4206e713579f66e130/library/core/src/ops/arith.rs:78:1

error[E0275]: overflow evaluating the requirement `<<S as ToNum>::Num as Add<<N<LEN> as ToNum>::Num>>::Output well-formed`
 --> <source>:6:44
  |
6 |     S: ToNum<Num: Add<Helper<LEN>, Output: Num>>,
  |                                            ^^^

I isolated this from a project of mine that has hit this a few times. The core issue seems to be the

trait A: B<A = Self> {}

pattern seen in the code above. However, the failure to evaluate the bound doesn't always trigger when using a trait of that form.

Meta

rustc --version --verbose:

rustc 1.87.0-nightly (f4a216d28 2025-03-02)
binary: rustc
commit-hash: f4a216d28ee635afce685b4206e713579f66e130
commit-date: 2025-03-02
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0

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

Start by compiling the minimal reproducer with the reported nightly toolchain and compare the current and new trait solvers. Read the trait-bound diagnostics and the referenced core/ops/arith.rs Add definition to understand the involved implicit bound. Done means the new solver no longer overflows for this pattern while preserving the expected compilation behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.