On Fri, Jun 19, 2020 at 12:04 PM Iskander Sharipov <[email protected]> wrote: > > https://golang.org/src/runtime/symtab.go?s=7594:7745#L244 > > type Func struct { > > opaque struct{} // unexported field to disallow conversions > > } > > > func (f *Func) raw() *_func { > > return (*_func)(unsafe.Pointer(f)) > > } > > > An empty struct Func (T1) is converted to a non-zero struct _func (T2). > > From the unsafe.Pointer documentation > (https://golang.org/pkg/unsafe/#Pointer): > > > T1->T2 conversion is safe if T2 is no larger than T1 ... (shortened for > > clarity) > > I'm guessing that documentation probably meant "allocation size", not the > "static type size"? > T1 pointer can be obtained from the T2 allocation, so it's allocation size > can be different from the unsafe.Sizeof(T1{}). > > Or it could be that runtime can violate that particular rule. > > My question: is it same to convert T1<->T2 even if sizeof(T1)<sizeof(T2), but > the memory was allocated for the bigger object?
Your last point is correct: the runtime package is allowed to violate the rules. In this case it is safe because Func values are never allocated and are never garbage collected. They are always part of the read-only section of the program itself. Ian -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOyqgcWo1WpWbVAtBwKcg9p4eN00ptEZohE8EeV6RPFL89pk4Q%40mail.gmail.com.
