-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: StructOf forbids anonymous fields without name #24781
Comments
(CC: @griesemer @mdempsky @dsnet) |
Possibly related to #24721. |
I just ran into this: https://play.golang.org/p/rv5GDx0s02w There seem to be three things going on
|
(Thanks Sean for triaging the recently mentioned issue.) The problem seems to be an inconsistency in the way the abi and reflect packages represent structs with embedded members. Consider this playground example where the reflected field types are equivalent, but the native type has no field name and the reflect type has the same name as its embedded type:
When the reflect package calls I tried investigating further but I ran into a wall because I can't tell where |
The declaration of // implemented in package runtime
//go:noescape
func unsafe_New(*abi.Type) unsafe.Pointer Searching for //go:linkname reflect_unsafe_New reflect.unsafe_New
func reflect_unsafe_New(typ *_type) unsafe.Pointer {
return mallocgc(typ.Size_, typ, true)
} That is the implementation of That said I think that |
Change https://go.dev/cl/567897 mentions this issue: |
Yes this is what I was trying to point out, though I misattributed the issue to a downstream effect (unsafe_New) instead of walking back farther to the root cause cause (StructOf). I had forgotten that the runtime sometimes implements internal functions for packages so I did not consider looking elsewhere, and I agree that unsafe_New is a red herring. Thank you for explaining anyway, and for reading through the inaccurate terminology to decipher what I meant. Now I'm curious about the algorithm that the compiler uses to generate type descriptors. What do you think of the format of this playground example, where it prints out a description of the issue inline with the code that demonstrates it? |
The playground example was helpful, thanks. I sent a fix for the problem. |
What version of Go are you using (
go version
)?go version go1.10.1 linux/amd64
Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?GOARCH="amd64"
GOBIN=""
GOCACHE="/home/mero/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/mero"
GORACE=""
GOROOT="/home/mero/go"
GOTMPDIR=""
GOTOOLDIR="/home/mero/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build786947304=/tmp/go-build -gno-record-gcc-switches"
What did you do?
https://play.golang.org/p/NL_aQrr6F3V
What did you expect to see?
What did you see instead?
I believe
A()
andB()
should behave the same, but they don't.StructOf
seemingly creates a named field, even thoughAnonymous
is set to true. It is not possible, not to pass a name either, though, because thenStructOf
panics:The text was updated successfully, but these errors were encountered: