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

mem::transmute should be a const fn. #49450

Closed
gnzlbg opened this issue Mar 28, 2018 · 8 comments
Closed

mem::transmute should be a const fn. #49450

gnzlbg opened this issue Mar 28, 2018 · 8 comments
Labels
A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@gnzlbg
Copy link
Contributor

gnzlbg commented Mar 28, 2018

A common request in stdsimd is being able to initialize static variables with architecture-specific vector types.

There are many ways to hack this out, but AFAICt all of them basically reduce to using an union to implement a mem::transmute clone whose only different from mem::transmute is that it is a const fn.

Since this is already possible today, we should IMO just go ahead and make mem::transmute a const fn.

@frewsxcv frewsxcv added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Mar 28, 2018
@solson
Copy link
Member

solson commented Mar 28, 2018

The union-in-const transmute trick is actually already stable on nightly (without const fn, even), but only works on Copy types (which may work for SIMD, right?). Here's an example: https://play.rust-lang.org/?gist=c4c0b2c82ce182243d506b24af7d2e1c&version=nightly

@gnzlbg
Copy link
Contributor Author

gnzlbg commented Mar 28, 2018

@solson yes, that's what I meant with:

There are many ways to hack this out, but AFAICt all of them basically reduce to using an union to implement a mem::transmute clone whose only different from mem::transmute is that it is a const fn.

@hanna-kruppe
Copy link
Contributor

I'm ideologically opposed to the generality and special-ness of transmute anyway, so I'm not a big fan of extending it or making it more general in any way. For the SIMD use case specifically, I'd rather see safe reinterpreting methods/constructors, analogous to f32::{to,from}_bits.

@gnzlbg
Copy link
Contributor Author

gnzlbg commented Mar 28, 2018

For the SIMD use case specifically, I'd rather see safe reinterpreting methods/constructors, analogous to f32::{to,from}_bits.

These methods call transmute under the covers, and if transmute is not a const fn they basically have to re-implement their own transmute function that is.

@hanna-kruppe
Copy link
Contributor

Yes, and I am fine with that. It's an acceptable trade off for not presenting const fn transmute to users.

@gnzlbg
Copy link
Contributor Author

gnzlbg commented Mar 28, 2018

Yes, and I am fine with that. It's an acceptable trade off for not presenting const fn transmute to users.

The stdsimd crate currently offers FromBits/IntoBits traits that do what the f32/f64::from_bits functions do, but currently can't be const because trait methods can't be const (we could make these inherent methods by using from_{type}_bits or similar).

The current problem with this approach is that it requires O(N^2) implementations. A Compatible<T> approach like the one discussed here would be better, but it would still need const fn in trait methods.

@hanna-kruppe
Copy link
Contributor

But these are issues for any interface that is more specific than transmute, right? We'll definitely want such interfaces, so those problems needs solving in any case.

@XAMPPRocky XAMPPRocky added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-SIMD Area: SIMD (Single Instruction Multiple Data) A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. and removed A-SIMD Area: SIMD (Single Instruction Multiple Data) labels Jun 29, 2018
@oli-obk
Copy link
Contributor

oli-obk commented Oct 2, 2018

This has now been implemented behind a feature gate: #53535

The tracking issue is #53605 . Please raise any objections to the stabilization there.

My personal stance is that if we do not stabilize it, but allow unions in const fn, then users will just write

union Transmute<T, U> {
    t: T,
    u: U,
}
pub const unsafe fn my_hacky_transmute<T, U>(t: T) -> U {
    Transmute { t }.u
}

and everything will be worse with that than with transmute being const fn and having a bunch of type checks (size checks and lints about statically known bad transmutes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants