Skip to content

Commit

Permalink
Merge branch 'master' into loopvar
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoreilly authored Jul 19, 2024
2 parents c8bd682 + 81a9d11 commit 928bd9a
Show file tree
Hide file tree
Showing 193 changed files with 1,205 additions and 3,213 deletions.
9 changes: 9 additions & 0 deletions _test/assign19.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func main() {
a, b, c := 1, 2
_, _, _ = a, b, c
}

// Error:
// _test/assign19.go:4:2: cannot assign 2 values to 3 variables
23 changes: 23 additions & 0 deletions _test/issue-1640.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"errors"
)

func ShortVariableDeclarations() (i int, err error) {
r, err := 1, errors.New("test")
i = r
return
}

func main() {
_, er := ShortVariableDeclarations()
if er != nil {
println("ShortVariableDeclarations ok")
} else {
println("ShortVariableDeclarations not ok")
}
}

// Output:
// ShortVariableDeclarations ok
4 changes: 2 additions & 2 deletions extract/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ func init() {
if W.WString == nil {
return ""
}
{{end -}}
{{$m.Ret}} W.W{{$m.Name}}{{$m.Arg}}
{{end}}
{{- $m.Ret}} W.W{{$m.Name}}{{$m.Arg -}}
}
{{end}}
{{end}}
Expand Down
10 changes: 9 additions & 1 deletion interp/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,14 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
sbase = len(n.child) - n.nright
}

// If len(RHS) > 1, each node must be single-valued, and the nth expression
// on the right is assigned to the nth operand on the left, so the number of
// nodes on the left and right sides must be equal
if n.nright > 1 && n.nright != n.nleft {
err = n.cfgErrorf("cannot assign %d values to %d variables", n.nright, n.nleft)
return
}

wireChild(n)
for i := 0; i < n.nleft; i++ {
dest, src := n.child[i], n.child[sbase+i]
Expand Down Expand Up @@ -723,7 +731,7 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
if dest.typ.incomplete {
return
}
if sc.global {
if sc.global || sc.isRedeclared(dest) {
// Do not overload existing symbols (defined in GTA) in global scope.
sym, _, _ = sc.lookup(dest.ident)
}
Expand Down
1 change: 1 addition & 0 deletions interp/interp_consistent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestInterpConsistencyBuild(t *testing.T) {
file.Name() == "assign11.go" || // expect error
file.Name() == "assign12.go" || // expect error
file.Name() == "assign15.go" || // expect error
file.Name() == "assign19.go" || // expect error
file.Name() == "bad0.go" || // expect error
file.Name() == "break0.go" || // expect error
file.Name() == "cont3.go" || // expect error
Expand Down
59 changes: 59 additions & 0 deletions interp/interp_issue_1634_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package interp

import (
"bytes"
"io"
"os"
"reflect"
"testing"
)

func TestExportClosureArg(t *testing.T) {
outExp := []byte("0\n1\n2\n")
// catch stdout
backupStdout := os.Stdout
defer func() {
os.Stdout = backupStdout
}()
r, w, _ := os.Pipe()
os.Stdout = w

i := New(Options{})
err := i.Use(Exports{
"tmp/tmp": map[string]reflect.Value{
"Func": reflect.ValueOf(func(s *[]func(), f func()) { *s = append(*s, f) }),
},
})
if err != nil {
t.Error(err)
}
i.ImportUsed()

_, err = i.Eval(`
func main() {
fs := []func(){}
for i := 0; i < 3; i++ {
i := i
tmp.Func(&fs, func() { println(i) })
}
for _, f := range fs {
f()
}
}
`)
if err != nil {
t.Error(err)
}
// read stdout
if err = w.Close(); err != nil {
t.Fatal(err)
}
outInterp, err := io.ReadAll(r)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(outInterp, outExp) {
t.Errorf("\nGot: %q,\n want: %q", string(outInterp), string(outExp))
}
}
20 changes: 20 additions & 0 deletions interp/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,20 @@ func genFunctionWrapper(n *node) func(*frame) reflect.Value {
}
funcType := n.typ.TypeOf()

value := genValue(n)
isDefer := false
if n.anc != nil && n.anc.anc != nil && n.anc.anc.kind == deferStmt {
isDefer = true
}

return func(f *frame) reflect.Value {
v := value(f)
if !isDefer && v.Kind() == reflect.Func {
// fixes #1634, if v is already a func, then don't re-wrap
// because original wrapping cloned the frame but this doesn't
return v
}

return reflect.MakeFunc(funcType, func(in []reflect.Value) []reflect.Value {
// Allocate and init local frame. All values to be settable and addressable.
fr := newFrame(f, len(def.types), f.runid())
Expand Down Expand Up @@ -1400,6 +1413,13 @@ func call(n *node) {
}
runCfg(def.child[3].start, nf, def, n)

// Set return values
for i, v := range rvalues {
if v != nil {
v(f).Set(nf.data[i])
}
}

// Handle branching according to boolean result
if fnext != nil && !nf.data[0].Bool() {
return fnext
Expand Down
8 changes: 8 additions & 0 deletions interp/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ func (s *scope) lookup(ident string) (*symbol, int, bool) {
return nil, 0, false
}

func (s *scope) isRedeclared(n *node) bool {
if !isNewDefine(n, s) {
return false
}
// Existing symbol in the scope indicates a redeclaration.
return s.sym[n.ident] != nil
}

func (s *scope) rangeChanType(n *node) *itype {
if sym, _, found := s.lookup(n.child[1].ident); found {
if t := sym.typ; len(n.child) == 3 && t != nil && (t.cat == chanT || t.cat == chanRecvT) {
Expand Down
12 changes: 3 additions & 9 deletions stdlib/go1_21_compress_flate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions stdlib/go1_21_compress_zlib.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 5 additions & 15 deletions stdlib/go1_21_container_heap.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions stdlib/go1_21_context.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 3 additions & 9 deletions stdlib/go1_21_crypto.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 8 additions & 24 deletions stdlib/go1_21_crypto_cipher.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions stdlib/go1_21_crypto_elliptic.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 3 additions & 9 deletions stdlib/go1_21_database_sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 928bd9a

Please sign in to comment.