Skip to content

Commit 99d57ee

Browse files
committed
core: Mark all safe intrinsics with #[rustc_safe_intrinsic]
1 parent 0ab2c91 commit 99d57ee

File tree

10 files changed

+51
-2
lines changed

10 files changed

+51
-2
lines changed

compiler/rustc_error_codes/src/error_codes/E0094.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Erroneous code example:
66
#![feature(intrinsics)]
77
88
extern "rust-intrinsic" {
9+
#[rustc_safe_intrinsic]
910
fn size_of<T, U>() -> usize; // error: intrinsic has wrong number
1011
// of type parameters
1112
}
@@ -19,6 +20,7 @@ Example:
1920
#![feature(intrinsics)]
2021
2122
extern "rust-intrinsic" {
23+
#[rustc_safe_intrinsic]
2224
fn size_of<T>() -> usize; // ok!
2325
}
2426
```

compiler/rustc_error_codes/src/error_codes/E0211.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ used. Erroneous code examples:
77
#![feature(intrinsics)]
88
99
extern "rust-intrinsic" {
10+
#[rustc_safe_intrinsic]
1011
fn size_of<T>(); // error: intrinsic has wrong type
1112
}
1213
@@ -42,6 +43,7 @@ For the first code example, please check the function definition. Example:
4243
#![feature(intrinsics)]
4344
4445
extern "rust-intrinsic" {
46+
#[rustc_safe_intrinsic]
4547
fn size_of<T>() -> usize; // ok!
4648
}
4749
```

