diff mbox

Backport reflect.Call fixes to 4.8 branch

Message ID 874n6xh2ke.fsf@canonical.com
State New
Headers show

Commit Message

Michael Hudson-Doyle Nov. 27, 2013, 11:01 p.m. UTC
This patch brings the recent fix for calling a function or method that
takes or returns an empty struct via reflection to the 4.8 branch.

Cheers,
mwh

Comments

Ian Lance Taylor Dec. 3, 2013, 1:51 a.m. UTC | #1
On Wed, Nov 27, 2013 at 3:01 PM, Michael Hudson-Doyle
<michael.hudson@linaro.org> wrote:
> This patch brings the recent fix for calling a function or method that
> takes or returns an empty struct via reflection to the 4.8 branch.

Thanks.  Committed to 4.8 branch.

Ian
diff mbox

Patch

diff --git a/libgo/go/reflect/all_test.go b/libgo/go/reflect/all_test.go
index 526f09b..eecc459 100644
--- a/libgo/go/reflect/all_test.go
+++ b/libgo/go/reflect/all_test.go
@@ -1430,6 +1430,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 5cf3707..12bd0d7 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 *));