-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
fmt: map printing sort does not deterministically sort differing types #30398
Comments
Thank you @lukechampine for this report! |
What does |
The result of
|
Previously, the result of sorting a map[interface{}] containing multiple concrete types was non-deterministic. To ensure consistent results, sort first by type name, then by concrete value. Fixes golang#30398
Change https://golang.org/cl/163745 mentions this issue: |
Previously, the result of sorting a map[interface{}] containing multiple concrete types was non-deterministic. To ensure consistent results, sort first by the machine address of the value's type, then by the value itself. Sorting based on machine address results in unpredictable output, but will at least be deterministic across runs of the same program. Fixes golang#30398
Previously, the result of sorting a map[interface{}] containing multiple concrete types was non-deterministic. To ensure consistent results, sort first by the machine address of the value's type, then by the value itself. Sorting based on machine address results in unpredictable output, but will at least be deterministic across runs of the same program. Fixes golang#30398
@gopherbot, please open a backport issue for 1.12. |
Backport issue(s) opened: #30484 (for 1.12). Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://golang.org/wiki/MinorReleases. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What did you do?
What did you expect to see?
What did you see instead?
Non-deterministic result; differing types always compare to -1, so the result depends on map iteration, which is pseudo-random.
It's possible that I'm just misinterpreting the documentation here. The release notes say:
I read this as "values compare first lexicographically by type name, then by their concrete value." However,
fmtsort.compare
instead does:Which results in the
*reflect.rtype
values being compared by their machine addresses. But sincea
andb
have the same type, their addresses will be equal (not sure if this is always the case), so thiscompare
is ineffectual.I think the correct code implementation would be something like:
which produces deterministic output. I'm happy to submit a pull request for this if so desired.
The text was updated successfully, but these errors were encountered: