fix: convert literal nil to interface types
This commit is contained in:
12
_test/interface27.go
Normal file
12
_test/interface27.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
var errs = map[int]error{0: nil}
|
||||
|
||||
func main() {
|
||||
fmt.Println(errs)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// map[0:<nil>]
|
||||
12
_test/interface28.go
Normal file
12
_test/interface28.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
var errs = []error{nil}
|
||||
|
||||
func main() {
|
||||
fmt.Println(errs)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// [<nil>]
|
||||
@@ -2393,14 +2393,16 @@ func recv2(n *node) {
|
||||
}
|
||||
|
||||
func convertLiteralValue(n *node, t reflect.Type) {
|
||||
// Skip non-constant values, undefined target type or interface target type.
|
||||
if !(n.kind == basicLit || n.rval.IsValid()) || t == nil || t.Kind() == reflect.Interface {
|
||||
return
|
||||
}
|
||||
if n.rval.IsValid() {
|
||||
switch {
|
||||
case n.typ.cat == nilT:
|
||||
// Create a zero value of target type.
|
||||
n.rval = reflect.New(t).Elem()
|
||||
case !(n.kind == basicLit || n.rval.IsValid()) || t == nil || t.Kind() == reflect.Interface:
|
||||
// Skip non-constant values, undefined target type or interface target type.
|
||||
case n.rval.IsValid():
|
||||
// Convert constant value to target type.
|
||||
n.rval = n.rval.Convert(t)
|
||||
} else {
|
||||
default:
|
||||
// Create a zero value of target type.
|
||||
n.rval = reflect.New(t).Elem()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user