Skip to content
Open
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
21 changes: 18 additions & 3 deletions library/core/src/ptr/const_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1584,7 +1584,12 @@ impl<T, const N: usize> *const [T; N] {
}
}

/// Pointer equality is by address, as produced by the [`<*const T>::addr`](pointer::addr) method.
/// Pointers to [`Sized`] types are compared by their addresses, as produced by the
/// [`<*const T>::addr`](pointer::addr) method.
/// Pointers to [dynamically sized types] additionally have their metadata compared.
/// See [`core::ptr::eq`] for more information about metadata comparisons.
///
/// [dynamically sized types]: https://doc.rust-lang.org/reference/dynamically-sized-types.html
#[stable(feature = "rust1", since = "1.0.0")]
#[diagnostic::on_const(
message = "pointers cannot be reliably compared during const eval",
Expand All @@ -1606,7 +1611,12 @@ impl<T: PointeeSized> PartialEq for *const T {
)]
impl<T: PointeeSized> Eq for *const T {}

/// Pointer comparison is by address, as produced by the `[`<*const T>::addr`](pointer::addr)` method.
/// Pointers to [`Sized`] types are compared by their addresses, as produced by the
/// [`<*const T>::addr`](pointer::addr) method.
/// Pointers to [dynamically sized types] additionally have their metadata compared.
/// See [`core::ptr::eq`] for more information about metadata comparisons.
Comment on lines +1614 to +1617

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there anything we can say here to elaborate on what the actual behaviour for Ord (and PartialOrd, for those cases) would be, even if it's essentially meaningless for DSTs? The referenced core::ptr::eq documentation only really deals with equality comparisons, not ordering.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The behavior is the same as eq, but with the ordering operator instead of ==. That is, the metadata is treated in the same way (usize if length, pointer if vtable). I'm not sure the best way to explain this, especially because equality is a case of ord, so there's some overlap there too.

///
/// [dynamically sized types]: https://doc.rust-lang.org/reference/dynamically-sized-types.html
#[stable(feature = "rust1", since = "1.0.0")]
#[diagnostic::on_const(
message = "pointers cannot be reliably compared during const eval",
Expand All @@ -1626,7 +1636,12 @@ impl<T: PointeeSized> Ord for *const T {
}
}

/// Pointer comparison is by address, as produced by the `[`<*const T>::addr`](pointer::addr)` method.
/// Pointers to [`Sized`] types are compared by their addresses, as produced by the
/// [`<*const T>::addr`](pointer::addr) method.
/// Pointers to [dynamically sized types] additionally have their metadata compared.
/// See [`core::ptr::eq`] for more information about metadata comparisons.
///
/// [dynamically sized types]: https://doc.rust-lang.org/reference/dynamically-sized-types.html
#[stable(feature = "rust1", since = "1.0.0")]
#[diagnostic::on_const(
message = "pointers cannot be reliably compared during const eval",
Expand Down
21 changes: 18 additions & 3 deletions library/core/src/ptr/mut_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2025,7 +2025,12 @@ impl<T, const N: usize> *mut [T; N] {
}
}

/// Pointer equality is by address, as produced by the [`<*mut T>::addr`](pointer::addr) method.
/// Pointers to [`Sized`] types are compared by their addresses, as produced by the
/// [`<*mut T>::addr`](pointer::addr) method.
/// Pointers to [dynamically sized types] additionally have their metadata compared.
/// See [`core::ptr::eq`] for more information about metadata comparisons.
///
/// [dynamically sized types]: https://doc.rust-lang.org/reference/dynamically-sized-types.html
#[stable(feature = "rust1", since = "1.0.0")]
#[diagnostic::on_const(
message = "pointers cannot be reliably compared during const eval",
Expand All @@ -2047,7 +2052,12 @@ impl<T: PointeeSized> PartialEq for *mut T {
)]
impl<T: PointeeSized> Eq for *mut T {}

/// Pointer comparison is by address, as produced by the [`<*mut T>::addr`](pointer::addr) method.
/// Pointers to [`Sized`] types are compared by their addresses, as produced by the
/// [`<*mut T>::addr`](pointer::addr) method.
/// Pointers to [dynamically sized types] additionally have their metadata compared.
/// See [`core::ptr::eq`] for more information about metadata comparisons.
///
/// [dynamically sized types]: https://doc.rust-lang.org/reference/dynamically-sized-types.html
#[stable(feature = "rust1", since = "1.0.0")]
#[diagnostic::on_const(
message = "pointers cannot be reliably compared during const eval",
Expand All @@ -2067,7 +2077,12 @@ impl<T: PointeeSized> Ord for *mut T {
}
}

/// Pointer comparison is by address, as produced by the [`<*mut T>::addr`](pointer::addr) method.
/// Pointers to [`Sized`] types are compared by their addresses, as produced by the
/// [`<*mut T>::addr`](pointer::addr) method.
/// Pointers to [dynamically sized types] additionally have their metadata compared.
/// See [`core::ptr::eq`] for more information about metadata comparisons.
///
/// [dynamically sized types]: https://doc.rust-lang.org/reference/dynamically-sized-types.html
#[stable(feature = "rust1", since = "1.0.0")]
#[diagnostic::on_const(
message = "pointers cannot be reliably compared during const eval",
Expand Down
18 changes: 18 additions & 0 deletions library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1642,6 +1642,12 @@ impl<T: PointeeSized> fmt::Pointer for NonNull<T> {
#[stable(feature = "nonnull", since = "1.25.0")]
impl<T: PointeeSized> Eq for NonNull<T> {}

/// Pointers to [`Sized`] types are compared by their addresses, as produced by the
/// [`NonNull::addr`] method.
/// Pointers to [dynamically sized types] additionally have their metadata compared.
/// See [`core::ptr::eq`] for more information about metadata comparisons.
///
/// [dynamically sized types]: https://doc.rust-lang.org/reference/dynamically-sized-types.html
#[stable(feature = "nonnull", since = "1.25.0")]
impl<T: PointeeSized> PartialEq for NonNull<T> {
#[inline]
Expand All @@ -1651,6 +1657,12 @@ impl<T: PointeeSized> PartialEq for NonNull<T> {
}
}

/// Pointers to [`Sized`] types are compared by their addresses, as produced by the
/// [`NonNull::addr`] method.
/// Pointers to [dynamically sized types] additionally have their metadata compared.
/// See [`core::ptr::eq`] for more information about metadata comparisons.
///
/// [dynamically sized types]: https://doc.rust-lang.org/reference/dynamically-sized-types.html
#[stable(feature = "nonnull", since = "1.25.0")]
impl<T: PointeeSized> Ord for NonNull<T> {
#[inline]
Expand All @@ -1660,6 +1672,12 @@ impl<T: PointeeSized> Ord for NonNull<T> {
}
}

/// Pointers to [`Sized`] types are compared by their addresses, as produced by the
/// [`NonNull::addr`] method.
/// Pointers to [dynamically sized types] additionally have their metadata compared.
/// See [`core::ptr::eq`] for more information about metadata comparisons.
///
/// [dynamically sized types]: https://doc.rust-lang.org/reference/dynamically-sized-types.html
#[stable(feature = "nonnull", since = "1.25.0")]
impl<T: PointeeSized> PartialOrd for NonNull<T> {
#[inline]
Expand Down
Loading