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

Tracking Issue for using std::mem::transmute() in const fn (const_fn_transmute) #53605

Closed
TheDarkula opened this issue Aug 22, 2018 · 19 comments · Fixed by #85769
Closed

Tracking Issue for using std::mem::transmute() in const fn (const_fn_transmute) #53605

TheDarkula opened this issue Aug 22, 2018 · 19 comments · Fixed by #85769
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC Libs-Tracked Libs issues that are tracked on the team's project board. requires-nightly This issue requires a nightly compiler in some way. T-lang Relevant to the language team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@TheDarkula
Copy link
Contributor

TheDarkula commented Aug 22, 2018

Using std::mem::transmute() in constant functions is behind the const_transmute/const_fn_transmute feature gate.
(In constants, it is stable.)

Blocked on rust-lang/const-eval#14.

@mjbshaw
Copy link
Contributor

mjbshaw commented Oct 2, 2018

I, for one, would love this, because I've had to use the union transmute hack more times than I feel comfortable. std::mem::transmute is still (super) unsafe, but at least it makes sure the types have the same size.

@TheDarkula
Copy link
Contributor Author

@mjbshaw It's already been merged :)

@mjbshaw
Copy link
Contributor

mjbshaw commented Oct 2, 2018

@TheDarkula I know. I'm just trying to show support for eventually stabilizing this feature (rather than being removed because people are opposed to it).

@jamesmunns
Copy link
Member

Even with the flag, is this possible? I am getting an error stating that the use of unsafe functions are not allowed in const fn:

pub const unsafe fn rx_buffer_init() -> [BufferDescriptor; BUFFER_CT] {
    transmute::<
        [u8; size_of::<BufferDescriptor>() * BUFFER_CT],
        [BufferDescriptor; BUFFER_CT]
    >([0u8; size_of::<BufferDescriptor>() * BUFFER_CT])
}

gives me

error: call to unsafe function is unsafe and unsafe operations are not allowed in const fn
  --> foo/src/test.rs:18:5
   |
18 | /     transmute::<
19 | |         [u8; size_of::<BufferDescriptor>() * BUFFER_CT],
20 | |         [BufferDescriptor; BUFFER_CT]
21 | |     >([0u8; size_of::<BufferDescriptor>() * BUFFER_CT])
   | |____________________________________________________________^ call to unsafe function
   |
   = note: consult the function's documentation for information on how to avoid undefined behavior

@mgostIH
Copy link

mgostIH commented Mar 2, 2019

Is there some blocker on this or could this be starting to get stabilized? I'm mostly looking out for a way to define a function from a u8 array without any indirection but I have no idea whether this is supposed to be supported or not in the design of const fn.

@MSxDOS
Copy link

MSxDOS commented Jun 22, 2019

unsafe in const fn has been stabilized in 1.33. Is there anything else left preventing this from being stabilized as well?

EDIT: Ugh, in fact, there is. For example, usize -> function pointer throws

error[E0080]: it is undefined behavior to use this value
...
type validation failed: encountered 10220800, but expected a pointer
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior

... which makes const_transmute pretty much useless for me.

@Centril Centril added T-lang Relevant to the language team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) B-unstable Blocker: Implemented in the nightly compiler and unstable. requires-nightly This issue requires a nightly compiler in some way. labels Aug 29, 2019
@RalfJung
Copy link
Member

Given that union field access is already possible inside const (but not inside const fn), it seems somewhat silly to block transmute. All that does is make code less readable by pushing people to use unions instead of transmutes.

So I propose we stabilize transmute in const, but not const fn. However, currently our handling of intrinsics in const-qualif is somewhat broken, so that should be fixed first.

@RalfJung
Copy link
Member

RalfJung commented Sep 1, 2019

See #64011 for stabilizing transmute inside const but not const fn.

@WaffleLapkin
Copy link
Member

Is there any news about the stabilization?

@jplatte
Copy link
Contributor

jplatte commented Feb 26, 2020

@WaffleLapkin: @oli-obk reopened #64011 today because it got unblocked, then closed it again because apparently stabilizing transmute inside const but not const fn is no longer feasible.

