Skip to content

Commit 79a0584

Browse files
authored
Unrolled build for #127337
Rollup merge of #127337 - celinval:intrinsics-fallback, r=oli-obk Move a few intrinsics to Rust abi Move a few more intrinsic functions to the convention added in #121192. In the second commit, I added documentation about their safety requirements. Let me know if you would like me to move the second commit to a different PR. Note: I kept the same signature of `pref_align_of`, but I was wondering why this function is considered unsafe?
2 parents fcc325f + b096c08 commit 79a0584

3 files changed

+166
-63
lines changed

library/core/src/intrinsics.rs

+162-59
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,6 @@ extern "rust-intrinsic" {
947947
#[rustc_const_stable(feature = "const_unreachable_unchecked", since = "1.57.0")]
948948
#[rustc_nounwind]
949949
pub fn unreachable() -> !;
950-
951950
}
952951

953952
/// Informs the optimizer that a condition is always true.
@@ -1018,78 +1017,40 @@ extern "rust-intrinsic" {
10181017
#[rustc_nounwind]
10191018
pub fn breakpoint();
10201019

1021-
/// The size of a type in bytes.
1022-
///
1023-
/// Note that, unlike most intrinsics, this is safe to call;
1024-
/// it does not require an `unsafe` block.
1025-
/// Therefore, implementations must not require the user to uphold
1026-
/// any safety invariants.
1027-
///
1028-
/// More specifically, this is the offset in bytes between successive
1029-
/// items of the same type, including alignment padding.
1030-
///
1031-
/// The stabilized version of this intrinsic is [`core::mem::size_of`].
1020+
#[cfg(bootstrap)]
10321021
#[rustc_const_stable(feature = "const_size_of", since = "1.40.0")]
10331022
#[rustc_safe_intrinsic]
10341023
#[rustc_nounwind]
10351024
pub fn size_of<T>() -> usize;
10361025

1037-
/// The minimum alignment of a type.
1038-
///
1039-
/// Note that, unlike most intrinsics, this is safe to call;
1040-
/// it does not require an `unsafe` block.
1041-
/// Therefore, implementations must not require the user to uphold
1042-
/// any safety invariants.
1043-
///
1044-
/// The stabilized version of this intrinsic is [`core::mem::align_of`].
1026+
#[cfg(bootstrap)]
10451027
#[rustc_const_stable(feature = "const_min_align_of", since = "1.40.0")]
10461028
#[rustc_safe_intrinsic]
10471029
#[rustc_nounwind]
10481030
pub fn min_align_of<T>() -> usize;
1049-
/// The preferred alignment of a type.
1050-
///
1051-
/// This intrinsic does not have a stable counterpart.
1052-
/// It's "tracking issue" is [#91971](https://github.com/rust-lang/rust/issues/91971).
1031+
1032+
#[cfg(bootstrap)]
10531033
#[rustc_const_unstable(feature = "const_pref_align_of", issue = "91971")]
10541034
#[rustc_nounwind]
10551035
pub fn pref_align_of<T>() -> usize;
10561036

1057-
/// The size of the referenced value in bytes.
1058-
///
1059-
/// The stabilized version of this intrinsic is [`crate::mem::size_of_val`].
1037+
#[cfg(bootstrap)]
10601038
#[rustc_const_unstable(feature = "const_size_of_val", issue = "46571")]
10611039
#[rustc_nounwind]
10621040
pub fn size_of_val<T: ?Sized>(_: *const T) -> usize;
1063-
/// The required alignment of the referenced value.
1064-
///
1065-
/// The stabilized version of this intrinsic is [`core::mem::align_of_val`].
1041+
1042+
#[cfg(bootstrap)]
10661043
#[rustc_const_unstable(feature = "const_align_of_val", issue = "46571")]
10671044
#[rustc_nounwind]
10681045
pub fn min_align_of_val<T: ?Sized>(_: *const T) -> usize;
10691046

1070-
/// Gets a static string slice containing the name of a type.
1071-
///
1072-
/// Note that, unlike most intrinsics, this is safe to call;
1073-
/// it does not require an `unsafe` block.
1074-
/// Therefore, implementations must not require the user to uphold
1075-
/// any safety invariants.
1076-
///
1077-
/// The stabilized version of this intrinsic is [`core::any::type_name`].
1047+
#[cfg(bootstrap)]
10781048
#[rustc_const_unstable(feature = "const_type_name", issue = "63084")]
10791049
#[rustc_safe_intrinsic]
10801050
#[rustc_nounwind]
10811051
pub fn type_name<T: ?Sized>() -> &'static str;
10821052

1083-
/// Gets an identifier which is globally unique to the specified type. This
1084-
/// function will return the same value for a type regardless of whichever
1085-
/// crate it is invoked in.
1086-
///
1087-
/// Note that, unlike most intrinsics, this is safe to call;
1088-
/// it does not require an `unsafe` block.
1089-
/// Therefore, implementations must not require the user to uphold
1090-
/// any safety invariants.
1091-
///
1092-
/// The stabilized version of this intrinsic is [`core::any::TypeId::of`].
1053+
#[cfg(bootstrap)]
10931054
#[rustc_const_unstable(feature = "const_type_id", issue = "77125")]
10941055
#[rustc_safe_intrinsic]
10951056
#[rustc_nounwind]
@@ -2424,15 +2385,7 @@ extern "rust-intrinsic" {
24242385
#[rustc_nounwind]
24252386
pub fn discriminant_value<T>(v: &T) -> <T as DiscriminantKind>::Discriminant;
24262387

2427-
/// Returns the number of variants of the type `T` cast to a `usize`;
2428-
/// if `T` has no variants, returns `0`. Uninhabited variants will be counted.
2429-
///
2430-
/// Note that, unlike most intrinsics, this is safe to call;
2431-
/// it does not require an `unsafe` block.
2432-
/// Therefore, implementations must not require the user to uphold
2433-
/// any safety invariants.
2434-
///
2435-
/// The to-be-stabilized version of this intrinsic is [`crate::mem::variant_count`].
2388+
#[cfg(bootstrap)]
24362389
#[rustc_const_unstable(feature = "variant_count", issue = "73662")]
24372390
#[rustc_safe_intrinsic]
24382391
#[rustc_nounwind]
@@ -2773,8 +2726,11 @@ pub const unsafe fn const_deallocate(_ptr: *mut u8, _size: usize, _align: usize)
27732726
// Runtime NOP
27742727
}
27752728

2776-
/// `ptr` must point to a vtable.
27772729
/// The intrinsic will return the size stored in that vtable.
2730+
///
2731+
/// # Safety
2732+
///
2733+
/// `ptr` must point to a vtable.
27782734
#[rustc_nounwind]
27792735
#[unstable(feature = "core_intrinsics", issue = "none")]
27802736
#[rustc_intrinsic]
@@ -2783,8 +2739,11 @@ pub unsafe fn vtable_size(_ptr: *const ()) -> usize {
27832739
unreachable!()
27842740
}
27852741

2786-
/// `ptr` must point to a vtable.
27872742
/// The intrinsic will return the alignment stored in that vtable.
2743+
///
2744+
/// # Safety
2745+
///
2746+
/// `ptr` must point to a vtable.
27882747
#[rustc_nounwind]
27892748
#[unstable(feature = "core_intrinsics", issue = "none")]
27902749
#[rustc_intrinsic]
@@ -2793,6 +2752,150 @@ pub unsafe fn vtable_align(_ptr: *const ()) -> usize {
27932752
unreachable!()
27942753
}
27952754

2755+
/// The size of a type in bytes.
2756+
///
2757+
/// Note that, unlike most intrinsics, this is safe to call;
2758+
/// it does not require an `unsafe` block.
2759+
/// Therefore, implementations must not require the user to uphold
2760+
/// any safety invariants.
2761+
///
2762+
/// More specifically, this is the offset in bytes between successive
2763+
/// items of the same type, including alignment padding.
2764+
///
2765+
/// The stabilized version of this intrinsic is [`core::mem::size_of`].
2766+
#[rustc_nounwind]
2767+
#[unstable(feature = "core_intrinsics", issue = "none")]
2768+
#[rustc_const_stable(feature = "const_size_of", since = "1.40.0")]
2769+
#[rustc_intrinsic]
2770+
#[rustc_intrinsic_must_be_overridden]
2771+
#[cfg(not(bootstrap))]
2772+
pub const fn size_of<T>() -> usize {
2773+
unreachable!()
2774+
}
2775+
2776+
/// The minimum alignment of a type.
2777+
///
2778+
/// Note that, unlike most intrinsics, this is safe to call;
2779+
/// it does not require an `unsafe` block.
2780+
/// Therefore, implementations must not require the user to uphold
2781+
/// any safety invariants.
2782+
///
2783+
/// The stabilized version of this intrinsic is [`core::mem::align_of`].
2784+
#[rustc_nounwind]
2785+
#[unstable(feature = "core_intrinsics", issue = "none")]
2786+
#[rustc_const_stable(feature = "const_min_align_of", since = "1.40.0")]
2787+
#[rustc_intrinsic]
2788+
#[rustc_intrinsic_must_be_overridden]
2789+
#[cfg(not(bootstrap))]
2790+
pub const fn min_align_of<T>() -> usize {
2791+
unreachable!()
2792+
}
2793+
2794+
/// The preferred alignment of a type.
2795+
///
2796+
/// This intrinsic does not have a stable counterpart.
2797+
/// It's "tracking issue" is [#91971](https://github.com/rust-lang/rust/issues/91971).
2798+
#[rustc_nounwind]
2799+
#[unstable(feature = "core_intrinsics", issue = "none")]
2800+
#[rustc_const_unstable(feature = "const_pref_align_of", issue = "91971")]
2801+
#[rustc_intrinsic]
2802+
#[rustc_intrinsic_must_be_overridden]
2803+
#[cfg(not(bootstrap))]
2804+
pub const unsafe fn pref_align_of<T>() -> usize {
2805+
unreachable!()
2806+
}
2807+
2808+
/// Returns the number of variants of the type `T` cast to a `usize`;
2809+
/// if `T` has no variants, returns `0`. Uninhabited variants will be counted.
2810+
///
2811+
/// Note that, unlike most intrinsics, this is safe to call;
2812+
/// it does not require an `unsafe` block.
2813+
/// Therefore, implementations must not require the user to uphold
2814+
/// any safety invariants.
2815+
///
2816+
/// The to-be-stabilized version of this intrinsic is [`crate::mem::variant_count`].
2817+
#[rustc_nounwind]
2818+
#[unstable(feature = "core_intrinsics", issue = "none")]
2819+
#[rustc_const_unstable(feature = "variant_count", issue = "73662")]
2820+
#[rustc_intrinsic]
2821+
#[rustc_intrinsic_must_be_overridden]
2822+
#[cfg(not(bootstrap))]
2823+
pub const fn variant_count<T>() -> usize {
2824+
unreachable!()
2825+
}
2826+
2827+
/// The size of the referenced value in bytes.
2828+
///
2829+
/// The stabilized version of this intrinsic is [`crate::mem::size_of_val`].
2830+
///
2831+
/// # Safety
2832+
///
2833+
/// See [`crate::mem::size_of_val_raw`] for safety conditions.
2834+
#[rustc_nounwind]
2835+
#[unstable(feature = "core_intrinsics", issue = "none")]
2836+
#[rustc_const_unstable(feature = "const_size_of_val", issue = "46571")]
2837+
#[rustc_intrinsic]
2838+
#[rustc_intrinsic_must_be_overridden]
2839+
#[cfg(not(bootstrap))]
2840+
pub const unsafe fn size_of_val<T: ?Sized>(_ptr: *const T) -> usize {
2841+
unreachable!()
2842+
}
2843+
2844+
/// The required alignment of the referenced value.
2845+
///
2846+
/// The stabilized version of this intrinsic is [`core::mem::align_of_val`].
2847+
///
2848+
/// # Safety
2849+
///
2850+
/// See [`crate::mem::align_of_val_raw`] for safety conditions.
2851+
#[rustc_nounwind]
2852+
#[unstable(feature = "core_intrinsics", issue = "none")]
2853+
#[rustc_const_unstable(feature = "const_align_of_val", issue = "46571")]
2854+
#[rustc_intrinsic]
2855+
#[rustc_intrinsic_must_be_overridden]
2856+
#[cfg(not(bootstrap))]
2857+
pub const unsafe fn min_align_of_val<T: ?Sized>(_ptr: *const T) -> usize {
2858+
unreachable!()
2859+
}
2860+
2861+
/// Gets a static string slice containing the name of a type.
2862+
///
2863+
/// Note that, unlike most intrinsics, this is safe to call;
2864+
/// it does not require an `unsafe` block.
2865+
/// Therefore, implementations must not require the user to uphold
2866+
/// any safety invariants.
2867+
///
2868+
/// The stabilized version of this intrinsic is [`core::any::type_name`].
2869+
#[rustc_nounwind]
2870+
#[unstable(feature = "core_intrinsics", issue = "none")]
2871+
#[rustc_const_unstable(feature = "const_type_name", issue = "63084")]
2872+
#[rustc_intrinsic]
2873+
#[rustc_intrinsic_must_be_overridden]
2874+
#[cfg(not(bootstrap))]
2875+
pub const fn type_name<T: ?Sized>() -> &'static str {
2876+
unreachable!()
2877+
}
2878+
2879+
/// Gets an identifier which is globally unique to the specified type. This
2880+
/// function will return the same value for a type regardless of whichever
2881+
/// crate it is invoked in.
2882+
///
2883+
/// Note that, unlike most intrinsics, this is safe to call;
2884+
/// it does not require an `unsafe` block.
2885+
/// Therefore, implementations must not require the user to uphold
2886+
/// any safety invariants.
2887+
///
2888+
/// The stabilized version of this intrinsic is [`core::any::TypeId::of`].
2889+
#[rustc_nounwind]
2890+
#[unstable(feature = "core_intrinsics", issue = "none")]
2891+
#[rustc_const_unstable(feature = "const_type_id", issue = "77125")]
2892+
#[rustc_intrinsic]
2893+
#[rustc_intrinsic_must_be_overridden]
2894+
#[cfg(not(bootstrap))]
2895+
pub const fn type_id<T: ?Sized + 'static>() -> u128 {
2896+
unreachable!()
2897+
}
2898+
27962899
/// Lowers in MIR to `Rvalue::Aggregate` with `AggregateKind::RawPtr`.
27972900
///
27982901
/// This is used to implement functions like `slice::from_raw_parts_mut` and

