Open
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