Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interp: fix mismatch assign statement panic #1607

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 8 additions & 0 deletions interp/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,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
1 change: 1 addition & 0 deletions interp/interp_consistent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,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
Loading