From 7007a8353f11017e5357039edcc38c7115d096a0 Mon Sep 17 00:00:00 2001 From: Kevin Chen Date: Fri, 26 Jan 2024 00:05:46 +0800 Subject: [PATCH] interp: fix mismatch assign statement panic (#1606) --- _test/assign19.go | 9 +++++++++ interp/cfg.go | 8 ++++++++ interp/interp_consistent_test.go | 1 + 3 files changed, 18 insertions(+) create mode 100644 _test/assign19.go diff --git a/_test/assign19.go b/_test/assign19.go new file mode 100644 index 000000000..497ee5058 --- /dev/null +++ b/_test/assign19.go @@ -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 diff --git a/interp/cfg.go b/interp/cfg.go index 39133a4c8..d0d19e9d0 100644 --- a/interp/cfg.go +++ b/interp/cfg.go @@ -644,6 +644,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] diff --git a/interp/interp_consistent_test.go b/interp/interp_consistent_test.go index b7d4817d5..79fb16c7a 100644 --- a/interp/interp_consistent_test.go +++ b/interp/interp_consistent_test.go @@ -37,6 +37,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