Skip to content

Commit

Permalink
fix: do not attempt to store data in _ var
Browse files Browse the repository at this point in the history
  • Loading branch information
mvertes authored Mar 9, 2020
1 parent 1ae2649 commit c7c1bea
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 9 deletions.
15 changes: 15 additions & 0 deletions _test/map21.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

var m = map[int]string{
1: "foo",
}

func main() {
var ok bool
if _, ok = m[1]; ok {
println("ok")
}
}

// Output:
// ok
14 changes: 14 additions & 0 deletions _test/map22.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

var m = map[int]string{
1: "foo",
}

func main() {
var s string
s, _ = m[1]
println(s)
}

// Output:
// foo
13 changes: 13 additions & 0 deletions _test/map23.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

var m = map[int]string{
1: "foo",
}

func main() {
_, _ = m[1]
println("ok")
}

// Output:
// ok
44 changes: 36 additions & 8 deletions interp/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -1096,46 +1096,74 @@ func getIndexMap2(n *node) {
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 != "_"

if !doValue && !doStatus {
nop(n)
return
}
if n.child[1].rval.IsValid() { // constant map index
mi := n.child[1].rval
if typ.cat == interfaceT {
switch {
case !doValue:
n.exec = func(f *frame) bltn {
v := value0(f).MapIndex(mi)
value2(f).SetBool(v.IsValid())
return next
}
case typ.cat == interfaceT:
n.exec = func(f *frame) bltn {
v := value0(f).MapIndex(mi)
if v.IsValid() {
dest(f).Set(v.Elem())
}
value2(f).SetBool(v.IsValid())
if doStatus {
value2(f).SetBool(v.IsValid())
}
return next
}
} else {
default:
n.exec = func(f *frame) bltn {
v := value0(f).MapIndex(mi)
if v.IsValid() {
dest(f).Set(v)
}
value2(f).SetBool(v.IsValid())
if doStatus {
value2(f).SetBool(v.IsValid())
}
return next
}
}
} else {
value1 := genValue(n.child[1]) // map index
if typ.cat == interfaceT {
switch {
case !doValue:
n.exec = func(f *frame) bltn {
v := value0(f).MapIndex(value1(f))
value2(f).SetBool(v.IsValid())
return next
}
case typ.cat == interfaceT:
n.exec = func(f *frame) bltn {
v := value0(f).MapIndex(value1(f))
if v.IsValid() {
dest(f).Set(v.Elem())
}
value2(f).SetBool(v.IsValid())
if doStatus {
value2(f).SetBool(v.IsValid())
}
return next
}
} else {
default:
n.exec = func(f *frame) bltn {
v := value0(f).MapIndex(value1(f))
if v.IsValid() {
dest(f).Set(v)
}
value2(f).SetBool(v.IsValid())
if doStatus {
value2(f).SetBool(v.IsValid())
}
return next
}
}
Expand Down
2 changes: 1 addition & 1 deletion interp/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func genValueInterface(n *node) func(*frame) reflect.Value {
return func(f *frame) reflect.Value {
v := value(f)
nod := n
for {
for v.IsValid() {
// traverse interface indirections to find out concrete type
vi, ok := v.Interface().(valueInterface)
if !ok {
Expand Down

0 comments on commit c7c1bea

Please sign in to comment.