library/core/src/intrinsics.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ extern "rust-intrinsic" {
788788
/// uninitialized at that point in the control flow.
789789
///
790790
/// This intrinsic should not be used outside of the compiler.
791+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
791792
pub fn rustc_peek<T>(_: T) -> T;
792793

793794
/// Aborts the execution of the process.
@@ -805,6 +806,7 @@ extern "rust-intrinsic" {
805806
/// On Unix, the
806807
/// process will probably terminate with a signal like `SIGABRT`, `SIGILL`, `SIGTRAP`, `SIGSEGV` or
807808
/// `SIGBUS`. The precise behaviour is not guaranteed and not stable.
809+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
808810
pub fn abort() -> !;
809811

810812
/// Informs the optimizer that this point in the code is not reachable,
@@ -843,6 +845,7 @@ extern "rust-intrinsic" {
843845
///
844846
/// This intrinsic does not have a stable counterpart.
845847
#[rustc_const_unstable(feature = "const_likely", issue = "none")]
848+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
846849
pub fn likely(b: bool) -> bool;
847850

848851
/// Hints to the compiler that branch condition is likely to be false.
@@ -857,6 +860,7 @@ extern "rust-intrinsic" {
857860
///
858861
/// This intrinsic does not have a stable counterpart.
859862
#[rustc_const_unstable(feature = "const_likely", issue = "none")]
863+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
860864
pub fn unlikely(b: bool) -> bool;
861865

862866
/// Executes a breakpoint trap, for inspection by a debugger.
@@ -876,6 +880,7 @@ extern "rust-intrinsic" {
876880
///
877881
/// The stabilized version of this intrinsic is [`core::mem::size_of`].
878882
#[rustc_const_stable(feature = "const_size_of", since = "1.40.0")]
883+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
879884
pub fn size_of<T>() -> usize;
880885

881886
/// The minimum alignment of a type.
@@ -887,6 +892,7 @@ extern "rust-intrinsic" {
887892
///
888893
/// The stabilized version of this intrinsic is [`core::mem::align_of`].
889894
#[rustc_const_stable(feature = "const_min_align_of", since = "1.40.0")]
895+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
890896
pub fn min_align_of<T>() -> usize;
891897
/// The preferred alignment of a type.
892898
///
@@ -915,6 +921,7 @@ extern "rust-intrinsic" {
915921
///
916922
/// The stabilized version of this intrinsic is [`core::any::type_name`].
917923
#[rustc_const_unstable(feature = "const_type_name", issue = "63084")]
924+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
918925
pub fn type_name<T: ?Sized>() -> &'static str;
919926

920927
/// Gets an identifier which is globally unique to the specified type. This
@@ -928,27 +935,31 @@ extern "rust-intrinsic" {
928935
///
929936
/// The stabilized version of this intrinsic is [`core::any::TypeId::of`].
930937
#[rustc_const_unstable(feature = "const_type_id", issue = "77125")]
938+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
931939
pub fn type_id<T: ?Sized + 'static>() -> u64;
932940

933941
/// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:
934942
/// This will statically either panic, or do nothing.
935943
///
936944
/// This intrinsic does not have a stable counterpart.
937945
#[rustc_const_stable(feature = "const_assert_type", since = "1.59.0")]
946+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
938947
pub fn assert_inhabited<T>();
939948

940949
/// A guard for unsafe functions that cannot ever be executed if `T` does not permit
941950
/// zero-initialization: This will statically either panic, or do nothing.
942951
///
943952
/// This intrinsic does not have a stable counterpart.
944953
#[rustc_const_unstable(feature = "const_assert_type2", issue = "none")]
954+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
945955
pub fn assert_zero_valid<T>();
946956

947957
/// A guard for unsafe functions that cannot ever be executed if `T` has invalid
948958
/// bit patterns: This will statically either panic, or do nothing.
949959
///
950960
/// This intrinsic does not have a stable counterpart.
951961
#[rustc_const_unstable(feature = "const_assert_type2", issue = "none")]
962+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
952963
pub fn assert_uninit_valid<T>();
953964

954965
/// Gets a reference to a static `Location` indicating where it was called.
@@ -960,6 +971,7 @@ extern "rust-intrinsic" {
960971
///
961972
/// Consider using [`core::panic::Location::caller`] instead.
962973
#[rustc_const_unstable(feature = "const_caller_location", issue = "76156")]
974+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
963975
pub fn caller_location() -> &'static crate::panic::Location<'static>;
964976

965977
/// Moves a value out of scope without running drop glue.
@@ -972,6 +984,7 @@ extern "rust-intrinsic" {
972984
/// Therefore, implementations must not require the user to uphold
973985
/// any safety invariants.
974986
#[rustc_const_unstable(feature = "const_intrinsic_forget", issue = "none")]
987+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
975988
pub fn forget<T: ?Sized>(_: T);
976989

977990
/// Reinterprets the bits of a value of one type as another type.
@@ -1251,6 +1264,7 @@ extern "rust-intrinsic" {
12511264
///
12521265
/// The stabilized version of this intrinsic is [`mem::needs_drop`](crate::mem::needs_drop).
12531266
#[rustc_const_stable(feature = "const_needs_drop", since = "1.40.0")]
1267+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
12541268
pub fn needs_drop<T: ?Sized>() -> bool;
12551269

12561270
/// Calculates the offset from a pointer.
@@ -1295,6 +1309,7 @@ extern "rust-intrinsic" {
12951309
/// any safety invariants.
12961310
///
12971311
/// Consider using [`pointer::mask`] instead.
1312+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
12981313
pub fn ptr_mask<T>(ptr: *const T, mask: usize) -> *const T;
12991314

13001315
/// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
@@ -1486,6 +1501,7 @@ extern "rust-intrinsic" {
14861501
///
14871502
/// The stabilized version of this intrinsic is
14881503
/// [`f32::min`]
1504+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
14891505
pub fn minnumf32(x: f32, y: f32) -> f32;
14901506
/// Returns the minimum of two `f64` values.
14911507
///
@@ -1496,6 +1512,7 @@ extern "rust-intrinsic" {
14961512
///
14971513
/// The stabilized version of this intrinsic is
14981514
/// [`f64::min`]
1515+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
14991516
pub fn minnumf64(x: f64, y: f64) -> f64;
15001517
/// Returns the maximum of two `f32` values.
15011518
///
@@ -1506,6 +1523,7 @@ extern "rust-intrinsic" {
15061523
///
15071524
/// The stabilized version of this intrinsic is
15081525
/// [`f32::max`]
1526+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
15091527
pub fn maxnumf32(x: f32, y: f32) -> f32;
15101528
/// Returns the maximum of two `f64` values.
15111529
///
@@ -1516,6 +1534,7 @@ extern "rust-intrinsic" {
15161534
///
15171535
/// The stabilized version of this intrinsic is
15181536
/// [`f64::max`]
1537+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
15191538
pub fn maxnumf64(x: f64, y: f64) -> f64;
15201539

15211540
/// Copies the sign from `y` to `x` for `f32` values.
@@ -1636,6 +1655,7 @@ extern "rust-intrinsic" {
16361655
/// primitives via the `count_ones` method. For example,
16371656
/// [`u32::count_ones`]
16381657
#[rustc_const_stable(feature = "const_ctpop", since = "1.40.0")]
1658+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
16391659
pub fn ctpop<T: Copy>(x: T) -> T;
16401660

16411661
/// Returns the number of leading unset bits (zeroes) in an integer type `T`.
@@ -1673,6 +1693,7 @@ extern "rust-intrinsic" {
16731693
/// assert_eq!(num_leading, 16);
16741694
/// ```
16751695
#[rustc_const_stable(feature = "const_ctlz", since = "1.40.0")]
1696+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
16761697
pub fn ctlz<T: Copy>(x: T) -> T;
16771698

16781699
/// Like `ctlz`, but extra-unsafe as it returns `undef` when
@@ -1729,6 +1750,7 @@ extern "rust-intrinsic" {
17291750
/// assert_eq!(num_trailing, 16);
17301751
/// ```
17311752
#[rustc_const_stable(feature = "const_cttz", since = "1.40.0")]
1753+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
17321754
pub fn cttz<T: Copy>(x: T) -> T;
17331755

17341756
/// Like `cttz`, but extra-unsafe as it returns `undef` when
@@ -1761,6 +1783,7 @@ extern "rust-intrinsic" {
17611783
/// primitives via the `swap_bytes` method. For example,
17621784
/// [`u32::swap_bytes`]
17631785
#[rustc_const_stable(feature = "const_bswap", since = "1.40.0")]
1786+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
17641787
pub fn bswap<T: Copy>(x: T) -> T;
17651788

17661789
/// Reverses the bits in an integer type `T`.
@@ -1774,6 +1797,7 @@ extern "rust-intrinsic" {
17741797
/// primitives via the `reverse_bits` method. For example,
17751798
/// [`u32::reverse_bits`]
17761799
#[rustc_const_stable(feature = "const_bitreverse", since = "1.40.0")]
1800+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
17771801
pub fn bitreverse<T: Copy>(x: T) -> T;
17781802

17791803
/// Performs checked integer addition.
@@ -1787,6 +1811,7 @@ extern "rust-intrinsic" {
17871811
/// primitives via the `overflowing_add` method. For example,
17881812
/// [`u32::overflowing_add`]
17891813
#[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1814+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
17901815
pub fn add_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
17911816

17921817
/// Performs checked integer subtraction
@@ -1800,6 +1825,7 @@ extern "rust-intrinsic" {
18001825
/// primitives via the `overflowing_sub` method. For example,
18011826
/// [`u32::overflowing_sub`]
18021827
#[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1828+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
18031829
pub fn sub_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
18041830

18051831
/// Performs checked integer multiplication
@@ -1813,6 +1839,7 @@ extern "rust-intrinsic" {
18131839
/// primitives via the `overflowing_mul` method. For example,
18141840
/// [`u32::overflowing_mul`]
18151841
#[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1842+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
18161843
pub fn mul_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
18171844

18181845
/// Performs an exact division, resulting in undefined behavior where
@@ -1887,6 +1914,7 @@ extern "rust-intrinsic" {
18871914
/// primitives via the `rotate_left` method. For example,
18881915
/// [`u32::rotate_left`]
18891916
#[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1917+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
18901918
pub fn rotate_left<T: Copy>(x: T, y: T) -> T;
18911919

18921920
/// Performs rotate right.
@@ -1900,6 +1928,7 @@ extern "rust-intrinsic" {
19001928
/// primitives via the `rotate_right` method. For example,
19011929
/// [`u32::rotate_right`]
19021930
#[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1931+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
19031932
pub fn rotate_right<T: Copy>(x: T, y: T) -> T;
19041933

19051934
/// Returns (a + b) mod 2<sup>N</sup>, where N is the width of T in bits.
@@ -1913,6 +1942,7 @@ extern "rust-intrinsic" {
19131942
/// primitives via the `wrapping_add` method. For example,
19141943
/// [`u32::wrapping_add`]
19151944
#[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1945+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
19161946
pub fn wrapping_add<T: Copy>(a: T, b: T) -> T;
19171947
/// Returns (a - b) mod 2<sup>N</sup>, where N is the width of T in bits.
19181948
///
@@ -1925,6 +1955,7 @@ extern "rust-intrinsic" {
19251955
/// primitives via the `wrapping_sub` method. For example,
19261956
/// [`u32::wrapping_sub`]
19271957
#[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1958+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
19281959
pub fn wrapping_sub<T: Copy>(a: T, b: T) -> T;
19291960
/// Returns (a * b) mod 2<sup>N</sup>, where N is the width of T in bits.
19301961
///
@@ -1937,6 +1968,7 @@ extern "rust-intrinsic" {
19371968
/// primitives via the `wrapping_mul` method. For example,
19381969
/// [`u32::wrapping_mul`]
19391970
#[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1971+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
19401972
pub fn wrapping_mul<T: Copy>(a: T, b: T) -> T;
19411973

19421974
/// Computes `a + b`, saturating at numeric bounds.
@@ -1950,6 +1982,7 @@ extern "rust-intrinsic" {
19501982
/// primitives via the `saturating_add` method. For example,
19511983
/// [`u32::saturating_add`]
19521984
#[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
1985+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
19531986
pub fn saturating_add<T: Copy>(a: T, b: T) -> T;
19541987
/// Computes `a - b`, saturating at numeric bounds.
19551988
///
@@ -1962,6 +1995,7 @@ extern "rust-intrinsic" {
19621995
/// primitives via the `saturating_sub` method. For example,
19631996
/// [`u32::saturating_sub`]
19641997
#[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
1998+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
19651999
pub fn saturating_sub<T: Copy>(a: T, b: T) -> T;
19662000

19672001
/// Returns the value of the discriminant for the variant in 'v';
@@ -1974,6 +2008,7 @@ extern "rust-intrinsic" {
19742008
///
19752009
/// The stabilized version of this intrinsic is [`core::mem::discriminant`].
19762010
#[rustc_const_unstable(feature = "const_discriminant", issue = "69821")]
2011+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
19772012
pub fn discriminant_value<T>(v: &T) -> <T as DiscriminantKind>::Discriminant;
19782013

19792014
/// Returns the number of variants of the type `T` cast to a `usize`;
@@ -1986,6 +2021,7 @@ extern "rust-intrinsic" {
19862021
///
19872022
/// The to-be-stabilized version of this intrinsic is [`mem::variant_count`].
19882023
#[rustc_const_unstable(feature = "variant_count", issue = "73662")]
2024+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
19892025
pub fn variant_count<T>() -> usize;
19902026

19912027
/// Rust's "try catch" construct which invokes the function pointer `try_fn`
@@ -2019,6 +2055,7 @@ extern "rust-intrinsic" {
20192055
/// Therefore, implementations must not require the user to uphold
20202056
/// any safety invariants.
20212057
#[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
2058+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
20222059
pub fn ptr_guaranteed_cmp<T>(ptr: *const T, other: *const T) -> u8;
20232060

20242061
/// Allocates a block of memory at compile time.
@@ -2069,6 +2106,7 @@ extern "rust-intrinsic" {
20692106
///
20702107
/// [`std::hint::black_box`]: crate::hint::black_box
20712108
#[rustc_const_unstable(feature = "const_black_box", issue = "none")]
2109+
#[cfg_attr(not(bootstrap), rustc_safe_intrinsic)]
20722110
pub fn black_box<T>(dummy: T) -> T;
20732111

20742112
/// `ptr` must point to a vtable.

src/test/ui/error-codes/E0094.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#![feature(intrinsics)]
2+
23
extern "rust-intrinsic" {
4+
#[rustc_safe_intrinsic]
35
fn size_of<T, U>() -> usize; //~ ERROR E0094
46
}
57

src/test/ui/error-codes/E0094.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0094]: intrinsic has wrong number of type parameters: found 2, expected 1
2-
--> $DIR/E0094.rs:3:15
2+
--> $DIR/E0094.rs:5:15
33
|
44
LL | fn size_of<T, U>() -> usize;
55
| ^^^^^^ expected 1 type parameter

src/test/ui/extern/extern-with-type-bounds.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
extern "rust-intrinsic" {
44
// Real example from libcore
5+
#[rustc_safe_intrinsic]
56
fn type_id<T: ?Sized + 'static>() -> u64;
67

78
// Silent bounds made explicit to make sure they are actually
@@ -10,6 +11,7 @@ extern "rust-intrinsic" {
1011

1112
// Bounds aren't checked right now, so this should work
1213
// even though it's incorrect.
14+
#[rustc_safe_intrinsic]
1315
fn size_of<T: Clone>() -> usize;
1416

1517
// Unresolved bounds should still error.

src/test/ui/extern/extern-with-type-bounds.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0405]: cannot find trait `NoSuchTrait` in this scope
2-
--> $DIR/extern-with-type-bounds.rs:16:20
2+
--> $DIR/extern-with-type-bounds.rs:18:20
33
|
44
LL | fn align_of<T: NoSuchTrait>() -> usize;
55
| ^^^^^^^^^^^ not found in this scope

src/test/ui/intrinsics/intrinsic-alignment.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
mod rusti {
77
extern "rust-intrinsic" {
88
pub fn pref_align_of<T>() -> usize;
9+
#[rustc_safe_intrinsic]
910
pub fn min_align_of<T>() -> usize;
1011
}
1112
}

src/test/ui/structs-enums/rec-align-u32.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::mem;
1010
mod rusti {
1111
extern "rust-intrinsic" {
1212
pub fn pref_align_of<T>() -> usize;
13+
#[rustc_safe_intrinsic]
1314
pub fn min_align_of<T>() -> usize;
1415
}
1516
}

src/test/ui/structs-enums/rec-align-u64.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::mem;
1212
mod rusti {
1313
extern "rust-intrinsic" {
1414
pub fn pref_align_of<T>() -> usize;
15+
#[rustc_safe_intrinsic]
1516
pub fn min_align_of<T>() -> usize;
1617
}
1718
}

0 commit comments

Comments
 (0)