Skip to content

Documentation of PartialEq has example that violates transitivity #66476

Description

@robamler

The Documentation of PartialEq states that implementations must be transitive: for all for all a, b and c, a == b and b == c implies a == c. This is followed by a note indicating that the transitivity must also hold if a, b, and c are of different types.

However, the last example implementation in the documentation is not transitive:

let b1 = Book { isbn: 1, format: BookFormat::Paperback };
let b2 = Book { isbn: 2, format: BookFormat::Paperback };

assert!(b1 == BookFormat::Paperback);
assert!(BookFormat::Paperback == b2);

// The following should hold by transitivity but doesn't.
assert!(b1 == b2); // <-- PANICS

I can think of three possible ways to fix this:

  • Remove the last example.
  • Keep the last example as an explicit counterexample to warn how easy it is to accidentally write implementations that violate the contract.
  • Modify the last example in a way that fixes the issue (I'm not sure what that would look like).

I can submit a pull request if I get some guidance which of the above solutions to choose (I personally have a weak preference for the second one).

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-bugCategory: This is a bug.T-libs-api[DEPRECATED; DO NOT USE]

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions