compiler: restrict return values count for manifest methods
Exported functions from main package shouldn't have more than one return value.
This commit is contained in:
parent
171364f07f
commit
9b9d72937b
3 changed files with 166 additions and 17 deletions
|
@ -2,6 +2,7 @@ package compiler_test
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
@ -427,3 +428,147 @@ func TestUnnamedParameterCheck(t *testing.T) {
|
|||
require.NoError(t, err) // it's OK for exported method to have unnamed params as it won't be included into manifest
|
||||
})
|
||||
}
|
||||
|
||||
func TestReturnValuesCountCheck(t *testing.T) {
|
||||
t.Run("void", func(t *testing.T) {
|
||||
t.Run("exported", func(t *testing.T) {
|
||||
t.Run("func", func(t *testing.T) {
|
||||
src := `package testcase
|
||||
var a int
|
||||
func Main() {
|
||||
a = 5
|
||||
}`
|
||||
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
t.Run("method", func(t *testing.T) {
|
||||
src := `package testcase
|
||||
type A int
|
||||
var a int
|
||||
func (rcv A) Main() {
|
||||
a = 5
|
||||
}`
|
||||
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
})
|
||||
t.Run("unexported", func(t *testing.T) {
|
||||
src := `package testcase
|
||||
var a int
|
||||
func main() {
|
||||
a = 5
|
||||
}`
|
||||
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
})
|
||||
t.Run("single return", func(t *testing.T) {
|
||||
t.Run("exported", func(t *testing.T) {
|
||||
t.Run("func", func(t *testing.T) {
|
||||
src := `package testcase
|
||||
var a int
|
||||
func Main() int {
|
||||
a = 5
|
||||
return a
|
||||
}`
|
||||
eval(t, src, big.NewInt(5))
|
||||
})
|
||||
t.Run("method", func(t *testing.T) {
|
||||
src := `package testcase
|
||||
type A int
|
||||
var a int
|
||||
func (rcv A) Main() int {
|
||||
a = 5
|
||||
return a
|
||||
}`
|
||||
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
})
|
||||
t.Run("unexported", func(t *testing.T) {
|
||||
src := `package testcase
|
||||
var a int
|
||||
func main() int {
|
||||
a = 5
|
||||
return a
|
||||
}`
|
||||
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
})
|
||||
t.Run("multiple unnamed return vals", func(t *testing.T) {
|
||||
t.Run("exported", func(t *testing.T) {
|
||||
t.Run("func", func(t *testing.T) {
|
||||
src := `package testcase
|
||||
var a int
|
||||
func Main() (int, int) {
|
||||
a = 5
|
||||
return a, a
|
||||
}`
|
||||
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
||||
require.Error(t, err)
|
||||
require.ErrorIs(t, err, compiler.ErrInvalidExportedRetCount)
|
||||
})
|
||||
t.Run("method", func(t *testing.T) {
|
||||
src := `package testcase
|
||||
type A int
|
||||
var a int
|
||||
func (rcv A) Main() (int, int) {
|
||||
a = 5
|
||||
return a, a
|
||||
}`
|
||||
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
||||
require.NoError(t, err) // OK for method to have multiple return values as it won't be included into manifest
|
||||
})
|
||||
})
|
||||
t.Run("unexported", func(t *testing.T) {
|
||||
src := `package testcase
|
||||
var a int
|
||||
func main() (int, int) {
|
||||
a = 5
|
||||
return a, a
|
||||
}`
|
||||
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
||||
require.NoError(t, err) // OK for unexported function to have multiple return values as it won't be included into manifest
|
||||
})
|
||||
})
|
||||
t.Run("multiple named return vals", func(t *testing.T) {
|
||||
t.Run("exported", func(t *testing.T) {
|
||||
t.Run("func", func(t *testing.T) {
|
||||
src := `package testcase
|
||||
var a int
|
||||
func Main() (a int, b int) {
|
||||
a = 5
|
||||
b = 2
|
||||
return
|
||||
}`
|
||||
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
||||
require.Error(t, err)
|
||||
require.ErrorIs(t, err, compiler.ErrInvalidExportedRetCount)
|
||||
})
|
||||
t.Run("method", func(t *testing.T) {
|
||||
src := `package testcase
|
||||
type A int
|
||||
var a int
|
||||
func (rcv A) Main() (a int, b int) {
|
||||
a = 5
|
||||
b = 2
|
||||
return
|
||||
}`
|
||||
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
||||
require.NoError(t, err) // OK for method to have multiple return values as it won't be included into manifest
|
||||
})
|
||||
})
|
||||
t.Run("unexported", func(t *testing.T) {
|
||||
src := `package testcase
|
||||
var a int
|
||||
func main() (a int, b int) {
|
||||
a = 5
|
||||
b = 2
|
||||
return
|
||||
}`
|
||||
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
||||
require.NoError(t, err) // OK for unexported function to have multiple return values as it won't be included into manifest
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue