From 8b922c057cb1c0e55e43a72b23995ac2d1525ef1 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 29 Jan 2020 17:04:49 +0300 Subject: [PATCH] compiler: fix a bug with assignment to underscore When using underscore it does not appear in the list of local variables, so it can't be assigned. In this commit the value is dropped. --- pkg/compiler/codegen.go | 8 ++++++-- pkg/compiler/return_test.go | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index 1cce7c6e7..3ab378404 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -253,8 +253,12 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { ast.Walk(c, n.Rhs[i]) } - l := c.scope.loadLocal(t.Name) - c.emitStoreLocal(l) + if t.Name == "_" { + emitOpcode(c.prog.BinWriter, opcode.DROP) + } else { + l := c.scope.loadLocal(t.Name) + c.emitStoreLocal(l) + } } case *ast.SelectorExpr: diff --git a/pkg/compiler/return_test.go b/pkg/compiler/return_test.go index 03a8589fa..a97c3a25a 100644 --- a/pkg/compiler/return_test.go +++ b/pkg/compiler/return_test.go @@ -37,6 +37,21 @@ func TestMultipleReturn2(t *testing.T) { eval(t, src, big.NewInt(9)) } +func TestMultipleReturnUnderscore(t *testing.T) { + src := ` + package hello + func f3() (int, int, int) { + return 5, 6, 7 + } + + func Main() int { + a, _, c := f3() + return a+c + } + ` + eval(t, src, big.NewInt(12)) +} + func TestMultipleReturnWithArg(t *testing.T) { src := ` package hello