reflect: Handle calls to functions that take or return empty structs
authorian <ian@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 19 Nov 2013 02:30:03 +0000 (02:30 +0000)
committerian <ian@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 19 Nov 2013 02:30:03 +0000 (02:30 +0000)
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: svn+ssh://gcc.gnu.org/svn/gcc/trunk@205001 138bc75d-0d04-0410-961f-82ee72b054a4

libgo/go/reflect/all_test.go
libgo/runtime/go-reflect-call.c

index 6ab02f7..918adce 100644 (file)
@@ -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":
index 0fed68a..07b99d7 100644 (file)
@@ -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 *));