const trait bound not allowed in non-trait impl in rust

Viewed 233

I am experimenting with const trait bounds, and I have noticed that while it is possible to define a const trait bound on a const fn, it is not possible to do so on an impl (not an impl const Trait) where all the functions within the impl that rely on the const bound are themselves const.

For example, say I define

struct Foo<T>(T);

then this code compiles

const fn add<T: ~const Add>(a: Foo<T>, b: Foo<T>) -> Foo<T> {
    a + b
}

whereas this code produces the error "~const is not allowed here only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions"

impl<T: ~const Add> Foo<T> {
    const fn add(a: Foo<T>, b: Foo<T>) -> Foo<T> {
        a + b
    }
}

I understand that this is because at the moment I am not telling the compiler that every function that uses T in the impl will be const. Is there any way of providing this guarantee, without having to workaround by adding a where T: ~const Add to every function in the impl?

0 Answers
Related