diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index e16b59087..ff0eeb5bf 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -493,8 +493,15 @@ func (c *codegen) convertFuncDecl(file ast.Node, decl *ast.FuncDecl, pkg *types. // to support other types. if decl.Recv != nil { for _, arg := range decl.Recv.List { + // Use underscore instead of unnamed receiver name, e.g.: + // func (MyCustomStruct) DoSmth(arg1 int) {...} + // Unnamed receiver will never be referenced, thus we can use the same approach as for multiple unnamed parameters handling, see #2204. + recvName := "_" + if len(arg.Names) != 0 { + recvName = arg.Names[0].Name + } // only create an argument here, it will be stored via INITSLOT - c.scope.newVariable(varArgument, arg.Names[0].Name) + c.scope.newVariable(varArgument, recvName) } } diff --git a/pkg/compiler/function_call_test.go b/pkg/compiler/function_call_test.go index 1babb84e7..c409108be 100644 --- a/pkg/compiler/function_call_test.go +++ b/pkg/compiler/function_call_test.go @@ -385,3 +385,17 @@ func TestUnusedFunctions(t *testing.T) { eval(t, src, big.NewInt(2231)) }) } + +func TestUnnamedMethodReceiver(t *testing.T) { + src := `package foo + type CustomInt int + func Main() int { + var i CustomInt + i = 5 + return i.Do(2) + } + func (CustomInt) Do(arg int) int { + return arg + }` + eval(t, src, big.NewInt(2)) +}