1
0
mirror of https://github.com/golang/go synced 2024-11-18 13:04:46 -07:00

do not create interfaces containing interfaces

R=r
DELTA=16  (14 added, 0 deleted, 2 changed)
OCL=27464
CL=27466
This commit is contained in:
Russ Cox 2009-04-14 19:03:57 -07:00
parent ff73221d6f
commit 4b8c13dc20
3 changed files with 16 additions and 2 deletions

View File

@ -310,6 +310,12 @@ func TestInterfaceValue(t *testing.T) {
assert(v2.Type().String(), "interface { }"); assert(v2.Type().String(), "interface { }");
v3 := v2.(reflect.InterfaceValue).Value(); v3 := v2.(reflect.InterfaceValue).Value();
assert(v3.Type().String(), "float"); assert(v3.Type().String(), "float");
i3 := v2.Interface();
if f, ok := i3.(float); !ok {
a, typ, c := sys.Reflect(i3);
t.Error("v2.Interface() did not return float, got ", typ);
}
} }
func TestFunctionValue(t *testing.T) { func TestFunctionValue(t *testing.T) {

View File

@ -58,9 +58,12 @@ func (c *commonValue) Addr() Addr {
func (c *commonValue) Interface() interface {} { func (c *commonValue) Interface() interface {} {
var i interface {}; var i interface {};
if c.typ.Size() > 8 { // TODO(rsc): how do we know it is 8? switch {
case c.typ.Kind() == InterfaceKind:
i = *(*interface{})(c.addr);
case c.typ.Size() > 8: // TODO(rsc): how do we know it is 8?
i = sys.Unreflect(uint64(uintptr(c.addr)), c.typ.String(), true); i = sys.Unreflect(uint64(uintptr(c.addr)), c.typ.String(), true);
} else { default:
if uintptr(c.addr) == 0 { if uintptr(c.addr) == 0 {
panicln("reflect: address 0 for", c.typ.String()); panicln("reflect: address 0 for", c.typ.String());
} }

View File

@ -766,6 +766,11 @@ sys·Unreflect(uint64 it, String type, bool indir, Iface ret)
if(cmpstring(type, emptystring) == 0) if(cmpstring(type, emptystring) == 0)
goto out; goto out;
if(type.len > 10 && mcmp(type.str, (byte*)"interface ", 10) == 0) {
printf("sys.Unreflect: cannot put %S in interface\n", type);
throw("sys.Unreflect");
}
// if we think the type should be indirect // if we think the type should be indirect
// and caller does not, play it safe, return nil. // and caller does not, play it safe, return nil.
sigt = findtype(type, indir); sigt = findtype(type, indir);