From ec3d1fae5985e04c3c8e8a408e9c08e244fbd991 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 6 Jul 2022 17:52:57 +0300 Subject: [PATCH] compiler: allow to find appropriate methods via selectors c.funcs contains function names using base types, while methods can be defined on pointers and the value returned from c.getFuncNameFromSelector will have an asterisk. We can't have the same name used for (*T) and (T) methods, so just stripping the asterisk allows to get the right one. --- pkg/compiler/codegen.go | 6 +++++- pkg/compiler/inline_test.go | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index 693bd7efd..937e52bf4 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -2067,7 +2067,11 @@ func (c *codegen) getFuncNameFromSelector(e *ast.SelectorExpr) (string, bool) { ident := e.X.(*ast.Ident) if c.typeInfo.Selections[e] != nil { typ := c.typeInfo.Types[ident].Type.String() - return c.getIdentName(typ, e.Sel.Name), true + name := c.getIdentName(typ, e.Sel.Name) + if name[0] == '*' { + name = name[1:] + } + return name, true } return c.getIdentName(ident.Name, e.Sel.Name), false } diff --git a/pkg/compiler/inline_test.go b/pkg/compiler/inline_test.go index d8734ebd9..b68a816d3 100644 --- a/pkg/compiler/inline_test.go +++ b/pkg/compiler/inline_test.go @@ -355,3 +355,22 @@ func TestInlinedMethod(t *testing.T) { }` eval(t, src, big.NewInt(100542)) } + +func TestInlinedMethodWithPointer(t *testing.T) { + src := `package foo + import "github.com/nspcc-dev/neo-go/pkg/compiler/testdata/inline" + func Main() int { + // It's important for this variable to not be named 't'. + var z = &inline.T{} + i := z.Inc(42) + if i != 0 || z.N != 42 { + return 0 + } + i = z.Inc(100500) + if i != 42 { + return 0 + } + return z.N + }` + eval(t, src, big.NewInt(100542)) +}