So I think this is now blocked on union accesses in const fn being stabilized (#51909). I don't really understand the path forward for that one based on its comments.

@RalfJung
Copy link
Member

RalfJung commented Jun 2, 2020

Should we give this another try? Not much progress has been made on the unconst side of things (also see here), but allowing transmute where we already allow union could help get rid of a lot of nasty union-type-punning code (probably half of which is wrong because it forgets to add repr(C) to the union).

@oli-obk
Copy link
Contributor

oli-obk commented Jun 2, 2020

I'm creating a new stabilization PR to show the workarounds needed to allow such a split. We can then decide whether we'll take on that technical debt.

Manishearth added a commit to Manishearth/rust that referenced this issue Jul 10, 2020
Stabilize `transmute` in constants and statics but not const fn

cc rust-lang#53605 (leaving issue open so we can add `transmute` to `const fn` later)

Previous attempt: rust-lang#64011

r? @RalfJung

cc @rust-lang/wg-const-eval
Manishearth added a commit to Manishearth/rust that referenced this issue Jul 11, 2020
Stabilize `transmute` in constants and statics but not const fn

cc rust-lang#53605 (leaving issue open so we can add `transmute` to `const fn` later)

Previous attempt: rust-lang#64011

r? @RalfJung

cc @rust-lang/wg-const-eval
Manishearth added a commit to Manishearth/rust that referenced this issue Jul 11, 2020
Stabilize `transmute` in constants and statics but not const fn

cc rust-lang#53605 (leaving issue open so we can add `transmute` to `const fn` later)

Previous attempt: rust-lang#64011

r? @RalfJung

cc @rust-lang/wg-const-eval
Manishearth added a commit to Manishearth/rust that referenced this issue Jul 11, 2020
Stabilize `transmute` in constants and statics but not const fn

cc rust-lang#53605 (leaving issue open so we can add `transmute` to `const fn` later)

Previous attempt: rust-lang#64011

r? @RalfJung

cc @rust-lang/wg-const-eval
Manishearth added a commit to Manishearth/rust that referenced this issue Jul 11, 2020
Stabilize `transmute` in constants and statics but not const fn

cc rust-lang#53605 (leaving issue open so we can add `transmute` to `const fn` later)

Previous attempt: rust-lang#64011

r? @RalfJung

cc @rust-lang/wg-const-eval
flip1995 pushed a commit to flip1995/rust that referenced this issue Jul 14, 2020
Stabilize `transmute` in constants and statics but not const fn

cc rust-lang#53605 (leaving issue open so we can add `transmute` to `const fn` later)

Previous attempt: rust-lang#64011

r? @RalfJung

cc @rust-lang/wg-const-eval
@KodrAus KodrAus added the Libs-Tracked Libs issues that are tracked on the team's project board. label Jul 29, 2020
@oli-obk oli-obk changed the title Tracking Issue For Using std::mem::transmute() In Constants Tracking Issue for using std::mem::transmute() in const fn Aug 7, 2020
@RalfJung RalfJung changed the title Tracking Issue for using std::mem::transmute() in const fn Tracking Issue for using std::mem::transmute() in const fn (const_fn_transmute) Aug 7, 2020
@Ekleog
Copy link

Ekleog commented Dec 18, 2020

Is there any summary of what the current status is here to actually get transmute to be const in const fn?

(Coming here because I'd have liked to make Path::new() const, but AFAIU this requires either pointer deref or transmute to be const, and transmute sounds easier to stabilize than pointer deref)

@RalfJung
Copy link
Member

RalfJung commented Dec 19, 2020

Raw pointers and transmute are pretty much equal in terms of "what it takes to make them const-stable".

The next step is to figure out what we want to do with UB during CTFE, for which I recently proposed an RFC: rust-lang/rfcs#3016. The key PR needed to implement that RFC is up at #78407.

@programmerjake
Copy link
Member

I think it would be a good idea to split out into a separate feature that can be stabilized transmute of references to types that are repr(transparent) wrappers:

#[repr(transparent)]
pub struct MySlice(pub [u8]);

impl MySlice {
    pub const fn new(v: &[u8]) -> &MySlice {
        unsafe { std::mem::transmute(v) }
    }
}

@RalfJung
Copy link
Member

RalfJung commented Apr 2, 2021

I think once we find some kind of consensus on rust-lang/rfcs#3016 (which turned out to be much harder than I anticipated), there's not much blocking stabilization here.

@benesch
Copy link
Contributor

benesch commented Jun 18, 2021

So, rust-lang/rfcs#3016 was merged. @RalfJung, does that mean we can proceed with stabilization here?

@benesch
Copy link
Contributor

benesch commented Jun 18, 2021

It seems to me that the answer is probably yes, since #75196 has a stabilization PR out and was previously blocked on rust-lang/const-eval#14.

benesch added a commit to benesch/uncased that referenced this issue Jun 18, 2021
Add a static_uncased_str macro, which can create `&'static UncasedStr`s
from `&'static str`s. This won't be necessary once rust-lang/rust#53605
lands, but that's at least a few months away.
benesch added a commit to benesch/uncased that referenced this issue Jun 18, 2021
Add a static_uncased_str macro, which can create `&'static UncasedStr`s
from `&'static str`s. This won't be necessary once rust-lang/rust#53605
lands, but that's at least a few months away.
@RalfJung
Copy link
Member

The stabilization PR is already up. :)
#85769

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC Libs-Tracked Libs issues that are tracked on the team's project board. requires-nightly This issue requires a nightly compiler in some way. T-lang Relevant to the language team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.