You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tracking Issue for using std::mem::transmute() in const fn (const_fn_transmute) #53605
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.
Reacted by Tiago de Paula, Jonas Platte and Brandon H. Gomes
@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).
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
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.
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.
Reacted by James Munns, Michael Bradshaw and Jonas Platte
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.
changed the title [-]Tracking Issue For Using std::mem::transmute() In Constants[/-][+]Tracking Issue for using std::mem::transmute() in const fn[/+]on Aug 7, 2020
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)[/+]on Aug 7, 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)
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.
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:
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.
Activity
Auto merge of #53535 - TheDarkula:master, r=oli-obk
mjbshaw commentedon 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::transmuteis still (super) unsafe, but at least it makes sure the types have the same size.TheDarkula commentedon Oct 2, 2018
@mjbshaw It's already been merged :)
mjbshaw commentedon 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 commentedon Oct 19, 2018
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:gives me
mgostIH commentedon 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
u8array without any indirection but I have no idea whether this is supposed to be supported or not in the design of const fn.MSxDOS commentedon Jun 22, 2019
unsafeinconst fnhas 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
... which makes
const_transmutepretty much useless for me.RalfJung commentedon Aug 29, 2019
Given that
unionfield access is already possible insideconst(but not insideconst fn), it seems somewhat silly to blocktransmute. All that does is make code less readable by pushing people to use unions instead of transmutes.So I propose we stabilize
transmuteinconst, but notconst fn. However, currently our handling of intrinsics in const-qualif is somewhat broken, so that should be fixed first.11 remaining items
[-]Tracking Issue For Using std::mem::transmute() In Constants[/-][+]Tracking Issue for using std::mem::transmute() in const fn[/+][-]Tracking Issue for using std::mem::transmute() in const fn[/-][+]Tracking Issue for using std::mem::transmute() in const fn (const_fn_transmute)[/+]Ekleog commentedon Dec 18, 2020
Is there any summary of what the current status is here to actually get
transmuteto beconstinconst fn?(Coming here because I'd have liked to make
Path::new()const, but AFAIU this requires either pointer deref or transmute to beconst, and transmute sounds easier to stabilize than pointer deref)RalfJung commentedon 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 commentedon Apr 2, 2021
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:RalfJung commentedon 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 commentedon Jun 18, 2021
So, rust-lang/rfcs#3016 was merged. @RalfJung, does that mean we can proceed with stabilization here?
benesch commentedon 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.
Introduce macro to create static UncasedStrs
Introduce macro to create static UncasedStrs
RalfJung commentedon Jun 18, 2021
The stabilization PR is already up. :)
#85769