Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transmute does not see the size of generic-over-constants types #82334

Closed
jonasmalacofilho opened this issue Feb 20, 2021 · 3 comments
Closed
Labels
C-bug Category: This is a bug.

Comments

@jonasmalacofilho
Copy link

jonasmalacofilho commented Feb 20, 2021

I tried this code:

pub fn noop<const N: usize>(src: [i32; N]) -> [i32; N] {
    unsafe { std::mem::transmute::<_, _>(src) }
}

I expected a successful compilation.

Instead, this happened:

error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
 --> src/lib.rs:2:14
  |
2 |     unsafe { std::mem::transmute::<_, _>(src) }
  |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `[i32; N]` does not have a fixed size

Meta

rustc --version --verbose:

rustc 1.52.0-nightly (a143517d4 2021-02-16)
binary: rustc
commit-hash: a143517d44cac50b20cbd3a0b579addab40dd399
commit-date: 2021-02-16
host: x86_64-unknown-linux-gnu
release: 1.52.0-nightly
LLVM version: 11.0.1

Note: this also reproduces with the current beta (1.51.0-beta.2) on the Rust Playground [link].

Backtrace

$ RUST_BACKTRACE=1 cargo build
   Compiling transmute_no_fixed_size_with_min_const_generics v0.1.0 (/home/jonas/Code/issues/rust/transmute_no_fixed_size_with_min_const_generics)
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
 --> src/lib.rs:2:14
  |
2 |     unsafe { std::mem::transmute::<_, _>(src) }
  |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `[i32; N]` does not have a fixed size

error: aborting due to previous error

For more information about this error, try `rustc --explain E0512`.
error: could not compile `transmute_no_fixed_size_with_min_const_generics`

To learn more, run the command again with --verbose.

@jonasmalacofilho jonasmalacofilho added the C-bug Category: This is a bug. label Feb 20, 2021
@the8472
Copy link
Member

the8472 commented Feb 20, 2021

Have you tried transmute_copy?

@Mark-Simulacrum
Copy link
Member

I think this is expected - transmute doesn't work with generics of any kind AFAIK. That said, the error could be better.

I think this is a duplicate of #61956, though. Thanks!

@jonasmalacofilho
Copy link
Author

jonasmalacofilho commented Feb 21, 2021

I think this is expected - transmute doesn't work with generics of any kind AFAIK. That said, the error could be better.

Oh, I hadn't come across this yet.

I think this is a duplicate of [...]

I also missed the other issue; apparently I made a typo somewhere when searching for existing issues.

Sorry about for noise, and thanks!


Have you tried transmute_copy?

I did and it compiles, since it doesn't check the sizes.

Still, thanks! : )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

3 participants