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

Extend ptr::null and null_mut to all thin (including extern) types #94954

Merged
merged 1 commit into from
May 25, 2022
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
46 changes: 46 additions & 0 deletions library/core/src/ptr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,10 +507,33 @@ pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
#[rustc_promotable]
#[rustc_const_stable(feature = "const_ptr_null", since = "1.24.0")]
#[rustc_diagnostic_item = "ptr_null"]
#[cfg(bootstrap)]
pub const fn null<T>() -> *const T {
invalid(0)
}

/// Creates a null raw pointer.
///
/// # Examples
///
/// ```
/// use std::ptr;
///
/// let p: *const i32 = ptr::null();
/// assert!(p.is_null());
/// ```
#[inline(always)]
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_promotable]
#[rustc_const_stable(feature = "const_ptr_null", since = "1.24.0")]
#[rustc_allow_const_fn_unstable(ptr_metadata)]
#[rustc_diagnostic_item = "ptr_null"]
#[cfg(not(bootstrap))]
pub const fn null<T: ?Sized + Thin>() -> *const T {
from_raw_parts(0 as *const (), ())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have used invalid rather than a int2ptr cast.
(The strict provenance stuff landed in between this PR here being created and landing, which I guess is why this was missed.)

Fixed in #97764

}

/// Creates a null mutable raw pointer.
///
/// # Examples
Expand All @@ -527,6 +550,7 @@ pub const fn null<T>() -> *const T {
#[rustc_promotable]
#[rustc_const_stable(feature = "const_ptr_null", since = "1.24.0")]
#[rustc_diagnostic_item = "ptr_null_mut"]
#[cfg(bootstrap)]
pub const fn null_mut<T>() -> *mut T {
invalid_mut(0)
}
Expand Down Expand Up @@ -657,6 +681,28 @@ where
addr as *mut T
}

/// Creates a null mutable raw pointer.
///
/// # Examples
///
/// ```
/// use std::ptr;
///
/// let p: *mut i32 = ptr::null_mut();
/// assert!(p.is_null());
/// ```
#[inline(always)]
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_promotable]
#[rustc_const_stable(feature = "const_ptr_null", since = "1.24.0")]
#[rustc_allow_const_fn_unstable(ptr_metadata)]
#[rustc_diagnostic_item = "ptr_null_mut"]
#[cfg(not(bootstrap))]
pub const fn null_mut<T: ?Sized + Thin>() -> *mut T {
from_raw_parts_mut(0 as *mut (), ())
}

/// Forms a raw slice from a pointer and a length.
///
/// The `len` argument is the number of **elements**, not the number of bytes.
Expand Down
12 changes: 12 additions & 0 deletions library/core/tests/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ fn test_is_null() {

let nmi: *mut dyn ToString = null_mut::<isize>();
assert!(nmi.is_null());

#[cfg(not(bootstrap))]
{
extern "C" {
type Extern;
}
let ec: *const Extern = null::<Extern>();
assert!(ec.is_null());

let em: *mut Extern = null_mut::<Extern>();
assert!(em.is_null());
}
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/cast/casts-issue-46365.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ struct Lorem {
}

fn main() {
let _foo: *mut Lorem = core::ptr::null_mut(); // no error here
let _foo: *mut Lorem = core::ptr::NonNull::dangling().as_ptr(); // no error here
}