Skip to content
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

Point value of struct with anonymous interface field created by reflect not AssignableTo the interface type #47343

Closed
xiezhenye opened this issue Jul 22, 2021 · 3 comments

Comments

@xiezhenye
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.16.5 darwin/arm64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/zhenyexie/Library/Caches/go-build"
GOENV="/Users/zhenyexie/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/zhenyexie/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/zhenyexie/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.16.5"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/zc/tvrbqknd2lq349yxhm1z3cch0000gn/T/go-build350205865=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

run

package main

import (
        "reflect"
)

type I1 interface {
        Get() string
}

type P struct {
        I1
}


type s string

func (s s) Get() string {
        return string(s)
}

func main() {
        var a I1
        typ := reflect.TypeOf(&a).Elem()
        pt := reflect.StructOf([]reflect.StructField{
                {
                        Name: typ.Name(),
                        Anonymous: true,
                        Type: typ,
                },
        })
        ptv := reflect.New(pt)
        println(ptv.Type().String())
        println(ptv.Type().AssignableTo(reflect.TypeOf(&a).Elem()))
        // equine to  &struct { I1 }{ s("aaa") }
}

What did you expect to see?

*struct { I1 main.I1 }
false

What did you see instead?

*struct { I1 main.I1 }
true

@bcmills
Copy link
Contributor

bcmills commented Jul 22, 2021

This looks like a known defect in reflect.StructOf. Per https://beta.pkg.go.dev/reflect#StructOf:

StructOf currently does not generate wrapper methods for embedded fields and panics if passed unexported StructFields. These limitations may be lifted in a future version.

@xiezhenye
Copy link
Author

println(ptv.Type().Elem().AssignableTo(reflect.TypeOf(&a).Elem())) // returns true

a1 := ptv.Elem().Interface()
a = a1.(I1)

a.Get() 
/*
panic: 
unexpected fault address 0x1400000c048
fatal error: fault
[signal SIGBUS: bus error code=0x1 addr=0x1400000c048 pc=0x1400000c048]

*/

@seankhliao
Copy link
Member

Duplicate of #15924

@seankhliao seankhliao marked this as a duplicate of #15924 Jul 25, 2021
@golang golang locked and limited conversation to collaborators Jul 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants