fix: correct comparison of interface type to nil

This commit is contained in:
Marc Vertes
2020-03-25 13:56:05 +01:00
committed by GitHub
parent ebde09b47d
commit 7327ff2811
3 changed files with 80 additions and 24 deletions

9
_test/interface29.go Normal file
View File

@@ -0,0 +1,9 @@
package main
func main() {
var a interface{}
println(a == nil)
}
// Output:
// true

9
_test/interface30.go Normal file
View File

@@ -0,0 +1,9 @@
package main
func main() {
var a interface{}
println(a != nil)
}
// Output:
// false

View File

@@ -2594,56 +2594,94 @@ func slice0(n *node) {
func isNil(n *node) {
var value func(*frame) reflect.Value
if n.child[0].typ.cat == funcT {
value = genValueAsFunctionWrapper(n.child[0])
c0 := n.child[0]
if c0.typ.cat == funcT {
value = genValueAsFunctionWrapper(c0)
} else {
value = genValue(n.child[0])
value = genValue(c0)
}
tnext := getExec(n.tnext)
dest := genValue(n)
if n.fnext != nil {
fnext := getExec(n.fnext)
n.exec = func(f *frame) bltn {
if value(f).IsNil() {
dest(f).SetBool(true)
return tnext
if c0.typ.cat == interfaceT {
n.exec = func(f *frame) bltn {
if (value(f).Interface().(valueInterface) == valueInterface{}) {
dest(f).SetBool(true)
return tnext
}
dest(f).SetBool(false)
return fnext
}
} else {
n.exec = func(f *frame) bltn {
if value(f).IsNil() {
dest(f).SetBool(true)
return tnext
}
dest(f).SetBool(false)
return fnext
}
dest(f).SetBool(false)
return fnext
}
} else {
n.exec = func(f *frame) bltn {
dest(f).SetBool(value(f).IsNil())
return tnext
if c0.typ.cat == interfaceT {
n.exec = func(f *frame) bltn {
dest(f).SetBool(value(f).Interface().(valueInterface) == valueInterface{})
return tnext
}
} else {
n.exec = func(f *frame) bltn {
dest(f).SetBool(value(f).IsNil())
return tnext
}
}
}
}
func isNotNil(n *node) {
var value func(*frame) reflect.Value
if n.child[0].typ.cat == funcT {
value = genValueAsFunctionWrapper(n.child[0])
c0 := n.child[0]
if c0.typ.cat == funcT {
value = genValueAsFunctionWrapper(c0)
} else {
value = genValue(n.child[0])
value = genValue(c0)
}
tnext := getExec(n.tnext)
dest := genValue(n)
if n.fnext != nil {
fnext := getExec(n.fnext)
n.exec = func(f *frame) bltn {
if value(f).IsNil() {
dest(f).SetBool(false)
return fnext
if c0.typ.cat == interfaceT {
n.exec = func(f *frame) bltn {
if (value(f).Interface().(valueInterface) == valueInterface{}) {
dest(f).SetBool(false)
return fnext
}
dest(f).SetBool(true)
return tnext
}
} else {
n.exec = func(f *frame) bltn {
if value(f).IsNil() {
dest(f).SetBool(false)
return fnext
}
dest(f).SetBool(true)
return tnext
}
dest(f).SetBool(true)
return tnext
}
} else {
n.exec = func(f *frame) bltn {
dest(f).SetBool(!value(f).IsNil())
return tnext
if c0.typ.cat == interfaceT {
n.exec = func(f *frame) bltn {
dest(f).SetBool(!(value(f).Interface().(valueInterface) == valueInterface{}))
return tnext
}
} else {
n.exec = func(f *frame) bltn {
dest(f).SetBool(!value(f).IsNil())
return tnext
}
}
}
}