Skip to content

Commit

Permalink
intrinsics: deprecate calling them via the unstable std::intrinsics path
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Jan 4, 2025
1 parent 33cacf0 commit 53b3bec
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 36 deletions.
24 changes: 20 additions & 4 deletions library/core/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1909,7 +1909,11 @@ pub const fn forget<T: ?Sized>(_: T) {
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_allowed_through_unstable_modules]
#[cfg_attr(bootstrap, rustc_allowed_through_unstable_modules)]
#[cfg_attr(
not(bootstrap),
rustc_allowed_through_unstable_modules = "import this function via `std::mem` instead"
)]
#[rustc_const_stable(feature = "const_transmute", since = "1.56.0")]
#[rustc_diagnostic_item = "transmute"]
#[rustc_nounwind]
Expand Down Expand Up @@ -4353,7 +4357,11 @@ pub const fn ptr_metadata<P: ptr::Pointee<Metadata = M> + ?Sized, M>(_ptr: *cons
/// [`Vec::append`]: ../../std/vec/struct.Vec.html#method.append
#[doc(alias = "memcpy")]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_allowed_through_unstable_modules]
#[cfg_attr(bootstrap, rustc_allowed_through_unstable_modules)]
#[cfg_attr(
not(bootstrap),
rustc_allowed_through_unstable_modules = "import this function via `std::mem` instead"
)]
#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
Expand Down Expand Up @@ -4457,7 +4465,11 @@ pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: us
/// ```
#[doc(alias = "memmove")]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_allowed_through_unstable_modules]
#[cfg_attr(bootstrap, rustc_allowed_through_unstable_modules)]
#[cfg_attr(
not(bootstrap),
rustc_allowed_through_unstable_modules = "import this function via `std::mem` instead"
)]
#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
Expand Down Expand Up @@ -4540,7 +4552,11 @@ pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
/// ```
#[doc(alias = "memset")]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_allowed_through_unstable_modules]
#[cfg_attr(bootstrap, rustc_allowed_through_unstable_modules)]
#[cfg_attr(
not(bootstrap),
rustc_allowed_through_unstable_modules = "import this function via `std::mem` instead"
)]
#[rustc_const_stable(feature = "const_ptr_write", since = "1.83.0")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/tests/ui/std_instead_of_core.fixed
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//@aux-build:proc_macro_derive.rs

#![warn(clippy::std_instead_of_core)]
#![allow(unused_imports)]
#![allow(unused_imports, deprecated)]

extern crate alloc;

Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/tests/ui/std_instead_of_core.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//@aux-build:proc_macro_derive.rs

#![warn(clippy::std_instead_of_core)]
#![allow(unused_imports)]
#![allow(unused_imports, deprecated)]

extern crate alloc;

