Skip to content

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

Closed

Description

@gnzlbg

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.

Activity

  1. solson commented on Mar 28, 2018

    @solson
    Contributor

    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

  2. gnzlbg commented on Mar 28, 2018

    @gnzlbg
    ContributorAuthor

    @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.

  3. hanna-kruppe commented on Mar 28, 2018

    @hanna-kruppe
    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.

  4. gnzlbg commented on Mar 28, 2018

    @gnzlbg
    ContributorAuthor

    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.

  5. hanna-kruppe commented on Mar 28, 2018

    @hanna-kruppe
    Contributor

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

  6. gnzlbg commented on Mar 28, 2018

    @gnzlbg
    ContributorAuthor

    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.

  7. hanna-kruppe commented on Mar 28, 2018

    @hanna-kruppe
    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.

  8. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    A-SIMDArea: SIMD (Single Instruction Multiple Data)
    and removed
    A-SIMDArea: SIMD (Single Instruction Multiple Data)
    on Jun 29, 2018
  9. oli-obk commented on Oct 2, 2018

    @oli-obk
    Contributor

    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)

  10. added
    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)
    on Dec 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-libs-api[DEPRECATED; DO NOT USE]

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions