interp: do not wrap empty interface
The empty interface (interface{}), and its variants (such as []interface{} and map[string]interface{}), are commonly used in Go to (json) Unmarshal arbitrary data. Within Yaegi, all interface types are wrapped in a valueInterface struct in order to retain all the information needed for a consistent internal state (as reflect is not enough to achieve that). However, this wrapping ends up being problematic when it comes to the type assertions related to the aforementioned Unmarshaling.
Therefore, this PR is an attempt to consider the empty interface (and its variants) as an exception within Yaegi, that should never be wrapped within a valueInterface, and to treat it similarly to the other basic Go types. The assumption is that the wrapping should not be needed, as there is no information about implemented methods to maintain.
Fixes #984
Fixes #829
Fixes #1015
This commit is contained in:
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -10,6 +9,10 @@ type MyWriter interface {
|
||||
Write(p []byte) (i int, err error)
|
||||
}
|
||||
|
||||
type DummyWriter interface {
|
||||
Write(p []byte) (i int, err error)
|
||||
}
|
||||
|
||||
type TestStruct struct{}
|
||||
|
||||
func (t TestStruct) Write(p []byte) (n int, err error) {
|
||||
@@ -25,14 +28,18 @@ type MyStringer interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
type DummyStringer interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
func usesStringer(s MyStringer) {
|
||||
fmt.Println(s.String())
|
||||
}
|
||||
|
||||
func main() {
|
||||
aType := reflect.TypeOf((*MyWriter)(nil)).Elem()
|
||||
|
||||
var t interface{}
|
||||
// TODO(mpl): restore when we can deal with empty interface.
|
||||
// var t interface{}
|
||||
var t DummyWriter
|
||||
t = TestStruct{}
|
||||
var tw MyWriter
|
||||
var ok bool
|
||||
@@ -45,8 +52,6 @@ func main() {
|
||||
}
|
||||
n, _ := t.(MyWriter).Write([]byte("hello world"))
|
||||
fmt.Println(n)
|
||||
bType := reflect.TypeOf(TestStruct{})
|
||||
fmt.Println(bType.Implements(aType))
|
||||
|
||||
// not redundant with the above, because it goes through a slightly different code path.
|
||||
if _, ok := t.(MyWriter); !ok {
|
||||
@@ -56,6 +61,8 @@ func main() {
|
||||
fmt.Println("TestStruct implements MyWriter")
|
||||
}
|
||||
|
||||
// TODO(mpl): restore
|
||||
/*
|
||||
t = 42
|
||||
foo, ok := t.(MyWriter)
|
||||
if !ok {
|
||||
@@ -70,8 +77,10 @@ func main() {
|
||||
} else {
|
||||
fmt.Println("42 implements MyWriter")
|
||||
}
|
||||
*/
|
||||
|
||||
var tt interface{}
|
||||
// var tt interface{}
|
||||
var tt DummyStringer
|
||||
tt = time.Nanosecond
|
||||
var myD MyStringer
|
||||
myD, ok = tt.(MyStringer)
|
||||
@@ -82,9 +91,6 @@ func main() {
|
||||
usesStringer(myD)
|
||||
}
|
||||
fmt.Println(tt.(MyStringer).String())
|
||||
cType := reflect.TypeOf((*MyStringer)(nil)).Elem()
|
||||
dType := reflect.TypeOf(time.Nanosecond)
|
||||
fmt.Println(dType.Implements(cType))
|
||||
|
||||
if _, ok := tt.(MyStringer); !ok {
|
||||
fmt.Println("time.Nanosecond does not implement MyStringer")
|
||||
@@ -92,6 +98,8 @@ func main() {
|
||||
fmt.Println("time.Nanosecond implements MyStringer")
|
||||
}
|
||||
|
||||
// TODO(mpl): restore
|
||||
/*
|
||||
tt = 42
|
||||
bar, ok := tt.(MyStringer)
|
||||
if !ok {
|
||||
@@ -106,20 +114,15 @@ func main() {
|
||||
} else {
|
||||
fmt.Println("42 implements MyStringer")
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
// Output:
|
||||
// TestStruct implements MyWriter
|
||||
// 11
|
||||
// 11
|
||||
// true
|
||||
// TestStruct implements MyWriter
|
||||
// 42 does not implement MyWriter
|
||||
// 42 does not implement MyWriter
|
||||
// time.Nanosecond implements MyStringer
|
||||
// 1ns
|
||||
// 1ns
|
||||
// true
|
||||
// time.Nanosecond implements MyStringer
|
||||
// 42 does not implement MyStringer
|
||||
// 42 does not implement MyStringer
|
||||
|
||||
Reference in New Issue
Block a user