From 970769e5b287a06f4afaab0870c836fa6ba6bb5e Mon Sep 17 00:00:00 2001 From: Evgeniy Stratonikov Date: Tue, 18 Jan 2022 11:46:48 +0300 Subject: [PATCH] compiler: allow to use `_` in constants When `_` is unused it can be omitted from constant values mapping. Catched when compiling `netmap` contract from nspcc-dev/neofs-contract. Signed-off-by: Evgeniy Stratonikov --- pkg/compiler/codegen.go | 8 +++++--- pkg/compiler/global_test.go | 12 ++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index ae6be05ad..c2a513d91 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -555,9 +555,11 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { vs := spec.(*ast.ValueSpec) for i := range vs.Names { obj := c.currPkg.Types.Scope().Lookup(vs.Names[i].Name) - c.constMap[c.getIdentName("", vs.Names[i].Name)] = types.TypeAndValue{ - Type: obj.Type(), - Value: obj.(*types.Const).Val(), + if obj != nil { // can be nil if unused + c.constMap[c.getIdentName("", vs.Names[i].Name)] = types.TypeAndValue{ + Type: obj.Type(), + Value: obj.(*types.Const).Val(), + } } } } diff --git a/pkg/compiler/global_test.go b/pkg/compiler/global_test.go index c4a444932..1d910797a 100644 --- a/pkg/compiler/global_test.go +++ b/pkg/compiler/global_test.go @@ -12,6 +12,18 @@ import ( "github.com/stretchr/testify/require" ) +func TestUnusedGlobal(t *testing.T) { + src := `package foo + const ( + _ int = iota + a + ) + func Main() int { + return 1 + }` + eval(t, src, big.NewInt(1)) +} + func TestChangeGlobal(t *testing.T) { src := `package foo var a int