1
0
mirror of https://github.com/golang/go synced 2024-11-15 09:20:58 -07:00
go/test/fixedbugs/issue53137.go
Cuong Manh Le 085529bd5f cmd/compile: fix wrong unsafe.Offsetof evaluation inside generic function
For instantiated generic functions, all implicit dot operations are
resolved. Thus unsafe.Offsetof may calculating the offset against the
wrong base selector.

To fix it, we must remove any implicit dot operations to find the first
non-implicit one, which is the right base selector for calculating the
offset.

Fixes #53137

Change-Id: I38504067ce0f274615b306edc8f7d7933bdb631a
Reviewed-on: https://go-review.googlesource.com/c/go/+/409355
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Keith Randall <khr@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2022-05-31 14:58:09 +00:00

33 lines
466 B
Go

// run
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"unsafe"
)
type Embedded struct {
B int
}
type S[K any] struct {
A K
Embedded
}
func showOffsets[K any](d *S[K]) {
o1 := unsafe.Offsetof(d.B)
o2 := unsafe.Offsetof(d.Embedded)
if o1 != o2 {
panic("offset mismatch")
}
}
func main() {
showOffsets(new(S[int]))
}