Implicit type generic causes "overflow evaluating the requirement [...]"

⚓ Rust    📅 2025-12-18    👤 surdeus    👁️ 8      

surdeus

Hi!
I have a really strange issue with a Rust trait.
This is a reduced version of my code: (see also in the playground)

mod disturbing {
    use std::marker::PhantomData;

    use crate::MyTrait;

    pub struct DisturbingStruct<A> {
        _marker: PhantomData<A>,
    }

    impl<'slf, A> MyTrait for &'slf DisturbingStruct<A> where &'slf A: MyTrait {}
}

pub trait MyTrait {}

pub struct ExampleStruct;

impl MyTrait for &ExampleStruct {}

fn test<'a, T: 'a>(_t: T)
where
    &'a T: MyTrait,
{
}

fn main() {
    test(ExampleStruct); // This line doesn't compile
    // test::<ExampleStruct>(ExampleStruct); // But this one does
}

This code doesn't compiles, except when the disturbing module is removed, or when the T generic of test is explicit.

The rest of the time, the compiler gives this error:

error[E0275]: overflow evaluating the requirement `&DisturbingStruct<_>: MyTrait`
  --> src/main.rs:28:5
   |
28 |     test(ExampleStruct); // This line doesn't compile
   |     ^^^^^^^^^^^^^^^^^^^
   |
   = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`playground`)
note: required for `&DisturbingStruct<DisturbingStruct<_>>` to implement `MyTrait`
  --> src/main.rs:12:19
   |
12 |     impl<'slf, A> MyTrait for &'slf DisturbingStruct<A> where &'slf A: MyTrait {}
   |                   ^^^^^^^     ^^^^^^^^^^^^^^^^^^^^^^^^^                ------- unsatisfied trait bound introduced here
   = note: 126 redundant requirements hidden
   = note: required for `&DisturbingStruct<DisturbingStruct<DisturbingStruct<...>>>` to implement `MyTrait`
note: required by a bound in `test`
  --> src/main.rs:23:12
   |
21 | fn test<'a, T: 'a>(_t: T)
   |    ---- required by a bound in this function
22 | where
23 |     &'a T: MyTrait,
   |            ^^^^^^^ required by this bound in `test`
   = note: the full name for the type has been written to '/playground/target/release/deps/playground-0d123c47591c0a56.long-type-988347796800678344.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 `playground` (bin "playground") due to 1 previous error

Can someone explain why this happens? I saw some similar topics on this forum, but couldn't find one on this particular issue.

2 posts - 1 participant

Read full topic

🏷️ Rust_feed