Skip to content

Commit 6c754dc

Browse files
committed
Fixed type checks of functions
1 parent e8727eb commit 6c754dc

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

type.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type Type = reflect.Type
1111
type typesTable map[string]Type
1212

1313
var (
14+
nilType = reflect.TypeOf(nil)
1415
boolType = reflect.TypeOf(true)
1516
numberType = reflect.TypeOf(float64(0))
1617
textType = reflect.TypeOf("")
@@ -393,7 +394,10 @@ func funcType(ntype Type) (Type, bool) {
393394
case reflect.Interface:
394395
return interfaceType, true
395396
case reflect.Func:
396-
return ntype, true
397+
if ntype.NumOut() > 0 {
398+
return ntype.Out(0), true
399+
}
400+
return nilType, true
397401
}
398402

399403
return nil, false

type_test.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ var typeTests = []typeTest{
7171
"Int in Int..Int",
7272
"FieldStr == ''",
7373
"FieldStr2 == ''",
74+
"OkFn() and OkFn()",
75+
"Foo.Fn() or Foo.Fn()",
7476
}
7577

7678
var typeErrorTests = []typeErrorTest{
@@ -262,6 +264,10 @@ var typeErrorTests = []typeErrorTest{
262264
"Int .. Ok",
263265
"invalid operation: Int .. Ok (mismatched types int and bool)",
264266
},
267+
{
268+
"NilFn() and OkFn()",
269+
"invalid operation: NilFn() and OkFn() (mismatched types <nil> and bool)",
270+
},
265271
}
266272

267273
type abc interface {
@@ -272,7 +278,7 @@ type bar struct {
272278
}
273279
type foo struct {
274280
Bar bar
275-
Fn func()
281+
Fn func() bool
276282
Abc abc
277283
}
278284

@@ -304,6 +310,8 @@ type payload struct {
304310
IntPtr *int
305311
StrPtr *string
306312
Foo2p **foo
313+
OkFn func() bool
314+
NilFn func()
307315
}
308316

309317
func TestType(t *testing.T) {

0 commit comments

Comments
 (0)