aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCuong Manh Le <cuong.manhle.vn@gmail.com>2022-05-30 21:34:01 +0700
committerKeith Randall <khr@golang.org>2022-05-31 14:58:09 +0000
commit085529bd5fa3ab508784f6a3d42f15d2dafddc65 (patch)
treeef631824e361c8a78bdcb4cb6487c6a88b55bcb1 /src
parentcfd202c701d3c1fda740a8c3c725fbb704054591 (diff)
downloadgo-085529bd5fa3ab508784f6a3d42f15d2dafddc65.tar.xz
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>
Diffstat (limited to 'src')
-rw-r--r--src/cmd/compile/internal/typecheck/const.go12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/typecheck/const.go b/src/cmd/compile/internal/typecheck/const.go
index 22fa9e7d95..6109850c24 100644
--- a/src/cmd/compile/internal/typecheck/const.go
+++ b/src/cmd/compile/internal/typecheck/const.go
@@ -805,6 +805,18 @@ func evalunsafe(n ir.Node) int64 {
sel.X = Expr(sel.X)
sbase := sel.X
+ // Implicit dot may already be resolved for instantiating generic function. So we
+ // need to remove any implicit dot until we reach the first non-implicit one, it's
+ // the right base selector. See issue #53137.
+ var clobberBase func(n ir.Node) ir.Node
+ clobberBase = func(n ir.Node) ir.Node {
+ if sel, ok := n.(*ir.SelectorExpr); ok && sel.Implicit() {
+ return clobberBase(sel.X)
+ }
+ return n
+ }
+ sbase = clobberBase(sbase)
+
tsel := Expr(sel)
n.X = tsel
if tsel.Type() == nil {