tests/mir-opt/lower_intrinsics.non_const.LowerIntrinsics.panic-abort.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
fn non_const() -> usize {
55
let mut _0: usize;
6-
let _1: extern "rust-intrinsic" fn() -> usize {std::intrinsics::size_of::<T>};
7-
let mut _2: extern "rust-intrinsic" fn() -> usize {std::intrinsics::size_of::<T>};
6+
let _1: fn() -> usize {std::intrinsics::size_of::<T>};
7+
let mut _2: fn() -> usize {std::intrinsics::size_of::<T>};
88
scope 1 {
99
debug size_of_t => _1;
1010
}

tests/mir-opt/lower_intrinsics.non_const.LowerIntrinsics.panic-unwind.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
fn non_const() -> usize {
55
let mut _0: usize;
6-
let _1: extern "rust-intrinsic" fn() -> usize {std::intrinsics::size_of::<T>};
7-
let mut _2: extern "rust-intrinsic" fn() -> usize {std::intrinsics::size_of::<T>};
6+
let _1: fn() -> usize {std::intrinsics::size_of::<T>};
7+
let mut _2: fn() -> usize {std::intrinsics::size_of::<T>};
88
scope 1 {
99
debug size_of_t => _1;
1010
}

0 commit comments

Comments
 (0)