From 0629479560bfe7e5dec35c0e04e1bfc1b11e97ef Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 19 May 2020 16:04:41 +0300 Subject: [PATCH] compiler: support non-struct methods There is no need to restrict ourselves just to structs. --- pkg/compiler/codegen.go | 9 +-------- pkg/compiler/type_test.go | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index a1cdc3d27..da8599e53 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -304,15 +304,8 @@ func (c *codegen) convertFuncDecl(file ast.Node, decl *ast.FuncDecl) { // to support other types. if decl.Recv != nil { for _, arg := range decl.Recv.List { - ident := arg.Names[0] - // Currently only method receives for struct types is supported. - _, ok := c.typeInfo.Defs[ident].Type().Underlying().(*types.Struct) - if !ok { - c.prog.Err = fmt.Errorf("method receives for non-struct types is not yet supported") - return - } // only create an argument here, it will be stored via INITSLOT - c.scope.newVariable(varArgument, ident.Name) + c.scope.newVariable(varArgument, arg.Names[0].Name) } } diff --git a/pkg/compiler/type_test.go b/pkg/compiler/type_test.go index c8db6fb0c..b188eeb19 100644 --- a/pkg/compiler/type_test.go +++ b/pkg/compiler/type_test.go @@ -1,6 +1,9 @@ package compiler_test -import "testing" +import ( + "math/big" + "testing" +) func TestCustomType(t *testing.T) { src := ` @@ -22,3 +25,15 @@ func TestCustomType(t *testing.T) { ` eval(t, src, []byte("some short string")) } + +func TestCustomTypeMethods(t *testing.T) { + src := `package foo + type bar int + func (b bar) add(a bar) bar { return a + b } + func Main() bar { + var b bar + b = 10 + return b.add(32) + }` + eval(t, src, big.NewInt(42)) +}