Stabilize ptr::{from_ref, from_mut}

This commit is contained in:
Maybe Waffle 2023-11-11 11:33:10 +00:00 committed by David Tolnay
parent 4d1bd0db7f
commit b863e9ba57
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
7 changed files with 74 additions and 77 deletions

View File

@ -720,7 +720,8 @@ pub fn from_exposed_addr_mut<T>(addr: usize) -> *mut T
/// type or mutability, in particular if the code is refactored.
#[inline(always)]
#[must_use]
#[unstable(feature = "ptr_from_ref", issue = "106116")]
#[stable(feature = "ptr_from_ref", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "ptr_from_ref", since = "CURRENT_RUSTC_VERSION")]
#[rustc_never_returns_null_ptr]
#[rustc_diagnostic_item = "ptr_from_ref"]
pub const fn from_ref<T: ?Sized>(r: &T) -> *const T {
@ -733,7 +734,9 @@ pub const fn from_ref<T: ?Sized>(r: &T) -> *const T {
/// type or mutability, in particular if the code is refactored.
#[inline(always)]
#[must_use]
#[unstable(feature = "ptr_from_ref", issue = "106116")]
#[stable(feature = "ptr_from_ref", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "ptr_from_ref", since = "CURRENT_RUSTC_VERSION")]
#[rustc_allow_const_fn_unstable(const_mut_refs)]
#[rustc_never_returns_null_ptr]
pub const fn from_mut<T: ?Sized>(r: &mut T) -> *mut T {
r

View File

@ -338,7 +338,6 @@
#![feature(portable_simd)]
#![feature(prelude_2024)]
#![feature(ptr_as_uninit)]
#![feature(ptr_from_ref)]
#![feature(raw_os_nonzero)]
#![feature(round_ties_even)]
#![feature(slice_internals)]

View File

@ -1,7 +1,5 @@
// check-pass
#![feature(ptr_from_ref)]
use std::ptr;
extern "C" fn c_fn() {}

View File

@ -1,5 +1,5 @@
warning: function pointers are not nullable, so checking them for null will always return false
--> $DIR/ptr_null_checks.rs:14:8
--> $DIR/ptr_null_checks.rs:12:8
|
LL | if (fn_ptr as *mut ()).is_null() {}
| ^------^^^^^^^^^^^^^^^^^^^^^^
@ -10,7 +10,7 @@ LL | if (fn_ptr as *mut ()).is_null() {}
= note: `#[warn(useless_ptr_null_checks)]` on by default
warning: function pointers are not nullable, so checking them for null will always return false
--> $DIR/ptr_null_checks.rs:16:8
--> $DIR/ptr_null_checks.rs:14:8
|
LL | if (fn_ptr as *const u8).is_null() {}
| ^------^^^^^^^^^^^^^^^^^^^^^^^^
@ -20,7 +20,7 @@ LL | if (fn_ptr as *const u8).is_null() {}
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
warning: function pointers are not nullable, so checking them for null will always return false
--> $DIR/ptr_null_checks.rs:18:8
--> $DIR/ptr_null_checks.rs:16:8
|
LL | if (fn_ptr as *const ()) == std::ptr::null() {}
| ^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -30,7 +30,7 @@ LL | if (fn_ptr as *const ()) == std::ptr::null() {}
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
warning: function pointers are not nullable, so checking them for null will always return false
--> $DIR/ptr_null_checks.rs:20:8
--> $DIR/ptr_null_checks.rs:18:8
|
LL | if (fn_ptr as *mut ()) == std::ptr::null_mut() {}
| ^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -40,7 +40,7 @@ LL | if (fn_ptr as *mut ()) == std::ptr::null_mut() {}
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
warning: function pointers are not nullable, so checking them for null will always return false
--> $DIR/ptr_null_checks.rs:22:8
--> $DIR/ptr_null_checks.rs:20:8
|
LL | if (fn_ptr as *const ()) == (0 as *const ()) {}
| ^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -50,7 +50,7 @@ LL | if (fn_ptr as *const ()) == (0 as *const ()) {}
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
warning: function pointers are not nullable, so checking them for null will always return false
--> $DIR/ptr_null_checks.rs:24:8
--> $DIR/ptr_null_checks.rs:22:8
|
LL | if <*const _>::is_null(fn_ptr as *const ()) {}
| ^^^^^^^^^^^^^^^^^^^^------^^^^^^^^^^^^^^
@ -60,7 +60,7 @@ LL | if <*const _>::is_null(fn_ptr as *const ()) {}
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
warning: function pointers are not nullable, so checking them for null will always return false
--> $DIR/ptr_null_checks.rs:26:8
--> $DIR/ptr_null_checks.rs:24:8
|
LL | if (fn_ptr as *mut fn() as *const fn() as *const ()).is_null() {}
| ^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -70,7 +70,7 @@ LL | if (fn_ptr as *mut fn() as *const fn() as *const ()).is_null() {}
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
warning: function pointers are not nullable, so checking them for null will always return false
--> $DIR/ptr_null_checks.rs:28:8
--> $DIR/ptr_null_checks.rs:26:8
|
LL | if (fn_ptr as *mut fn() as *const fn()).cast_mut().is_null() {}
| ^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -80,7 +80,7 @@ LL | if (fn_ptr as *mut fn() as *const fn()).cast_mut().is_null() {}
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
warning: function pointers are not nullable, so checking them for null will always return false
--> $DIR/ptr_null_checks.rs:30:8
--> $DIR/ptr_null_checks.rs:28:8
|
LL | if ((fn_ptr as *mut fn()).cast() as *const fn()).cast_mut().is_null() {}
| ^^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -90,7 +90,7 @@ LL | if ((fn_ptr as *mut fn()).cast() as *const fn()).cast_mut().is_null() {
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
warning: function pointers are not nullable, so checking them for null will always return false
--> $DIR/ptr_null_checks.rs:32:8
--> $DIR/ptr_null_checks.rs:30:8
|
LL | if (fn_ptr as fn() as *const ()).is_null() {}
| ^--------------^^^^^^^^^^^^^^^^^^^^^^^^
@ -100,7 +100,7 @@ LL | if (fn_ptr as fn() as *const ()).is_null() {}
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
warning: function pointers are not nullable, so checking them for null will always return false
--> $DIR/ptr_null_checks.rs:34:8
--> $DIR/ptr_null_checks.rs:32:8
|
LL | if (c_fn as *const fn()).is_null() {}
| ^----^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -110,7 +110,7 @@ LL | if (c_fn as *const fn()).is_null() {}
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
warning: references are not nullable, so checking them for null will always return false
--> $DIR/ptr_null_checks.rs:38:8
--> $DIR/ptr_null_checks.rs:36:8
|
LL | if (&mut 8 as *mut i32).is_null() {}
| ^------^^^^^^^^^^^^^^^^^^^^^^^
@ -118,13 +118,13 @@ LL | if (&mut 8 as *mut i32).is_null() {}
| expression has type `&mut i32`
warning: returned pointer of `from_mut` call is never null, so checking it for null will always return false
--> $DIR/ptr_null_checks.rs:40:8
--> $DIR/ptr_null_checks.rs:38:8
|
LL | if ptr::from_mut(&mut 8).is_null() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: references are not nullable, so checking them for null will always return false
--> $DIR/ptr_null_checks.rs:42:8
--> $DIR/ptr_null_checks.rs:40:8
|
LL | if (&8 as *const i32).is_null() {}
| ^--^^^^^^^^^^^^^^^^^^^^^^^^^
@ -132,25 +132,25 @@ LL | if (&8 as *const i32).is_null() {}
| expression has type `&i32`
warning: returned pointer of `from_ref` call is never null, so checking it for null will always return false
--> $DIR/ptr_null_checks.rs:44:8
--> $DIR/ptr_null_checks.rs:42:8
|
LL | if ptr::from_ref(&8).is_null() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: returned pointer of `from_ref` call is never null, so checking it for null will always return false
--> $DIR/ptr_null_checks.rs:46:8
--> $DIR/ptr_null_checks.rs:44:8
|
LL | if ptr::from_ref(&8).cast_mut().is_null() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: returned pointer of `from_ref` call is never null, so checking it for null will always return false
--> $DIR/ptr_null_checks.rs:48:8
--> $DIR/ptr_null_checks.rs:46:8
|
LL | if (ptr::from_ref(&8).cast_mut() as *mut i32).is_null() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: references are not nullable, so checking them for null will always return false
--> $DIR/ptr_null_checks.rs:50:8
--> $DIR/ptr_null_checks.rs:48:8
|
LL | if (&8 as *const i32) == std::ptr::null() {}
| ^--^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -158,7 +158,7 @@ LL | if (&8 as *const i32) == std::ptr::null() {}
| expression has type `&i32`
warning: references are not nullable, so checking them for null will always return false
--> $DIR/ptr_null_checks.rs:53:8
--> $DIR/ptr_null_checks.rs:51:8
|
LL | if (ref_num as *const i32) == std::ptr::null() {}
| ^-------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -166,7 +166,7 @@ LL | if (ref_num as *const i32) == std::ptr::null() {}
| expression has type `&i32`
warning: references are not nullable, so checking them for null will always return false
--> $DIR/ptr_null_checks.rs:55:8
--> $DIR/ptr_null_checks.rs:53:8
|
LL | if (b"\0" as *const u8).is_null() {}
| ^-----^^^^^^^^^^^^^^^^^^^^^^^^
@ -174,7 +174,7 @@ LL | if (b"\0" as *const u8).is_null() {}
| expression has type `&[u8; 1]`
warning: references are not nullable, so checking them for null will always return false
--> $DIR/ptr_null_checks.rs:57:8
--> $DIR/ptr_null_checks.rs:55:8
|
LL | if ("aa" as *const str).is_null() {}
| ^----^^^^^^^^^^^^^^^^^^^^^^^^^
@ -182,7 +182,7 @@ LL | if ("aa" as *const str).is_null() {}
| expression has type `&str`
warning: references are not nullable, so checking them for null will always return false
--> $DIR/ptr_null_checks.rs:59:8
--> $DIR/ptr_null_checks.rs:57:8
|
LL | if (&[1, 2] as *const i32).is_null() {}
| ^-------^^^^^^^^^^^^^^^^^^^^^^^^^
@ -190,7 +190,7 @@ LL | if (&[1, 2] as *const i32).is_null() {}
| expression has type `&[i32; 2]`
warning: references are not nullable, so checking them for null will always return false
--> $DIR/ptr_null_checks.rs:61:8
--> $DIR/ptr_null_checks.rs:59:8
|
LL | if (&mut [1, 2] as *mut i32) == std::ptr::null_mut() {}
| ^-----------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -198,7 +198,7 @@ LL | if (&mut [1, 2] as *mut i32) == std::ptr::null_mut() {}
| expression has type `&mut [i32; 2]`
warning: references are not nullable, so checking them for null will always return false
--> $DIR/ptr_null_checks.rs:63:8
--> $DIR/ptr_null_checks.rs:61:8
|
LL | if (static_i32() as *const i32).is_null() {}
| ^------------^^^^^^^^^^^^^^^^^^^^^^^^^
@ -206,7 +206,7 @@ LL | if (static_i32() as *const i32).is_null() {}
| expression has type `&i32`
warning: references are not nullable, so checking them for null will always return false
--> $DIR/ptr_null_checks.rs:65:8
--> $DIR/ptr_null_checks.rs:63:8
|
LL | if (&*{ static_i32() } as *const i32).is_null() {}
| ^------------------^^^^^^^^^^^^^^^^^^^^^^^^^
@ -214,13 +214,13 @@ LL | if (&*{ static_i32() } as *const i32).is_null() {}
| expression has type `&i32`
warning: returned pointer of `as_ptr` call is never null, so checking it for null will always return false
--> $DIR/ptr_null_checks.rs:69:8
--> $DIR/ptr_null_checks.rs:67:8
|
LL | if ptr::NonNull::new(&mut 8).unwrap().as_ptr().is_null() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: returned pointer of `as_ptr` call is never null, so checking it for null will always return false
--> $DIR/ptr_null_checks.rs:71:8
--> $DIR/ptr_null_checks.rs:69:8
|
LL | if ptr::NonNull::<u8>::dangling().as_ptr().is_null() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@ -1,7 +1,5 @@
// check-fail
#![feature(ptr_from_ref)]
extern "C" {
// N.B., mutability can be easily incorrect in FFI calls -- as
// in C, the default is mutable pointers.

View File

@ -1,5 +1,5 @@
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:19:16
--> $DIR/reference_casting.rs:17:16
|
LL | let _num = &mut *(num as *const i32 as *mut i32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -8,7 +8,7 @@ LL | let _num = &mut *(num as *const i32 as *mut i32);
= note: `#[deny(invalid_reference_casting)]` on by default
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:21:16
--> $DIR/reference_casting.rs:19:16
|
LL | let _num = &mut *(num as *const i32).cast_mut();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -16,7 +16,7 @@ LL | let _num = &mut *(num as *const i32).cast_mut();
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:23:16
--> $DIR/reference_casting.rs:21:16
|
LL | let _num = &mut *std::ptr::from_ref(num).cast_mut();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -24,7 +24,7 @@ LL | let _num = &mut *std::ptr::from_ref(num).cast_mut();
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:25:16
--> $DIR/reference_casting.rs:23:16
|
LL | let _num = &mut *std::ptr::from_ref({ num }).cast_mut();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -32,7 +32,7 @@ LL | let _num = &mut *std::ptr::from_ref({ num }).cast_mut();
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:27:16
--> $DIR/reference_casting.rs:25:16
|
LL | let _num = &mut *{ std::ptr::from_ref(num) }.cast_mut();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -40,7 +40,7 @@ LL | let _num = &mut *{ std::ptr::from_ref(num) }.cast_mut();
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:29:16
--> $DIR/reference_casting.rs:27:16
|
LL | let _num = &mut *(std::ptr::from_ref({ num }) as *mut i32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -48,7 +48,7 @@ LL | let _num = &mut *(std::ptr::from_ref({ num }) as *mut i32);
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:31:16
--> $DIR/reference_casting.rs:29:16
|
LL | let _num = &mut *(num as *const i32).cast::<i32>().cast_mut();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -56,7 +56,7 @@ LL | let _num = &mut *(num as *const i32).cast::<i32>().cast_mut();
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:33:16
--> $DIR/reference_casting.rs:31:16
|
LL | let _num = &mut *(num as *const i32).cast::<i32>().cast_mut().cast_const().cast_mut();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -64,7 +64,7 @@ LL | let _num = &mut *(num as *const i32).cast::<i32>().cast_mut().cast_cons
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:35:16
--> $DIR/reference_casting.rs:33:16
|
LL | let _num = &mut *(std::ptr::from_ref(static_u8()) as *mut i32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -72,7 +72,7 @@ LL | let _num = &mut *(std::ptr::from_ref(static_u8()) as *mut i32);
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:37:16
--> $DIR/reference_casting.rs:35:16
|
LL | let _num = &mut *std::mem::transmute::<_, *mut i32>(num);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -80,7 +80,7 @@ LL | let _num = &mut *std::mem::transmute::<_, *mut i32>(num);
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:39:16
--> $DIR/reference_casting.rs:37:16
|
LL | let _num = &mut *(std::mem::transmute::<_, *mut i32>(num) as *mut i32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -88,7 +88,7 @@ LL | let _num = &mut *(std::mem::transmute::<_, *mut i32>(num) as *mut i32);
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:41:16
--> $DIR/reference_casting.rs:39:16
|
LL | let _num = &mut *std::cell::UnsafeCell::raw_get(
| ________________^
@ -100,7 +100,7 @@ LL | | );
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:47:16
--> $DIR/reference_casting.rs:45:16
|
LL | let deferred = num as *const i32 as *mut i32;
| ----------------------------- casting happend here
@ -110,7 +110,7 @@ LL | let _num = &mut *deferred;
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:50:16
--> $DIR/reference_casting.rs:48:16
|
LL | let deferred = (std::ptr::from_ref(num) as *const i32 as *const i32).cast_mut() as *mut i32;
| ---------------------------------------------------------------------------- casting happend here
@ -120,7 +120,7 @@ LL | let _num = &mut *deferred;
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:53:16
--> $DIR/reference_casting.rs:51:16
|
LL | let deferred = (std::ptr::from_ref(num) as *const i32 as *const i32).cast_mut() as *mut i32;
| ---------------------------------------------------------------------------- casting happend here
@ -131,7 +131,7 @@ LL | let _num = &mut *deferred_rebind;
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:55:16
--> $DIR/reference_casting.rs:53:16
|
LL | let _num = &mut *(num as *const _ as usize as *mut i32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -139,7 +139,7 @@ LL | let _num = &mut *(num as *const _ as usize as *mut i32);
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:57:16
--> $DIR/reference_casting.rs:55:16
|
LL | let _num = &mut *(std::mem::transmute::<_, *mut _>(num as *const i32) as *mut i32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -147,7 +147,7 @@ LL | let _num = &mut *(std::mem::transmute::<_, *mut _>(num as *const i32) a
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:64:16
--> $DIR/reference_casting.rs:62:16
|
LL | let num = NUM as *const i32 as *mut i32;
| ----------------------------- casting happend here
@ -158,7 +158,7 @@ LL | let _num = &mut *num;
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:68:16
--> $DIR/reference_casting.rs:66:16
|
LL | let _num = &mut *(cell as *const _ as *mut i32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -167,7 +167,7 @@ LL | let _num = &mut *(cell as *const _ as *mut i32);
= note: even for types with interior mutability, the only legal way to obtain a mutable pointer from a shared reference is through `UnsafeCell::get`
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:72:9
--> $DIR/reference_casting.rs:70:9
|
LL | &mut *((this as *const _) as *mut _)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -175,7 +175,7 @@ LL | &mut *((this as *const _) as *mut _)
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:77:18
--> $DIR/reference_casting.rs:75:18
|
LL | unsafe { &mut *std::cell::UnsafeCell::raw_get(x as *const _ as *const _) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -183,7 +183,7 @@ LL | unsafe { &mut *std::cell::UnsafeCell::raw_get(x as *const _ as *con
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:82:18
--> $DIR/reference_casting.rs:80:18
|
LL | unsafe { &mut *std::cell::UnsafeCell::raw_get(x as *const _ as *const _) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -191,7 +191,7 @@ LL | unsafe { &mut *std::cell::UnsafeCell::raw_get(x as *const _ as *con
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:92:5
--> $DIR/reference_casting.rs:90:5
|
LL | *(a as *const _ as *mut _) = String::from("Replaced");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -199,7 +199,7 @@ LL | *(a as *const _ as *mut _) = String::from("Replaced");
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:94:5
--> $DIR/reference_casting.rs:92:5
|
LL | *(a as *const _ as *mut String) += " world";
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -207,7 +207,7 @@ LL | *(a as *const _ as *mut String) += " world";
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:96:5
--> $DIR/reference_casting.rs:94:5
|
LL | *std::ptr::from_ref(num).cast_mut() += 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -215,7 +215,7 @@ LL | *std::ptr::from_ref(num).cast_mut() += 1;
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:98:5
--> $DIR/reference_casting.rs:96:5
|
LL | *std::ptr::from_ref({ num }).cast_mut() += 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -223,7 +223,7 @@ LL | *std::ptr::from_ref({ num }).cast_mut() += 1;
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:100:5
--> $DIR/reference_casting.rs:98:5
|
LL | *{ std::ptr::from_ref(num) }.cast_mut() += 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -231,7 +231,7 @@ LL | *{ std::ptr::from_ref(num) }.cast_mut() += 1;
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:102:5
--> $DIR/reference_casting.rs:100:5
|
LL | *(std::ptr::from_ref({ num }) as *mut i32) += 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -239,7 +239,7 @@ LL | *(std::ptr::from_ref({ num }) as *mut i32) += 1;
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:104:5
--> $DIR/reference_casting.rs:102:5
|
LL | *std::mem::transmute::<_, *mut i32>(num) += 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -247,7 +247,7 @@ LL | *std::mem::transmute::<_, *mut i32>(num) += 1;
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:106:5
--> $DIR/reference_casting.rs:104:5
|
LL | *(std::mem::transmute::<_, *mut i32>(num) as *mut i32) += 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -255,7 +255,7 @@ LL | *(std::mem::transmute::<_, *mut i32>(num) as *mut i32) += 1;
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:108:5
--> $DIR/reference_casting.rs:106:5
|
LL | / std::ptr::write(
LL | |
@ -267,7 +267,7 @@ LL | | );
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:113:5
--> $DIR/reference_casting.rs:111:5
|
LL | *((&std::cell::UnsafeCell::new(0)) as *const _ as *mut i32) = 5;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -276,7 +276,7 @@ LL | *((&std::cell::UnsafeCell::new(0)) as *const _ as *mut i32) = 5;
= note: even for types with interior mutability, the only legal way to obtain a mutable pointer from a shared reference is through `UnsafeCell::get`
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:117:5
--> $DIR/reference_casting.rs:115:5
|
LL | let value = num as *const i32 as *mut i32;
| ----------------------------- casting happend here
@ -286,7 +286,7 @@ LL | *value = 1;
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:121:5
--> $DIR/reference_casting.rs:119:5
|
LL | let value = value as *mut i32;
| ----------------- casting happend here
@ -296,7 +296,7 @@ LL | *value = 1;
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:124:5
--> $DIR/reference_casting.rs:122:5
|
LL | let value = num as *const i32 as *mut i32;
| ----------------------------- casting happend here
@ -306,7 +306,7 @@ LL | *value = 1;
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:127:5
--> $DIR/reference_casting.rs:125:5
|
LL | let value = num as *const i32 as *mut i32;
| ----------------------------- casting happend here
@ -317,7 +317,7 @@ LL | *value_rebind = 1;
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:129:5
--> $DIR/reference_casting.rs:127:5
|
LL | *(num as *const i32).cast::<i32>().cast_mut() = 2;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -325,7 +325,7 @@ LL | *(num as *const i32).cast::<i32>().cast_mut() = 2;
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:131:5
--> $DIR/reference_casting.rs:129:5
|
LL | *(num as *const _ as usize as *mut i32) = 2;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -333,7 +333,7 @@ LL | *(num as *const _ as usize as *mut i32) = 2;
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:133:5
--> $DIR/reference_casting.rs:131:5
|
LL | let value = num as *const i32 as *mut i32;
| ----------------------------- casting happend here
@ -344,7 +344,7 @@ LL | std::ptr::write(value, 2);
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:135:5
--> $DIR/reference_casting.rs:133:5
|
LL | let value = num as *const i32 as *mut i32;
| ----------------------------- casting happend here
@ -355,7 +355,7 @@ LL | std::ptr::write_unaligned(value, 2);
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:137:5
--> $DIR/reference_casting.rs:135:5
|
LL | let value = num as *const i32 as *mut i32;
| ----------------------------- casting happend here
@ -366,7 +366,7 @@ LL | std::ptr::write_volatile(value, 2);
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:141:9
--> $DIR/reference_casting.rs:139:9
|
LL | *(this as *const _ as *mut _) = a;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@ -4,7 +4,6 @@
#![allow(dead_code)]
#![feature(never_type)]
#![feature(pointer_is_aligned)]
#![feature(ptr_from_ref)]
#![feature(strict_provenance)]
use std::mem::size_of;