Skip to content

spec: define that structs are compared field-by-field as listed in source code #8606

Closed
ferrmin/go
#516

Description

@mdempsky
gc handles struct/array comparisons by short-circuiting if it finds any unequal
fields/elements, and this behavior is noticeable because the Go spec requires
comparisons to panic in some cases; e.g., see http://play.golang.org/p/5jqSUAT1xC

However, unlike short-circuiting for evaluating "a && b", it doesn't
seem that short-circuiting of field/element comparisons is specified by the spec. 
Arguably, the spec currently requires that instead both comparisons in the above program
should panic.

Not a major issue, but thought I'd file an issue to note it.  A couple possible ways to
address it:

1. Ignore it since it probably doesn't matter in practice.
2. Specify gc's behavior since it's intuitive and easy to explain.
3. Specify a set of allowable behaviors (e.g., allow short-circuiting or not; and/or
allow any particular ordering for element/field comparisons).
4. Change gc to not (visibly) short-circuit comparisons that involve comparing interface
types; e.g., comparing two [512]int arrays can still short-circuit, but comparing two
struct{a int; b, c interface{}; d int} structs would need to always compare the b and c
fields, and a and d could be compared conditionally.

Activity

  1. ianlancetaylor commented on Aug 28, 2014

    @ianlancetaylor
    Contributor

    Comment 1:

    Labels changed: added repo-main, release-none.

  2. griesemer commented on Aug 28, 2014

    @griesemer
    Contributor

    Comment 2:

    Labels changed: added documentation.

    Owner changed to @griesemer.

    Status changed to Accepted.

  3. added this to the Unplanned milestone on Apr 10, 2015
  4. bradfitz commented on Apr 26, 2020

    @bradfitz
    Contributor

    @griesemer, @ianlancetaylor, I closed #38676 as a duplicate of this one but it'd be nice to get a decision here for https://go-review.googlesource.com/c/go/+/230207 ("cmd/compile: improve generated eq algs for structs containing interfaces").

    /cc @josharian @mdempsky @randall77

  5. mdempsky commented on Apr 26, 2020

    @mdempsky
    ContributorAuthor

    I'm inclined to say the spec should explicitly say the order of field/element comparison is unspecified, and a struct/array with both unequal and incomparable fields/elements may either compare false or panic.

    I'd say go ahead with CL 230207.

  6. ianlancetaylor commented on Apr 26, 2020

    @ianlancetaylor
    Contributor

    I agree with @mdempsky.

    If we ever decide to implement more struct order of evaluation, this would be another place where we ought to specify the order.

  7. 66 remaining items

  8. moved this to Accepted in Proposalson Aug 10, 2022
  9. ianlancetaylor commented on Nov 4, 2022

    @ianlancetaylor
    Contributor

    Just a note that I think that we made a decision here: we compare structs for equality by comparing field by field, and exiting early when two fields are unequal. But I don't think that decision was ever added to the spec. (@griesemer)

  10. mdempsky commented on Nov 4, 2022

    @mdempsky
    ContributorAuthor

    To nag again, this issue affects array comparison order too, and it remains unconfirmed whether arrays were intentionally dropped from the accepted retitling on June 10, 2020.

  11. griesemer commented on Nov 10, 2022

    @griesemer
    Contributor

    It seems to me that the same problems that might occur with unspecified struct field comparison order would apply to array comparisons. I'll send out a CL specifying both, in the spirit of this issue. We can discuss on the issue if we want to keep the clarification for arrays.

  12. gopherbot commented on Nov 10, 2022

    @gopherbot
    Contributor

    Change https://go.dev/cl/449536 mentions this issue: spec: clarify struct field and array element comparison order

  13. mdempsky commented on Nov 10, 2022

    @mdempsky
    ContributorAuthor

    It seems to me that the same problems that might occur with unspecified struct field comparison order would apply to array comparisons.

    I feel like my comments aren't being heard.

    My original issue report from 2014 mentions structs and arrays equally: they were both mentioned in the issue title and the very first sentence of the issue body. Whenever I replied mentioning structs, I wrote "struct/array" (1, 2, 3).

    @rsc retitled it in June 2020 to remove the mention of arrays. I explicitly asked that same day if that was intentional, and never got a response.

    I asked again 6 days ago after @ianlancetaylor pinged the issues, and still I'm not getting any acknowledgement.

  14. ianlancetaylor commented on Nov 10, 2022

    @ianlancetaylor
    Contributor

    I agree with @griesemer that this should apply to arrays as well as structs.

  15. modified the milestones: Backlog, Go1.20 on Jun 4, 2023
  16. removed this from Proposalson Nov 16, 2023
  17. added a commit that references this issue on Feb 20, 2024
    2041bde
  18. locked and limited conversation to collaborators on Jun 3, 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

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions