interp: fix handling of empty interfaces in map index expressions

There should be no need now to wrap empty interfaces in order to
retrieve its value.

Fixes #1344.
This commit is contained in:
Marc Vertes
2022-05-19 18:08:09 +02:00
committed by GitHub
parent d183f4205e
commit 25edcfee7a
2 changed files with 13 additions and 34 deletions

13
_test/issue-1344.go Normal file
View File

@@ -0,0 +1,13 @@
package main
import "fmt"
func main() {
var m = map[string]interface{}{"a": "a"}
a, _ := m["a"]
b, ok := a.(string)
fmt.Println("a:", a, ", b:", b, ", ok:", ok)
}
// Output:
// a: a , b: a , ok: true

View File

@@ -1766,9 +1766,6 @@ func getIndexArray(n *node) {
}
}
// valueInterfaceType is the reflection type of valueInterface.
var valueInterfaceType = reflect.TypeOf((*valueInterface)(nil)).Elem()
// getIndexMap retrieves map value from index.
func getIndexMap(n *node) {
dest := genValue(n)
@@ -1833,7 +1830,6 @@ func getIndexMap2(n *node) {
value0 := genValue(n.child[0]) // map
value2 := genValue(n.anc.child[1]) // status
next := getExec(n.tnext)
typ := n.anc.child[0].typ
doValue := n.anc.child[0].ident != "_"
doStatus := n.anc.child[1].ident != "_"
@@ -1850,21 +1846,6 @@ func getIndexMap2(n *node) {
value2(f).SetBool(v.IsValid())
return next
}
case isInterfaceSrc(typ):
n.exec = func(f *frame) bltn {
v := value0(f).MapIndex(mi)
if v.IsValid() {
if e := v.Elem(); e.Type().AssignableTo(valueInterfaceType) {
dest(f).Set(e)
} else {
dest(f).Set(reflect.ValueOf(valueInterface{n, e}))
}
}
if doStatus {
value2(f).SetBool(v.IsValid())
}
return next
}
default:
n.exec = func(f *frame) bltn {
v := value0(f).MapIndex(mi)
@@ -1886,21 +1867,6 @@ func getIndexMap2(n *node) {
value2(f).SetBool(v.IsValid())
return next
}
case isInterfaceSrc(typ):
n.exec = func(f *frame) bltn {
v := value0(f).MapIndex(value1(f))
if v.IsValid() {
if e := v.Elem(); e.Type().AssignableTo(valueInterfaceType) {
dest(f).Set(e)
} else {
dest(f).Set(reflect.ValueOf(valueInterface{n, e}))
}
}
if doStatus {
value2(f).SetBool(v.IsValid())
}
return next
}
default:
n.exec = func(f *frame) bltn {
v := value0(f).MapIndex(value1(f))