Skip to content

Nonsensical error message with const generics and std::mem::sized_of #62645

Closed
@Aaron1011

Description

@Aaron1011

The following code:

#![feature(const_generics)]

trait IsZst {
    type ZST;
}

impl<T: Sized> IsZst for T where T: Sized {
    type ZST = ZstHelper<{std::mem::size_of::<T>() == 0}>;
}

struct ZstHelper<const T: bool>;

produces the following error:

error[E0277]: the size for values of type `T` cannot be known at compilation time
 --> src/lib.rs:8:27
  |
8 |     type ZST = ZstHelper<{std::mem::size_of::<T>() == 0}>;
  |                           ^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `T`
  = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
  = help: consider adding a `where T: std::marker::Sized` bound
  = note: required by `std::mem::size_of`

despite the fact that we have that T: Sized. If there's another reason why this code shouldn't compile, the error message should clearly state it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-genericsArea: const generics (parameters and arguments)A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.F-const_generics`#![feature(const_generics)]`F-on_unimplementedError messages that can be tackled with `#[rustc_on_unimplemented]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions