diff options
author | Ian Lance Taylor <iant@google.com> | 2013-11-19 02:30:03 +0000 |
---|---|---|
committer | Ian Lance Taylor <iant@google.com> | 2013-11-19 02:30:03 +0000 |
commit | 09c396a8fb4ae0fc86677e6578f51480a06b7fe0 (patch) | |
tree | 16f6d5875810d941d00dcc7096e6e77ae76ce1bf /libgo | |
parent | f8b4200d9b92bce00ef51447eb078d183ef6b323 (diff) |
reflect: Handle calls to functions that take or return empty structs
Fixes issue 6761
This simple change seems to work fine, slightly to my surprise.
This includes the tests I submitted to the main Go repository at
https://codereview.appspot.com/26570046
git-svn-id: https://gcc.gnu.org/svn/gcc/trunk@205001 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libgo')
-rw-r--r-- | libgo/go/reflect/all_test.go | 40 | ||||
-rw-r--r-- | libgo/runtime/go-reflect-call.c | 5 |
2 files changed, 44 insertions, 1 deletions
diff --git a/libgo/go/reflect/all_test.go b/libgo/go/reflect/all_test.go index 6ab02f7d854..918adce887f 100644 --- a/libgo/go/reflect/all_test.go +++ b/libgo/go/reflect/all_test.go @@ -1434,6 +1434,46 @@ func TestFunc(t *testing.T) { } } +type emptyStruct struct{} + +type nonEmptyStruct struct { + member int +} + +func returnEmpty() emptyStruct { + return emptyStruct{} +} + +func takesEmpty(e emptyStruct) { +} + +func returnNonEmpty(i int) nonEmptyStruct { + return nonEmptyStruct{member: i} +} + +func takesNonEmpty(n nonEmptyStruct) int { + return n.member +} + +func TestCallWithStruct(t *testing.T) { + r := ValueOf(returnEmpty).Call([]Value{}) + if len(r) != 1 || r[0].Type() != TypeOf(emptyStruct{}) { + t.Errorf("returning empty struct returned %s instead", r) + } + r = ValueOf(takesEmpty).Call([]Value{ValueOf(emptyStruct{})}) + if len(r) != 0 { + t.Errorf("takesEmpty returned values: %s", r) + } + r = ValueOf(returnNonEmpty).Call([]Value{ValueOf(42)}) + if len(r) != 1 || r[0].Type() != TypeOf(nonEmptyStruct{}) || r[0].Field(0).Int() != 42 { + t.Errorf("returnNonEmpty returned %s", r) + } + r = ValueOf(takesNonEmpty).Call([]Value{ValueOf(nonEmptyStruct{member: 42})}) + if len(r) != 1 || r[0].Type() != TypeOf(1) || r[0].Int() != 42 { + t.Errorf("takesNonEmpty returned %s", r) + } +} + func TestMakeFunc(t *testing.T) { switch runtime.GOARCH { case "amd64", "386": diff --git a/libgo/runtime/go-reflect-call.c b/libgo/runtime/go-reflect-call.c index 0fed68a50e7..07b99d7433b 100644 --- a/libgo/runtime/go-reflect-call.c +++ b/libgo/runtime/go-reflect-call.c @@ -98,9 +98,12 @@ go_struct_to_ffi (const struct __go_struct_type *descriptor) const struct __go_struct_field *fields; int i; + field_count = descriptor->__fields.__count; + if (field_count == 0) { + return &ffi_type_void; + } ret = (ffi_type *) __go_alloc (sizeof (ffi_type)); ret->type = FFI_TYPE_STRUCT; - field_count = descriptor->__fields.__count; fields = (const struct __go_struct_field *) descriptor->__fields.__values; ret->elements = (ffi_type **) __go_alloc ((field_count + 1) * sizeof (ffi_type *)); |