Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use of unsafe.Sizeof in type declaration #1544

Closed
mvertes opened this issue Apr 20, 2023 · 0 comments · Fixed by #1545
Closed

use of unsafe.Sizeof in type declaration #1544

mvertes opened this issue Apr 20, 2023 · 0 comments · Fixed by #1545
Labels
area/core bug Something isn't working

Comments

@mvertes
Copy link
Member

mvertes commented Apr 20, 2023

The following program sample.go triggers an unexpected result

package main

import "unsafe"

type T struct {
    i uint64
}

var d T

var b [unsafe.Sizeof(d)]byte

func main() {
    println(len(b))
}

Expected result

$ go run ./sample.go
8

Got

$ yaegi ./sample.go
panic: reflect: call of reflect.Value.Interface on zero Value

goroutine 1 [running]:
reflect.valueInterface({0x0?, 0x0?, 0x140001ea3f0?}, 0xa8?)
	reflect/value.go:1485 +0xfc
reflect.Value.Interface(...)
	reflect/value.go:1480
github.com/traefik/yaegi/interp.nodeType2(0x140001f18c0, 0x140001ea3f0, 0x1400042f900, {0x1400019c730, 0x0, 0x1})
	github.com/traefik/yaegi/interp/type.go:494 +0x504
github.com/traefik/yaegi/interp.nodeType(...)
	github.com/traefik/yaegi/interp/type.go:400
github.com/traefik/yaegi/interp.(*Interpreter).gta.func1(0x1400042f680)
	github.com/traefik/yaegi/interp/gta.go:118 +0xfb8
github.com/traefik/yaegi/interp.(*node).Walk(0x1400042f680, 0x1400022b848, 0x0)
	github.com/traefik/yaegi/interp/interp.go:287 +0x34
github.com/traefik/yaegi/interp.(*node).Walk(0x1400042f540, 0x1400022b848, 0x0)
	github.com/traefik/yaegi/interp/interp.go:291 +0x74
github.com/traefik/yaegi/interp.(*node).Walk(0x1400042e000, 0x1400022b848, 0x0)
	github.com/traefik/yaegi/interp/interp.go:291 +0x74
github.com/traefik/yaegi/interp.(*Interpreter).gta(0x140001f18c0, 0x1400042e000, {0x140002d48a8, 0x4}, {0x140002d48a8, 0x4}, {0x140002d48a8, 0x4})
	github.com/traefik/yaegi/interp/gta.go:20 +0x158
github.com/traefik/yaegi/interp.(*Interpreter).gtaRetry(0x140001f18c0?, {0x1400022ba30?, 0x140001ea360?, 0x1400022b970?}, {0x140002d48a8, 0x4}, {0x140002d48a8, 0x4})
	github.com/traefik/yaegi/interp/gta.go:395 +0x118
github.com/traefik/yaegi/interp.(*Interpreter).CompileAST(0x140001f18c0, {0x10383ff90?, 0x140001ea360?})
	github.com/traefik/yaegi/interp/program.go:92 +0x124
github.com/traefik/yaegi/interp.(*Interpreter).compileSrc(0x140001f18c0, {0x140002a2b40?, 0x95?}, {0x16d16f470?, 0x1400019c600?}, 0x40?)
	github.com/traefik/yaegi/interp/program.go:64 +0xd4
github.com/traefik/yaegi/interp.(*Interpreter).eval(0x140001f18c0, {0x140002a2b40?, 0x95?}, {0x16d16f470?, 0x200?}, 0x0?)
	github.com/traefik/yaegi/interp/interp.go:556 +0x28
github.com/traefik/yaegi/interp.(*Interpreter).EvalPath(0x140001f18c0, {0x16d16f470, 0x19})
	github.com/traefik/yaegi/interp/interp.go:514 +0xa4
main.runFile(0x16d16f470?, {0x16d16f470, 0x19}, 0x0)
	github.com/traefik/yaegi/cmd/yaegi/run.go:153 +0xe8
main.run({0x140001aa010?, 0x1, 0x1})
	github.com/traefik/yaegi/cmd/yaegi/run.go:116 +0xafc
main.main()
	github.com/traefik/yaegi/cmd/yaegi/yaegi.go:144 +0x310

Yaegi Version

v0.15.1

Additional Notes

No response

@mvertes mvertes added bug Something isn't working area/core labels Apr 20, 2023
mvertes added a commit to mvertes/yaegi that referenced this issue Apr 20, 2023
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 traefik#1544.
traefiker pushed a commit that referenced this issue Apr 26, 2023
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/core bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant