fix: handle printf changing nil type

This commit is contained in:
Nicholas Wiersma
2020-06-03 17:36:04 +02:00
committed by GitHub
parent 6f878052f8
commit f1cff308e6
3 changed files with 56 additions and 3 deletions

22
_test/recover3.go Normal file
View File

@@ -0,0 +1,22 @@
package main
import "fmt"
func main() {
println("hello")
var r interface{} = 1
r = recover()
fmt.Printf("%v\n", r)
if r == nil {
println("world")
}
if r != nil {
println("exception")
}
}
// Output:
// hello
// <nil>
// world

25
_test/recover4.go Normal file
View File

@@ -0,0 +1,25 @@
package main
import "fmt"
func div(a, b int) (result int) {
defer func() {
r := recover()
fmt.Printf("r = %#v\n", r)
if r != nil {
result = 0
}
}()
return a / b
}
func main() {
println(div(30, 2))
}
// Output:
// r = <nil>
// 15

View File

@@ -465,7 +465,9 @@ func _recover(n *node) {
dest := genValue(n)
n.exec = func(f *frame) bltn {
if f.anc.recovered != nil {
if f.anc.recovered == nil {
dest(f).Set(reflect.ValueOf(valueInterface{}))
} else {
dest(f).Set(reflect.ValueOf(valueInterface{n, reflect.ValueOf(f.anc.recovered)}))
f.anc.recovered = nil
}
@@ -2766,7 +2768,9 @@ func isNil(n *node) {
fnext := getExec(n.fnext)
if c0.typ.cat == interfaceT {
n.exec = func(f *frame) bltn {
if (value(f).Interface().(valueInterface) == valueInterface{}) {
vi := value(f).Interface().(valueInterface)
if (vi == valueInterface{} ||
vi.node.kind == basicLit && vi.node.typ.cat == nilT) {
dest(f).SetBool(true)
return tnext
}
@@ -2813,7 +2817,9 @@ func isNotNil(n *node) {
fnext := getExec(n.fnext)
if c0.typ.cat == interfaceT {
n.exec = func(f *frame) bltn {
if (value(f).Interface().(valueInterface) == valueInterface{}) {
vi := value(f).Interface().(valueInterface)
if (vi == valueInterface{} ||
vi.node.kind == basicLit && vi.node.typ.cat == nilT) {
dest(f).SetBool(false)
return fnext
}