]> Cypherpunks repositories - gostls13.git/commitdiff
reflect: add a test for Calling a Method of a direct interface type
authorCherry Zhang <cherryyz@google.com>
Wed, 8 May 2019 03:34:13 +0000 (23:34 -0400)
committerCherry Zhang <cherryyz@google.com>
Wed, 8 May 2019 13:56:52 +0000 (13:56 +0000)
Gccgo's implementation of direct interface types has bugs that
causes reflect Call of method from Type.Method fail. CL 175837
and CL 175798 fix the bug. This CL adds a test.

Change-Id: I4e5f2cb96304c1ac7be04ca6d2851bac52b8eb24
Reviewed-on: https://go-review.googlesource.com/c/go/+/175880
Run-TryBot: Cherry Zhang <cherryyz@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/reflect/all_test.go

index 2057530f0715a4e7aa185e589fd7373d26486983..0dbf4c5e877e50eb38047b1f832ef26516f9ca4f 100644 (file)
@@ -2298,6 +2298,39 @@ func TestVariadicMethodValue(t *testing.T) {
        }
 }
 
+type DirectIfaceT struct {
+       p *int
+}
+
+func (d DirectIfaceT) M() int { return *d.p }
+
+func TestDirectIfaceMethod(t *testing.T) {
+       x := 42
+       v := DirectIfaceT{&x}
+       typ := TypeOf(v)
+       m, ok := typ.MethodByName("M")
+       if !ok {
+               t.Fatalf("cannot find method M")
+       }
+       in := []Value{ValueOf(v)}
+       out := m.Func.Call(in)
+       if got := out[0].Int(); got != 42 {
+               t.Errorf("Call with value receiver got %d, want 42", got)
+       }
+
+       pv := &v
+       typ = TypeOf(pv)
+       m, ok = typ.MethodByName("M")
+       if !ok {
+               t.Fatalf("cannot find method M")
+       }
+       in = []Value{ValueOf(pv)}
+       out = m.Func.Call(in)
+       if got := out[0].Int(); got != 42 {
+               t.Errorf("Call with pointer receiver got %d, want 42", got)
+       }
+}
+
 // Reflect version of $GOROOT/test/method5.go
 
 // Concrete types implementing M method.