You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
spec: define that structs are compared field-by-field as listed in source code #8606
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.
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.
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)
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.
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.
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).
@rscretitled 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.
Activity
ianlancetaylor commentedon Aug 28, 2014
Comment 1:
Labels changed: added repo-main, release-none.
griesemer commentedon Aug 28, 2014
Comment 2:
Labels changed: added documentation.
Owner changed to @griesemer.
Status changed to Accepted.
bradfitz commentedon Apr 26, 2020
@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
mdempsky commentedon Apr 26, 2020
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.
ianlancetaylor commentedon Apr 26, 2020
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.
66 remaining items
ianlancetaylor commentedon Nov 4, 2022
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)
mdempsky commentedon Nov 4, 2022
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.
griesemer commentedon Nov 10, 2022
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.
gopherbot commentedon Nov 10, 2022
Change https://go.dev/cl/449536 mentions this issue:
spec: clarify struct field and array element comparison ordermdempsky commentedon Nov 10, 2022
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.
ianlancetaylor commentedon Nov 10, 2022
I agree with @griesemer that this should apply to arrays as well as structs.
spec: clarify struct field and array element comparison order