Skip to content

Commit

Permalink
Merge pull request #2593 from nspcc-dev/fix-compiler
Browse files Browse the repository at this point in the history
compiler: allow to call methods on return values
  • Loading branch information
roman-khimov authored Jul 12, 2022
2 parents 31a559e + e1a581b commit 9414538
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
7 changes: 4 additions & 3 deletions pkg/compiler/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {

f, ok = c.funcs[name]
if ok {
f.selector = fun.X.(*ast.Ident)
f.selector = fun.X
isBuiltin = isCustomBuiltin(f)
if canInline(f.pkg.Path(), f.decl.Name.Name) {
c.inlineCall(f, n)
Expand Down Expand Up @@ -2069,15 +2069,16 @@ func (c *codegen) getFuncFromIdent(fun *ast.Ident) (*funcScope, bool) {
// getFuncNameFromSelector returns fully-qualified function name from the selector expression.
// Second return value is true iff this was a method call, not foreign package call.
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()
typ := c.typeInfo.Types[e.X].Type.String()
name := c.getIdentName(typ, e.Sel.Name)
if name[0] == '*' {
name = name[1:]
}
return name, true
}

ident := e.X.(*ast.Ident)
return c.getIdentName(ident.Name, e.Sel.Name), false
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/compiler/func_scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type funcScope struct {

// Selector of the function if there is any. Only functions imported
// from other packages should have a selector.
selector *ast.Ident
selector ast.Expr

// The declaration of the function in the AST. Nil if this scope is not a function.
decl *ast.FuncDecl
Expand Down
21 changes: 21 additions & 0 deletions pkg/compiler/function_call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@ import (
"github.com/stretchr/testify/require"
)

func TestReturnValueReceiver(t *testing.T) {
t.Run("regular", func(t *testing.T) {
src := `package foo
import "github.com/nspcc-dev/neo-go/pkg/compiler/testdata/method"
func Main() int {
return method.NewX().GetA()
}`
eval(t, src, big.NewInt(42))
})
t.Run("inline", func(t *testing.T) {
src := `package foo
import "github.com/nspcc-dev/neo-go/pkg/compiler/testdata/inline"
func Main() int {
return inline.NewT().GetN()
}`
eval(t, src, big.NewInt(42))
})
}

func TestSimpleFunctionCall(t *testing.T) {
src := `
package testcase
Expand Down
8 changes: 8 additions & 0 deletions pkg/compiler/testdata/inline/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,11 @@ func (t *T) Inc(i int) int {
t.N += i
return n
}

func NewT() T {
return T{N: 42}
}

func (t T) GetN() int {
return t.N
}
16 changes: 16 additions & 0 deletions pkg/compiler/testdata/method/struct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package method

// X is some type.
type X struct {
a int
}

// GetA returns the value of a.
func (x X) GetA() int {
return x.a
}

// NewX creates a new X instance.
func NewX() X {
return X{a: 42}
}

0 comments on commit 9414538

Please sign in to comment.