go: reflect: StructOf doesn't generate wrapper methods for embedded fields
% go version
go version devel +bbd1dcd Wed Jun 1 21:32:46 2016 +0000 linux/amd64
% cat ~/a.go
package main
import (
"bytes"
"io"
"reflect"
"sync"
)
func main() {
t := reflect.StructOf([]reflect.StructField{
{Type: reflect.TypeOf(sync.Mutex{}), Anonymous: true},
{Type: reflect.TypeOf(bytes.Buffer{}), Anonymous: true},
})
x := reflect.New(t).Interface()
_ = x.(io.Writer)
}
% go run ~/a.go
panic: interface conversion: *truct { sync.Mutex; bytes.Buffer } is not io.Writer: missing method Write
I expected that x would have generated wrapper methods for Mutex.Lock, etc.
Interestingly, adding this declaration inside func main (not at package level):
var _ interface{} = new(struct {
sync.Mutex
bytes.Buffer
})
causes the program to succeed. Presumably it causes the compiler to generate the correct type information, which reflect.StructOf then finds.
About this issue
- Original URL
- State: open
- Created 8 years ago
- Reactions: 3
- Comments: 16 (8 by maintainers)
Commits related to this issue
- reflect: document StructOf embedded fields limitation This CL documents that StructOf currently does not generate wrapper methods for embedded fields. Updates #15924 Change-Id: I932011b1491d6876770... — committed to mk0x9/go by sbinet 8 years ago
- reflect: make StructOf panic for methods that don't work When StructOf is used with an anonymous field that has methods, and that anonymous field is not the first field, the methods we generate are i... — committed to golang/go by ianlancetaylor 7 years ago
- reflect: prevent additional StructOf embedded method cases The current implementation does not generate wrappers for methods of embedded non-interface types. We can only skip the wrapper if kindDirec... — committed to golang/go by ianlancetaylor 6 years ago
- fix: make interpreter methods discoverable by runtime When generating an interface wrapper, lookup existing wrappers by method to get the one with the biggest set of methods implemented by interprete... — committed to traefik/yaegi by mvertes 4 years ago
- fix: make interpreter methods discoverable by runtime (#722) * fix: make interpreter methods discoverable by runtime When generating an interface wrapper, lookup existing wrappers by method to ge... — committed to traefik/yaegi by mvertes 4 years ago
- interp: improve handling of composed interfaces wrappers This change implements a workaround to better support composed interfaces in yaegi and let the interpreter to define objects which impletment ... — committed to mvertes/yaegi by mvertes 2 years ago
- interp: improve handling of composed interfaces wrappers This change implements a workaround to better support composed interfaces in yaegi and let the interpreter define objects which implement mu... — committed to traefik/yaegi by mvertes 2 years ago
- interp: improve handling of embedded fields with binary methods Only structures with one embedded field can be marked anonymous, due to golang/go#15924. Also check only that the method is defined, do... — committed to mvertes/yaegi by mvertes a year ago
- interp: improve handling of embedded fields with binary methods Only structures with one embedded field can be marked anonymous, due to golang/go#15924. Also check only that the method is defined, do... — committed to traefik/yaegi by mvertes a year ago
- Apologies for the large PR, but I think in this case it's important to present all the pieces at the same time. This PR creates a system for supporting YAML files using different `api_version`s from ... — committed to abcxyz/abc by drevell 9 months ago
- Support multiple api_versions in the same codebase (#228) Apologies for the large PR, but I think in this case it's important to present all the pieces at the same time. This PR creates a system ... — committed to abcxyz/abc by drevell 9 months ago
while implementing
reflect.NamedOf, I noticed thatreflect.StructOfcreates wrapper methods for embedded interfaces, and I was surprised that declaring methods at runtime would simply require callingreflect.MakeFunc(see https://github.com/golang/go/blob/master/src/reflect/type.go#L2386).Alas, it seems not.
The following example crashes with SIGSEGV on both my local Go 1.11.4 installation and on playground https://play.golang.org/p/jny1YKZsHlp
on my local Go 1.11.4, the output is:
on playground https://play.golang.org/p/jny1YKZsHlp the output is instead:
I would expect that either
reflect.StructOffails (due to unsupported interface embedding) or that its returned type does not implementfmt.Stringer- I was surely not expecting that the method call crashes at runtime.