From a6389aca5ef4ba058ba4aa44f4d6d7e5567c49dc Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sat, 25 Apr 2020 17:16:03 +0200 Subject: [PATCH] fix: improve handling of nil --- _test/nil2.go | 12 ++++++++++++ interp/type.go | 21 ++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 _test/nil2.go diff --git a/_test/nil2.go b/_test/nil2.go new file mode 100644 index 00000000..05e897df --- /dev/null +++ b/_test/nil2.go @@ -0,0 +1,12 @@ +package main + +func test() error { return nil } + +func main() { + if err := test(); nil == err { + println("err is nil") + } +} + +// Output: +// err is nil diff --git a/interp/type.go b/interp/type.go index 7c29d5bb..0bc74849 100644 --- a/interp/type.go +++ b/interp/type.go @@ -1204,6 +1204,9 @@ func isStruct(t *itype) bool { func isBool(t *itype) bool { return t.TypeOf().Kind() == reflect.Bool } func isInt(t reflect.Type) bool { + if t == nil { + return false + } switch t.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: return true @@ -1212,6 +1215,9 @@ func isInt(t reflect.Type) bool { } func isUint(t reflect.Type) bool { + if t == nil { + return false + } switch t.Kind() { case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: return true @@ -1220,6 +1226,9 @@ func isUint(t reflect.Type) bool { } func isComplex(t reflect.Type) bool { + if t == nil { + return false + } switch t.Kind() { case reflect.Complex64, reflect.Complex128: return true @@ -1228,6 +1237,9 @@ func isComplex(t reflect.Type) bool { } func isFloat(t reflect.Type) bool { + if t == nil { + return false + } switch t.Kind() { case reflect.Float32, reflect.Float64: return true @@ -1236,11 +1248,14 @@ func isFloat(t reflect.Type) bool { } func isByteArray(t reflect.Type) bool { + if t == nil { + return false + } k := t.Kind() return (k == reflect.Array || k == reflect.Slice) && t.Elem().Kind() == reflect.Uint8 } -func isFloat32(t reflect.Type) bool { return t.Kind() == reflect.Float32 } -func isFloat64(t reflect.Type) bool { return t.Kind() == reflect.Float64 } +func isFloat32(t reflect.Type) bool { return t != nil && t.Kind() == reflect.Float32 } +func isFloat64(t reflect.Type) bool { return t != nil && t.Kind() == reflect.Float64 } func isNumber(t reflect.Type) bool { return isInt(t) || isFloat(t) || isComplex(t) } -func isString(t reflect.Type) bool { return t.Kind() == reflect.String } +func isString(t reflect.Type) bool { return t != nil && t.Kind() == reflect.String }