Skip to content

StructuralEq trait docs are outdated #115881

Closed

Description

@RalfJung

The StructuralEq trait docs are saying

/// In a more ideal world, we could check that requirement by just checking that
/// the given type implements both the `StructuralPartialEq` trait *and*
/// the `Eq` trait. However, you can have ADTs that *do* `derive(PartialEq, Eq)`,
/// and be a case that we want the compiler to accept, and yet the constant's
/// type fails to implement `Eq`.
///
/// Namely, a case like this:
///
/// ```rust
/// #[derive(PartialEq, Eq)]
/// struct Wrap<X>(X);
///
/// fn higher_order(_: &()) { }
///
/// const CFN: Wrap<fn(&())> = Wrap(higher_order);
///
/// fn main() {
///     match CFN {
///         CFN => {}
///         _ => {}
///     }
/// }
/// ```
///
/// (The problem in the above code is that `Wrap<fn(&())>` does not implement
/// `PartialEq`, nor `Eq`, because `for<'a> fn(&'a _)` does not implement those
/// traits.)
///
/// Therefore, we cannot rely on naive check for `StructuralPartialEq` and
/// mere `Eq`.

However, with the FnPtr trait, fn(&()) does implement Eq and PartialEq.

Is the StructuralEq trait still necessary? Eq has no methods, so if PartialEq is structural then there's no way that Eq is not, right?

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Sep 15, 2023
  2. RalfJung commented on Sep 15, 2023

    @RalfJung
    MemberAuthor

    Cc @rust-lang/types

  3. added
    T-typesRelevant to the types team, which will review and decide on the PR/issue.
    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Sep 15, 2023
  4. RalfJung commented on Sep 16, 2023

    @RalfJung
    MemberAuthor

    Also I don't think the current consensus is that we should check matched constants to be Eq. #84045 got rejected, which is a fairly clear sign that PartialEq is sufficient for matching and everything else can be handled with lints.

    So I don't think StructuralEq still serves any purpose.

  5. nikomatsakis commented on Sep 16, 2023

    @nikomatsakis
    Contributor

    I would be very happy to settle this story once and for all. I still feel a bit confused about our plan here, though I recall thinking @RalfJung that your proposals made sense to me.

  6. RalfJung commented on Sep 16, 2023

    @RalfJung
    MemberAuthor

    I think my prior proposals don't entirely work any more with things like #67343 or code like this -- we have really honed in on StructuralPartialEq being a shallow property, not a statement about being able to create valtrees. We also have ConstParamTy as a separate trait now; I think that might be closer to what I originally imagined.

    On the plus side, I seem to recall @lcnr was set on keeping StructuralPartialEq shallow; so maybe it's easier to come to an agreement now that I've come around to accepting that. :)

    Also see this Zulip thread I just started.

  7. added 3 commits that reference this issue on Jan 25, 2024
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 toolsT-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions