-
Notifications
You must be signed in to change notification settings - Fork 18k
go/types: vet and test fail to typecheck valid package using unsafe.Sizeof #60431
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
Comments
I'm surprised that we get different results for |
I found some more time to work on a better reproducer and realized that The correct constant is 784 rather than 587; using this makes For the issue with vet, here is a smaller reproducer: package issue60431
import (
"unsafe"
)
type Bug struct {
_ int32
_ bool
}
var _ = [1]struct{}{}[unsafe.Sizeof(Bug{})-8] Here |
The default size computation of go/types doesn't match the compiler, and one of the things that is different is the missing end-of-struct padding, as you have found out (I've argued in the past that that padding can lead to quite a bit of waste if such structs are used as elements in array or slices). Running the following program in the type checker testing framework (which understands the package p
import "unsafe"
type S struct {
_ int32
_ bool
}
var _ = trace(unsafe.Sizeof(S{})) prints:
That is, the size of The "correct" fix is for |
Change https://go.dev/cl/501495 mentions this issue: |
Change https://go.dev/cl/501475 mentions this issue: |
For golang/go#60431 For golang/go#60734 Change-Id: I6a15a24e3e121635b33d77fde9170a41514c0db1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/501495 Run-TryBot: Cuong Manh Le <[email protected]> Reviewed-by: Damien Neil <[email protected]> Auto-Submit: Cuong Manh Le <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> gopls-CI: kokoro <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
Change https://go.dev/cl/506715 mentions this issue: |
Change https://go.dev/cl/506856 mentions this issue: |
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?
Statically asserted the size of a generated struct to ensure that I wouldn't forget to update tests if the definition from which it is generated changes:
Then build, vet, and test:
What did you expect to see?
PASS.
What did you see instead?
go build
succeeds.go vet
reportsvet: front/order_test.go:262:23: invalid argument: index 160 out of bounds [0:1]
.go test
reportsfront/order_test.go:262:23: invalid argument: index 197 out of bounds [0:1]
.Likely relates to #40322, in particular #40322 (comment).
The text was updated successfully, but these errors were encountered: