Skip to content

Clarify that pointers are not always compared by their addresses#157682

Open
asquared31415 wants to merge 1 commit into
rust-lang:mainfrom
asquared31415:ptr_cmp_docs
Open

Clarify that pointers are not always compared by their addresses#157682
asquared31415 wants to merge 1 commit into
rust-lang:mainfrom
asquared31415:ptr_cmp_docs

Conversation

@asquared31415

@asquared31415 asquared31415 commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

This makes the doc comments on pointer types correctly clarify that for fat pointers, their metadata is compared as well for equality and ordering operations. Additionally adds this documentation to the NonNull type. This makes the documentation match the reference

Fixes #157640
Supersedes #136364 which missed this qualification

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 9, 2026
@rustbot

rustbot commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator

r? @LawnGnome

rustbot has assigned @LawnGnome.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 10 candidates
  • Random selection from LawnGnome, Mark-Simulacrum, clarfonthey, jhpratt

@theemathas

Copy link
Copy Markdown
Contributor

Do we need to specify what it means to do an Ord comparison between vtables?

@SimonSapin

Copy link
Copy Markdown
Contributor

I think it’s fine to specify that ordering of vtables is arbitrary. What’s more troublesome IMO is that their identity is undefined: separate codegen units can generate vtables independently for the same (trait, type) and give them different addresses: #46139

@asquared31415

Copy link
Copy Markdown
Contributor Author

This has been previously discussed at length and there is a warn-by-default lint that triggers when comparing pointers to trait objects.

@theemathas

Copy link
Copy Markdown
Contributor

Probably could use a link to https://doc.rust-lang.org/nightly/std/ptr/fn.eq.html somewhere.

Comment thread library/core/src/ptr/const_ptr.rs Outdated
@asquared31415

Copy link
Copy Markdown
Contributor Author

Probably could use a link to https://doc.rust-lang.org/nightly/std/ptr/fn.eq.html somewhere.

ptr::eq has the same semantics, but it does have a lot more explanation, that would be useful to avoid duplicating all the warnings.

@LawnGnome LawnGnome left a comment

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.

I think this is good for the PartialEq case and think the choice of core::ptr::eq as the reference there to avoid duplication is a good one, but I'm not sure this documentation answers the question a reader would have in the Ord and PartialOrd cases (namely, what is about to happen if I call this).

View changes since this review

Comment on lines +1614 to +1617
/// 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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect docs for comparing raw pointers to DST

6 participants