Impossible to implement Drop if you use const generic expressions?

Playground link

Code:

#![allow(incomplete_features)]
#![allow(dead_code)]
#![feature(const_generics)]
#![feature(const_evaluatable_checked)]

struct HasArray<const N: usize> 
    where [i32; N / 2]: Sized // won't compile without this
{
    x: [i32; N / 2]
}

impl<const N: usize> Drop for HasArray<N>
    where [i32; N / 2]: Sized // won't compile with this, and without it compiler tells you to add it!
{
    fn drop(&mut self) {}
}

Error output:

   Compiling playground v0.0.1 (/playground)
error[E0367]: `Drop` impl requires `the constant `<HasArray<N> as Drop>::{constant#0}` can be evaluated` but the struct it is implemented for does not
  --> src/lib.rs:13:17
   |
13 |     where [i32; N / 2]: Sized
   |                 ^^^^^
   |
note: the implementor must specify the same requirement
  --> src/lib.rs:6:1
   |
6  | / struct HasArray<const N: usize> 
7  | |     where [i32; N / 2]: Sized // won't compile without this
8  | | {
9  | |     x: [i32; N / 2]
10 | | }
   | |_^

How I ran into this:

  • I implemented my own bitset type using const generics, which requires storing an array of [u64; N/8] for the bits.
  • Compiler yells at me I need to add where [u64; N / 8]: Sized which is a little strange because how could it not be? Fixes the error though.
  • I then tried to store this bitset type in another struct that has N as a constant generic parameter.
  • Compiler yells at me I need to add where [u64; N / 8]: Sized on my outer struct as well, fixes the error
  • I try to add a Drop impl on my outer struct
  • Compiler yells at me I need to add where [u64; N / 8]: Sized on my impl as well, causes new error, so I'm stuck
  • Google error, seems it's by design?

The code above is simplified to only have one struct type, but I wanted to give the fuller explanation because while it's unlikely you want to implement Drop for a bitset, it's not unreasonable to want it for arbitrary structs that might use bitsets.

I wasn't sure if I should post this on internals or users, but there is no forum on there specifically for unstable features.

Seems to be a bug. Not totally unexpected, since const generics are still an incomplete feature. There’s already an issue on this problem.

Can't implement Drop on type with const_evaluatable_checked where bound · Issue #79248 · rust-lang/rust · GitHub

Some details on why the where clause is needed in the first place, as it seems you’re unfamiliar with const_evaluatable_checked:

Const well-formedness and const equality - HackMD

Ah thanks, got the search terms wrong.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.