Skip to content

Cannot transmute between ZST and zero-sized generic array #98104

Open
@lukas-code

Description

I tried this code: (Playground)

struct GenericZst<T> {
    a: [T; 0],
}

fn foo<T>() -> GenericZst<T> {
    unsafe { ::core::mem::transmute::<(), GenericZst<T>>(()) }
}

I expected this to compile, because [T; 0] is always zero-sized.

Instead, it fails to compile with this error:

error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
 --> src/lib.rs:6:14
  |
6 |     unsafe { ::core::mem::transmute::<(), GenericZst<T>>(()) }
  |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: source type: `()` (0 bits)
  = note: target type: `GenericZst<T>` (size can vary because of T)

For more information about this error, try `rustc --explain E0512`.
error: could not compile `playground` due to previous error

Meta

rustc --version --verbose:

rustc 1.63.0-nightly (ca122c7eb 2022-06-13)
binary: rustc
commit-hash: ca122c7ebb3ab50149c9d3d24ddb59c252b32272
commit-date: 2022-06-13
host: x86_64-unknown-linux-gnu
release: 1.63.0-nightly
LLVM version: 14.0.5

somewhat related: #98064

Metadata

Assignees

No one assigned

    Labels

    A-arrayArea: `[T; N]`C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions