Skip to content

Commit

Permalink
fix: allow reuse of _ symbol in the same scope
Browse files Browse the repository at this point in the history
  • Loading branch information
mvertes authored Feb 3, 2020
1 parent 137b165 commit 712891d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
11 changes: 11 additions & 0 deletions _test/var10.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

var _ = true
var _ = "hello"

func main() {
println("hello")
}

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

var a, _, _, b = 1, true, "foo", 2

func main() {
println(a, b)
}

// Output:
// 1 2
16 changes: 13 additions & 3 deletions interp/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,17 @@ func assign(n *node) {
}

if n.nleft == 1 {
if s, d, i := svalue[0], dvalue[0], ivalue[0]; i != nil {
switch s, d, i := svalue[0], dvalue[0], ivalue[0]; {
case n.child[0].ident == "_":
n.exec = func(f *frame) bltn {
return next
}
case i != nil:
n.exec = func(f *frame) bltn {
d(f).SetMapIndex(i(f), s(f))
return next
}
} else {
default:
n.exec = func(f *frame) bltn {
d(f).Set(s(f))
return next
Expand All @@ -316,7 +321,6 @@ func assign(n *node) {
default:
t = typ.TypeOf()
}
//types[i] = n.child[sbase+i].typ.TypeOf()
types[i] = t
}

Expand All @@ -326,10 +330,16 @@ func assign(n *node) {
n.exec = func(f *frame) bltn {
t := make([]reflect.Value, len(svalue))
for i, s := range svalue {
if n.child[i].ident == "_" {
continue
}
t[i] = reflect.New(types[i]).Elem()
t[i].Set(s(f))
}
for i, d := range dvalue {
if n.child[i].ident == "_" {
continue
}
if j := ivalue[i]; j != nil {
d(f).SetMapIndex(j(f), t[i]) // Assign a map entry
} else {
Expand Down

0 comments on commit 712891d

Please sign in to comment.