rust-lang / rust-lang/rust

Weird closure argument type inference error messages due to unexpected trait solver recursion

Open
#159,904 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug needs-triage
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

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)

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

Reproduce the minimized example in src/main.rs with cargo +nightly check, then compare the default trait solver output with RUSTFLAGS='-Znext-solver'. Trace the compiler's handling of the closure argument inference and trait-solver overflow diagnostics; done means the reported errors explain the inference problem without the misleading or unexplained overflow.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.