Generic_const_exprs bounds where [(); R::ENTRIES]:

I had a struct containing a array of values

struct WithConstGeneric<const R:usize, T>
{
    values: [T; R]
}

And I thought it would be nice to use an associated constant instead

#![feature(generic_const_exprs)]

struct WithTrait<T, R: Resolution>
{
    values: [T; R::ENTRIES]
}

trait Resolution {
    const ENTRIES: usize;
}
struct Hour;
impl Resolution for Hour {
    const ENTRIES: usize = 24;
}

to my suprise this does not compile.
The error message helpfully points out how to make it work.

error: unconstrained generic constant
  --> src/main.rs:18:13
   |
18 |     values: [T; R::ENTRIES]
   |             ^^^^^^^^^^^^^^^
   |    help: try adding a `where` bound
   |
13 | struct WithTrait<T, R: Resolution> where [(); R::ENTRIES]:
   |    

I was again surprised that where bounds with nothing on the right of the : are even a thing but I guess it makes sense when the only requirement is that the type is valid.
It seems like it should not be needed given that struct WithConstGeneric<const R:usize, T> works without a where bound. The question is am I wrong and it is needed for some reason I failed to consider or can / will the need for this bound be removed in the future given that generic_const_exprs is explicitly marked as incomplete?

1 Like

generic_const_exprs is very early stage and highly experimental. You can read the design document here. It has the following to say about the current where bounds for const well-formedness:

We currently use where [(); expr]: as a way to add additional const wf bounds.
Once we have started experimenting with this it is probably worth it to add
a more intuitive way to add const wf bounds.

2 Likes

Thanks a lot the design document clears this up.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.