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.
This commit is contained in:
Roman Khimov 2022-07-06 17:52:57 +03:00
parent b57dd2cad6
commit ec3d1fae59
2 changed files with 24 additions and 1 deletions

View file

@ -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
}

View file

@ -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))
}