interp: improve support of unsafe

Unsafe functions such as `unsafe.Alignof`, `unsafe.Offsetof` and `unsafe.Sizeof` can be used for type declarations early on during compile, and as such, must be treated as builtins returning constants at compile time. It is still necessary to explicitely enable unsafe support in yaegi.

The support of `unsafe.Add` has also been added.

Fixes #1544.
This commit is contained in:
Marc Vertes
2023-04-26 10:16:05 +02:00
committed by GitHub
parent d6ad13acea
commit dc7c64ba88
10 changed files with 131 additions and 58 deletions

View File

@@ -492,6 +492,9 @@ func nodeType2(interp *Interpreter, sc *scope, n *node, seen []*node) (t *itype,
}
return nil, err
}
if !c0.rval.IsValid() {
return nil, c0.cfgErrorf("undefined array size")
}
if length, ok = c0.rval.Interface().(int); !ok {
v, ok := c0.rval.Interface().(constant.Value)
if !ok {
@@ -1339,7 +1342,7 @@ func (t *itype) numOut() int {
}
case builtinT:
switch t.name {
case "append", "cap", "complex", "copy", "imag", "len", "make", "new", "real", "recover":
case "append", "cap", "complex", "copy", "imag", "len", "make", "new", "real", "recover", "unsafe.Alignof", "unsafe.Offsetof", "unsafe.Sizeof":
return 1
}
}