Skip to content

Commit 3f94afe

Browse files
committed
Stabilize const_fn_transmute
1 parent 82c5814 commit 3f94afe

File tree

7 files changed

+12
-11
lines changed

7 files changed

+12
-11
lines changed

core/src/intrinsics.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,9 @@ extern "rust-intrinsic" {
911911
/// cause [undefined behavior][ub] with this function. `transmute` should be
912912
/// the absolute last resort.
913913
///
914+
/// Transmuting pointers to integers in a `const` context is [undefined behavior][ub].
915+
/// Any attempt to use the resulting value for integer operations will abort const-evaluation.
916+
///
914917
/// The [nomicon](../../nomicon/transmutes.html) has additional
915918
/// documentation.
916919
///
@@ -1128,8 +1131,6 @@ extern "rust-intrinsic" {
11281131
/// }
11291132
/// ```
11301133
#[stable(feature = "rust1", since = "1.0.0")]
1131-
// NOTE: While this makes the intrinsic const stable, we have some custom code in const fn
1132-
// checks that prevent its use within `const fn`.
11331134
#[rustc_const_stable(feature = "const_transmute", since = "1.46.0")]
11341135
#[rustc_diagnostic_item = "transmute"]
11351136
pub fn transmute<T, U>(e: T) -> U;

core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
#![feature(rtm_target_feature)]
160160
#![feature(f16c_target_feature)]
161161
#![feature(hexagon_target_feature)]
162-
#![feature(const_fn_transmute)]
162+
#![cfg_attr(bootstrap, feature(const_fn_transmute))]
163163
#![feature(abi_unadjusted)]
164164
#![feature(adx_target_feature)]
165165
#![feature(associated_type_bounds)]

core/src/num/int_macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,7 +2096,7 @@ macro_rules! int_impl {
20962096
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
20972097
// SAFETY: const sound because integers are plain old datatypes so we can always
20982098
// transmute them to arrays of bytes
2099-
#[rustc_allow_const_fn_unstable(const_fn_transmute)]
2099+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_fn_transmute))]
21002100
#[inline]
21012101
pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] {
21022102
// SAFETY: integers are plain old datatypes so we can always transmute them to
@@ -2202,7 +2202,7 @@ macro_rules! int_impl {
22022202
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
22032203
// SAFETY: const sound because integers are plain old datatypes so we can always
22042204
// transmute to them
2205-
#[rustc_allow_const_fn_unstable(const_fn_transmute)]
2205+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_fn_transmute))]
22062206
#[inline]
22072207
pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
22082208
// SAFETY: integers are plain old datatypes so we can always transmute to them

core/src/num/uint_macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,7 +1926,7 @@ macro_rules! uint_impl {
19261926
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
19271927
// SAFETY: const sound because integers are plain old datatypes so we can always
19281928
// transmute them to arrays of bytes
1929-
#[rustc_allow_const_fn_unstable(const_fn_transmute)]
1929+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_fn_transmute))]
19301930
#[inline]
19311931
pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] {
19321932
// SAFETY: integers are plain old datatypes so we can always transmute them to
@@ -2032,7 +2032,7 @@ macro_rules! uint_impl {
20322032
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
20332033
// SAFETY: const sound because integers are plain old datatypes so we can always
20342034
// transmute to them
2035-
#[rustc_allow_const_fn_unstable(const_fn_transmute)]
2035+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_fn_transmute))]
20362036
#[inline]
20372037
pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
20382038
// SAFETY: integers are plain old datatypes so we can always transmute to them

core/src/str/converts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ pub fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> {
157157
#[inline]
158158
#[stable(feature = "rust1", since = "1.0.0")]
159159
#[rustc_const_stable(feature = "const_str_from_utf8_unchecked", since = "1.55.0")]
160-
#[rustc_allow_const_fn_unstable(const_fn_transmute)]
160+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_fn_transmute))]
161161
pub const unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
162162
// SAFETY: the caller must guarantee that the bytes `v` are valid UTF-8.
163163
// Also relies on `&str` and `&[u8]` having the same layout.

core/src/str/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ impl str {
231231
#[rustc_const_stable(feature = "str_as_bytes", since = "1.39.0")]
232232
#[inline(always)]
233233
#[allow(unused_attributes)]
234-
#[rustc_allow_const_fn_unstable(const_fn_transmute)]
234+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_fn_transmute))]
235235
pub const fn as_bytes(&self) -> &[u8] {
236236
// SAFETY: const sound because we transmute two types with the same layout
237237
unsafe { mem::transmute(self) }

std/src/net/ip.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,7 @@ impl Ipv6Addr {
10871087
///
10881088
/// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
10891089
/// ```
1090-
#[rustc_allow_const_fn_unstable(const_fn_transmute)]
1090+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_fn_transmute))]
10911091
#[rustc_const_stable(feature = "const_ipv6", since = "1.32.0")]
10921092
#[stable(feature = "rust1", since = "1.0.0")]
10931093
#[inline]
@@ -1149,7 +1149,7 @@ impl Ipv6Addr {
11491149
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).segments(),
11501150
/// [0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff]);
11511151
/// ```
1152-
#[rustc_allow_const_fn_unstable(const_fn_transmute)]
1152+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_fn_transmute))]
11531153
#[rustc_const_stable(feature = "const_ipv6", since = "1.50.0")]
11541154
#[stable(feature = "rust1", since = "1.0.0")]
11551155
#[inline]

0 commit comments

Comments
 (0)