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

cmd/compile: code inside unsafe.Sizeof expression not evaluated #20262

Closed
josharian opened this issue May 5, 2017 · 1 comment

Comments

Projects
None yet
3 participants
@josharian
Copy link
Contributor

commented May 5, 2017

https://play.golang.org/p/5VUF7XrYjp

package main

import (
	"unsafe"
)

var x int

const sz = unsafe.Sizeof(func() int { x++; return 0 }())

func main() {
	println(x)
}

My reading of the spec says this should print 1, but it prints 0.

Relevant bits of the spec:

The functions Alignof and Sizeof take an expression x of any type and return the alignment or size, respectively, of a hypothetical variable v as if v was declared via var v = x.

So we should be evaluating the function expression, as if it were the RHS of an expression, at which point const sz should be evaluated before var x, thus incrementing x.

Seems low priority. If we investigate fixing, we should test handling of iota inside function bodies.

Related: #20260

@josharian josharian added this to the Go1.10 milestone May 5, 2017

@griesemer

This comment has been minimized.

Copy link
Contributor

commented May 5, 2017

Closing as dup of #12946.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.