Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilize ptr::{from_ref, from_mut} #117824

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions library/core/src/ptr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,8 @@ where
/// 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 {
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/lint/ptr_null_checks.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// check-pass

#![feature(ptr_from_ref)]

use std::ptr;

extern "C" fn c_fn() {}
Expand Down
54 changes: 27 additions & 27 deletions tests/ui/lint/ptr_null_checks.stderr
Original file line number Diff line number Diff line change
@@ -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() {}
| ^------^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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() {}
| ^------^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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() {}
| ^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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() {}
| ^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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 ()) {}
| ^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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 ()) {}
| ^^^^^^^^^^^^^^^^^^^^------^^^^^^^^^^^^^^
Expand All @@ -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() {}
| ^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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() {}
| ^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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() {}
| ^^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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() {}
| ^--------------^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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() {}
| ^----^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -110,117 +110,117 @@ 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() {}
| ^------^^^^^^^^^^^^^^^^^^^^^^^
| |
| 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() {}
| ^--^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| 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() {}
| ^--^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| 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() {}
| ^-------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| 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() {}
| ^-----^^^^^^^^^^^^^^^^^^^^^^^^
| |
| 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() {}
| ^----^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| 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() {}
| ^-------^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| 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() {}
| ^-----------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| 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() {}
| ^------------^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| 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() {}
| ^------------------^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| 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() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/lint/reference_casting.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Loading
Loading