interp: do not allow function declaration without body

Such function declaration denotes either a linkname (an access to
an arbitrary, typically unexported symbol, solved by go compiler),
or a foreign C or assembly implementation of the body.

Those cases are not supported (or planned to be) by the interpreter.

Fixes #1431.
This commit is contained in:
Marc Vertes
2022-08-03 10:06:06 +02:00
committed by GitHub
parent d3fc5e990e
commit 255b1cf1de
2 changed files with 6 additions and 0 deletions

View File

@@ -360,6 +360,11 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
fallthrough
case funcDecl:
// Do not allow function declarations without body.
if len(n.child) < 4 {
err = n.cfgErrorf("function declaration without body is unsupported (linkname or assembly can not be interpreted).")
return false
}
n.val = n
// Compute function type before entering local scope to avoid
// possible collisions with function argument names.

View File

@@ -701,6 +701,7 @@ func TestEvalCall(t *testing.T) {
{src: ` test := func(a, b int) int { return a }
blah := func() (int, float64) { return 1, 1.1 }
a := test(blah())`, err: "3:15: cannot use func() (int,float64) as type (int,int)"},
{src: "func f()", err: "function declaration without body is unsupported"},
})
}