Why must pointer trait bounds be specified redundantly?

Viewed 94

Consider the following situation, with two Traits. The first Trait is for pointer types, and the second Trait is for regular objects. The second trait has a HRTB so that any reference to a type that implements the second trait, must implement the first trait:

pub trait PtrTrait {}

pub trait RegTrait
where
    for<'a> &'a Self: PtrTrait,
{
}

Yes, this actually works! In the following, the compiler would complain if the first generic impl were not included:

pub struct S();

// Note, this impl IS needed for the next impl to compile.
impl<'a> PtrTrait for &'a S {}

impl RegTrait for S {}

The problem is that the compiler doesn't seem to remember about this when writing a generic function. The following function definition doesn't compile:

pub fn bar<T: RegTrait>() {}

The error from rustc simply says that the trait bound for<'a> &'a T: PtrTrait is not satisfied for the type &'a T. If I add a where clause to the function:

where for<'a> &'a T: PtrTrait

then it works - but why should that be needed if it's already a constraint on the trait itself?

playground

0 Answers
Related