Skip to content

Commit

Permalink
interp: fix handling of empty interfaces in map index expressions
Browse files Browse the repository at this point in the history
There should be no need now to wrap empty interfaces in order to
retrieve its value.

Fixes #1344.
  • Loading branch information
mvertes committed May 19, 2022
1 parent d183f42 commit 25edcfe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 34 deletions.
13 changes: 13 additions & 0 deletions _test/issue-1344.go
Original file line number Diff line number Diff line change
@@ -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
34 changes: 0 additions & 34 deletions interp/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 != "_"

Expand All @@ -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)
Expand All @@ -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))
Expand Down

0 comments on commit 25edcfe

Please sign in to comment.