compiler: disallow generic function parameter types

Need to be reverted and properly handled within the scope of #2376.

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
This commit is contained in:
Anna Shaleva 2023-08-16 18:25:16 +03:00
parent 1b1b454ce4
commit 380de580a7
2 changed files with 20 additions and 0 deletions

View file

@ -563,6 +563,11 @@ func (c *codegen) checkGenericsFuncDecl(n *ast.FuncDecl, funcName string) error
} }
} }
// Generic function parameters type: func SumInts[V int64 | int32](vals []V) V
if n.Type.TypeParams != nil {
errGenerics = errors.New("function type parameters")
}
if errGenerics != nil { if errGenerics != nil {
return fmt.Errorf("%w: %s has %s", ErrGenericsUnsuppored, funcName, errGenerics.Error()) return fmt.Errorf("%w: %s has %s", ErrGenericsUnsuppored, funcName, errGenerics.Error())
} }

View file

@ -36,3 +36,18 @@ func TestGenericMethodReceiver(t *testing.T) {
require.ErrorIs(t, err, compiler.ErrGenericsUnsuppored) require.ErrorIs(t, err, compiler.ErrGenericsUnsuppored)
}) })
} }
func TestGenericFuncArgument(t *testing.T) {
src := `
package sum
func SumInts[V int64 | int32 | int16](vals []V) V { // doesn't make sense with NeoVM, but still it's a valid go code.
var s V
for i := range vals {
s += vals[i]
}
return s
}
`
_, _, err := compiler.CompileWithOptions("foo.go", strings.NewReader(src), nil)
require.ErrorIs(t, err, compiler.ErrGenericsUnsuppored)
}