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

document PartialEq, PartialOrd, Ord requirements more explicitly #85637

Merged
merged 3 commits into from
Jun 21, 2021

Conversation

RalfJung
Copy link
Member

This is the result of discussion in #50230, in particular this summary comment.

Fixes #50230.

@rust-highfive
Copy link
Collaborator

r? @joshtriplett

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 24, 2021
/// - transitivity: `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`.
/// - duality: `a < b` if and only if `b > a`.
Copy link
Member Author

Choose a reason for hiding this comment

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

This is probably the most questionable part of this PR -- it could be considered a new requirement. On the other hand, given that we demand symmetry for ==, it would seem really strange to not also demand duality here.

/// - transitivity: `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`.
/// - duality: `a < b` if and only if `b > a`.
Copy link
Member Author

Choose a reason for hiding this comment

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

This is probably the most questionable part of this PR -- it could be considered a new requirement. On the other hand, given that we demand symmetry for ==, it would seem really strange to not also demand duality here.

/// The comparison must satisfy, for all `a`, `b` and `c`:
///
/// - asymmetry: if `a < b` then `!(a > b)`, as well as `a > b` implying `!(a < b)`; and
Copy link
Member Author

Choose a reason for hiding this comment

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

I removed this line because:

  • It is redundant: it already follows from a < b being defined as partial_cmp(a, b) == Some(Less), which implies !(a > b) (defined as partial_cmp(a, b) != Some(Greater)).
  • "asymmetry" is the wrong term, an "asymmetric" relation is a relation that satisfies "if a < b then !(b < a)".

asymmtery (in the correct sense of the word) is a consequence of duality, so we could state it in the corollary section if you wish. antisymmetry is more closely related to what the docs are currently stating, but it is defined for <=-style relations: R is antisymmetric if R(a, b) && R(b, a) implies a == b.

/// - transitivity: `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`.
/// - duality: `a < b` if and only if `b > a`.
Copy link
Member Author

Choose a reason for hiding this comment

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

There is another property here that we might want to add: compatibility of partial_cmp with ==:
when a1 == a2 and b1 == b2, then partial_cmp(a1, b1) == partial_cmp(a2, b2).

@steveklabnik
Copy link
Member

cc @rust-lang/libs

/// If [`PartialOrd`] or [`Ord`] are also implemented for `Self` and `Rhs`, their methods must also
/// be consistent with `PartialEq` (see the documentation of those traits for the exact
/// requirememts). It's easy to accidentally make them disagree by deriving some of the traits and
/// manually implementing others.
Copy link
Member

Choose a reason for hiding this comment

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

I think this sentence is begging for a code example. I was saying to myself, "Oh wow, I should watch out for that, I'm hoping an example comes next" while reading it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Note that I did not write this sentence, I just moved it around. I don't know what the original author had in their mind when writing this.

Copy link
Member Author

Choose a reason for hiding this comment

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

So what I am saying is, this issue is pre-existing, and I hope my PR doesn't have to fix it. :) It seems largely orthogonal to what this PR is about.

/// If [`Ord`] is also implemented for `Self` and `Rhs`, it must also be consistent with
/// `partial_cmp` (see the documentation of that trait for the exact requirements). It's
/// easy to accidentally make them disagree by deriving some of the traits and manually
/// implementing others.
Copy link
Member

Choose a reason for hiding this comment

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

Same thought as above on an example.

library/core/src/cmp.rs Outdated Show resolved Hide resolved
library/core/src/cmp.rs Outdated Show resolved Hide resolved
@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 14, 2021
library/core/src/cmp.rs Outdated Show resolved Hide resolved
Co-authored-by: Jane Lusby <jlusby42@gmail.com>
@RalfJung
Copy link
Member Author

@rust-lang/libs this has been sitting without a review for quite some time now

@m-ou-se m-ou-se added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Jun 20, 2021
@m-ou-se m-ou-se assigned m-ou-se and unassigned joshtriplett Jun 20, 2021
@m-ou-se
Copy link
Member

m-ou-se commented Jun 20, 2021

Since unsafe code can't count on these requirements anyway, we don't have to be too careful with adding clarifications like this about what these traits mean. None of your changes are surprising new requirements. Just clarifications of what was already implied/known. Thanks!

@bors r+

We might want to clarify that 'must' in this documentation doesn't mean you get UB otherwise, and that you may not rely on these guarantees in unsafe code.

The HashSet documentation contains:

The behavior resulting from such a logic error is not specified, but will not result in undefined behavior. This could include panics, incorrect results, aborts, memory leaks, and non-termination.

We could add something like that here too.

@bors
Copy link
Contributor

bors commented Jun 20, 2021

📌 Commit 45675f3 has been approved by m-ou-se

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 20, 2021
@RalfJung
Copy link
Member Author

We might want to clarify that 'must' in this documentation doesn't mean you get UB otherwise, and that you may not rely on these guarantees in unsafe code.

That would be #73682. Could you post your suggestion there?

JohnTitor added a commit to JohnTitor/rust that referenced this pull request Jun 21, 2021
document PartialEq, PartialOrd, Ord requirements more explicitly

This is the result of discussion in rust-lang#50230, in particular [this summary comment](rust-lang#50230 (comment)).

Fixes rust-lang#50230.
This was referenced Jun 21, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 21, 2021
Rollup of 8 pull requests

Successful merges:

 - rust-lang#83739 (Account for bad placeholder errors on consts/statics with trait objects)
 - rust-lang#85637 (document PartialEq, PartialOrd, Ord requirements more explicitly)
 - rust-lang#86152 (Lazify is_really_default condition in the RustdocGUI bootstrap step)
 - rust-lang#86156 (Fix a bug in the linkchecker)
 - rust-lang#86427 (Updated release note)
 - rust-lang#86452 (fix panic-safety in specialized Zip::next_back)
 - rust-lang#86484 (Do not set depth to 0 in fully_expand_fragment)
 - rust-lang#86491 (expand: Move some more derive logic to rustc_builtin_macros)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit e6732e0 into rust-lang:master Jun 21, 2021
@rustbot rustbot added this to the 1.55.0 milestone Jun 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document "latent requirements" of PartialOrd
10 participants