Consider this code snippet. It admittedly looks a little silly, but that's because it has been minimized from something much more complex.
use std::ops::Add;
struct Wrapper<T>(T);
impl<'a, T> Add<usize> for &'a Wrapper<T>
where
&'a T: Add<usize, Output = usize>,
{
type Output = usize;
fn add(self, x: usize) -> usize {
&self.0 + x
}
}
fn main() {
let _f = |x| &x + 42usize;
}
I would expect the build to fail with a clear type inference error, as there is no way to know what the type of the x argument of the _f closure is. Indeed both x: usize and x: Wrapper<usize> would work.
But instead, the build fails with an error that's only remotely related to the problem at hand as the trait solver reaches the recursion limit trying to determine if Add is implemented for infinitely nested &Wrapper<Wrapper<Wrapper<Wrapper<...>>>:
$ cargo +nightly check
error[E0275]: overflow evaluating the requirement `&Wrapper<_>: Add<usize>`
--> src/main.rs:16:21
|
16 | let _f = |x| &x + 42usize;
| ^
|
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`reproducer`)
note: required for `&Wrapper<Wrapper<_>>` to implement `Add<usize>`
--> src/main.rs:5:13
|
5 | impl<'a, T> Add<usize> for &'a Wrapper<T>
| ^^^^^^^^^^ ^^^^^^^^^^^^^^
6 | where
7 | &'a T: Add<usize, Output = usize>,
| -------------- unsatisfied trait bound introduced here
= note: 126 redundant requirements hidden
= note: required for `&Wrapper<Wrapper<Wrapper<Wrapper<Wrapper<Wrapper<Wrapper<Wrapper<_>>>>>>>>` to implement `Add<usize>`
= note: the full name for the type has been written to '/home/hadrien/Bureau/reproducer/target/debug/deps/reproducer-d546312e19ea6546.long-type-4775855609470034830.txt'
= note: consider using `--verbose` to print the full type name to the console
For more information about this error, try `rustc --explain E0275`.
error: could not compile `reproducer` (bin "reproducer") due to 1 previous error
I am not 100% sure why this happens, but my guess is that the type inference engine somehow tries all possible types for x and this does not end well here as the list of types that this generic impl might cover is infinite.
In any case, enabling the new trait solver leads to an error message that is much improved, as it now correctly explains that this is a closure argument type inference problem. But a secondary overflow error is still present, and it is even more mysterious now as there is no longer any hint of what is overflowing and why:
$ RUSTFLAGS='-Znext-solver' cargo +nightly check
Checking reproducer v0.1.0 (/home/hadrien/Bureau/reproducer)
error[E0282]: type annotations needed
--> src/main.rs:16:15
|
16 | let _f = |x| &x + 42usize;
| ^ -- type must be known at this point
|
help: consider giving this closure parameter an explicit type
|
16 | let _f = |x: /* Type */| &x + 42usize;
| ++++++++++++
error[E0275]: overflow evaluating the requirement `&_: Add<usize>`
--> src/main.rs:16:21
|
16 | let _f = |x| &x + 42usize;
| ^
Some errors have detailed explanations: E0275, E0282.
For more information about an error, try `rustc --explain E0275`.
error: could not compile `reproducer` (bin "reproducer") due to 2 previous errors
So overall, I think some work on error message ergonomics is still needed here.
Meta
rustc --version --verbose:
rustc 1.99.0-nightly (da86f4d07 2026-07-24)
(also present on 1.97 stable)
Consider this code snippet. It admittedly looks a little silly, but that's because it has been minimized from something much more complex.
I would expect the build to fail with a clear type inference error, as there is no way to know what the type of the
xargument of the_fclosure is. Indeed bothx: usizeandx: Wrapper<usize>would work.But instead, the build fails with an error that's only remotely related to the problem at hand as the trait solver reaches the recursion limit trying to determine if
Addis implemented for infinitely nested&Wrapper<Wrapper<Wrapper<Wrapper<...>>>:I am not 100% sure why this happens, but my guess is that the type inference engine somehow tries all possible types for
xand this does not end well here as the list of types that this generic impl might cover is infinite.In any case, enabling the new trait solver leads to an error message that is much improved, as it now correctly explains that this is a closure argument type inference problem. But a secondary overflow error is still present, and it is even more mysterious now as there is no longer any hint of what is overflowing and why:
So overall, I think some work on error message ergonomics is still needed here.
Meta
rustc --version --verbose:(also present on 1.97 stable)