Zero-sized assert during type checking

I'm using the const generic trick to test if a type is zero-sized:

const fn check_zero_sized<T>() {
    if std::mem::size_of::<T>() != 0 {
        panic!("type is not zero-sized");

trait ZeroSized {
    const _CHECK: ();

impl<T> ZeroSized for T {
    // Can we add #[track_caller] here?
    const _CHECK: () = check_zero_sized::<T>();

fn test<T: ZeroSized>(_t: T) {
    // assert zero-sizedness
    let _ = T::_CHECK;

fn main() {

But unfortunately this will only trigger an error when running cargo build, but not on cargo check. This means that tools like rust-analyser also don't pick up the error. You are basically forced to cargo build/run all the time while writing code.

I'm wondering if there is a way to force the failure earlier to provide a bit of a better developer experience?

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.