Expand Down
22 changes: 11 additions & 11 deletions src/tools/clippy/tests/ui/transmute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,31 @@ fn my_vec() -> MyVec<i32> {
#[warn(clippy::useless_transmute)]
unsafe fn _generic<'a, T, U: 'a>(t: &'a T) {
// FIXME: should lint
// let _: &'a T = core::intrinsics::transmute(t);
// let _: &'a T = core::mem::transmute(t);

let _: &'a U = core::intrinsics::transmute(t);
let _: &'a U = core::mem::transmute(t);

let _: *const T = core::intrinsics::transmute(t);
let _: *const T = core::mem::transmute(t);
//~^ ERROR: transmute from a reference to a pointer
//~| NOTE: `-D clippy::useless-transmute` implied by `-D warnings`

let _: *mut T = core::intrinsics::transmute(t);
let _: *mut T = core::mem::transmute(t);
//~^ ERROR: transmute from a reference to a pointer

let _: *const U = core::intrinsics::transmute(t);
let _: *const U = core::mem::transmute(t);
//~^ ERROR: transmute from a reference to a pointer
}

#[warn(clippy::useless_transmute)]
fn useless() {
unsafe {
let _: Vec<i32> = core::intrinsics::transmute(my_vec());
let _: Vec<i32> = core::mem::transmute(my_vec());
//~^ ERROR: transmute from a type (`std::vec::Vec<i32>`) to itself

let _: Vec<i32> = core::mem::transmute(my_vec());
//~^ ERROR: transmute from a type (`std::vec::Vec<i32>`) to itself

let _: Vec<i32> = std::intrinsics::transmute(my_vec());
let _: Vec<i32> = std::mem::transmute(my_vec());
//~^ ERROR: transmute from a type (`std::vec::Vec<i32>`) to itself

let _: Vec<i32> = std::mem::transmute(my_vec());
Expand Down Expand Up @@ -94,17 +94,17 @@ fn crosspointer() {
let int_mut_ptr: *mut Usize = &mut int as *mut Usize;

unsafe {
let _: Usize = core::intrinsics::transmute(int_const_ptr);
let _: Usize = core::mem::transmute(int_const_ptr);
//~^ ERROR: transmute from a type (`*const Usize`) to the type that it points to (
//~| NOTE: `-D clippy::crosspointer-transmute` implied by `-D warnings`

let _: Usize = core::intrinsics::transmute(int_mut_ptr);
let _: Usize = core::mem::transmute(int_mut_ptr);
//~^ ERROR: transmute from a type (`*mut Usize`) to the type that it points to (`U

let _: *const Usize = core::intrinsics::transmute(my_int());
let _: *const Usize = core::mem::transmute(my_int());
//~^ ERROR: transmute from a type (`Usize`) to a pointer to that type (`*const Usi

let _: *mut Usize = core::intrinsics::transmute(my_int());
let _: *mut Usize = core::mem::transmute(my_int());
//~^ ERROR: transmute from a type (`Usize`) to a pointer to that type (`*mut Usize
}
}
Expand Down
36 changes: 18 additions & 18 deletions src/tools/clippy/tests/ui/transmute.stderr
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
error: transmute from a reference to a pointer
--> tests/ui/transmute.rs:31:23
|
LL | let _: *const T = core::intrinsics::transmute(t);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `t as *const T`
LL | let _: *const T = core::mem::transmute(t);
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `t as *const T`
|
= note: `-D clippy::useless-transmute` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::useless_transmute)]`

error: transmute from a reference to a pointer
--> tests/ui/transmute.rs:35:21
|
LL | let _: *mut T = core::intrinsics::transmute(t);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `t as *const T as *mut T`
LL | let _: *mut T = core::mem::transmute(t);
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `t as *const T as *mut T`

error: transmute from a reference to a pointer
--> tests/ui/transmute.rs:38:23
|
LL | let _: *const U = core::intrinsics::transmute(t);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `t as *const T as *const U`
LL | let _: *const U = core::mem::transmute(t);
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `t as *const T as *const U`

error: transmute from a type (`std::vec::Vec<i32>`) to itself
--> tests/ui/transmute.rs:45:27
|
LL | let _: Vec<i32> = core::intrinsics::transmute(my_vec());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | let _: Vec<i32> = core::mem::transmute(my_vec());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: transmute from a type (`std::vec::Vec<i32>`) to itself
--> tests/ui/transmute.rs:48:27
Expand All @@ -34,8 +34,8 @@ LL | let _: Vec<i32> = core::mem::transmute(my_vec());
error: transmute from a type (`std::vec::Vec<i32>`) to itself
--> tests/ui/transmute.rs:51:27
|
LL | let _: Vec<i32> = std::intrinsics::transmute(my_vec());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | let _: Vec<i32> = std::mem::transmute(my_vec());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: transmute from a type (`std::vec::Vec<i32>`) to itself
--> tests/ui/transmute.rs:54:27
Expand Down Expand Up @@ -64,29 +64,29 @@ LL | let _: *const usize = std::mem::transmute(1 + 1usize);
error: transmute from a type (`*const Usize`) to the type that it points to (`Usize`)
--> tests/ui/transmute.rs:97:24
|
LL | let _: Usize = core::intrinsics::transmute(int_const_ptr);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | let _: Usize = core::mem::transmute(int_const_ptr);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::crosspointer-transmute` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::crosspointer_transmute)]`

error: transmute from a type (`*mut Usize`) to the type that it points to (`Usize`)
--> tests/ui/transmute.rs:101:24
|
LL | let _: Usize = core::intrinsics::transmute(int_mut_ptr);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | let _: Usize = core::mem::transmute(int_mut_ptr);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: transmute from a type (`Usize`) to a pointer to that type (`*const Usize`)
--> tests/ui/transmute.rs:104:31
|
LL | let _: *const Usize = core::intrinsics::transmute(my_int());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | let _: *const Usize = core::mem::transmute(my_int());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: transmute from a type (`Usize`) to a pointer to that type (`*mut Usize`)
--> tests/ui/transmute.rs:107:29
|
LL | let _: *mut Usize = core::intrinsics::transmute(my_int());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | let _: *mut Usize = core::mem::transmute(my_int());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: transmute from a `u8` to a `bool`
--> tests/ui/transmute.rs:114:28
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ use core::unicode::UNICODE_VERSION; //~ ERROR use of unstable library feature `u
// Known accidental stabilizations with known users
// fully stable @ core::mem::transmute
use core::intrinsics::transmute; // depended upon by rand_core
//~^WARN deprecated
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ LL | use core::unicode::UNICODE_VERSION;
= help: add `#![feature(unicode_internals)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: aborting due to 1 previous error
warning: use of deprecated module `std::intrinsics`: import this function via `std::mem` instead
--> $DIR/accidental-stable-in-unstable.rs:10:23
|
LL | use core::intrinsics::transmute; // depended upon by rand_core
| ^^^^^^^^^
|
= note: `#[warn(deprecated)]` on by default

error: aborting due to 1 previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0658`.

0 comments on commit 53b3bec

Please sign in to comment.