Defining an unrelated type leads to recursion limit error

⚓ Rust    📅 2025-09-02    👤 surdeus    👁️ 3      

surdeus

I do not understand why defining the Add operator for AD (in the code below) leads to an overflow evaluting requirements in a statement that I think is in no way using AD:

/*
If I execute 'cargo run', I get:
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/package`
Hello World

If I execut 'cargo test', I get:
   Compiling package v0.1.0 (/home/bradbell/trash/rust/package)
error[E0275]: overflow evaluating the requirement `for<'a> &'a Simd<_, _>: Add`
  --> src/main.rs:65:24
   |
65 |         forward_0    : add_fun,
   |                        ^^^^^^^
   |
   = help: consider increasing the recursion limit by adding
*/
// -------------------------------------------------------------------------
// AD
pub struct AD<F> {
    pub value : F,
}
//
#[cfg(test)]
impl<F> std::ops::Add< &AD<F> > for &AD<F>
where
    for<'a> &'a F: std::ops::Add<&'a F, Output=F>,
    F    : Clone
{   type Output = AD<F>;
    //
    fn add(self , rhs : &AD<F> ) -> AD<F>
    {
        AD{ value : rhs.value.clone() }
    }
}
// AD is not used below here
// -------------------------------------------------------------------------
//
// LazyLock
use std::sync::LazyLock;
//
// FunType
pub type FunType<V> = fn(
    _var_zero : &mut Vec<V> ,
);
//
// add_fun
fn add_fun<V> ( var_zero : &mut Vec<V> )
where for<'a> &'a V : std::ops::Add< &'a V, Output = V>,
{
    var_zero[2] = &var_zero[0] + &var_zero[1];
}
//
// FunInfo
#[derive(Clone)]
pub struct FunInfo<V> {
    pub forward_0 : FunType<V>,
}
//
// fun_init
pub fn fun_init<V>() -> FunInfo<V>
where for<'a> &'a V : std::ops::Add< &'a V, Output = V>,
{
    FunInfo {
        forward_0    : add_fun,
    }
}
//
// GlobalData
pub trait GlobalData
where Self : Sized + 'static,
{
    fn get() -> &'static std::sync::LazyLock< FunInfo<Self> >;
}
impl GlobalData for f32 {
    fn get() -> &'static LazyLock< FunInfo<f32> > {
        pub static GLOBAL_DATA :
            LazyLock< FunInfo<f32> > = LazyLock::new( || fun_init() );
        &GLOBAL_DATA
    }
}
//
// main()
fn main() {
    println!("Hello World");
}

2 posts - 2 participants

Read full topic

🏷️ Rust_feed