Skip to content

Commit 9209493

Browse files
authored
Rollup merge of rust-lang#78208 - liketechnik:issue-69399, r=oli-obk
replace `#[allow_internal_unstable]` with `#[rustc_allow_const_fn_unstable]` for `const fn`s `#[allow_internal_unstable]` is currently used to side-step feature gate and stability checks. While it was originally only meant to be used only on macros, its use was expanded to `const fn`s. This pr adds stricter checks for the usage of `#[allow_internal_unstable]` (only on macros) and introduces the `#[rustc_allow_const_fn_unstable]` attribute for usage on `const fn`s. This pr does not change any of the functionality associated with the use of `#[allow_internal_unstable]` on macros or the usage of `#[rustc_allow_const_fn_unstable]` (instead of `#[allow_internal_unstable]`) on `const fn`s (see rust-lang#69399 (comment)). Note: The check for `#[rustc_allow_const_fn_unstable]` currently only validates that the attribute is used on a function, because I don't know how I would check if the function is a `const fn` at the place of the check. I therefore openend this as a 'draft pull request'. Closes rust-lang#69399 r? @oli-obk
2 parents b0b7c5d + 56c1bcf commit 9209493

File tree

14 files changed

+36
-16
lines changed

14 files changed

+36
-16
lines changed

alloc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#![allow(explicit_outlives_requirements)]
7373
#![allow(incomplete_features)]
7474
#![deny(unsafe_op_in_unsafe_fn)]
75+
#![cfg_attr(not(bootstrap), feature(rustc_allow_const_fn_unstable))]
7576
#![cfg_attr(not(test), feature(generator_trait))]
7677
#![cfg_attr(test, feature(test))]
7778
#![cfg_attr(test, feature(new_uninit))]

alloc/src/raw_vec.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ impl<T> RawVec<T, Global> {
150150
impl<T, A: AllocRef> RawVec<T, A> {
151151
/// Like `new`, but parameterized over the choice of allocator for
152152
/// the returned `RawVec`.
153-
#[allow_internal_unstable(const_fn)]
153+
#[cfg_attr(not(bootstrap), rustc_allow_const_fn_unstable(const_fn))]
154+
#[cfg_attr(bootstrap, allow_internal_unstable(const_fn))]
154155
pub const fn new_in(alloc: A) -> Self {
155156
// `cap: 0` means "unallocated". zero-sized types are ignored.
156157
Self { ptr: Unique::dangling(), cap: 0, alloc }

core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
#![warn(missing_debug_implementations)]
6464
#![allow(explicit_outlives_requirements)]
6565
#![allow(incomplete_features)]
66+
#![cfg_attr(not(bootstrap), feature(rustc_allow_const_fn_unstable))]
6667
#![feature(allow_internal_unstable)]
6768
#![feature(arbitrary_self_types)]
6869
#![feature(asm)]

core/src/num/int_macros.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,7 +2045,8 @@ assert_eq!(
20452045
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
20462046
// SAFETY: const sound because integers are plain old datatypes so we can always
20472047
// transmute them to arrays of bytes
2048-
#[allow_internal_unstable(const_fn_transmute)]
2048+
#[cfg_attr(not(bootstrap), rustc_allow_const_fn_unstable(const_fn_transmute))]
2049+
#[cfg_attr(bootstrap, allow_internal_unstable(const_fn_transmute))]
20492050
#[inline]
20502051
pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] {
20512052
// SAFETY: integers are plain old datatypes so we can always transmute them to
@@ -2193,7 +2194,8 @@ fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT),
21932194
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
21942195
// SAFETY: const sound because integers are plain old datatypes so we can always
21952196
// transmute to them
2196-
#[allow_internal_unstable(const_fn_transmute)]
2197+
#[cfg_attr(not(bootstrap), rustc_allow_const_fn_unstable(const_fn_transmute))]
2198+
#[cfg_attr(bootstrap, allow_internal_unstable(const_fn_transmute))]
21972199
#[inline]
21982200
pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
21992201
// SAFETY: integers are plain old datatypes so we can always transmute to them

core/src/num/uint_macros.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,7 +1803,8 @@ assert_eq!(
18031803
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
18041804
// SAFETY: const sound because integers are plain old datatypes so we can always
18051805
// transmute them to arrays of bytes
1806-
#[allow_internal_unstable(const_fn_transmute)]
1806+
#[cfg_attr(not(bootstrap), rustc_allow_const_fn_unstable(const_fn_transmute))]
1807+
#[cfg_attr(bootstrap, allow_internal_unstable(const_fn_transmute))]
18071808
#[inline]
18081809
pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] {
18091810
// SAFETY: integers are plain old datatypes so we can always transmute them to
@@ -1951,7 +1952,8 @@ fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT),
19511952
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
19521953
// SAFETY: const sound because integers are plain old datatypes so we can always
19531954
// transmute to them
1954-
#[allow_internal_unstable(const_fn_transmute)]
1955+
#[cfg_attr(not(bootstrap), rustc_allow_const_fn_unstable(const_fn_transmute))]
1956+
#[cfg_attr(bootstrap, allow_internal_unstable(const_fn_transmute))]
19551957
#[inline]
19561958
pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
19571959
// SAFETY: integers are plain old datatypes so we can always transmute to them

core/src/slice/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ impl<T> [T] {
8888
#[rustc_const_stable(feature = "const_slice_len", since = "1.32.0")]
8989
#[inline]
9090
// SAFETY: const sound because we transmute out the length field as a usize (which it must be)
91-
#[allow_internal_unstable(const_fn_union)]
91+
#[cfg_attr(not(bootstrap), rustc_allow_const_fn_unstable(const_fn_union))]
92+
#[cfg_attr(bootstrap, allow_internal_unstable(const_fn_union))]
9293
pub const fn len(&self) -> usize {
9394
// SAFETY: this is safe because `&[T]` and `FatPtr<T>` have the same layout.
9495
// Only `std` can make this guarantee.

core/src/str/converts.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ 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_unstable(feature = "const_str_from_utf8_unchecked", issue = "75196")]
160-
#[allow_internal_unstable(const_fn_transmute)]
160+
#[cfg_attr(not(bootstrap), rustc_allow_const_fn_unstable(const_fn_transmute))]
161+
#[cfg_attr(bootstrap, allow_internal_unstable(const_fn_transmute))]
161162
pub const unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
162163
// SAFETY: the caller must guarantee that the bytes `v` are valid UTF-8.
163164
// Also relies on `&str` and `&[u8]` having the same layout.

core/src/str/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ impl str {
219219
#[rustc_const_stable(feature = "str_as_bytes", since = "1.32.0")]
220220
#[inline(always)]
221221
#[allow(unused_attributes)]
222-
#[allow_internal_unstable(const_fn_transmute)]
222+
#[cfg_attr(not(bootstrap), rustc_allow_const_fn_unstable(const_fn_transmute))]
223+
#[cfg_attr(bootstrap, allow_internal_unstable(const_fn_transmute))]
223224
pub const fn as_bytes(&self) -> &[u8] {
224225
// SAFETY: const sound because we transmute two types with the same layout
225226
unsafe { mem::transmute(self) }

core/src/task/wake.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ impl RawWakerVTable {
130130
#[rustc_promotable]
131131
#[stable(feature = "futures_api", since = "1.36.0")]
132132
#[rustc_const_stable(feature = "futures_api", since = "1.36.0")]
133-
#[allow_internal_unstable(const_fn_fn_ptr_basics)]
133+
#[cfg_attr(not(bootstrap), rustc_allow_const_fn_unstable(const_fn_fn_ptr_basics))]
134+
#[cfg_attr(bootstrap, allow_internal_unstable(const_fn_fn_ptr_basics))]
134135
pub const fn new(
135136
clone: unsafe fn(*const ()) -> RawWaker,
136137
wake: unsafe fn(*const ()),

proc_macro/src/bridge/client.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ fn run_client<A: for<'a, 's> DecodeMut<'a, 's, ()>, R: Encode<()>>(
401401
}
402402

403403
impl Client<fn(crate::TokenStream) -> crate::TokenStream> {
404-
#[allow_internal_unstable(const_fn)]
404+
#[cfg_attr(not(bootstrap), rustc_allow_const_fn_unstable(const_fn))]
405+
#[cfg_attr(bootstrap, allow_internal_unstable(const_fn))]
405406
pub const fn expand1(f: fn(crate::TokenStream) -> crate::TokenStream) -> Self {
406407
extern "C" fn run(
407408
bridge: Bridge<'_>,
@@ -414,7 +415,8 @@ impl Client<fn(crate::TokenStream) -> crate::TokenStream> {
414415
}
415416

416417
impl Client<fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream> {
417-
#[allow_internal_unstable(const_fn)]
418+
#[cfg_attr(not(bootstrap), rustc_allow_const_fn_unstable(const_fn))]
419+
#[cfg_attr(bootstrap, allow_internal_unstable(const_fn))]
418420
pub const fn expand2(
419421
f: fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream,
420422
) -> Self {
@@ -459,7 +461,8 @@ impl ProcMacro {
459461
}
460462
}
461463

462-
#[allow_internal_unstable(const_fn)]
464+
#[cfg_attr(not(bootstrap), rustc_allow_const_fn_unstable(const_fn))]
465+
#[cfg_attr(bootstrap, allow_internal_unstable(const_fn))]
463466
pub const fn custom_derive(
464467
trait_name: &'static str,
465468
attributes: &'static [&'static str],
@@ -468,15 +471,17 @@ impl ProcMacro {
468471
ProcMacro::CustomDerive { trait_name, attributes, client: Client::expand1(expand) }
469472
}
470473

471-
#[allow_internal_unstable(const_fn)]
474+
#[cfg_attr(not(bootstrap), rustc_allow_const_fn_unstable(const_fn))]
475+
#[cfg_attr(bootstrap, allow_internal_unstable(const_fn))]
472476
pub const fn attr(
473477
name: &'static str,
474478
expand: fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream,
475479
) -> Self {
476480
ProcMacro::Attr { name, client: Client::expand2(expand) }
477481
}
478482

479-
#[allow_internal_unstable(const_fn)]
483+
#[cfg_attr(not(bootstrap), rustc_allow_const_fn_unstable(const_fn))]
484+
#[cfg_attr(bootstrap, allow_internal_unstable(const_fn))]
480485
pub const fn bang(
481486
name: &'static str,
482487
expand: fn(crate::TokenStream) -> crate::TokenStream,

0 commit comments

Comments
 (0)