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

unsafe: allow Offsetof for array entries #16740

Closed
randall77 opened this issue Aug 16, 2016 · 7 comments
Closed

unsafe: allow Offsetof for array entries #16740

randall77 opened this issue Aug 16, 2016 · 7 comments

Comments

@randall77
Copy link
Contributor

It would be nice if unsafe.Offsetof could handle array entries as well as struct fields.

package main

import "unsafe"

type T struct {
    a int
    b [3]int
}

func main() {
    println(unsafe.Offsetof(T{}.b))
    println(unsafe.Offsetof(T{}.b[1]))
}

At the second line of main, I get an invalid expression error:

go/tmp1.go:12: invalid expression unsafe.Offsetof((T literal).b[1])

@griesemer @mdempsky

@randall77
Copy link
Contributor Author

@edressel

@randall77
Copy link
Contributor Author

See #16727

@griesemer griesemer self-assigned this Aug 16, 2016
@mdempsky
Copy link
Member

mdempsky commented Aug 16, 2016

Note that currently Offsetof(a.b.c) tells the offset of c from a.b, not from a. So to be consistent, in your example, Offsetof(T{}.b[1]) should probably be 4 (the offset from T{}.b), not 8 (the offset from T{}).

@griesemer
Copy link
Contributor

A minor language change, would be backward-compatible.

@randall77
Copy link
Contributor Author

Another tricky bit: should we require the index to be constant? If not, then there's some evaluation that needs to happen inside the Offsetof(), which is contrary to what Offsetof does now.

@mdempsky : I didn't realize that it only applied to the last field. That's surprising.

@randall77
Copy link
Contributor Author

So if Offsetof only works on the last offset, then this proposal isn't useful.
Offsetof(a.b.c[2]) would be the same as 2 * Sizeof(a.b.c[0]).
Or maybe it could do the last two offsets if the last one was an index, so at least a.b[2] would work. But this is getting weird, I'm just as inclined to drop it.

@mdempsky
Copy link
Member

FWIW, I've brainstormed before about extending Offsetof to take two parameters like C++'s offsetof operator. Then you could write Offsetof(a, b.c) vs Offsetof(a.b, c) to distinguish intent.

It would be a bit awkward though because then "b" in Offsetof(a, b) doesn't actually refer to a free variable b. Not a problem per se, but some tools might need to be aware of that.

It also wouldn't work for array indexes since Offsetof(a, [1]) isn't currently valid syntax, but at least Offsetof(T{}, b[1]) would be.

@golang golang locked and limited conversation to collaborators Aug 16, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants