Skip to content

Commit

Permalink
fix: improve handling of nil
Browse files Browse the repository at this point in the history
  • Loading branch information
mvertes committed Apr 25, 2020
1 parent de8cb7d commit a6389ac
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
12 changes: 12 additions & 0 deletions _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
21 changes: 18 additions & 3 deletions interp/type.go
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 }

0 comments on commit a6389ac

Please sign in to comment.