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
reflect: ConvertibleTo returns false in go1.17 for slice values that previously returned true #47785
Comments
it looks like this was introduced in 7473a6a#diff-8d7fac7a0925d669f941654f9b1cc5ea0be70dd93bfa3ab255456e510cd98cd7R1602 which added a PkgPath equality requirement to |
If anything, this is a bug in 1.16. If you add
The compiler complains that the conversion is not allowed (both in 1.16 and 1.17). Converting slices with different named element types, even if their structure is the same, is not allowed. |
Based on @randall77's comment, it seems like the new behavior is technically more correct. Did the actual conversion also used to succeed? If so, this might be a breaking change (not saying we should roll it back, but maybe something to be aware of). |
yeah, it did (see the test case), which is really unfortunate |
The behavior changed at https://go-review.googlesource.com/c/go/+/309729 @griesemer Am I right, that the new behavior is correct? |
It seems like this a breaking change that is in bounds per https://golang.org/doc/go1compat ("If a compiler or library has a bug that violates the specification, a program that depends on the buggy behavior may break if the bug is fixed. We reserve the right to fix such bugs.") At the very least this needs to be documented in the release notes, but it's worth asking if the benefits gained by the change outweigh the breakage incurred, or if there is an alternate way to fix the issue https://go-review.googlesource.com/c/go/+/309729 was addressing without breaking existing programs. |
It definitely seems like a bug. This code prints false:
In 1.16, |
But I agree, we don't want to break existing programs lightly. We'll have to ponder whether/how to fix this, or document it, or whatever. |
As discussed above, this change is correct. The types are not convertible and I agree that this should be in the release notes. It didn't occur to me that this would affect It's unfortunate that this breaks existing programs, but the earlier behavior was clearly a bug. The reflect package should not be reporting that types are convertible when they are not convertible in the language. We actually got the correct answer here in Go releases up to 1.7. It broke in Go 1.8. Looks like the breaking change was https://golang.org/cl/24190. |
24190 was just the spec change, the actual breaking CL was https://go-review.googlesource.com/c/go/+/30191 |
Change https://golang.org/cl/343329 mentions this issue: |
Conversion between slices with different element types is not allowed. Previously (1.8 <= goversion <= 1.16), this conversion was allowed if the base types were from different packages and had identical names. Update #47785 Change-Id: I359de5b6fe3ff35bdbf9ab5a13902a0f820cac66 Reviewed-on: https://go-review.googlesource.com/c/go/+/343329 Trust: Keith Randall <khr@golang.org> Run-TryBot: Keith Randall <khr@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Remilestoned to 1.17 for a doc fix. |
Change https://golang.org/cl/343669 mentions this issue: |
Change https://golang.org/cl/343690 mentions this issue: |
CL 309729 fixed a bug in Convert/ConvertibleTo that we should mention in the release notes for 1.17. Update golang/go#47785 Change-Id: I4490c15ddf1f52404f9def19e2c9dafac754fd95 Reviewed-on: https://go-review.googlesource.com/c/website/+/343690 Trust: Keith Randall <khr@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Change https://golang.org/cl/343669 can be abandoned, as we don't generally keep the release-branch's HTML up to date. The x/website change will correctly update the documentation. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Used the reflect ConvertibleTo function (via a call to https://github.com/stretchr/testify/blob/v1.7.0/assert/assertions.go#L78-L96) to compare slices of two different types with identical names/members/layouts in different packages.
This worked as expected prior to go 1.17.
Minimal reproducer at https://github.com/liggitt/convertible117/blob/master/convertible_test.go
What did you expect to see?
What did you see instead?
The text was updated successfully, but these errors were encountered: