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

Open
Merovius opened this Issue Apr 9, 2018 · 3 comments

Comments

Projects
None yet
5 participants
@Merovius

Merovius commented Apr 9, 2018

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?

true
true

What did you see instead?

true
false
interface conversion: interface {} is struct { X main.X }, not struct { main.X }


I believe A() and B() should behave the same, but they don't. StructOf seemingly creates a named field, even though Anonymous is set to true. It is not possible, not to pass a name either, though, because then StructOf panics:

true
panic: reflect.StructOf: field 0 has no name

goroutine 1 [running]:
reflect.StructOf(0x1052ff5c, 0x1, 0x1, 0x1e5e20, 0x0, 0x0)
/usr/local/go/src/reflect/type.go:2404 +0x3fa0
main.B(0x122660, 0x1052ffa4)
/tmp/sandbox310635867/main.go:22 +0xc0
main.main()
/tmp/sandbox310635867/main.go:14 +0x60

@bcmills bcmills added this to the Go1.11 milestone Apr 9, 2018

@bcmills

This comment has been minimized.

Member

bcmills commented Apr 9, 2018

@griesemer

This comment has been minimized.

Contributor

griesemer commented Apr 9, 2018

Possibly related to #24721.

@jimmyfrasche

This comment has been minimized.

Member

jimmyfrasche commented Jun 22, 2018

I just ran into this: https://play.golang.org/p/rv5GDx0s02w

There seem to be three things going on

  1. If you create an anonymous field with no name, it should use the type name (feature request)
  2. Whether you give a name, it's not treating the field as anonymous (prints field name and type instead of just field name derived from type)
  3. #24721 as @griesemer said above
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment