Skip to content

Commit f7c0466

Browse files
authored
compiler,reflect: fix NumMethods for Interface type
1 parent 81ce7fb commit f7c0466

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

compiler/interface.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,19 @@ func (c *compilerContext) pkgPathPtr(pkgpath string) llvm.Value {
124124
func (c *compilerContext) getTypeCode(typ types.Type) llvm.Value {
125125
ms := c.program.MethodSets.MethodSet(typ)
126126
hasMethodSet := ms.Len() != 0
127-
if _, ok := typ.Underlying().(*types.Interface); ok {
127+
_, isInterface := typ.Underlying().(*types.Interface)
128+
if isInterface {
128129
hasMethodSet = false
129130
}
130131

132+
// As defined in https://pkg.go.dev/reflect#Type:
133+
// NumMethod returns the number of methods accessible using Method.
134+
// For a non-interface type, it returns the number of exported methods.
135+
// For an interface type, it returns the number of exported and unexported methods.
131136
var numMethods int
132-
if hasMethodSet {
133-
for i := 0; i < ms.Len(); i++ {
134-
if ms.At(i).Obj().Exported() {
135-
numMethods++
136-
}
137+
for i := 0; i < ms.Len(); i++ {
138+
if isInterface || ms.At(i).Obj().Exported() {
139+
numMethods++
137140
}
138141
}
139142

compiler/testdata/interface.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ target triple = "wasm32-unknown-wasi"
99
@"reflect/types.type:basic:int" = linkonce_odr constant { i8, ptr } { i8 -62, ptr @"reflect/types.type:pointer:basic:int" }, align 4
1010
@"reflect/types.type:pointer:basic:int" = linkonce_odr constant { i8, i16, ptr } { i8 -43, i16 0, ptr @"reflect/types.type:basic:int" }, align 4
1111
@"reflect/types.type:pointer:named:error" = linkonce_odr constant { i8, i16, ptr } { i8 -43, i16 0, ptr @"reflect/types.type:named:error" }, align 4
12-
@"reflect/types.type:named:error" = linkonce_odr constant { i8, i16, ptr, ptr, ptr, [7 x i8] } { i8 116, i16 0, ptr @"reflect/types.type:pointer:named:error", ptr @"reflect/types.type:interface:{Error:func:{}{basic:string}}", ptr @"reflect/types.type.pkgpath.empty", [7 x i8] c".error\00" }, align 4
12+
@"reflect/types.type:named:error" = linkonce_odr constant { i8, i16, ptr, ptr, ptr, [7 x i8] } { i8 116, i16 1, ptr @"reflect/types.type:pointer:named:error", ptr @"reflect/types.type:interface:{Error:func:{}{basic:string}}", ptr @"reflect/types.type.pkgpath.empty", [7 x i8] c".error\00" }, align 4
1313
@"reflect/types.type.pkgpath.empty" = linkonce_odr unnamed_addr constant [1 x i8] zeroinitializer, align 1
1414
@"reflect/types.type:interface:{Error:func:{}{basic:string}}" = linkonce_odr constant { i8, ptr } { i8 84, ptr @"reflect/types.type:pointer:interface:{Error:func:{}{basic:string}}" }, align 4
1515
@"reflect/types.type:pointer:interface:{Error:func:{}{basic:string}}" = linkonce_odr constant { i8, i16, ptr } { i8 -43, i16 0, ptr @"reflect/types.type:interface:{Error:func:{}{basic:string}}" }, align 4

testdata/reflect.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ func showValue(rv reflect.Value, indent string) {
456456
case reflect.Interface:
457457
println(indent + " interface")
458458
println(indent+" nil:", rv.IsNil())
459+
println(indent+" NumMethod:", rv.NumMethod())
459460
if !rv.IsNil() {
460461
showValue(rv.Elem(), indent+" ")
461462
}

testdata/reflect.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ reflect type: ptr
8080
reflect type: interface settable=true addrable=true
8181
interface
8282
nil: true
83+
NumMethod: 1
8384
reflect type: ptr
8485
pointer: true int
8586
nil: false
@@ -240,6 +241,7 @@ reflect type: struct
240241
reflect type: interface caninterface=false
241242
interface
242243
nil: true
244+
NumMethod: 1
243245
reflect type: struct
244246
struct: 3
245247
field: 0 a
@@ -371,12 +373,14 @@ reflect type: slice comparable=false
371373
reflect type: interface settable=true addrable=true
372374
interface
373375
nil: false
376+
NumMethod: 0
374377
reflect type: int
375378
int: 3
376379
indexing: 1
377380
reflect type: interface settable=true addrable=true
378381
interface
379382
nil: false
383+
NumMethod: 0
380384
reflect type: string
381385
string: str 3
382386
reflect type: uint8
@@ -389,6 +393,7 @@ reflect type: slice comparable=false
389393
reflect type: interface settable=true addrable=true
390394
interface
391395
nil: false
396+
NumMethod: 0
392397
reflect type: complex128
393398
complex: (-4.000000e+000+2.500000e+000i)
394399
reflect type: ptr

0 commit comments

Comments
 (0)